Console App GetMeassage never returns. - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Console App GetMeassage never returns.

Hello.

I am trying to add a window to my console app.
I create a thread for message loop, but it doesn't
work because GetMessage never returns.
Can you help me?

Many thanks.

[code]


#include


HANDLE handle=NULL;
HINSTANCE hInst=NULL;
HWND hwnd=NULL;
MSG msg;
DWORD thdid=NULL;


//--------------------
DWORD WINAPI pmpthd(LPVOID){ //message loop thread
int status;

while(1){
status=GetMessage(&msg,NULL,0,0);
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message==WM_QUIT || status<=0)break;
}
return 0;
}
//-------------------


LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){

switch(msg){

case WM_CREATE:
ShowWindow(hwnd,SW_RESTORE);
break;

case WM_KEYDOWN:
switch(wParam){
case VK_ESCAPE:
PostQuitMessage(0);
break;
}

MessageBox(hwnd,"OK","OK",MB_OK);

break;
}

return DefWindowProc(hwnd,msg,wParam,lParam);
}


void outwin(){

hInst=GetModuleHandle(NULL);

WNDCLASSEX wc;

wc.cbSize =sizeof(WNDCLASSEX);
wc.lpszClassName="MyClass";
wc.lpfnWndProc =WndProc;
wc.hInstance =hInst;
wc.style =0; //CS_HREDRAW | CS_VREDRAW;
wc.hCursor =LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH) COLOR_BTNFACE;
wc.cbClsExtra =0;
wc.cbWndExtra =0;
wc.hIcon =NULL;
wc.hIconSm =NULL;
wc.lpszMenuName =NULL;

if(!RegisterClassEx(&wc)){MessageBox(NULL,"Register class failed","RC FAILED",MB_OK);return;}

hwnd=CreateWindowEx(WS_EX_TOPMOST, //WS_EX_APPWINDOW,
"MyClass",
"Output Window (Enter for Fullscreen)",
WS_CAPTION|WS_VISIBLE|WS_BORDER, //WS_OVERLAPPEDWINDOW,
50,
50,
320,
200,
NULL,
NULL,
hInst,
NULL
);

if(!hwnd){MessageBox(NULL,"Create window failed","CW FAILED",MB_OK);return;}


handle=CreateThread(0,0,pmpthd,0,0,&thdid);
}


////////////////////////////


int main(int argc,char* argv[]){

outwin();

while(1);

return 0;
}


[/code]

Comments

  • vlad777vlad777 Posts: 53Member

    I figured it out.

    Window or dialog must be created from the thread procedure
    that contains message loop.

    In my case outwin() must be called from pmpthd() not from main().

    Cheers.
Sign In or Register to comment.