Can't find my bug!
I''m having trouble creating a window! I''m using vc++5, win32. When I have the code to create the window inside my WinMain it works. As soon as I move the exact same code to a near function (called IniApp at the bottom) it will not create the window! I''ve been trying to figure this out for days now! Please help me
(how do you do those code insert tags with this forum?)
anyway here it is:
#include windows.h
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int IniApp(HINSTANCE hInst, int nCmdShow);
char szProgName[] = "ProgName";
HWND g_hWnd = NULL;
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPreInst, LPSTR lpszCmdLine, int nCmdShow)
{
int nRetVal=0;
nRetVal = IniApp(hInst, nCmdShow);
if(nRetVal < 0) return 0; //I always get a -2 error
MSG lpMsg;
while(GetMessage(&lpMsg,0,0,0))
{
TranslateMessage(&lpMsg);
DispatchMessage(&lpMsg);
}
return(lpMsg.wParam);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT messg, WPARAM wParam, LPARAM lParam)
{
switch(messg)
{
case WM_PAINT:
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(g_hWnd,&ps);
TextOut(hdc,100,50,"Testing!$!%!@#$",15);
ValidateRect(hWnd,NULL);
EndPaint(g_hWnd,&ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return(DefWindowProc(g_hWnd,messg,wParam,lParam));
break;
}
return 0;
}
int IniApp(HINSTANCE hInst, int nCmdShow)
{
WNDCLASS wcApp;
wcApp.lpszClassName = szProgName;
wcApp.hInstance = hInst;
wcApp.lpfnWndProc = WndProc;
wcApp.hCursor = LoadCursor(NULL,IDC_ARROW);
wcApp.hIcon = NULL;
wcApp.lpszMenuName = 0;
wcApp.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH);
wcApp.style = CS_HREDRAW / CS_VREDRAW;
wcApp.cbClsExtra = 0;
wcApp.cbWndExtra = 0;
if(!RegisterClass(&wcApp))
{
return -1;
}
g_hWnd = CreateWindow(szProgName,"Simple Windows Template",
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,(HWND)NULL,(HMENU)NULL,
hInst,(LPSTR)NULL); //create window always fails
if(g_hWnd == NULL)
{
return -2;
}
ShowWindow(g_hWnd,nCmdShow);
UpdateWindow(g_hWnd);
return 1;
}
Well, I might just be blind here, but where is the WndProc defined for
wcApp.lpfnWndProc = WndProc;
?
I usually use
wcApp.lpfnWndProc = ::DefWindowProc;
I also usually put
wcApp.lpszMenuName = NULL;
rather than
wcApp.lpszMenuName = 0;
but I''m not sure whether that actually makes any difference.
HTH, good luck.
-fel
wcApp.lpfnWndProc = WndProc;
?
I usually use
wcApp.lpfnWndProc = ::DefWindowProc;
I also usually put
wcApp.lpszMenuName = NULL;
rather than
wcApp.lpszMenuName = 0;
but I''m not sure whether that actually makes any difference.
HTH, good luck.
-fel
I found your bug. You are calling DefWindowProc() with g_hWnd instead of hWnd. As your WndProc is called while creating the window g_hWnd is still 0 the first time it''s called.
As for the code tag it is (code) & (/code) with square brackets instead of paranthesis.
As for the code tag it is (code) & (/code) with square brackets instead of paranthesis.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement