Here paint is begin and it is assigned to hdc. Once your drawing is finished then you have to call EndPaint() method.
Call all the functions between BeginPaint() & EndPaint() methods.
Now let's see some basic drawing functions.
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.
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,
- PS_SOLID
- PS_DASH
- PS_DOT
- PS_DASHDOT
- PS_DASHDOTDOT
- PS_NULL
- 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.
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,
- HS_HORIZONTAL
- HS_BDIAGONAL
- HS_VERTICAL
- HS_CROSS
- HS_FDIAGONAL
- HS_DIAGCROSS
e.g :
HBRUSH hbrush;
hbrush=CreateHatchBrush(HS_CROSS,#00ff00);
Color