   how to get the main window of current thread? (Win32)
  I am using Win32 API to write my game, and can't find the api for getting the main window of the current thread!In MFC, there are AfxGetMainWnd(), so I trace into MFC's source code but still can't find anything.I appreciate every help with this! Thanks!   
  Do you want a HWND to the current window?Dave   ;  Try GetGUIThreadInfo.   ;Quote:Original post by LessBreadTry GetGUIThreadInfo.Oh, it may be the right one! Thanks!But, I search all the source code of MFC, but can't find any referrence of GetGUIThreadInfo! Does this mean that MFC uses another thing to do it?   ;  I don't know for certain, but I presume at some point that MFC gets the HWND by calling CreateWindowEx.   ;  You're finding it hard to find this info because there is no such thing as a "main" window.  A thread can create as many windows as it wants.  You can use GetGUIThreadInfo or EnumThreadWindows and some heuristics to try to get something meanful but be ready to handle it when the thread has multiple top-level windows, or zero windows, or it has windows but they're disabled, etc, etc.   ;  Mike, but I don't find any GetGUIThreadInfo or EnumThreadWindows in the source code of CWinApp and CWinThread.I wonder if anyone knows where does MFC init the CWinThread::m_pMainWnd or CWinThread::m_pActiveWnd ??;  If you're using the Win32 API, why does it matter how MFC does it?   ;Quote:Original post by LessBreadIf you're using the Win32 API, why does it matter how MFC does it?The desire of learning... :)In fact, I can save the HWND when creating a window of course. But I think MFC may have a better solution.   ;Quote:Original post by michaeljQuote:Original post by LessBreadIf you're using the Win32 API, why does it matter how MFC does it?The desire of learning... :)In fact, I can save the HWND when creating a window of course. But I think MFC may have a better solution.As others have said, there is no such thing as a "main window" in Win32. It's a concept MFC uses. The main window of the thread is the window which, when closed, will cause the thread itself to terminate. MFC holds a pointer to it through CWinThread::m_pMainWnd. You can't find where it's initialized because you probably use the Document/View architecture and the code is lost somewhere in the Document Template functions. If you choose not to use Document/View, you can see that things are really simple(taken directly from the code generated by Visual Studio):BOOL CAsddsApp::InitInstance(){...// To create the main window, this code creates a new frame window// object and then sets it as the application's main window object.CMainFrame* pFrame = new CMainFrame;m_pMainWnd = pFrame;...}There's no "magic" here. The "main window" is an MFC concept and is handled by MFC and the framework, not Win32 API. If you are building your own framework on top of Win32 and want to implement the concept of "main window", you will do it all yourself.   