MFC obtains the pointer (object) between windows (bodies)

Source: Internet
Author: User
In many dialog box operations, MFC is often used to call functions or variables in another dialog box. You can use the following methods to solve this problem.

Hwnd =: findwindow (null, _ T ("sphere"); // obtain the handle of the dialog box.

C *** dialog ** pwnd = (C *** dialog **) C *** dialog: fromhandle (hwnd); // get the object pointer of the dialog box by the handle

Pwnd-> XXX (); // call the XXX () function in C *** dialog ();

For more information, see http://www.cnblogs.com/del/archive/2008/02/28/4105432.html.

Use the window function: cwnd * getwindow to obtain 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 ();
Set the control as the window focus: getdlgitem (idc_tree1)-> setfocus ();
Get control handle: hwnd hwndctrl =: getdlgitem (mainhwnd,
Idc_tree1); // obtain the handle of the tree control idc_tree
Idc_combo1
Obtain 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 ();
(1) Summary of various class handles obtained 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 generated by the Wizard provided by VC.
In the App Wizard (exe) framework, pointer acquisition and operation problems exist 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.

The key lies in understanding the meaning of m_pmainwnd, afxgetapp (), and afxgetmainwnd!

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 in the middle of the class you define.

(2) In MFCConversion between handles, pointers, and IDs

Win32 directly operates the handle, and each handle corresponds to a Windows window. VC encapsulates the handle class and indirectly operates the handle. Now the handle is only a member variable of the class.

From handle to pointer
Cwnd * pwnd = cwnd: fromhandle (hwnd); // a temporary cwnd object is created // and attached.

Pwnd-> attach (hwnd); // attaches a Windows window to a cwnd object

From pointer to handle
Hwnd = pwnd-> getsafehandle ();
Hwnd = pwnd-> m_hwnd;

In SDK programming, window IDs, handles, and pointers are mutually converted functions.

Mutual conversion between ID--HANDLE--HWND
ID-> handle ----------- hwnd =: getdlgitem (hparentwnd, ID );
ID-> pointer ----------- cwnd: getdlgitem ();
Handle-> ID ----------- id = getwindowlong (hwnd, gwl_id );
Handle-> pointer -------- cwnd * pwnd = cwnd: fromhandle (hwnd );
Pointer-> ID ---------- id = getwindowlong (pwnd-> getsafehwnd, gwl_id );
Getdlgctrlid ();
Pointer-> handle -------- hwnd = cwnd. getsafehandle () or mywnd-> m_hwnd;
Hicon-> ID--------HICON hicon = afxgetapp ()-> loadicon (niconid );
Hicon = loadicon (afxgetapp ()-> m_hinstance, makeintresource (niconid ));


Note: three methods for obtaining window handle

1. hwnd findwindow (maid, lpwindowname)

Hwnd find1_wex (hwnd hwndparent, hwnd hwndchildafter, lpctstr lpclassname, lptstr lpwindowname)

2. hwnd windowfrompoint (point & Point) // obtain the Windows hwnd at the current cursor position


This article from csdn blog: http://blog.csdn.net/cy757/archive/2009/08/07/4423756.aspx


Handle and pointer conversion in VC ++

1. Convert the handle and pointer of the MFC window
(1) A Window Object usually has a corresponding handle variable, so we can get the handle using the m_hwnd attribute of this object.
(2) Use the getsafehwnd function to get the handle of the window class of the program
(3) Use the fromhandle function to obtain the desired pointer through the handle.
Other methods:
Getactivewindow: Get the current active window handle
Afxgetmainwnd: Get the main window handle
Getforegroundwindow: the front-end window handle.
Findwindow: Find the window specified by the Parameter
Enumwindow enumeration window

2. Conversion of handles and pointers of context-independent devices
(1) This-> m_hdc
(2) CDC: getsafehdc
(3) You can use the fromhandle function to obtain the desired pointer through the handle.

3. Convert the handles and pointers of the GDI object
(1) This-> m_hobject
(2) cgdiobject: getsafehandle
(3) You can use the fromhandle function to obtain the desired pointer through the handle.

4. Mutual conversion of window, control pointer and handle

(1) convert pointer to handle

In the MFC application, you must first obtain the window pointer and then convert it into a handle.

Cwnd * pwnd;

Handle hwnd = pwnd-> getsafehwnd ();

(2) convert a handle to a pointer

In the MFC application, obtain the handle of the dialog box Control and Its pointer.

Handle hwnd;

Getdlgitem (idc_xxx, & hwnd );

Cwnd * pwnd = fromhandle (hwnd );

How to obtain the program window pointer

(1) obtain the window pointer of the main framework (it can be used at any time, as long as it is in the MFC Program)

Cwnd * pwnd = afxgetmainwnd ();

(2) obtain the control pointer in the dialog box

Cwnd * pwnd = getdlgitem (idc_xxx );

(3) obtain the handle of a control in the dialog box.

Handle getdlgitem (m_hdlg, m_nid_dlgitem );

(4) obtain the handle of the GDI object

Handle m_hgdiobj = m_pgdiobj-> getsafehanle ();

 

This article from csdn blog: http://blog.csdn.net/lovegod12/archive/2009/04/19/4092249.aspx

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.