Using C#, how do I set tab positions in a multiline textbox? - c#

Is there a graceful way to set custom tab sizes/positions in a multiline textbox in C#?

You need to send the EM_SETTABSTOPS message, like this:
static class NativeMethods {
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, ref int lParam);
}
static void SetTabs(TextBox box) {
//EM_SETTABSTOPS - http://msdn.microsoft.com/en-us/library/bb761663%28VS.85%29.aspx
int lParam = 16; //Set tab size to 4 spaces
NativeMethods.SendMessage(box.Handle, 0x00CB, new IntPtr(1), ref lParam);
box.Invalidate();
}

Apart from by vb 2013 the friendly people at microsoft have decided you no longer need the windows handle and you can no longer get at it.

Related

How can i SendMessage to other processes windows then notepad?

In the top of form1
const int WM_SETTEXT = 0X000C;
//include FindWindowEx
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
//include SendMessage
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
Then in the constructor
//getting notepad's process | at least one instance of notepad must be running
Process notepadProccess = Process.GetProcessesByName("devenv")[0];
//getting notepad's textbox handle from the main window's handle
//the textbox is called 'Edit'
IntPtr notepadTextbox = FindWindowEx(notepadProccess.MainWindowHandle, IntPtr.Zero, "Edit", null);
//sending the message to the textbox
SendMessage(notepadTextbox, WM_SETTEXT, 0, "Testing");
This is working for the notepad.
But if i want to send a message to another visual studio opened window of a project ?
For example i created a new project in a new visual studio and created in the project a new class called it test
And i want to send to the class some text for example:
using system;
For example this is the new class window:
After sending the message it will be like this:

C# - trigger key down event for active control

I found command System.Windows.Forms.SendKeys.Send() for sending keypress some key. This function work if open external app like a notepad and set focus and I will be see that my Key printed in this text field. How do same but with key down event, System.Windows.Forms.SendKeys.SendDown("A");, for example?
I tried call in Timer this command System.Windows.Forms.SendKeys.Send() but have runtime error associated with very fast taped.
You can't use the SendKeys class for that, unfortunately. You will need to go to a lower level API.
Poking a window with a keydown message
In Windows, keyboard events are sent to windows and controls via the Windows message pump. A piece of code using PostMessage should do the trick:
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
const uint WM_KEYDOWN = 0x0100;
void SendKeyDownToProcess(string processName, System.Windows.Forms.Keys key)
{
Process p = Process.GetProcessesByName(processName).FirstOrDefault();
if (p != null)
{
PostMessage(p.MainWindowHandle, WM_KEYDOWN, (int)key, 0);
}
}
Note that the application receiving these events may not do anything with it until a corresponding WM_KEYUP is received. You can get other message constants from here.
Poking a control other than the main window
The above code will send a keydown to the "MainWindowHandle." If you need to send it to something else (e.g. the active control) you will need to call PostMessage with a handle other than p.MainWindowHandle. The question is... how do you get that handle?
This is actually very involved... you will need to temporarily attach your thread to the window's message input and poke it to figure out what the handle is. This can only work if the current thread exists in a Windows Forms application and has an active message loop.
An explanation can be found here, as well as this example:
using System.Runtime.InteropServices;
public partial class FormMain : Form
{
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport("user32.dll")]
static extern IntPtr AttachThreadInput(IntPtr idAttach,
IntPtr idAttachTo, bool fAttach);
[DllImport("user32.dll")]
static extern IntPtr GetFocus();
public FormMain()
{
InitializeComponent();
}
private void timerUpdate_Tick(object sender, EventArgs e)
{
labelHandle.Text = "hWnd: " +
FocusedControlInActiveWindow().ToString();
}
private IntPtr FocusedControlInActiveWindow()
{
IntPtr activeWindowHandle = GetForegroundWindow();
IntPtr activeWindowThread =
GetWindowThreadProcessId(activeWindowHandle, IntPtr.Zero);
IntPtr thisWindowThread = GetWindowThreadProcessId(this.Handle, IntPtr.Zero);
AttachThreadInput(activeWindowThread, thisWindowThread, true);
IntPtr focusedControlHandle = GetFocus();
AttachThreadInput(activeWindowThread, thisWindowThread, false);
return focusedControlHandle;
}
}
The good news-- if SendKeys worked for you, then you might not need to do all this-- SendKeys also sends messages to the main window handle.

Show Hotkeys at All Times

