Get main window pointer in MFC

Source: Internet
Author: User
Tags ole
Get main window pointer in MFC 2010/07/14 14:44
Author:Gods_ giant Ant

AfxGetMainWnd


cwnd* AfxGetMainWnd ();

return Value:

If the server has an object that can be activated onsite in the container, and the container is active, the function returns a pointer to the frame window object that contains the field activity document.
If there are no objects that can be activated in the container, or if your application is not an OLE server, this function only returns the m_pMainWnd of your application object.
If AfxGetMainWnd is called by the application's main thread, it returns the main window of the application according to the rules above. If the function is called by the application's secondary thread, the function returns the main window that is connected to the calling thread.

Description:

If your application is an OLE server, you should call this function to get the active main window pointer of the application instead of directly referencing the m_pMainWnd member of the Application object.
If your application is not an OLE server, then calling this function is equivalent to directly referencing the m_pMainWnd member of the Application object.



Above is the description in MSDN, look at the sentence with the font color red, if you are not in the main thread call AfxGetMainWnd (), then you will not get the main window of the program, as I now do a program used in the UI thread

So, to get the main window pointer in a non main thread, you can use the following statement
AfxGetApp ()->m_pmainwnd

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.