Sign in to follow this  

Opening a new window at runtime with MFC

This topic is 3587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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...?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 functions
protected:
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,

Share this post


Link to post
Share on other sites

This topic is 3587 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this