Drawing a Rectangle

Introduction

There are three main types of rectangles you will deal with: those that already exist in your application, those you programmatically create, and those you let the user draw.

Here is an example of a triangle drawn using Win32:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    HPEN hpen, hpenOld;
    HBRUSH hbrush, hbrushOld;
    HDC hdc = this->Canvas->Handle;

    // Red pen for the border
    hpen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
    // Blue brush for the interior.
    hbrush = CreateSolidBrush(RGB(0, 0, 255));

    // Select the new pen and brush then draw a rectangle.
    hpenOld = SelectObject(hdc, hpen);
    hbrushOld = SelectObject(hdc, hbrush);
    Rectangle(hdc, 100,100, 400,250);

    // After using your objects, delete them and restore the originals
    SelectObject(hdc, hpenOld);
    DeleteObject(hpen);
    SelectObject(hdc, hbrushOld);
    DeleteObject(hbrush);
}
//---------------------------------------------------------------------------

 

 


Copyright 2003 FunctionX, Inc.