Question

I have a simple question. How can I access aero snap programmatically from my C# code. Like, if I click a button "Snap Left", I want my program window to snap to the left, just like when its drug over there manually. I looked all over SO, but all the questions seem to be about aero snap not working with a form, and keeping it from snapping a form. Not programmatically snapping a form. I'm happy to use interloping. Thanks

Was it helpful?

Solution

What you can do, assuming you are on Windows 7, is to send the AreoSnap Keypress to the currently active window.

This codeproject has a very nice article on doing just that.

Also check out this SO question.

It seems that one way to do this is use sendmessage in User32.dll.

Here is an example of this, assuming "notepad" is the program you want to send the keystroke to:

[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
private void button1_Click(object sender, EventArgs e)
{
    Process [] notepads=Process.GetProcessesByName("notepad");
    if(notepads.Length==0)return;            
    if (notepads[0] != null)
    {
        IntPtr child= FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
        SendMessage(child, 0x000C, 0, textBox1.Text);
    }
}

OTHER TIPS

I also needed to do this and found the Windows Input Simulator very helpful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top