Getting Started With Windows Programming In C/C++ : Basic Drawing

Firstly, create the Win32 project in Visual Studio and create the window.

If you don't know how to get started with Win32 & to create window,
In the previous chapter we already drew a text using DrawText() method. In previous chapter,as you can see how to call paint method for window. This can be done using WM_PAINT message. So,to draw on window, first you have to define two structure variables of HDC & PAINTSTRUCT.
 
HDC : Stands for Handle to Device Context.this structure is used to obtain graphical components.
PAINTSTRUCT : This is the Paint Structure.this structure allows to draw shapes.
 
Once these two structure variables are defined then define the WM_PAINT message option in switch statement. To start drawing on the window,first you have to Begin the paint on the window using BeginPaint() method. 
 
Syntax : BeginPaint(HWND hwnd,PAINTSTRUCT*ps)
 
e.g : hdc=BeginPaint(hwnd,&ps);
 
Here paint is begin and it is assigned to hdc. Once your drawing is finished then you have to call EndPaint() method.
 
Syntax : EndPaint(HWND hwnd,PAINTSTRUCT*ps)
 
Call all the functions between BeginPaint() & EndPaint() methods.
 
Now let's see some basic drawing functions.
  1. LineTo(HDC,xEnd,yEnd) : This function draws a line to other coordinate.

  2. Rectangle(HDC,left,top,right,bottom) : This function draws a rectangle.

  3. RoundRect(HDC,left,top,right,bottom,width,height) : This function draws a bounded rectangle.

  4. Ellipse(HDC,left,right,right,bottom) : This function draws an ellipse.

  5. Polygon(HDC,points,numpoints) : This function draws an polygon.

  6. Arc(HDC,left,right,bottom,xStart,yStart,xEnd,yEnd) : This function draws an arc.

  7. Chord(HDC,left,right,bottom,xStart,yStart,xEnd,yEnd) : This function draws an chord.

  8. Pie(HDC,left,right,bottom,xStart,yStart,xEnd,yEnd) : This function draws an pie.

  9. TextOut(x,y,text,text_length) : This function draws a string on x,y position. etc.
Some drawing functions are the same as those are provided by graphics.h header file in simple C. Above all functions takes one first parameter of HDC. Remember the created HDC variable must have the value of BeginPaint() otherwise nothing will be drawn on the window.
 
Getting Client Rectangle
 
We all use ClientRectangle function to identify the width & height of the current control. To get client rectangle values of the window,first you have to define the structure variable of RECT.

Call GetClientRect(HWND hwnd,RECT&rect).
 
e.g :
  • RECT rect;
  • GetClientRect(hwnd,&rect);
Getting created window Width & Height
 
Define the integer type width & height variables. Define the WM_SIZE switch case statement. The width & height of current window is provided by LPARAM variable.

Call LOWORD() & HIWORD() functions.
 
LOWORD : This function returns a long value of the window width.
HIWORD : This function returns the height of window.
 
e.g :

case WM_SIZE :
width=LOWORD(lParam);
height=HIWORD(lParam);
return 0;
 
Pen
 
As you know what is pen in graphics, it is the color of drawn line. To use pen in Win32,create the structure variable of HPEN(Handle to Pen). And then call CreatePen() method.
 
Syntax : CreatePen(Pen_Style,line_width,Color)
 
e.g :

