Various methods to get the window handle

Source: Internet
Author: User

 

Writing a DLL encapsulation Dialog Box always involves how to get the window handle. Mark it and forget it.

1. directly obtain the handle

Getactivewindow (); // activate the window handle.

Getforegroundwindows (); // front-end window handle

Afxgetapp ()-> getmainwnd ()-> getsafehwnd () // It is common and involves many methods.

2. Enumeration type

Hwnd getwindow (
HwndHwnd,
UintUcmd); // You must first haveHwnd, and then specify ucmdRelationship

Hwnd find1_wex (
HwndHwndparent,
HwndHwndchildafter,
LpctstrLpszclass,
LpctstrLpszwindow); // Handle from the class name and window title. There may be more than one handle, so it is not uniquely determined.

3. Handle of other parameters

Process ID:

MARK: The ID obtained by the handle can be getwindowthreadprocessid ()

The handle of ID can only be enumerated and compared.

Repeat all the forms and obtain their corresponding hwnd to further obtain the corresponding PID, which is consistent with the known PID. Of course, you may need to judge the form status, if a process has only one form, no more.

Example:
If you want to display all windows in the list box
Bool callback enumwindowsproc (
Hwnd, // handle to parent window
Lparam // application-defined value
)
{
Char lpwintitle [256];
: Getwindowtext (hwnd, lpwintitle, 256-1 );
Cstring m_strtitle;
M_strtitle.format ("% s", lpwintitle );
Clistbox * p_l = (clistbox *) lparam;
If (! M_strtitle.isempty ())
{
P_l-> addstring (lpwintitle );
}
Return true;
}

Call enumwindows (enumwindowsproc, (lparam) getdlgitem (idc_list1 ));

 

This sectionProgramMark in the attached Assis DLL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.