How To Get A Handle To A Window

Table of contents:

How To Get A Handle To A Window
How To Get A Handle To A Window

Video: How To Get A Handle To A Window

Video: How To Get A Handle To A Window
Video: How to take of a wined up window handle(Make sure you don't lose the clip) 2024, May
Anonim

The concept of the user interface in Windows is based on the concept of a window. Dialogues, buttons, control panels, lists are all windows. Therefore, in order to perform any actions with any element of the interface of your own or someone else's application, you need to get a window handle.

How to get a handle to a window
How to get a handle to a window

Necessary

  • - Platform SDK package;
  • - compiler.

Instructions

Step 1

Get the handle to the window by creating it. Make calls to the CreateWindow or CreateWindowEx API functions, or call the appropriate wrapper methods (on the necessary objects) of the classes of the used framework or library that encapsulate the functionality of working with windows or controls. The CreateWindow and CreateWindowEx functions directly return the window handle on successful creation. If called unsuccessfully, they return NULL. The error reason code can be requested using the GetLastError API function. Prototypes and detailed descriptions of the parameters of the CreateWindow and CreateWindowEx functions can be obtained from the links https://msdn.microsoft.com/en-us/library/windows/desktop/ms632679%28v=vs.85%29.aspx and https:// msdn.microsoft.com / en-us / library / windows / desktop / ms632680% 28v = vs.85% 29.aspx When using classes of various frameworks, the creation of operating system window objects can occur either explicitly (by calling a method) or implicitly (implementation of the RAII strategy). Therefore, it is better to get the window handle on a ready-made initialized object. You can learn about the methods of the classes that return the window handle encapsulated by the object in the documentation of the corresponding framework. For example, in MFC, a similar method is GetSafeHwnd of the CWnd class.

Step 2

Get a handle to a window by searching for it. Use the FindWindow and FindWindowEx APIs or the appropriate class object wrappers for the framework you are using. FindWindow returns a handle to the top-level window if found, or NULL on failure. The search is performed by class name and window title. Description of the parameters and aspects of the function can be found at the link https://msdn.microsoft.com/en-us/library/windows/desktop/ms633499%28v=vs.85%29.aspx. The semantics of the FindWindowEx function are similar to FindWindow with the only the difference is that it searches for child windows. The documentation for FindWindowEx is available at

Step 3

Find the handle to the window while listing their sets. Use the API functions EnumWindows, EnumChildWindows, EnumThreadWindows, or methods of class objects of the framework you are using. The EnumWindows and EnumChildWindows functions enumerate top-level windows and child windows, respectively. The EnumThreadWindows function enumerates all non-child windows of the specified thread. Each of these functions must be passed a pointer to a callback procedure, which will be passed the handles of the found windows as parameters during operation. By combining enumeration functions, getting and analyzing window properties in the callback procedure, you can find the handle of the required window. The documentation for the functions described is given at the links: - EnumWindows: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633497%28v=vs.85%29.aspx; - EnumChildWindows: https:// msdn.microsoft.com/en-us/library/windows/desktop/ms633494%28v=vs.85%29.aspx; - EnumThreadWindows: https://msdn.microsoft.com/en-us/library/windows/desktop /ms633495%28v=vs.85%29.aspx.

Step 4

Get a handle to a window at known coordinates. The WindowFromPoint, ChildWindowFromPoint, ChildWindowFromPointEx functions return window handles to the scopes of the given point. The WindowFromPoint function is the easiest to use, but does not return the window handles for hidden and disabled windows. You can find documentation on its use at https://msdn.microsoft.com/en-us/library/windows/desktop/ms633558%28v=vs.85%29.aspx. The ChildWindowFromPoint and ChildWindowFromPointEx functions find handles for the child windows of a given parent window belonging to a given point. In this case, the behavior of ChildWindowFromPointEx can be flexibly controlled using an additional parameter. The documentation for these functions is provided by the links: - ChildWindowFromPoint: https://msdn.microsoft.com/en-us/library/windows/desktop/ms632676%28v=vs.85%29.aspx; - ChildWindowFromPointEx: https:// msdn.microsoft.com/en-us/library/windows/desktop/ms632677%28v=vs.85%29.aspx.

Recommended: