|
-
June 11th, 2011, 02:40 PM
#1
[RESOLVED] Text drawing prog compiles, but FREEZES
I am learning Visual C++ and making a program, which works like Paint, sort of. Idea is that I click the mouse button and text appears in the window. The thing is, the program compiles, but window doesn't appear. The program does appear in the taskbar.
Here is the code:
Code:
#include <Windows.h>
#include <string>
#include <vector>
using namespace std;
HWND ghMainWnd = 0;
HINSTANCE ghAppInst = 0;
struct TextObj
{
string s;
POINT p;
};
vector<TextObj> gTextObjs;
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int showCmd)
{
ghAppInst = hInstance;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = ghAppInst;
wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
wc.hCursor = ::LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)::GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = "MyWndClassName";
RegisterClass( &wc );
ghMainWnd = CreateWindow("MyWndClassName", "TextOut Example",WS_OVERLAPPEDWINDOW, 200, 200, 640, 480, 0, 0,ghAppInst, 0);
if(ghMainWnd == 0)
{
MessageBox(0, "CreateWindow - Failed", 0, 0);
return false;
}
ShowWindow(ghMainWnd, showCmd);
UpdateWindow(ghMainWnd);
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while( GetMessage(&msg, 0, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc = 0;
PAINTSTRUCT ps;
TextObj to;
switch( msg )
{
case WM_LBUTTONDOWN:
to.s = "Hello, world.";
to.p.x = LOWORD(lParam);
to.p.y = HIWORD(lParam);
gTextObjs.push_back( to );
InvalidateRect(hWnd, 0, false);
return 0;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
for(int i=0; i<gTextObjs.size(); i++)
TextOut(hdc, gTextObjs[i].p.x, gTextObjs[i].p.y, gTextObjs[i].s.c_str(), gTextObjs[i].s.size());
EndPaint(hWnd, &ps);
return 0;
case WM_KEYDOWN:
if( wParam == VK_ESCAPE )
DestroyWindow(ghMainWnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
Last edited by ovidiucucu; June 13th, 2011 at 01:28 AM.
Reason: added [CODE] tags
-
June 11th, 2011, 04:24 PM
#2
Re: Text drawing prog compiles, but FREEZES
CreateWidow is failing. From MSDN, "To get extended error information, call GetLastError"
-
June 11th, 2011, 07:12 PM
#3
Re: Text drawing prog compiles, but FREEZES
The thing is, the program compiles, but window doesn't appear.
The fact that something compiles has nothing to do with the fact that it actually works
-
June 11th, 2011, 09:08 PM
#4
Re: Text drawing prog compiles, but FREEZES
 Originally Posted by Skizmo
The fact that something compiles has nothing to do with the fact that it actually works 
Too many posters think that typing in code and compiling successfully is all that is needed to have a program work correctly. What winds up happening is that they are ill-prepared to debug a program, or even know that debugging is part of program development.
Regards,
Paul McKenzie
-
June 13th, 2011, 12:00 AM
#5
Re: Text drawing prog compiles, but FREEZES
OOOOKEY
any idea what seems to be the problem?
-
June 13th, 2011, 02:18 AM
#6
Re: Text drawing prog compiles, but FREEZES
First, write the 'switch' statement from window procedure a little bit nicer (i.e. clearer), like:
Code:
// ...
switch(msg)
{
case WM_PAINT:
// ...code here...
break; <-- do not forget 'break'
// ...other cases here...
default: // <-- do not forget 'default'
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0; // <-- '0' means "handled" for usual messages
}
Then you may notice that you have forgotten a 'default'.
-
June 13th, 2011, 07:11 AM
#7
Re: Text drawing prog compiles, but FREEZES
 Originally Posted by Diimaass
OOOOKEY
any idea what seems to be the problem?
Again, From MSDN, "To get extended error information, call GetLastError"
-
June 13th, 2011, 07:40 AM
#8
Re: Text drawing prog compiles, but FREEZES
 Originally Posted by GCDEF
Again, From MSDN, "To get extended error information, call GetLastError"
You are absolutely right. Generally, we have to call GetLastError if a WinAPI fnction has failed, including CreateWindow (see this FAQ).
However, in the OP code, that doesn't help.
 Originally Posted by Diimaass
Code:
[...]
case WM_DESTROY:
PostQuitMessage(0);
return 0;
// <-- MISSING 'default:' HERE.
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
Again, because of a missing 'default' keyword, DefWindowProc is not called for unhandled messages.
-
June 14th, 2011, 12:24 AM
#9
Re: Text drawing prog compiles, but FREEZES
Oh "defult:" solution worked.
Thanks a million guys!!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|