Acquisition of window handle in various cases of VC

Source: Internet
Author: User



AfxGetMainWnd

AfxGetMainWnd getting its own window handle

HWND hwnd = AfxGetMainWnd ()->m_hwnd;


Gettopwindow

function function: This function examines the z-order of the child window associated with a particular parent window (Z-order: The direction of the vertical screen, that is, the stacking order), and returns a handle to the child window at the top of the z-order.
Function prototype: HWND Gettopwindow (HWND hwnd);
Parameters:
HWnd: The handle of the parent window being sequenced. If the parameter is NULL, the function returns the window handle at the top of the z-order.
return value:
If the function succeeds, the return value is the child window handle at the top of the z-order. If the specified window has no child windows, the return value is null.


GetForegroundWindow
function function: This function returns the window handle of the foreground window of the current system.
Function prototype: HWND GetForegroundWindow (VOID)
Return value: The function returns the handle of the foreground window back.


GetActiveWindow

function function: The function can get the window handle of the active window associated with the message queue of the thread that called the method (that is, the window handle of the active window that gets the current process).

Function prototype: HWND GetActiveWindow (VOID)

Return value: The return value is the handle to the active window associated with the calling thread's message queue. Otherwise, the return value is null.

GetSafeHwnd

function function: The safest way to get a handle (HWND) for a Window object (a CWnd derived object) pointer is to use the GetSafeHwnd () function.
Consider the following example for the reason:
CWnd *pwnd = FindWindow ("Explorewclass", NULL); Want to find the resource Manager

HWND hwnd = pwnd->m_hwnd; Get It's HWND

Such a code will appear when the pwnd is empty, a "general protection error", and close the application, because it is generally not possible to access a null pointer to its members, if using the following code:

CWnd *pwnd = FindWindow ("Explorewclass", NULL); Want to find the resource Manager

HWND hwnd = Pwnd->getsafehwnd (); Get It's HWND

There is no problem because GetSafeHwnd can still be used if pwnd is null, just return null


IsWindowVisible
function function: This function obtains the visual state of a given window.

Function prototype: BOOL IsWindowVisible (HWND hwnd);

Parameters

HWnd: The handle of the window being tested.

return value:
If the specified window and its parent window have a ws_visible style, the return value is not 0, and if the specified window and its parent window do not have a ws_visible style, the return value is zero. Because the return value indicates whether the window has a ws_visible style, the function return value is nonzero even if the window is obscured by other windows.
Note:
The visual state of the window is indicated by the ws_visible bit. When the ws_visible bit is set, the window can be displayed, and any information that is drawn in the window will be displayed as long as the window has a ws_visible style.


IsWindow
function function: This function determines whether a given window handle indicates a window that already exists.
Function prototype: BOOL IsWindow (HWND hwnd);
Parameters:
HWnd: The handle of the window being tested.
return value:
If the window handle identifies an existing window, the return value is ture, and the return value is false if the window handle does not identify an existing window.

FindWindow
HWND FindWindow (LPCSTR lpclassname,lpcstr lpwindowname);
Parameters:
Lpclassname

Points to a null-terminated string that specifies the class name or an atom that can determine the class name string. If this argument is an atom, then it must be a global atom that has been created by the Globaladdatom function before calling this function. This atom (a 16bit value) must be placed in the low byte of the lpclassname, and the high byte of the Lpclassname is zeroed.

Lpwindowname

Points to a null-terminated string that specifies the window name (that is, the window caption). If this parameter is null, all window names are matched.
return value:
If the function executes successfully, the return value is the handle to the window that owns the specified window class name or window name.

If the function execution fails, the return value is NULL. You can get more detailed error information by calling the GetLastError function.
SetWindowText:
As for the change of the window title, we can make it through SetWindowText.
Note: If the properties of the window itself are not displayed, the function's call does not affect the window properties.
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")

Other:

1. Direct handle
GetActiveWindow (); To activate the window handle

Getforegroundwindows (); Handle to the foreground window

AfxGetApp ()->getmainwnd ()->getsafehwnd ()//very conventional, involves many methods

2. Enumeration type

HWND GetWindow (HWND hwnd, UINT ucmd); To have an HWND first, and then to indicate the ucmd relationship.

HWND FindWindowEx (HWND hwndparent,

HWND Hwndchildafter,

LPCTSTR Lpszclass, L

Pctstr Zwindow);

The handle by the Class name and window header may be more than one, so it is not unique to determine

3. Handle of other parameters

Process ID:

MARK: The ID of the handle can be used GetWindowThreadProcessId ()

And the handle of the ID can only be enumerated + compared.

Calendar all forms, and obtain their corresponding HWND further to obtain the corresponding PID, the known PID is, of course, you may want to determine the form state, if the process has only one form that is not necessary.

Cases:
If you want to display all the windows in the list box
BOOL CALLBACK Enumwindowsproc (
HWND hwnd,//Handle to parent window
LPARAM 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));

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Acquisition of window handle in various cases of VC

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.