Pen Styles


Introduction

A pen is an instrument used to draw a line or a curve. In Microsoft Windows, a pen is represented by a structure called HPEN. The VCL simplifies the creation of a pen through a class call TPen. The following example shows how to use a VCL's TPen and how to use a Win32 API's HPEN in a VCL project.

Header File:

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
const TPenStyle pStyle[] = { psSolid,	psDash, psDot, psDashDot,
                           psDashDotDot, psClear, psInsideFrame };
const UINT pHatch[] = { HS_BDIAGONAL, HS_CROSS, HS_DIAGCROSS,
                        HS_FDIAGONAL, HS_HORIZONTAL, HS_VERTICAL };
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TGroupBox *GroupBox1;
    TLabel *Label1;
    TComboBox *ComboBox1;
    TComboBox *ComboBox2;
    TLabel *Label2;
    void __fastcall FormCreate(TObject *Sender);
    void __fastcall ComboBox1DrawItem(TWinControl *Control, int Index,
          TRect &Rect, TOwnerDrawState State);
    void __fastcall ComboBox2DrawItem(TWinControl *Control, int Index,
          TRect &Rect, TOwnerDrawState State);
    void __fastcall FormPaint(TObject *Sender);
private:
    HPEN hPen;
    LOGBRUSH LBrush;	// User declarations
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Source File:

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    for(int i = 0; i < 8; i++)
        ComboBox1->Items->Add(pStyle[i]);

    for(int j = 0; j < 6; j++)
        ComboBox2->Items->Add(pHatch[j]);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1DrawItem(TWinControl *Control, int Index,
      TRect &Rect, TOwnerDrawState State)
{
    if( State.Contains(odSelected) )
        ComboBox1->Canvas->Brush->Color = clHighlight;
    else
        ComboBox1->Canvas->Brush->Color = clWhite;

    ComboBox1->Canvas->FillRect(Rect);

    ComboBox1->Canvas->Pen->Style = pStyle[Index];
    ComboBox1->Canvas->Pen->Width = 1;
    ComboBox1->Canvas->MoveTo(0, Rect.Top + 8);
    ComboBox1->Canvas->LineTo(Rect.Width(), Rect.Top + 8);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox2DrawItem(TWinControl *Control, int Index,
      TRect &Rect, TOwnerDrawState State)
{                                       
    if( State.Contains(odSelected) )
        ComboBox2->Canvas->Brush->Color = clHighlight;
    else
        ComboBox2->Canvas->Brush->Color = clWhite;

    ComboBox2->Canvas->FillRect(Rect);

    LBrush.lbStyle = BS_HATCHED;
    LBrush.lbColor = RGB(0, 0, 0);
    LBrush.lbHatch = pHatch[Index];
    hPen = ExtCreatePen(PS_GEOMETRIC, 8, &LBrush, 0, NULL);

    ComboBox2->Canvas->Pen->Handle = hPen;
    ComboBox2->Canvas->MoveTo(0, Rect.Top + 9);
    ComboBox2->Canvas->LineTo(Rect.Width(), Rect.Top + 9);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
    TRect rc;
    HDC hdc;
    int i;

    rc = GetClientRect();
    rc.left   = 220;
    rc.top    = 24;
    rc.right  = rc.left + 140;

    LBrush.lbStyle = BS_SOLID;
    LBrush.lbColor = RGB(0,0,0);
    LBrush.lbHatch = 0;

    for (i = 0; i < 7; i++)
    {
        Canvas->Pen->Style = pStyle[i];
        Canvas->Pen->Width = 1;
        Canvas->MoveTo(rc.left, rc.top + (i * 20));
        Canvas->LineTo(rc.right, rc.top + (i * 20));
    }

    rc.left  = 380;
    rc.right = rc.left + 140;

    for (i = 0; i < 6; i++)
    {

        LBrush.lbStyle = BS_HATCHED;
        LBrush.lbColor = RGB(0, 0, 0);
        LBrush.lbHatch = pHatch[i];
        hPen = ExtCreatePen(PS_GEOMETRIC, 12, &LBrush, 0, NULL);
        this->Canvas->Pen->Handle = hPen;

        Canvas->MoveTo(rc.left, rc.top + (i * 20));
        Canvas->LineTo(rc.right, rc.top + (i * 20));
    }
}
//---------------------------------------------------------------------------

Result:

Example: Using the ExtCreatePen() function:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
    DWORD pStyle = PS_GEOMETRIC | PS_DASHDOTDOT |
                   PS_ENDCAP_SQUARE | PS_JOIN_BEVEL;
    DWORD pWidth = 16;

    LOGBRUSH lBrush;
    lBrush.lbStyle = BS_HATCHED;
    lBrush.lbColor = clBlue;
    lBrush.lbHatch = HS_DIAGCROSS;

    HPEN hPen = ExtCreatePen(pStyle, pWidth, &lBrush, NULL, NULL);

    Canvas->Pen->Handle = hPen;

    Canvas->MoveTo(100, 200);
    Canvas->LineTo(540, 200);
}
//---------------------------------------------------------------------------

 

 


Copyright © 2003-2015, FunctionX, Inc.