In many Windows Programming scenarios, we need a Window Handle to perform some tasks, like:
1. Send specified messages to the window,
2. Retrieve/Modify the text of the window,
3. Subclass the window to change its behaviors,
, and so on…
If the window is in our .NET Winform application, we can get its handle directly via Control.Handle property,
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.handle(VS.80).aspx
If the window is one from another native application, to get its handle, we have to call the Windows API FindWindow or FindWindowEx.
FindWindow function is used to retrieve a handle of a top-level window whose class name and window name match the specified strings.
http://msdn.microsoft.com/en-us/library/ms633499(VS.85).aspx
FindWindowEx can be used to find children windows of a specified parent window.
http://msdn.microsoft.com/en-us/library/ms633500(VS.85).aspx
The followings are Visual C#.NET PInvoke declarations for the two WinAPIs,
---------------------------------------------------
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
----------------------------------------------------
Codes:
IntPtr toplevelWindow = FindWindow(“classname”, ”windowcaption”);
IntPtr childWindow = FindWindowEx(toplevelWindow, new IntPtr(0), “classname”, “windowcaption”);
Related Threads:
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/c7c787d0-9426-4b18-b15e-d43821bbb0ea/
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/4c4f60ce-3573-433d-994e-9c17f95187f0/
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d52b9825-dd13-4fd4-bfa8-722114f2ba44
Please remember to mark the replies as answers if they help and unmark them if they provide no help.