Thread: Drawing two or more rectangles on client area.. HELP!

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Drawing two or more rectangles on client area.. HELP!

    Here's my attempt on trying to draw two rectangles on the same window!

    Code:
    #include "stdafx.h"
    #include <windows.h>  //include all the basics
    #include <tchar.h>    //string and other mapping macros
    #include <string>
    
    // Define structure for colors
    typedef struct rgbColors {
    	int R,G,B;
    } RGBColor;
    
    #define MAXCOLOR 10
    
    //define an unicode string type alias
    typedef std::basic_string<TCHAR> ustring;
    //=============================================================================
    //message processsing function declarations
    LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
    int OnCreate(const HWND,CREATESTRUCT*);
    
    //non-message function declarations
    HWND CreateEdit(const HWND,const HINSTANCE,DWORD,const RECT&,const int, const ustring&);  
    
    void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc,HBRUSH hbrush, PAINTSTRUCT pntS, const RECT& rc);
    
    void PaintObject(HWND hwnd, const RECT& rc, RGBColor *c, int p);
    RECT initRecArea(int l,int r,int t,int b);
    
    inline int ErrMsg(const ustring&);
    
    //setup some edit control id's
    enum {
    	IDCE_SINGLELINE=200,
    	IDCE_MULTILINE,
    };
    //=============================================================================
    int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR pStr,int nCmd)
    {
    	ustring classname=_T("RFS Range");
    	WNDCLASSEX wcx={0};  //used for storing information about the wnd 'class'
    
    	wcx.cbSize			= sizeof(WNDCLASSEX);           
    	wcx.lpfnWndProc		= WndProc;             //wnd Procedure pointer
    	wcx.hInstance		= hInst;               //app instance
    	wcx.hIcon			= reinterpret_cast<HICON>(LoadImage(0,IDI_APPLICATION,IMAGE_ICON,0,0,LR_SHARED));
    	wcx.hCursor			= reinterpret_cast<HCURSOR>(LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0,LR_SHARED));
    	wcx.hbrBackground	= reinterpret_cast<HBRUSH>(COLOR_BTNFACE+1);   
    	wcx.lpszClassName	= classname.c_str(); 
    
    	if (!RegisterClassEx(&wcx))
    	{
    		ErrMsg(_T("Failed to register wnd class"));
    		return -1;
    	}
    
    	int desktopwidth=GetSystemMetrics(SM_CXSCREEN);
    	int desktopheight=GetSystemMetrics(SM_CYSCREEN);
    
    	HWND hwnd=CreateWindowEx(0,                     //extended styles
    		classname.c_str(),     //name: wnd 'class'
    		_T("RFS Range"),	   //wnd title
    		WS_OVERLAPPEDWINDOW,   //wnd style
    		CW_USEDEFAULT,
    		CW_USEDEFAULT,
    		CW_USEDEFAULT,
    		CW_USEDEFAULT,
    		/*desktopwidth/4,        //position:left
    		desktopheight/4,       //position: top
    		desktopwidth/2,        //width
    		desktopheight/2,       //height*/
    		0,                     //parent wnd handle
    		0,                     //menu handle/wnd id
    		hInst,                 //app instance
    		0);                    //user defined info
    	if (!hwnd)
    	{
    		ErrMsg(_T("Failed to create wnd"));
    		return -1;
    	}
    
    	ShowWindow(hwnd,nCmd); 
    	UpdateWindow(hwnd);
    	MSG msg;
    
    	while (GetMessage(&msg,0,0,0)>0)
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return static_cast<int>(msg.wParam);
    }
    
    //=============================================================================
    LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
    	int pos=0;
    	RGBColor colors[MAXCOLOR] = {{0x80,0x80,0x80},{0x00,0xFF,0x00},
    	{0xFF,0xFF,0xFF},{0x00,0x00,0x00}};
    
    	switch (uMsg)
    	{
    	case WM_CREATE:
    		//return OnCreate(hwnd,reinterpret_cast<CREATESTRUCT*>(lParam));
    		return 0;
    	case WM_PAINT:
    		// Draw rectangle area 1
    		PaintObject(hwnd,initRecArea(5,5,300,350),colors,pos);
    		// Draw rectangle area 2
    		PaintObject(hwnd,initRecArea(400,5,800,250),colors,pos);
    		return 0;
    
    	case WM_TIMER: 
    		/* Force the WM_PAINT */
    		InvalidateRect (hwnd, NULL, TRUE);
    		UpdateWindow(hwnd);
    		break;
    
    	case WM_DESTROY:
    		PostQuitMessage(0);    //signal end of application
    		return 0;
    	default:
    		//let system deal with msg
    		return DefWindowProc(hwnd,uMsg,wParam,lParam);  
    	}
    }
    //=============================================================================
    inline int ErrMsg(const ustring& s)
    {
    	return MessageBox(0,s.c_str(),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION);
    }
    //=============================================================================
    int OnCreate(const HWND hwnd,CREATESTRUCT *cs)
    {
    	RECT rc={10,10,200,30};
    
    	//the various edit control types are created by simply varying the style bits
    	CreateEdit(hwnd,cs->hInstance,0,rc,IDCE_SINGLELINE,_T(""));
    
    	rc.top+=40;
    	rc.bottom+=30;
    	CreateEdit(hwnd,cs->hInstance,ES_MULTILINE|WS_VSCROLL,rc,IDCE_MULTILINE,
    		_T("Multi\r\nline"));
    
    	return 0;
    }
    //=============================================================================
    HWND CreateEdit(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
    				const RECT& rc,const int id,const ustring& caption)
    {
    	dwStyle|=WS_CHILD|WS_VISIBLE;
    	return CreateWindowEx(WS_EX_CLIENTEDGE,             //extended styles
    		_T("edit"),                   //control 'class' name
    		caption.c_str(),              //control caption
    		dwStyle,                      //control style 
    		rc.left,                      //position: left
    		rc.top,                       //position: top
    		rc.right,                     //width
    		rc.bottom,                    //height
    		hParent,                      //parent window handle
    		//control's ID
    		reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),
    		hInst,                        //application instance
    		0);                           //user defined info
    }
    //=============================================================================
    
    // Purpose: Draws a rectangle which will have text boxes for input data
    void DrawARectangle(HWND hwnd, HPEN hpen, HDC hdc, 
    					HBRUSH hbrush, PAINTSTRUCT pntS, const RECT& rc) 
    {
    	HPEN hpenOld;
    	HBRUSH hbrushOld;
    
    	// Select the new pen and brush, and then draw.
    	hpenOld = (HPEN)SelectObject(hdc, hpen);
    	hbrushOld = (HBRUSH)SelectObject(hdc, hbrush);
    	Rectangle(hdc,rc.left,rc.top,rc.right,rc.bottom);
    
    	// Do not forget to clean up.
    	SelectObject(hdc, hpenOld);
    	DeleteObject(hpen);
    	SelectObject(hdc, hbrushOld);
    	DeleteObject(hbrush);
    }
    // Purpose: Draws an object on the window
    void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, int index)
    {
    	HDC hdc;
    	HPEN hpen;
    	HBRUSH hbrush;
    	PAINTSTRUCT pntS;
    
    	hdc = BeginPaint(hwnd, &pntS);									
    	hpen = CreatePen(PS_SOLID, 2, RGB(color[index+3].R, color[index+3].G, color[index+3].B));
    	hbrush = CreateSolidBrush(RGB(color[index].R, color[index].G, color[index].B));
    	DrawARectangle(hwnd, hpen, hdc, hbrush, pntS, rc);
    	EndPaint(hwnd, &pntS);
    }
    
    // Initialize rectangle area
    RECT initRecArea(int left, int top, int width, int height)
    {
    	RECT rec = {left, top, width, height};
    	return rec;
    }
    rectangles draws if one of PaintObject() function is called, calling both only draws the fisrt call... Any ideas on how to draw more than one rectangle on same window????

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Any ideas on how to draw more than one rectangle on same window????
    Code:
    void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, int index, HDC hdc, PAINTSTRUCT pntS);
    
    case WM_PAINT:
    		PAINTSTRUCT ps;
    		HDC hdc;
    		hdc = BeginPaint(hwnd, &ps);
    		PaintObject(hwnd,initRecArea(5,5,300,350),colors,pos,hdc, ps);
    		PaintObject(hwnd,initRecArea(400,5,800,250),colors,pos, hdc, ps);
    		EndPaint(hwnd, &ps);
    		return 0;
    
    // Purpose: Draws an object on the window
    void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, int index, HDC hdc, PAINTSTRUCT pntS)
    {
    //	HDC hdc;
    	HPEN hpen;
    	HBRUSH hbrush;
    //	PAINTSTRUCT pntS;
    //	hdc = BeginPaint(hwnd, &pntS);									
    	hpen = CreatePen(PS_SOLID, 2, RGB(color[index+3].R, color[index+3].G, color[index+3].B));
    	hbrush = CreateSolidBrush(RGB(color[index].R, color[index].G, color[index].B));
    	DrawARectangle(hwnd, hpen, hdc, hbrush, pntS, rc);
    //	EndPaint(hwnd, &pntS);
    }
    Also, your timer is not initialized. Initialize it in WM_CREATE:


    Code:
    SetTimer(hwnd, ID_TIMER, 500, NULL);

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by BobS0327 View Post
    Code:
    void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, int index, HDC hdc, PAINTSTRUCT pntS);
    
    case WM_PAINT:
    		PAINTSTRUCT ps;
    		HDC hdc;
    		hdc = BeginPaint(hwnd, &ps);
    		PaintObject(hwnd,initRecArea(5,5,300,350),colors,pos,hdc, ps);
    		PaintObject(hwnd,initRecArea(400,5,800,250),colors,pos, hdc, ps);
    		EndPaint(hwnd, &ps);
    		return 0;
    
    // Purpose: Draws an object on the window
    void PaintObject(HWND hwnd, const RECT& rc, RGBColor *color, int index, HDC hdc, PAINTSTRUCT pntS)
    {
    //	HDC hdc;
    	HPEN hpen;
    	HBRUSH hbrush;
    //	PAINTSTRUCT pntS;
    //	hdc = BeginPaint(hwnd, &pntS);									
    	hpen = CreatePen(PS_SOLID, 2, RGB(color[index+3].R, color[index+3].G, color[index+3].B));
    	hbrush = CreateSolidBrush(RGB(color[index].R, color[index].G, color[index].B));
    	DrawARectangle(hwnd, hpen, hdc, hbrush, pntS, rc);
    //	EndPaint(hwnd, &pntS);
    }
    Also, your timer is not initialized. Initialize it in WM_CREATE:


    Code:
    SetTimer(hwnd, ID_TIMER, 500, NULL);
    Thanks a lot, realized i had to create a device context for all drawings..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC: Clear Client Area
    By mrafcho001 in forum Windows Programming
    Replies: 2
    Last Post: 06-27-2005, 01:35 PM
  2. Displaying more shapes in the same client Area
    By sajeev_js in forum Windows Programming
    Replies: 1
    Last Post: 06-11-2005, 01:52 PM
  3. Problems Drawing OpenGL to the Client Area
    By Niytaan in forum Windows Programming
    Replies: 3
    Last Post: 10-27-2002, 07:15 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Clearing the client area
    By Isometric in forum Windows Programming
    Replies: 15
    Last Post: 01-29-2002, 10:07 PM