Page 217 - DCAP408_WEB_PROGRAMMING
P. 217
Unit 13: Windows GDI
To draw a filled ellipse we can use: Notes
BOOL Ellipse(HDC hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect );
This function takes the device context and the screen positions of a rectangle that represents the
bounding rectangle for the ellipse. The ellipse will be drawn to fit in this rectangle.
To draw a filled rectangle we can use:
Example: FillRect( HDC hDC, CONST RECT *lprc, HBRUSH hbr);
This function takes the device context, a rectangle structure and a brush. The RECT structure
contains member variables left, right, top and bottom. Brushes are described in the next section:
Pens and Brushes.
13.1.5 Pens and Brushes
GDI uses the concept of Pens and Brushes. A Pen defines the style and colour that pixels will be
drawn in while a brush determines the fill colour of shapes. A number of the GDI drawing
functions can be effected by the pen or brush chosen.
Pens
To create a pen we need to obtain a handle to one. This handle is named HPEN, we can keep hold
of this handle during the lifetime of our program but must remember to delete it before exiting.
To create a pen we use the function:
HPEN CreatePen( int fnPenStyle, int nWidth, COLORREF crColor);
This function takes a style, a pen width and a colour. The style must be one of the predefined
styles. The main ones are shown below:
PS_SOLID - Pen is solid.
PS_DASH - Pen is dashed.
PS_DOT - Pen is dotted.
PS_DASHDOT - Pen has alternating dashes and dots.
PS_DASHDOTDOT - Pen has alternating dashes and double dots.
PS_NULL - Pen is invisible.
So to create a solid green pen of 1 pixel width we would write:
HPEN greenPen=CreatePen(PS_SOLID, 1, RGB(0,255,0));
It is a good idea to create our pens (and brushes) in advance of drawing and then apply them as
required. To apply a pen so future drawing commands use it we must select it. This is known as
selecting it into the device context and is achieved by using SelectObject. One thing we must be
aware of is that when we no longer want to use our pen we need to re-select the old pen. Luckily
SelectObject returns the old pen. So to use our green pen to draw a line we may do this:
// Select our green pen into the device context and remember previous pen
HGDIOBJ oldPen=SelectObject(hdc,greenPen);
// Draw our line
LOVELY PROFESSIONAL UNIVERSITY 211