MFC obtains the object pointer of the dialog box through the dialog box handle

Source: Internet
Author: User
C *** dialog * pwnd = (C *** dialog **) fromhandle (hwnd); // get the object pointer of the dialog box by the handle
Pwnd-> XXX (); // call the XXX () function in C *** dialog ();

Use the window function: cwnd * getwindow to get the window pointer. pwnd-> m_hwnd (the handle of the Windows window attached to this Cwnd) At: fromhandle (hwnd); obtain the main window handle: cwnd * WND = afxgetmainwnd (); hwnd = WND-> getsafehwnd (); getdlgitem (idc_tree1) -> setfocus (); get control handle: hwnd hwndctrl =: getdlgitem (mainhwnd, idc_tree1); // get the handle of the tree control idc_tree idc_combo1 to get the handle of the current top-level window: hwnd mainhwnd =: getforegroundwindow (); // obtain the window handle of the current topmost to obtain the focus handle of the current window: hwnd currentfocus =: getfocus (); summary of various class handles in MFC

In VC programming, the biggest obstacle and problem is the message mechanism and pointer acquisition and operation. In fact, these contents are basically on every VC learning tool book.

This article is required and can solve many problems through msdn.
The following text mainly refers to some of my experiences in Pointer usage in programming. please correct me if it is inappropriate.
Generally, the framework we use is the MFC App Wizard (exe) Framework generated by Wizard provided by VC. There are pointer acquisition and Operation Problems in both multi-document and single-document.
The following section describes the general framework and then the usage of pointers in multithreading. The class to be used must contain the response header file. First, we generally obtain the instance pointer this, which is supported by the class (view, document, and dialog box). The purpose of this is to send a pointer to other classes or functions through functions in the class, this allows you to operate and use functions in a non-class.

1) Get the doc pointer in the view
Cyousdidoc * pdoc = getdocument (); one view can only have one document.
2) obtain the mainframe pointer in the app
The m_pmainwnd variable in cwinapp is the pointer to the mainframe. You can also: cmainframe * pmain = (cmainframe *) afxgetmainwnd ();
3) obtain the mainframe pointer in the view.
Cmainframe * pmain = (cmaimframe *) afxgetapp ()-> m_pmainwnd;
4) obtain the view (created) pointer
Cmainframe * pmain = (cmaimframe *) afxgetapp ()-> m_pmainwnd;
Cyouview * pview = (cyouview *) pmain-> getactiveview ();
5) Get the pointer to the current document
Cdocument * pcurrentdoc = (cframewnd *) m_pmainwnd-> getactivedocument ();
6) Get the status bar and toolbar pointer
Cstatusbar * pstatusbar = (cstatusbar *) afxgetmainwnd ()-> getdescendantwindow (afx_idw_status_bar );
Ctoolbar * ptoolbar = (ctoolbar *) afxgetmainwnd ()-> getdescendantwindow (afx_idw_toolbar );

7) if you add the toolbar and status bar variables to the framework, you can do the same.
(Cmainframe *) getparent ()-> m_wndtoolbar;
(Cmainframe *) getparent ()-> m_wndstatusbar;

8) obtain the menu pointer in mainframe.
Cmenu * pmenu = m_pmainwnd-> getmenu ();
9) obtain the application class from any class
AfxGetInstanceHandle gets the handle, and afxgetapp gets the pointer

B1. How can I obtain the "document class" handle in my own class and "application class?
SDI afxgetmainwnd ()-> getactiveview ()-> getdocument () to get the pointer
MDI afxgetmainwnd ()-> mdigetactive ()-> getactiveview ()-> getdocument () Get the pointer
B3. how to obtain the "document class" handle in "framework class?
SDI getactiveview ()-> getdocument () Get the pointer, MDI mdigetactive ()-> getactiveview ()-> getdocument () Get the pointer from cmainframe, getactiveview ()-> getdocument () get pointer from cchildframe
B4. how to obtain the "document class" handle in "View class?
Getdocument ()

C1. How can I obtain the "View class" handle in "document class?
Getview (), call the getfirstviewposition and getnextview functions to get the pointer.
C2. how do I obtain the "View class" handle in my own class and "application class?
SDI getactiveview get pointer
MDI mdigetactive ()-> getactiveview () Get pointer from cmainframe, getactiveview get pointer from cchildframe


Finally, we would like to remind you that after each handle is extracted, because the standard class handle is extracted for the first time, you must convert the standard handle to your own class handle during use.
For example:
Afxgetapp (); // obtain the winapp class handle,
Therefore, before performing the operation, remember to convert it into a custom class handle.
For example:
(Cmyapp *) afxgetapp ()-> xxxx (); // The xxxx () is a member of the defined class.

Afxfreelibrary
Decrements the reference count of the loaded dynamic-link library (DLL) module; when the reference count reaches zero, the module is unmapped.

Afxgetapp
Returns a pointer to the application's single cwinapp object.

Afxgetappname
Returns a string containing the application's name.

AfxGetInstanceHandle
Returns an hinstance representing this instance of the application.

Afxgetmainwnd
Returns a pointer to the current "Main" window of a non-ole application, or the in-place frame window of a server application.

Afxgetresourcehandle
Returns an hinstance to the source of the application's default resources. Use this to access the application's resources directly.

Afxinitrichedit
Initializes the version 1.0 rich Edit Control for the application.

Afxinitrichedit2
Initializes the version 2.0 and later rich Edit Control for the application.

Afxloadlibrary
Maps a DLL module and returns a handle that can be used to get the address of a DLL function.

Afxregisterwndclass
Registers a Windows window class to supplement those registered automatically by MFC.

Afxsocketinit
Called in a cwinapp: initinstance override to initialize Windows Sockets.

Afxsetresourcehandle
Sets the hinstance handle where the default resources of the application are loaded.

Afxregisterclass
Registers a window class in a DLL that uses MFC.

Afxbeginthread
Creates a new thread.

Afxendthread
Terminates the current thread.

Afxgetthread
Retrieves a pointer to the current cwinthread object.

Afxwininit
Called by the MFC-supplied winmain function, as part of the cwinapp initialization of a GUI-based application, to initialize MFC. Must be called directly for console applications using MFC.

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.