PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Window code problem!

Hi guys,

I have this little piece of code as a test and when I include it into my project it's acting very weird. First the window appears no problem it draws "Hello World!" etc... But when I close it, I don't seem to be receiving the WM_CLOSE neither the WM_DESTROY and the execution is stuck inside the GetMessage routine. The Window disappear on screen and everything seems normal on the PPC side, except that my program is hung and no WM_CLOSE or WM_DESTROY are passed to the application.

Code:
void TestWnd (HINSTANCE hInstance)
{
WNDCLASS wc;

wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = 0;
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = L"TestW";

RegisterClass(&wc;);

HWND h = CreateWindow(L"TestW", L"Caption", WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(h, SW_SHOW);
UpdateWindow(h);
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static SHACTIVATEINFO s_sai;
WNDCLASS wc;
wchar_t cn[MAX_PATH];
HDC hdc;
PAINTSTRUCT ps;

switch (message)
{
case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps;);
GetClientRect(hWnd, &rt;);
DrawText(hdc, L"Hello World!", 12, &rt;, DT_SINGLELINE | DT_VCENTER + DT_CENTER);
EndPaint(hWnd, &ps;);
break;

case WM_CLOSE:
DestroyWindow(hWnd);
break;

case WM_DESTROY:
PostQuitMessage(0);
break;

case WM_ACTIVATE:
SHHandleWMActivate(hWnd, wParam, lParam, &s;_sai, FALSE);
break;

case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s;_sai);
break;

default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
TestWnd (hInstance);

MSG msg;

while (GetMessage(&msg;, NULL, 0, 0) > 0)
{
TranslateMessage(&msg;);
DispatchMessage(&msg;);
}

return msg.wParam;
}


Thanx in advance for the help,
Kornalius
Is this on the PPC? Assuming it is, how do you close the window?

The "x" or "ok" button at the top right doesn't send a WM_CLOSE message to your window. It is in fact sending WM_COMMAND with the ID set to IDCANCEL or IDOK.

If you want your window to close when the user press it, simply send a WM_CLOSE message yourself when receiving IDOK/IDCANCEL.
Add this next to WM_CLOSE: just in case you're using the [X] button like Thierry mentioned.

Code:
case WM_COMMAND:
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
Oh! That is something new to me! :? Is there a site somewhere on the net that can explain these behaviors of the PPC OS? And would this explain why the whole application is froze inside the GetMessage function?

Regards,
Kornalius
Oups, I just tried this code:

Code:
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
if (wmId == IDOK || wmId == IDCANCEL)
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;


But it never goes inside the case WM_COMMAND at anytime during the window creations and even when I press the CLOSE button on the window. Is there something I have to do to activate the WM_COMMAND message?
I just tried creating a new PPC 2k3 Hello World project and the same thing happens. WM_COMMAND is never sent to the window. Maybe there is a setting inside the compiler or something that I am missing? I am using eVc 4.
It seems I was wrong... See here for more details: http://www.pocketpcdn.com/articles/close_app.html
Thanx that was of very good help. 8)

Regards,
Kornalius