简体   繁体   中英

How can i programmatically use aero snap features from C# code

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

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);
    }
}

我还需要这样做,并且发现Windows Input Simulator非常有用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM