tags:

views:

48

answers:

1

How can I get a window visible before processing?

I'm trying to do something like this:

void CMyClass::OnButton1Clicked()
{
 CString class_str = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW, ::LoadCursor(NULL,IDC_ARROW),(HBRUSH)::GetStockObject(WHITE_BRUSH),::LoadIcon(NULL, IDI_APPLICATION));
 CWnd* wnd = new CWnd();
 wnd->Create(class_str,"Test",WS_CHILD | WS_VISIBLE | WS_BORDER, CRect(0,0,100,100), AfxGetMainWnd(), 1432);

 //AfxMessageBox("test"); // <- if this is executed wnd gets visible before sleep()

 Sleep(5000);  //this would be processing

 wnd->DestroyWindow();
 delete wnd;

 return;
}

The window doesn't show at all. I've tried displaying it in an UI thread but it doesn't show either. If I do a message loop until window is created, it shows up but after a while the main message loop asserts.

I know it shouldn't be made like this but rather processing in worker thread, but with current code I'm working with it would require changes too big.

+2  A: 

Disclaimer: I don't know MFC, but I know the windows API, which is what MFC was built on top of.

Perhaps you need to make a call to ShowWindow(). Also, are you handling the WM_CREATE message? If so, perhaps you are returning the wrong value.

If the above doesn't apply or make sense, disregard.

Alexander Rafferty
I think this is correct. You have to call ShowWindow. And it would be good to check the result of Create.
dwo
ShowWindow makes no difference, and WS_VISIBLE style should show it on create anyway. I think the problem is the message loop not running and handling WM_PAINT on the new window. If I show message box before the processing, the window is drawn. That is what I want to achive without message box.
teemu
It has nothing to do with WM_PAINT. I have made a window show before handling the message loop.
Alexander Rafferty