CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Threaded View

  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

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