Is is possible to make the underlining hotkey always visible on my controls without having to press the Alt key in a Windows form with Visual Studio - C#?
I only have a certain time when I need the controls of the form to be always underlined with the "_" under a character. So it would be good to have just the code to do it.
I could have the setting for Windows to always show underlining of shortcuts and hotkeys, but I only need it to happen a certain time.
Assuming you are using WinForms, you should be able to rely on the underlying Win32 mechanism. And that is the WM_UPDATEUISTATE message. The documentation says:
An application sends the WM_UPDATEUISTATE message to change the UI state for the specified window and all its child windows.
So you can send the message to the handle of the top-level window. You need to pass UIS_CLEAR for the low-order word of wParam and UISF_HIDEACCEL for the high-order word of wParam.
Here is some rather crude sample code. Bear in mind that my C# expertise is very limited.
public partial class Form1 : Form
{
private const uint WM_UPDATEUISTATE = 0x0128;
private const uint WM_QUERYUISTATE = 0x0129;
private const uint UIS_CLEAR = 2;
private const uint UISF_HIDEACCEL = 0x2;
public Form1()
{
InitializeComponent();
}
private void Form1_HandleCreated(object sender, PaintEventArgs e)
{
ClearHideAccel();
}
private void ClearHideAccel()
{
UIntPtr wParam = (UIntPtr)((UISF_HIDEACCEL << 16) | UIS_CLEAR);
NativeMethods.SendMessage(this.Handle, WM_UPDATEUISTATE, wParam, IntPtr.Zero);
}
}
internal class NativeMethods
{
[DllImport("User32", SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, IntPtr lParam);
}

How to simulate combination of keys(such as shift+left for selecting text) using global keyboard hook and postmessage()?

I am using a global keyboard hook (WH_KEYBOARD_LL) and sending the keys to a browser handle. I am able to get a single key pressed by the user but not able to get the combination of keys pressed(such as shift+left for selecting text).The code goes below...
private IntPtr ProcessKey(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0
&& wParam == (IntPtr)WM_KEY_EVENT.WM_KEYDOWN
|| wParam == (IntPtr)WM_KEY_EVENT.WM_SYSKEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
int vkCode1 = Marshal.ReadInt32(wParam);//here I am getting runtime
//error as Attempted to read or write protected memory.
//This is often an indication that other memory is corrupt.
SafeNativeMethods.PostMessage(m_browserHandle,(uint)WM_KEY_EVENT.WM_KEYDOWN,
Convert.ToInt32((System.Windows.Forms.Keys)vkCode),
Convert.ToInt32((System.Windows.Forms.Keys)vkCode1));
}
return SafeNativeMethods.CallNextHookEx(_hookID, nCode, wParam, lParam);
}
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint msg, int wParam, int lParam);
public static class WM_KEY_EVENT
{
public static int WM_KEYDOWN = 0x0100;
public static int WM_SYSKEYDOWN = 0x0104;
public static int WM_KEYUP=0x0101;
public static int WM_SYSKEYUP=0x0105;
};
I read some where that we can get the combination of key press by using wParam, which gives error as shown in the code above. Please suggest how to avoid that error or some alternative way to do it.
Your code has some errors in it. You're treating wParam as a pointer (since you're calling ReadInt32 with it), but according to the documentation it contains the window message.
lParam you should derefernece (using Marshal.PtrToStructure) to a KBDLLHOOKSTRUCT, it contains the key code and modifier key state.
And I don't see the point in casting vkCode to a System.Windows.Fórms.Keys value, and then right back to an int again.

RadioButton click on another form

We have a legacy program with a GUI that we want to use under control of a C# program to compute some values. We can successfully enter values in the numerical input controls, press the compute button, and read the produced answers from text display boxes.
But we can't seem to control a pair of radio buttons .
Calling CheckRadioButton() returns a code of success, but the control does not change state.
Sending a message of BM_CLICK does not change the state.
Attempts at sending WM_LBUTTONDOWN and WM_LBUTTONUP events haven't changed the state.
Has anyone been successful at "remote control" of radio buttons?
Portions of code to illustrate what we are doing:
[DllImport("user32.dll", EntryPoint="SendMessage")]
public static extern int SendMessageStr(int hWnd, uint Msg, int wParam, string lParam);
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg, long wParam, long lParam);
[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError=true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
[DllImport("user32.dll", EntryPoint="CheckRadioButton")]
public static extern bool CheckRadioButton(IntPtr hwnd, int firstID, int lastID, int checkedID);
static IntPtr GetControlById(IntPtr parentHwnd, int controlId) {
IntPtr child = new IntPtr(0);
child = GetWindow(parentHwnd, GetWindow_Cmd.GW_CHILD);
while (GetWindowLong(child.ToInt32(), GWL_ID) != controlId) {
child = GetWindow(child, GetWindow_Cmd.GW_HWNDNEXT);
if (child == IntPtr.Zero) return IntPtr.Zero;
}
return child;
}
// find the handle of the parent window
IntPtr ParenthWnd = new IntPtr(0);
ParenthWnd = FindWindowByCaption(IntPtr.Zero, "Legacy Window Title");
// set "N" to 10
IntPtr hwndN = GetControlById(ParenthWnd, 17);
SendMessageStr(hwndN.ToInt32(), WM_SETTEXT, 0, "10");
// press "compute" button (seems to need to be pressed twice(?))
int hwndButton = GetControlById(ParenthWnd, 6).ToInt32();
SendMessage(hwndButton, BM_CLICK, 0, 0);
SendMessage(hwndButton, BM_CLICK, 0, 0);
// following code runs succesfully, but doesn't toggle the radio buttons
bool result = CheckRadioButton(ParenthWnd, 12, 13, 12);
Send the BM_SETCHECK message. Be sure to use a tool like Spy++ to see the messages.
in this case i used another message BM_SETSTATE
SendMessage((IntPtr)hWnd, Win32Api.BM_SETSTATE, (IntPtr)newState, IntPtr.Zero);

Categories