How to prevent any editing and selection attempts for ComboBox with ComboBoxStyle.DropDown (not DropDownList!) setting?
I override OnKeyDown and cut any keyboard input but mouse selection events still works and caret is visible.
I can propose you two (simple) methods to set a ComboBox text read-only-like.
Using SendMessage:
In this case, the ComboBox underlying Edit Control is set to ReadOnly and - as already said - its background color is painted with the non-active/disabled color (LTGRAY_BRUSH).
The same as if you set a TextBox control ReadOnly property to true.
[DllImport("User32.dll")]
internal static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll", SetLastError = true)]
internal static extern int SendMessage(IntPtr hWnd, uint uMsg, int wParam, int lParam);
private const uint EM_SETREADONLY = 0x00CF;
private void Form1_Load(object sender, EventArgs e)
{
IntPtr EditHandle = FindWindowEx(comboBox1.Handle, IntPtr.Zero, "Edit", null);
SendMessage(EditHandle, EM_SETREADONLY, 1, 0);
}
Discarding any key press using the Control KeyDown event:
In this case, the ComboBox background color of course does not change, but there might be undesired behaviours, depending on its use.
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
}
Update:
A method to hide the caret (without subclassing or inheriting):
Here I'm using FindWindowEx again. You can assign the IntPtr EditHandle to a class scope variable and get the Edit control handle once in the Form Load event.
[DllImport("user32")]
internal static extern bool HideCaret(IntPtr hWnd);
[DllImport("user32")]
internal static extern bool ShowCaret(IntPtr hWnd);
private void comboBox1_MouseUp(object sender, MouseEventArgs e)
{
comboBox1.Select(0, 0);
IntPtr EditHandle = FindWindowEx(comboBox1.Handle, IntPtr.Zero, "Edit", null);
HideCaret(EditHandle);
}
Related
I want to change the Caret in a Textbox in C# that it appears wider like in does in old DOS applications.
What i have:
Example what i want:
I tried:
using System.Runtime.InteropServices;
...
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
...
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
CreateCaret(textBox1.Handle, IntPtr.Zero, 20, textBox1.Height);
ShowCaret(textBox1.Handle);
}
}
But it still looks the same. Any help would be nice. Thanks in advance!
Edit:
This was just an example. My real code looks like:
TextBox textbox = new TextBox();
textbox.MaxLength = fieldLength;
textbox.Width = fieldLength*24;
textbox.MaxLength = maxChars;
this.Controls.Add(textbox);
CreateCaret(textbox.Handle, IntPtr.Zero, 20, textbox.Height);
ShowCaret(textbox.Handle);
The code gets called but doesnt change anything.
Edit2:
I tried the example and it works fine but my problem still exisits:
I cant change the Caret when creating the textbox. Its only possible for a textbox created with the form.
You didn't link the event correctly, you should change to :
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
this.Shown += Form1_Shown;
}
private void Form1_Shown(object sender, EventArgs e)
{
CreateCaret(textBox1.Handle, IntPtr.Zero, 20, textBox1.Height);
ShowCaret(textBox1.Handle);
}
}
What is going on it seems is that the shown event will only initially work. When you leave the textbox by tabbing away and back to the control the caret will be reset by underlying code.
Take a look the answer in this thread.
I took their DrawDaret Method and changed it a bit. Calling DrawCaret on the textbox1.Enter event doesn't work. possibly the textbox implementation will notify the Enter event and then change the caret. This would undo a change done in the Enter event handler regarding to the caret.
EDIT
But the control also has a GotFocus event which you can use.
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
DrawCaret(textBox1);
}
public void DrawCaret(Control ctrl)
{
var nHeight = 0;
var nWidth = 10;
nHeight = Font.Height;
CreateCaret(ctrl.Handle, IntPtr.Zero, nWidth, nHeight);
ShowCaret(ctrl.Handle);
}
}
I have been using this example to customize the save dialog,
http://www.codeproject.com/Articles/19566/Extend-OpenFileDialog-and-SaveFileDialog-the-easy
This works well and I could customize the dialog too. However, I see that the customized dialog does not follow the underlying windows style. For example, If I am in Windows 7 the dialog would look like this,
This is a save dialog from word and it does have few options like tags and stuff. But the look and feel is same as OS save dialog.
However, the custom save dialog with the above mentioned link would look like this,
Why would it not follow what OS offers? Is there any way to handle this?
Ok, I researched a bit and got to the point where I can use CommonSaveFileDialog from Microsoft.WindowsAPICodePack.Dialogs and create the underlying Save dialog ( which does match with Windows 7 style. ). I installed the WindowsAPI shell package and used the CommonSaveFileDialog control to create something like this,
The controls marked in red are actually CommonFileDialogLabel / CommonFileDialogTextBox / CommonFileDialogComboBox etc which are provided in those API. But now my question is how do I add a user control / custom control to this? I need full control over what I add so it could be a user control. Any idea.. Please help Thanks.
The suggested solution works as described:
The Save As file dialog (used in this example) is associated to a User Control, called CustomSaveFileDialog. It has the advantage that it is present in the Toolbox, and that it implements automatically the IDisposable interface. However, it could have also been a simple C# class.
This control has a constructor accepting an arbitrary application specific User Control hosting all the elements which are to show up in the File Dialog. When I got the question right, that is what is required.
The CustomSaveFileDialog has the following properties:
Accepting arbitrary User Controls Docked to the bottom of the File
Dialog, i.e. they follow the resizing of the File Dialog
No special behaviour for the additional elements (buttons, images,
checkboxes etc) is necessary. They act quite normally, as in other
windows.
This is the code of the described class:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace customSaveFileDialog
{
public partial class CustomSaveFileDialog : UserControl
{
//https://stackoverflow.com/questions/9665579/setting-up-hook-on-windows-messages
delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType,
IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
const uint WINEVENT_OUTOFCONTEXT = 0;
[DllImport("user32.dll")]
private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr
hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess,
uint idThread, uint dwFlags);
[DllImport("user32.dll")]
private static extern bool UnhookWinEvent(IntPtr hWinEventHook);
[DllImport("user32.dll")]
private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int w, int h, bool repaint);
private struct RECT { public int Left; public int Top; public int Right; public int Bottom; }
[DllImport("user32.dll")]
private static extern bool GetClientRect(IntPtr hWnd, out RECT rc);
[DllImport("kernel32.dll")]
private static extern uint GetLastError();
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hwndChild, IntPtr hwndNewParent);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetParent(IntPtr hWnd);
private IntPtr hDlg; // Save As dialog handle
private IntPtr hHook; // Event hook
private IntPtr hCtrl; // App. specific user control handle
UserControl ctrl; // App. specific user control
//Static variable containing the instance object
private static CustomSaveFileDialog customSaveFileDialog;
//public property for the user
//theSaveFileDialog has been added to the control in the designer from the Toolbox
public SaveFileDialog Dlg { get { return theSaveFileDialog; } }
//Event hook delegate
private static WinEventDelegate procDelegate = new WinEventDelegate(WinEventProc);
/// <summary>
/// Constructor
/// </summary>
/// <param name="ctrl">The User Control to be displayed in the file dialog</param>
public CustomSaveFileDialog(UserControl ctrl)
{
InitializeComponent();
customSaveFileDialog = this;
this.ctrl = ctrl;
hCtrl = ctrl.Handle;
//Setup Hook; for simplicity, hook all possible events from the current process
hHook = SetWinEventHook(1, 0x7fffffff, IntPtr.Zero,
procDelegate, (uint)Process.GetCurrentProcess().Id, 0, WINEVENT_OUTOFCONTEXT);
}
// Hook function
static void WinEventProc(IntPtr hWinEventHook, uint eventType,
IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
CustomSaveFileDialog csfdg = customSaveFileDialog;
if (csfdg.hDlg == IntPtr.Zero)
csfdg.hDlg = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Save As");
if (hwnd == csfdg.hDlg)
{
IntPtr hParent = GetParent(csfdg.hCtrl);
//this is done only once
if (!(hParent == csfdg.hDlg))
SetParent(csfdg.hCtrl, csfdg.hDlg); //Bind the user control to the Common Dialog
RECT cliRect;
GetClientRect(csfdg.hDlg, out cliRect);
//Position the button in the file dialog
MoveWindow(csfdg.hCtrl, cliRect.Left + 130, cliRect.Bottom - 55, 500, 60, true);
}
}
}
}
The essential part is the hooking of the windows events. This has been taken from that post.
It may be noted that the "FindWindowEx" function (in the WinEventProc) finds all Common Dialogs (and probably more) with a title of "Save As". If this should be a problem, more filtering would be necessary, e.g by searching in the current thread only. Such a search function may be found here.
Additionally (not shown in the above code) the "Dispose" method in CustormSaveFileDialog.desinger.cs contains the Unhook function with the hHook handle as the parameter.
The software has been tested in Windows7 in Debug mode. As a test, a simple Forms window with a button has been implemented:
//Test for the customized "Save As" dialog
private void button1_Click(object sender, EventArgs e)
{
//Arbitrary User Control
myUserControl ctrl = new myUserControl();
using (CustomSaveFileDialog csfdg = new CustomSaveFileDialog(ctrl))
{
csfdg.Dlg.FileName = "test";
//Show the Save As dialog associated to the CustomFileDialog control
DialogResult res = csfdg.Dlg.ShowDialog();
if (res == System.Windows.Forms.DialogResult.OK)
MessageBox.Show("Save Dialog Finished");
}
}
And - as well as a test - the applicatioin specific user control handles the following events:
using System;
using System.Windows.Forms;
namespace CustomFile
{
public partial class myUserControl : UserControl
{
public myUserControl()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button Clicked");
}
private void pictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("Image Clicked");
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (!checkBox1.Checked)
pictureBox1.Visible = false;
else
pictureBox1.Visible = true;
}
}
}
The following output is produced:
The next picture shows another screenshot, File Dialog resized, and the checkbox to display the image is unchecked.
I'm trying to use the youtube embedded player ( axshockwaveflash) and make a nice program out of it.
Thing is i'm trying to implement a button that'll replay/next/previous the current video.
what I have atm is:
private void btReplay_Click(object sender, EventArgs e)
{
if (!youtubePlayer.Focus())
{
youtubePlayer.Focus();
SendKeys.Send("0");
}
else
{
SendKeys.Send("0");
}
this.BringToFront();
}
The '0' key press makes the video replay from the start. Only it also makes the form dissappear between other open windows.
As you see i've tried using bringtofront but it won't work.
Any thoughts?
Also if anyone has any experience with this, i also want to play how to enable auto play the next video when using the 'END' key. I know about the autoplay=1 function but it won't seem to work when pressing the END key.
EDIT: Using WinForms btw
You didn't specify whether you use winForms or WPF. This snippet is for winforms.
Here I am giving you a static method that forces any given Form to front:
public static void bringWindowToFront(System.Windows.Forms.Form form)
{
uint foreThread = GetWindowThreadProcessId(GetForegroundWindow(), System.IntPtr.Zero);
uint appThread = GetCurrentThreadId();
const uint SW_SHOW = 5;
if (foreThread != appThread)
{
AttachThreadInput(foreThread, appThread, true);
BringWindowToTop(form.Handle);
ShowWindow(form.Handle, SW_SHOW);
AttachThreadInput(foreThread, appThread, false);
}
else
{
BringWindowToTop(form.Handle);
ShowWindow(form.Handle, SW_SHOW);
}
form.Activate();
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern bool BringWindowToTop(System.IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern System.IntPtr GetForegroundWindow();
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(System.IntPtr hWnd, System.IntPtr ProcessId);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ShowWindow(System.IntPtr hWnd, uint nCmdShow);
this.BringToFront();
this.TopMost = true;
worked for me, silly i didnt think of this.
I'm developing a custom HyperTerminal like application in a WinForms .Net 2.0 application. I have a multiline TextBox in a Panel in which you can interact with a hardware device.
My customer wants to have a custom Caret, a filled rectangle the size of one character space instead of the vertical line that is by default.
I know .Net does not provide an option to do this by default, but there must some Windows function to do it.
These are the list of Native Caret functions provided by Windows you can use them for you application.
[DllImport("User32.dll")]
static extern bool CreateCaret(IntPtr hWnd, int hBitmap, int nWidth, int nHeight);
[DllImport("User32.dll")]
static extern bool SetCaretPos(int x, int y);
[DllImport("User32.dll")]
static extern bool DestroyCaret();
[DllImport("User32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
[DllImport("User32.dll")]
static extern bool HideCaret(IntPtr hWnd);
Refer SharpDevelop, Source Code # src\Libraries\ICSharpCode.TextEditor\Project\Src\Gui\Caret.cs
Assume a form with a textbox on it:
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
CreateCaret(textBox1.Handle, IntPtr.Zero, 10, textBox1.Height);
ShowCaret(textBox1.Handle);
}
}
I would use System.Drawing to draw a custom cursor (bitmap), maybe with a timer to let it blink like another cursor.
Get the current position of the Cursor in pixels and draw a bitmap over that cursor. Can be tricky to find the correct position, but should be doable.
Have a look here for Owner drawn textbox in winforms.
Use:
richTextBoxConsole.GetPositionFromCharIndex(cursorPos)
Hide the normal caret and draw your own? Not tested, but should work I think.
I am attempting to use this code sample to control the Windows XP On-Screen Keyboard (OSK.exe) from a C# (.NET 3.5) Winforms application:
[DllImport("User32.dll")]public static extern Int32 SetForegroundWindow(int hWnd);
[DllImport("user32.dll")]public static extern int FindWindow(string lpClassName, string lpWindowName);
private void BringToFront(string className,string CaptionName)
{
SetForegroundWindow(FindWindow(className,CaptionName));
}
private void Form1_Load(object sender, EventArgs e)
{
BringToFront("Notepad", "Untitled - Notepad");
}
How do I determine the accurate className? I assume that the CaptionName is 'On-Screen Keyboard'.
Looks like the classname is: "OSKMainClass"
Here is the code I used to find this. It's just a simple C# Forms App
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern int GetClassName(int hWnd, StringBuilder lpClassName, int nMaxCount);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int hWnd = FindWindow(null, "On-Screen Keyboard");
StringBuilder buffer = new StringBuilder(128);
GetClassName(hWnd, buffer, buffer.Capacity);
MessageBox.Show(buffer.ToString());
}
Got this from the following sources Activate Any Window With API
and MSDN GetClassName function