Thread: Centering a map, using GDI

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    Centering a map, using GDI

    Hi,

    I'm having some problem trying to figure out a way how to center my rectangle inside my window. I'm trying to make the window ro stay centered even when the window is stretched or compressed. But I can't figure out a way how.


    Here's my code:
    -The DrawRect() function where I'm trying to center the rectangle
    -The rest of the methods are just used to make the program run.
    Code:
    LRESULT CALLBACK HelloWndProc (HWND, UINT, WPARAM, LPARAM);
    void DrawRect(HWND hwnd);
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    					PSTR szCMLine, int iCmdShow){
    	static TCHAR szAppName[] = TEXT ("HelloApplication");
    	static HWND	hwnd;
    	MSG		msg;
    	WNDCLASS wndclass;
    
    	wndclass.style		= CS_HREDRAW | CS_VREDRAW;
    	wndclass.lpfnWndProc = HelloWndProc;
    	wndclass.cbClsExtra	= 0;
    	wndclass.cbWndExtra = sizeof(long)*2;
    	wndclass.hInstance	= hInstance;
    	wndclass.hIcon		= LoadIcon (NULL, IDI_APPLICATION);
    	wndclass.hCursor	= LoadCursor (NULL, IDC_ARROW);
    	wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
    	wndclass.lpszMenuName = NULL;
    	wndclass.lpszClassName = szAppName;
    
    	if (!RegisterClass (&wndclass)){
    		MessageBox (NULL, TEXT ("This program requires Windows 95/98/NT"),
    					szAppName, MB_ICONERROR);
    		return 0;
    	}
    	hwnd = CreateWindow(szAppName,		
    						TEXT("Quiz 2"), 
    						WS_OVERLAPPEDWINDOW,	
    						CW_USEDEFAULT,		
    						CW_USEDEFAULT,		
    						800,		
    						600,		
    						NULL,				
    						NULL,				
    						hInstance,			
    						NULL);			
    	SetWindowLong(hwnd,0,0);
    	SetWindowLong(hwnd,4,0);
    	ShowWindow(hwnd, iCmdShow);
    	UpdateWindow(hwnd);
    	
    	while (TRUE){
    		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    		{
    			if (msg.message == WM_QUIT)
    				break;
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    		else{
    			DrawRect(hwnd);
    			Sleep(500);
    		}
    	}
    	return msg.wParam;
    }
    
    void DrawRect(HWND hwnd)
    {
    	HDC hdc;
    	HBRUSH hBrush = CreateSolidBrush(RGB(0, 2, 255));
    	HPEN hPen = CreatePen(PS_DASH, 1,(RGB(250, 5, 5)));
    
    	static int  cxClient = 400;
    	static int  cyClient = 300;
    	static int EndX, EndY;
    	long x, y;
    	RECT rect;
    	PAINTSTRUCT ps ;
    	hdc = GetDC(hwnd);
    	SelectObject(hdc, hBrush);
    	SelectObject(hdc, hPen);
    	x = GetWindowLong(hwnd,0);
    	y = GetWindowLong(hwnd,4);
    
    	BeginPaint(hwnd, &ps);
    	SetMapMode(hdc, MM_ISOTROPIC);
    	SetWindowExtEx(hdc, cxClient, -cyClient, NULL);
    	SetViewportExtEx(hdc, 20, 40, NULL);
    	SetViewportOrgEx(hdc, 400, 600, NULL);
    
    	x *=2;
    	y *=2;
    	if ((x>= 6000) || (y>= 160000)){
    		x = 200;
    		y = 600;
    	}
    	if ((x==0) || (y==0)){
    		x = 200;
    		y = 600;
    	}
    	
    	Rectangle(hdc,0,0,x,y);
    	SetWindowLong(hwnd,0,x);
    	SetWindowLong(hwnd,4,y);
    
    	EndPaint (hwnd, &ps) ;
    	
    }
    
    LRESULT CALLBACK HelloWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    	switch (message)
    	{
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			return 0;
    	}
    	return DefWindowProc(hwnd, message, wParam, lParam);
    }

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Is there a reason you are using a game loop/message pump?

    BeginPaint() and EndPaint() can ONLY be called in response to a WM_PAINT msg.

    Any HDC from GetDC() must be returned to the default state (all the default GDI objects returned, see below) and ReleaseDC() called.

    When you use SelectObject() to change the pan/brush/bmp the default GDI object is 'pushed' out. You should catch this object and retunr it (with SelectObject() ) before 'freeing' the HDC.
    Otherwise you are leaking GDI memory, a very limited form of memory. Soon your PC will not be able to redraw screens, change fonts or colours ect.


    Code:
    HDC hdc=GetDC(NULL);
    
    //create new pen
    HPEN hNewPen = CreatePen.....
    
    HPEN *pOriginalPen = (HPEN*) SelectObject( hdc, hNewPen);
    //use
    
    //clean up
    SelectObject( hdc, pOriginalPen);
    DeleteObject( hNewPen);
    ReleaseDC(hdc);

    As to the size....
    Code:
    RECT    Rect;
    int 	iMargin=-10;//start ten pix from the edges
    
    //find the area we have to draw on
    GetClientRect(hWnd, &Rect);
    
    //calc the useable area
    InflateRect(Rect,iMargin,iMargin);//shrinK the rect by the margin
    
    //OR
    
    Rect.left+=10;
    Rect.Right-=10;
    Rect.top+=10;
    Rect.bottom+=10;
    
    FrameRect(hdc, Rect);
    
    ReleaseDC();
    Your code will not work as is. The unhandled WM_PAINT msgs will overwrite your changes.

    Look at 'double buffering'.

    Creating a memory DC when the app starts,
    using it to draw to (in your DrawRect() function), then calling for a WM_PAINT msg,
    BitBlt() from the memory DC in the WM_PAINT handler
    Clean up the memory DC when the app closes.


    NOTE: Code may not work 'as is'! Just written off the top of my head......
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding a Map to a program
    By Shogun32 in forum C++ Programming
    Replies: 1
    Last Post: 05-04-2009, 09:42 AM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  4. Creating a map engine.
    By suzakugaiden in forum Game Programming
    Replies: 11
    Last Post: 06-21-2005, 05:06 AM
  5. Searching STL Map Inside STL Map Object :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2002, 09:11 AM