Various methods of getting form handle in VC

Source: Internet
Author: User
Tags message queue

AfxGetMainWnd
AfxGetMainWnd getting its own form handle
HWND hwnd = AfxGetMainWnd ()->m_hwnd;


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

GetForegroundWindow
function function: This function returns the form handle of the foreground form 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 form handle of the active form related to the message queue of the thread that called the method (that is, the form handle of the active form that gets the current process).
Function prototype: HWND GetActiveWindow (VOID)
Return value: The return value is a handle to the active form 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) pointer to a Form object (a CWnd derived object) is to use the GetSafeHwnd () function.
Consider the following examples for their reasons:
CWnd *pwnd = FindWindow ("Explorewclass", NULL); Want to find the resource Manager
HWND hwnd = pwnd->m_hwnd; Get It's HWND
This code will appear when the pwnd is empty, a "general protection error" and close the application, since it is generally not possible to access a null pointer to its members, assume the following code:
CWnd *pwnd = FindWindow ("Explorewclass", NULL); Want to find the resource Manager
HWND hwnd = Pwnd->getsafehwnd (); Get It's HWND
There will be no failure, because although pwnd is null, GetSafeHwnd can still be used, simply returning NULL


IsWindowVisible
function function: This function obtains the visual state of a given form.
Function prototype: BOOL IsWindowVisible (HWND hwnd);
The number of references;
HWnd: The handle of the tested form.
return value:
Assuming that the specified form and its parent form have a ws_visible style, the return value is not 0, assuming that the specified form and its parent form do not have a ws_visible style, and the return value is zero. Because the return value indicates whether the form has a ws_visible style, the function return value is nonzero even if the form is obscured by another form.
Note:
The visual state of the form is indicated by the ws_visible bit. When the ws_visible bit is set, the form can be displayed, and only the form has the ws_visible style, no matter what the drawing is in the form information will be displayed.


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


FindWindow:
HWND FindWindow (LPCSTR lpclassname,lpcstr lpwindowname);
Number of references:
Lpclassname
Points to a null-terminated string that specifies the class name or an atom that determines the class name string. Assuming that the parameter is an atom, 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 form name (that is, the form title). Assuming this parameter is NULL, the full form name is matched.
return value:
Assuming the function succeeds, the return value is the handle to the form that owns the specified form class name or form name.
If the function fails to run, the return value is NULL. The ability to get more specific error messages by calling the GetLastError function.


SetWindowText:
As for the change of the title of the form, we can use SetWindowText to realize
Note: The call to this function does not affect the form properties, assuming that the form itself is not displaying a caption.
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.