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 |