Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Can't find my bug!

This topic is 6738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post

Link to post
Share on other sites
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.


Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
Thanks a million people

My problem was using the global hWnd in my callback.
NULL is defined as being 0
::DefWindProc is great, but I need to use my own callback.

lets see if this works


One person said that the games industry is "a transfer of funds from the rich to the lucky"

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!