Opening a new window at runtime with MFC

Started by
4 comments, last by dangerdaveCS 16 years, 2 months ago
Hey, I'm trying to create a simple window via MFC at runtime to display debug output. I can get the window open and construct the initial contents, and it all looks hunky dorey - but the window is 'frozen'. For example, I cant drag it with the mouse - there is a permanent egg timer icon when the mouse cursor hovers over it. Also, more directly, SetWindowText calls to change a CEdit in the window do not fail, but do not visibly do anything either. It seems like some sort of thread/message passing issue, but I have no idea where to start. I've been scouring the web and MSDN, but can't find anything. Any hints on where to start looking would be much appreciated!
Dave.
Advertisement
OK, bit of an update. Turns out if I create the window in the main app's thread (i.e. the object derived from CWinApp) it works ok. It's only when I try to create the window in another thread that the new window just locks up, totally unresponsive.

I suspect its something to do with the main app object/thread being able to process events relating to windows, whereas other threads can't. Don't have a clue how to get around that though...?
Dave.
look up modeless dialogs.

essentially you have (warning: oversimplification)
class DebugWindow : CDialog{   // your window stuff}void ShowDebugWindow(){   DebugWindow* pDebug = new DebugWindow();   pDebug->Create(DebugWindow::IDD);   pDebug->ShowWindow(SW_SHOW);}


keep in mind that you'll need to delete the window at some point.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
If your window is "frozen" that means it's not responding to messages. Messages are posted to the message queue belonging to the thread that created the window: so for example if you use Thread A to create the window, Thread B won't receive it's messages.

I think ChaosEngine has the right idea here: if all your window is doing is displaying debug output, you don't really need a separate thread for it. Just create modeless dialog, and Windows will do most of the grunt work for you.
Thanks for the suggestions! Sounds like modeless dialogs is what I want, but I tried and still get the same issue (plus some others). Time to paste some code, methinks...


OK, so I have:
void DebugHandler::AddDebugFrame(void* pObj){	CDebugFrame* pFrame = new CDebugFrame;	pFrame->Create();	pFrame->MoveWindow(0, 0, 800, 200);	pFrame->ShowWindow(SW_SHOW);	DebugHandler::m_mappDebugFrames[pObj] = pFrame;}class CDebugFrame : public CDialog{	friend class DebugHandler;	public:	CDebugFrame();	virtual ~CDebugFrame();// Overrides	BOOL Create();// Generated message map functionsprotected:		afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);	afx_msg void OnSize(UINT nType, int cx, int cy);// Application specific.protected:	CString m_strTextBox;	CStatusBar  m_wndStatusBar;	CEdit m_wndTextBox;};BOOL CDebugFrame::Create(){	// Base class does the real work	return CDialog::Create(IDD_DEBUGDIALOG);}




Then I find that the following works OK:
BOOL CBioMASSApp::InitInstance(){	CWinApp::InitInstance();	DebugHandler::AddDebugFrame(this);...


But then this causes *both* the main window and the dialog to freeze (whereas it was just the dialog that froze before):
UINT __cdecl CBioMASSApp::ThreadApplication(LPVOID lpParameter){	CBioMASSApp* pApp = (CBioMASSApp*)lpParameter;	DebugHandler::AddDebugFrame(pApp);...


[Also now the OnCreate and OnSize message handlers are never called, whereas they were before, but first things first...]


Any ideas?

Thanks,
Dave.
OK, I managed to get around the freezing problem using a 'user interface thread' .. but this is extreme overkill for what I want. I would still welcome any ideas how to sort out the above issues.
Dave.

This topic is closed to new replies.

Advertisement