Obtain the window handle of Vc in various situations

Source: Internet
Author: User

The handle of the animation window. Otherwise, the return value is null.

 

Getsafehwnd

Function function: to obtain the handle (hwnd) of a window object (derived object of cwnd) pointer, the safest way is to use the getsafehwnd () function.
The following example shows the reason:
Cwnd * pwnd = findwindow ("invalid ewclass", null); // you want to find the resource manager.

Hwnd = pwnd-> m_hwnd; // obtain its hwnd

When the pwnd obtained at the beginning is empty, a general protection error occurs and the application is closed, because a null pointer cannot be used to access its members, use the following code:

Cwnd * pwnd = findwindow ("invalid ewclass", null); // you want to find the resource manager.

Hwnd = pwnd-> getsafehwnd (); // obtain its hwnd

This will not cause any problems, because although getsafehwnd can still be used when pwnd is null, it only returns NULL


Iswindowvisible
Function: This function obtains the visible state of a given window.

Function prototype: bool iswindowvisible (hwnd );

Parameters;

Hwnd: handle of the tested window.

Return Value:
If the specified window and its parent window have the ws_visible style, the return value is non-zero. If the specified window and its parent window do not have the ws_visible style, the return value is zero. The return value indicates whether the window has the ws_visible style. Therefore, the return value of the function is non-zero even if the window is overwritten by other windows.
Note:
The visible status of the window is indicated by the ws_visible bit. When the ws_visible bit is set, the window can be displayed. As long as the window has the ws_visible style, any information drawn in the window will be displayed.


Iswindow
Function: This function determines whether the given window handle identifies an existing window.
Function prototype: bool iswindow (hwnd );
Parameters:
Hwnd: handle of the tested window.
Return Value:
If the window handle identifies an existing window, the return value is true. If the window handle does not identify an existing window, the return value is false.

 

Findwindow
Hwnd findwindow (maid, lpstr lpwindowname );
Parameters:
Lpclassname

Point to a string ending with null to specify the class name or an atom that can determine the class name string. If this parameter is an atom, it must be a global atom that has been created through the globaladdatom function before calling this function. This atom (a 16-bit value) must be placed in the low byte of the lpclassname, and the high byte of the lpclassname must be set to zero.

Lpwindowname

A string that ends with null and is used to specify the window name (that is, the window title. If this parameter is null, all window names are matched.
Return Value:
If the function is successfully executed, the return value is the handle of the window with the specified window class name or window name.

If the function fails to be executed, the return value is null. You can call the getlasterror function to obtain more detailed error information.
Setwindowtext:
We can use setwindowtext to change the window title.
Note: If the window property does not display the title, the function call will not affect the window property.
// Set title for application's main frame window.
Afxgetmainwnd ()-> setwindowtext (_ T ("application title "))

// Set title for view's MDI child frame window.
Getparentframe ()-> setwindowtext ("_ T (" MDI child frame new title "))

// Set title for dialog's push button control.
Getdigitem (idc_button)-> setwindowtext (_ T ("button New title "))

 

 

 

Others:

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 (hwnd, uint ucmd); // you must first have hwnd and then specify the ucmd relationship.

Hwnd find1_wex (hwnd hwndparent,

Hwnd hwndchildafter,

Lpctstr lpszclass, l

Pctstr zwindow );

// Handle obtained by the class name and window title. There may be more than one handle, so it is not unique.

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

The working process of enumwindows () and enumwindowsproc () is as follows: when enumwindows obtains a window handle, it will call enumwindowspro and perform the desired processing in this function,

Then, enumwindows obtains the handle of the next window and submits it to enumwindowspro, knowing that all windows have been enumerated.
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 ));

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.