-
1 Attachment(s)
Window
i dont understand! i spent more than 2 hours trying to fix this program...for now it should just create a window...i wrote the code myself from what i remembered...and when i compare it to other similar examples....could anyone please tell me what i forgot?
thanks in advance!
-
Let's see:
1) You shouldn't call your global HWND variable hwnd, as it is the same name you used in WndProc (no error, just not good)
2) I don't think WS_BORDER is an extended window style as they all begin with WS_EX_
3) I think you have to handle the WM_CREATE message, but I#m not absolutely sure.
4) You didn't assign a value to wc.hIconSm. This can cause RegisterWindowEx to fail. If you don't want to use it, use WNDCLASS and RegisterClass instead (Petzold does this, btw)
5) GetMessage(&msg, hwnd, NULL, NULL)
This can cause one of the nastiest errors I know. Since this will only receive messages targeted at hwnd, it will not get the WM_QUIT message (that is targeted on no window) and therefor never leave the message loop, even after the widow was destroyed. The program will run on forever, so you can't (for example) recompile it. Use GetMessage(&msg, NULL, 0, 0) > 0 to receive every message in your app and even be save from the GetMessage error return (-1).
6) I think the main problem is that you forgot the calls to ShowWindow and UpdateWindow
-
Try this:)
PHP Code:
//[email protected]
//Tom Zhang
//13
#include <windows.h>
const char tzID[]="myWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
switch(msg){
case WM_SHOWWINDOW:
MessageBox(hwnd,"Hi, welcome asswhole!",MB_ICONINFORMATION|0);
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
WNDCLASSEX tz;
HWND hwnd;
MSG Msg;
tz.cbSize=sizeof(WNDCLASSEX);
tz.style=0;
tz.lpfnWndProc=WndProc;
tz.cbClsExtra=0;
tz.cbWndExtra=0;
tz.hInstance=hInstance;
tz.hIcon=LoadIcon(NULL,IDI_APPLICATION);
tz.hCursor=LoadCursor(NULL,IDC_ARROW);
tz.hbrBackground=(HBRUSH)(COLOR_WINDOW+8);
tz.lpszMenuName=NULL;
tz.lpszClassName=tzID;
tz.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
if(!RegisterClassEx(&tz)){
return 0;
}
hwnd=CreateWindowEx(WS_EX_CLIENTEDGE,tzID,"HI",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,300,200,NULL,NULL,hwnd,NULL);
if(hwnd==NULL){
return 0;
}
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg,NULL,0,0)){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
-
-
i found the bug...but thanks though :)