Page 218 - DCAP408_WEB_PROGRAMMING
P. 218
Windows Programming
Notes Polyline(hdc, pntArray, 2);
// Select the old pen back into the device context
SelectObject(hdc,oldPen);
Brushes
Creating and using Brushes is very similar to pens. We can use CreateSolidBrush to create a
solid coloured brush or CreateBrushIndirect to create a brush with specific styles (like hatched)
or even using a loaded bitmap. You can use a bitmap as a repeating pattern for your brush using
CreatePatternBrush. Here we will describe CreateSolidBrush for the others please look int he
MSDN help file.
HBRUSH CreateSolidBrush( COLORREF crColor)
This is a very simple function that takes the required colour and returns a handle to a brush. We
can use it in much the same way as the pen. To create a blue brush we would write:
HBRUSH blueBrush=CreateSolidBrush(RGB(0,255,0));
To use it to fill a rectangle:
RECT rct;
rct.left=10;
rct.right=100;
rct.top=10;
rct.bottom=200;
FillRect(hdc, &rct, blueBrush);
13.1.6 Window Size
When you create a window you can specify its size. However the size of the window does not
always relate to the drawing area as there are menu bars and borders that also take up room.
Additionally the user can simply alter the size of your window at any time. So we need a way of
determining the current drawing area. We do this using the GetClientRect call. This returns a
rectangle defining the area of the window your program (the client) can draw into. You must
pass in the handle to your window and the address of a rectangle that will be filled by the
function.
Example: RECT clientRect;
GetClientRect(hWnd,&clientRect);
13.1.7 Forcing a Redraw
As we have mentioned previously you can only draw your window when Windows tells you to
via a WM_PAINT message. Sometimes you would like to update your window whenever you
want. To do this you have to tell Windows that your window is dirty and needs a redraw. You
can do this by using InvalidateRect. This tells Windows your window is dirty and needs
redrawing. Windows then sends you a WM_PAINT message telling you to draw. InvalidateRect
takes three parameters, the handle to your window, the rectangular area of your window that
212 LOVELY PROFESSIONAL UNIVERSITY