Drawing Shapes   (overhead)

Basic shapes supported

·       points

·       lines

·       circles & ellipses

·       curves

·       polygons

·       bitmaps

·       metafiles

 

uses a painting analogy – pens draw lines & brushes fill in interiors

 


Pens

Pens are uses to draw lines. There are several stock pens. You can create as many pens as you want but only 1 is the current pen (the one used to draw).

 

HPEN pen1 = GetStockObject(WHITE_PEN);

     

HPEN pen2 = CreatePen(<pen style>, <width>, <color>);

    if successful it returns a non-null handle

 

styles: PS_SOLID/DASHED/DOT/DASHDOT/DASHDOTDOT/NULL

 

HPEN redPen = CreatePen(PS_DOT,1,RGB(255,0,0));//1 pix wide

 

To use a Pen you place the pen in the device context with:

   <old Pen>  SelectObject(<device context>, <Pen>);

   

 

HGDIOBJ SelectObject(HDC hdc, HGDIOBJ pen);

             where HGDIOBJ is handle to pen, brush, bitmap, etc.

 

when done delete your pen with:

     DeleteObject( <HGDIOBJ>);

 



Brushes

Brushes are like pens but they are used to fill areas (shapes, windows, etc.)

 

HBRUSH brush = GetStockObject(hdc, WHITE_BRUSH);

or   

3 ways to create your own:

    brush = CreateSolidBrush(<color>);

              = CreateHatchBrush(<style>, <color>);

                style:

                   HS_BDIAGONAL 45 deg  left–right down

                   HS_CROSS            cross hatch

                   HS_DIAGCROSS  45 deg cross hatch

                   HS_FDIAGONAL  45 deg left-right up

                   HS_HORIZONTAL/VERTICAL

              = CreatePatternBrush(<bitmap handle>);

                   use LoadBitmap() to load & get handle

 

in order to use you must select with

    SelectObject(hdc, blueBrush);

    remember to DeleteObject(blueBrush);

    


Geometric Shapes

 

Plotting points

to plot a colored point at location x,y

<color> SetPixel(<hdc>, <x>,<y>, <color>);

 

the color returned is that actual color plotted (in the case of palletized color)

 

Lines

// select a pen, move to start of line

BOOL MoveToEx(<hdc>, <x1>,<y1>, <ptr to old loc>);

        use NULL for <ptr to old loc> if you don’t want to save the current location

        returns SUCCESS/FAIL

// to draw from current loc

BOOL LineTo(hdc, <x2>,<y2>);  // line from (x1,y1) to(x2,y2)

 

Rectangle

Draws a rectangle using the current pen & brush

 

// draw an outlined solid box using current pen & brush

BOOL Rectangle(<hdc>, <left>,<top>,<right>,<bot>);

 

// filled rectangle, no border 

// includes upper left corner but not lower right

int FillRect(HDC hdc,CONST RECT *lpR, HBRUSH hbr);

   

// outlined rectangle, no fill

int FrameRect(HDC hdc,CONST RECT *lpR, HBRUSH hbr);

 

Circles & Ellipses

// draw figure in specified bounding box

BOOL Ellipse(hdc, <left>,<top>,<right>,<bot>);

// draw an ellipse, major(X) axis width 100, minor(Y) width 60, centered at 100,100

Ellipse(hdc, 100 – 50, 100 – 30, 100 + 50, 100 + 30);

 

Polygon

Draws an arbitrary polygon.

 

BOOL Polygon(<hdc>, <pt. list>, <# pts.>);

<pt. list> is an array of POINTs

EX: POINT poly1[5] = {0,0, 7,0, 7,7, 5,9, 3,3};

 

The API has a lot of shapes defined for you. They work similar to those given.