HPEN hpen;
hpen=CreatePen(PS_SOLID,2,#ff0000);

To use created pen for drawing coloured object,use SelectObject() method. Once your actions are completed then call DeleteObject().
 
e.g :

SelectObject(hdc,hpen);
Rectangle(0,0,50,60);
DeleteObject(hpen);
 
There are many pen styles,
  1. PS_SOLID
  2. PS_DASH
  3. PS_DOT
  4. PS_DASHDOT
  5. PS_DASHDOTDOT
  6. PS_NULL
  7. PS_INSIDEFRAME 
Brush
 
Brushes are used to fill drawn objects. To create brush in Win32,define the structure variable of HBRUSH (Handle to Brush). There are two methods used to create brush.
  1. SolidBrush
  2. HatchBrush 
To create SolidBrush,
 
Syntax : CreateSolidBrush(color); 
 
e.g :

HBRUSH hbrush;
hbrush=CreateSolidBrush(#0000f0);
To create HatchBrush(Style brush)
 
Syntax : CreateHatchBrush(hatch_style,color);
 
There are many styles of hatch brushes,
  1. HS_HORIZONTAL
  2. HS_BDIAGONAL
  3. HS_VERTICAL
  4. HS_CROSS
  5. HS_FDIAGONAL
  6. HS_DIAGCROSS
e.g :

HBRUSH hbrush;
hbrush=CreateHatchBrush(HS_CROSS,#00ff00);
 
Color
 
To use color for given object use RGB() method.

Syntax : RGB(red,blue,green); 
 
See the following program.

Copy & paste following program into your created Win32 project in Visual Studio. 
  1. #include"stdafx.h"  
  2. #include<Windows.h>  
  3.   
  4. LRESULT CALLBACK WndProc(HWNDUINTWPARAMLPARAM);  
  5.   
  6. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)  
  7. {  
  8.     WNDCLASS wndclass;  
  9.     MSG msg;  
  10.     HWND hwnd;  
  11.   
  12.     wndclass.lpszClassName = TEXT("Win32 Basic Drawing");  
  13.     wndclass.cbClsExtra = 0;  
  14.     wndclass.cbWndExtra = 0;  
  15.     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);  
  16.     wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);  
  17.     wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);  
  18.     wndclass.lpfnWndProc = WndProc;  
  19.     wndclass.lpszMenuName = NULL;  
  20.     wndclass.hInstance = hInstance;  
  21.     wndclass.style = CS_HREDRAW | CS_VREDRAW;  
  22.   
  23.     if (!RegisterClass(&wndclass))  
  24.     {  
  25.         MessageBox(NULL, TEXT("Window class is not registered"), TEXT("Error"), MB_OK);  
  26.         return 0;  
  27.     }  
  28.   
  29.   
  30.     hwnd = CreateWindow(TEXT("Win32 Basic Drawing"), TEXT("Win32 Basic Drawing"),  
  31.         WS_OVERLAPPEDWINDOW,  
  32.         20,  
  33.         20,  
  34.         850,  
  35.         550,  
  36.         NULL,  
  37.         NULL,  
  38.         hInstance,  
  39.         NULL  
  40.         );  
  41.   
  42.     ShowWindow(hwnd, iCmdShow);  
  43.     UpdateWindow(hwnd);  
  44.   
  45.     while (GetMessage(&msg, hwnd, 0, 0))  
  46.     {  
  47.         TranslateMessage(&msg);  
  48.         DispatchMessage(&msg);  
  49.     }  
  50.   
  51.     return msg.wParam;  
  52. }  
  53.   
  54.   
  55. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)  
  56. {  
  57.     HDC hdc;  
  58.     PAINTSTRUCT ps;  
  59.     int width,height;  
  60.     RECT rect = { 30, 20, 200, 100 };  
  61.     POINT pt[4] = {800,400,400,300,600,400,600,400};  
  62.     HWND hw;  
  63.   
  64.     switch (message)  
  65.     {  
  66.     case WM_CREATE :  
  67.           
  68.         return 0;  
  69.       
  70.     case WM_SIZE :  
  71.         width = LOWORD(lParam);  
  72.         height = HIWORD(lParam);  
  73.   
  74.     case WM_PAINT :  
  75.           
  76.         hdc = BeginPaint(hwnd, &ps);  
  77.   
  78.         HBRUSH hbrush;  
  79.         HPEN hpen;  
  80.   
  81.         //draw a rectangle with color blue  
  82.         hpen = CreatePen(PS_SOLID, 4, RGB(0, 0, 255));  
  83.         SelectObject(hdc, hpen);  
  84.         Rectangle(hdc, 10, 10, width - 10, height - 10);  
  85.         DeleteObject(hpen);  
  86.   
  87.         //fill a solid rectangle  
  88.         hbrush = CreateSolidBrush(RGB(250, 0, 0));  
  89.         FillRect(hdc, &rect, hbrush);  
  90.   
  91.         //fill a hatch rectangle  
  92.         hbrush = CreateHatchBrush(HS_CROSS, RGB(0, 240, 0));  
  93.         rect.left = 220;  
  94.         rect.top = 20;  
  95.         rect.right = 400;  
  96.         rect.bottom = 100;  
  97.         FillRect(hdc, &rect, hbrush);  
  98.   
  99.         //draw a ellipse  
  100.         hpen = CreatePen(PS_SOLID, 2, RGB(140, 0, 0));  
  101.         SelectObject(hdc, hpen);  
  102.         Ellipse(hdc,30,200,200,110);  
  103.         DeleteObject(hpen);  
  104.   
  105.         //fill an ellipse  
  106.         hbrush = CreateHatchBrush(HS_DIAGCROSS, RGB(255, 0, 250));  
  107.         SelectObject(hdc,hbrush);  
  108.         Ellipse(hdc, 420, 200, 220, 110);  
  109.         DeleteObject(hbrush);  
  110.   
  111.         //draw round rect  
  112.         hpen = CreatePen(PS_SOLID, 2, #00008c);  
  113.         hbrush = CreateHatchBrush(HS_HORIZONTAL, RGB(0, 0, 150));  
  114.         SelectObject(hdc, hpen);  
  115.         SelectObject(hdc, hbrush);  
  116.         RoundRect(hdc, 300, 210, 30, 300, 50, 50);  
  117.         DeleteObject(hbrush);  
  118.         DeleteObject(hpen);  
  119.   
  120.         //draw arc  
  121.         hpen = CreatePen(PS_SOLID, 2, RGB(0, 100, 0));  
  122.         SelectObject(hdc, hpen);  
  123.         Arc(hdc, 400, 320, 30, 450, 30, 40, 300, 400);  
  124.         DeleteObject(hpen);  
  125.   
  126.         //draw polygon  
  127.         hpen = CreatePen(PS_SOLID, 2, RGB(0, 140, 140));  
  128.         hbrush = CreateHatchBrush(HS_FDIAGONAL, RGB(0, 140, 140));  
  129.         SelectObject(hdc, hpen);  
  130.         SelectObject(hdc, hbrush);  
  131.         Polygon(hdc, pt, 4);  
  132.         DeleteObject(hbrush);  
  133.         DeleteObject(hpen);  
  134.   
  135.         //draw a text  
  136.         SetTextColor(hdc, RGB(0, 100, 0));  
  137.         TextOut(hdc, 600, 70, TEXT("Win32 Basic Drawing"),19);  
  138.           
  139.   
  140.         EndPaint(hwnd, &ps);  
  141.           
  142.         return 0;  
  143.     case WM_DESTROY:  
  144.     case WM_CLOSE :  
  145.         PostQuitMessage(EXIT_SUCCESS);  
  146.         return 0;  
  147.     }  
  148.     return DefWindowProc(hwnd, message, wParam, lParam);  
  149. }   
Remember Pen only draws a colored object & Brush only fill the color to the object. In above program,you can see,to draw a RoundRect i have selected two objects,one is pen & other is brush. Drawing complicated shapes in Win32 is completely depends on Mathematics.

Here's the output of above program,