MFC get main window and parent window pointer

Source: Internet
Author: User


About handle and pointer in VC + + and its transformation



1.MFC window handle and pointer conversion
(1) The General Window object will have a corresponding handle variable, so we can take the M_hwnd property of the object to get the handle.
(2) using the GetSafeHwnd function to get the handle of the program's window class
(3) using the FromHandle function to get the desired pointer through the handle
Some of the other ways:
GetActiveWindow the current active window handle
AfxGetMainWnd Fetch main Window handle
GetForegroundWindow take the foreground window handle
FindWindow look for the window specified by the parameter
Enumwindow Enumeration window



2. Handle and pointer conversion of context-independent devices
(1) THIS->M_HDC
(2) Cdc::getsafehdc
(3) You can use the FromHandle function to get the pointer you want through the handle



3. Handle and pointer conversion of 3.Gdi objects
(1) This->m_hobject
(2) Cgdiobject::getsafehandle
(3) You can use the FromHandle function to get the pointer you want through the handle



4. The conversion of the window, the control pointer and the handle to each other

(1) The pointer is converted to a handle

In an MFC application, you first get a pointer to a window and then convert it to a handle

cwnd* pwnd;

HANDLE hWnd = Pwnd->getsafehwnd ();

(2) The handle is converted to a pointer

In an MFC application, you first get a handle to the dialog box control and then get its pointer

HANDLE hWnd;

GetDlgItem (Idc_xxx,&hwnd);

CWnd * pwnd = FromHandle (hWnd);

Ways to get the program window pointer

(1) Get the main frame window pointer (any time can be used, as long as the MFC program)



cwnd* pwnd = AfxGetMainWnd ();



(2) Get the control pointer in the dialog box



cwnd* pwnd = GetDlgItem (idc_xxx);



(3) Get a handle to a control in the dialog box



HANDLE GetDlgItem (M_hdlg,m_nid_dlgitem);



(4) Obtaining a handle to a GDI object



HANDLE m_hgdiobj = M_pgdiobj->getsafehanle ();



Copyright NOTICE: This article is the original article of the blogger, without the permission of the blogger may not be reproduced.









If the parent window class name is Cfatherdlg, you can call GetParent in the child window to get the parent window object pointer, as
cfatherdlg* Pfather = (cfatherdlg*) getparent ();
This enables you to invoke the exposed methods and class member variables of all the parent classes with Pfather.
This Code applies only to dialog boxes, and document applications are not used






In fact, the best way is to pass the pointer
GetParent (); Get to the parent pointer and then make a cast

Here, you want to understand that pointers are all 32 bits, just because C + + is a strongly typed language, so do not convert the compilation does not pass
So casting can fool the compiler
Although the conversion, but because no matter what pointers are 32 bits, so there is no loss of data

For example, you can get CFrameWnd pointers in CMyView.
CFrameWnd *pframewnd = ((CFrameWnd *) getparent ());






Http://www.cctry.com/thread-2635-1-1.html



A variable COM is defined in the parent class, the Subclass modal dialog box, which has a tab control, a button on one page of the tab control, and I want to call the parent's COM with a single button. Using GetParent (), there is a problem, not getting to COM's state, get the code as follows:

csetzerodlg *parent = (Csetzerodlg *) getparent ();

Ccalibrationdlg *pparent = (Ccalibrationdlg *) parent->getparent ();

How can i solve it?

 




Gets the variable of a class that, when it is instantiated, can be accessed by object or object pointers whenever the object or object pointer of the class is fetched, which is a general method, regardless of whether he is a parent or a subclass. You want to access a variable COM variable defined in the parent class, just get the parent class object. The GetParent function is the window class pointer that gets the parent window object of the window, not the Parent object window pointer. Do not confuse the parent-child relationship of the class with the window's parent-child relationship.

 








MFC programming often encounters a child window passing parameters to the parent window, which requires a pointer to the parent window.



Example: The main dialog box Cmymaindlg through the button Buttona into the dialog Cmyparentdlg,cmyparentdlg buttonb into the dialog box via the button Cmychilddlg. Now you need to access the contents of Cmyparentdlg and Cmymaindlg separately in Cmychilddlg, and you need to add the following two statements to Cmychilddlg:



Cmymaindlg *PMMD = (cmymaindlg*) AfxGetMainWnd (); Get main window pointer
Cmyparentdlg *pmpd = (cmyparentdlg*) this->getparent (); Get parent window pointer



Where this represents the current window pointer, pointing to CMYCHILDDLG;PMMD and PMPD is the pointer to the main window and the parent window.



Copyright NOTICE: This article is the original article of the blogger, without the permission of the blogger may not be reproduced.











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.