CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2011
    Posts
    3

    [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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Text drawing prog compiles, but FREEZES

    CreateWidow is failing. From MSDN, "To get extended error information, call GetLastError"

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Text drawing prog compiles, but FREEZES

    Quote Originally Posted by Skizmo View Post
    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

  5. #5
    Join Date
    Jun 2011
    Posts
    3

    Re: Text drawing prog compiles, but FREEZES

    OOOOKEY
    any idea what seems to be the problem?

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,241

    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'.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Text drawing prog compiles, but FREEZES

    Quote Originally Posted by Diimaass View Post
    OOOOKEY
    any idea what seems to be the problem?
    Again, From MSDN, "To get extended error information, call GetLastError"

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,241

    Re: Text drawing prog compiles, but FREEZES

    Quote Originally Posted by GCDEF View Post
    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.
    Quote Originally Posted by Diimaass View Post
    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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  9. #9
    Join Date
    Jun 2011
    Posts
    3

    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
  •  





Click Here to Expand Forum to Full Width

Featured