Various methods for obtaining the window handle in VC.

Source: Internet
Author: User
Afxgetmainwnd
Afxgetmainwnd
Hwnd = afxgetmainwnd ()-> m_hwnd;

Gettopwindow
Function: This function checks the subwindow Z sequence associated with a specific parent window (Z sequence: vertical screen direction, that is, stacked order ), return the handle of the subwindow at the top of the Z-order.
Function prototype: hwnd gettopwindow (hwnd );
Parameters:
Hwnd: handle of the parent window in the queried order. If this parameter is null, the function returns the window handle at the top of the Z sequence.
Return Value:
If the function succeeds, the return value is the sub-window handle at the top of the Z sequence. If the specified window has no child window, the return value is null.

Getforegroundwindow
Function: This function returns the window handle of the foreground window of the current system.
Function prototype: hwnd getforegroundwindow (void)
Return Value: The handle returned by the function in the foreground window.

Getactivewindow

Function: This function obtains the window handle of the active window related to the Message Queue of the thread that calls this method (that is, the window handle of the active window of the current process ).
Function prototype: hwnd getactivewindow (void)
Return Value: the return value is the handle of the active window related to the Message Queue of the calling thread. 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") afxgetmainwnd
Afxgetmainwnd
Hwnd = afxgetmainwnd ()-> m_hwnd;

Gettopwindow
Function: This function checks the subwindow Z sequence associated with a specific parent window (Z sequence: vertical screen direction, that is, stacked order ), return the handle of the subwindow at the top of the Z-order.
Function prototype: hwnd gettopwindow (hwnd );
Parameters:
Hwnd: handle of the parent window in the queried order. If this parameter is null, the function returns the window handle at the top of the Z sequence.
Return Value:
If the function succeeds, the return value is the sub-window handle at the top of the Z sequence. If the specified window has no child window, the return value is null.

Getforegroundwindow
Function: This function returns the window handle of the foreground window of the current system.
Function prototype: hwnd getforegroundwindow (void)
Return Value: The handle returned by the function in the foreground window.

Getactivewindow

Function: This function obtains the window handle of the active window related to the Message Queue of the thread that calls this method (that is, the window handle of the active window of the current process ).
Function prototype: hwnd getactivewindow (void)
Return Value: the return value is the handle of the active window related to the Message Queue of the calling thread. 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 "))

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.