## wxPen

A pen is a drawing tool for drawing outlines. It is used for drawing lines and painting the outline of rectangles, ellipses, etc. It has a colour, a width and a style.

Derived from

wxGDIObject
wxObject

Include files

<wx/pen.h>

Predefined objects

Objects:

wxNullPen

Pointers:

wxRED_PEN
wxCYAN_PEN
wxGREEN_PEN
wxBLACK_PEN
wxWHITE_PEN
wxTRANSPARENT_PEN
wxBLACK_DASHED_PEN
wxGREY_PEN
wxMEDIUM_GREY_PEN
wxLIGHT_GREY_PEN

Remarks

On a monochrome display, wxWidgets shows all non-white pens as black.

Do not initialize objects on the stack before the program commences, since other required structures may not have been set up yet. Instead, define global pointers to objects and create them in OnInit or when required.

An application may wish to dynamically create pens with different characteristics, and there is the consequent danger that a large number of duplicate pens will be created. Therefore an application may wish to get a pointer to a pen by using the global list of pens wxThePenList, and calling the member function FindOrCreatePen. See the entry for wxPenList.

This class uses reference counting and copy-on-write internally so that assignments between two instances of this class are very cheap. You can therefore use actual objects instead of pointers without efficiency problems. If an instance of this class is changed it will create its own data internally so that other instances, which previously shared the data using the reference counting, are not affected.

See also

wxPenList, wxDC, wxDC::SetPen

Members

wxPen::wxPen
wxPen::~wxPen
wxPen::GetCap
wxPen::GetColour
wxPen::GetDashes
wxPen::GetJoin
wxPen::GetStipple
wxPen::GetStyle
wxPen::GetWidth
wxPen::IsOk
wxPen::SetCap
wxPen::SetColour
wxPen::SetDashes
wxPen::SetJoin
wxPen::SetStipple
wxPen::SetStyle
wxPen::SetWidth
wxPen::operator =
wxPen::operator ==
wxPen::operator !=

### wxPen::wxPen

wxPen()

Default constructor. The pen will be uninitialised, and wxPen:IsOk will return false.

wxPen(const wxColour& colour, int width = 1, int style = `wxSOLID`)

Constructs a pen from a colour object, pen width and style.

wxPen(const wxString& colourName, int width, int style)

Constructs a pen from a colour name, pen width and style.

wxPen(const wxBitmap& stipple, int width)

Constructs a stippled pen from a stipple bitmap and a width.

wxPen(const wxPen& pen)

Copy constructor, uses reference counting.

Parameters

colour

A colour object.

colourName

A colour name.

width

Pen width. Under Windows, the pen width cannot be greater than 1 if the style is wxDOT, wxLONG_DASH, wxSHORT_DASH, wxDOT_DASH, or wxUSER_DASH.

stipple

A stipple bitmap.

pen

A pointer or reference to a pen to copy.

style

The style may be one of the following:

wxSOLID | Solid style.
wxTRANSPARENT | No pen is used.
wxDOT | Dotted style.
wxLONG_DASH | Long dashed style.
wxSHORT_DASH | Short dashed style.
wxDOT_DASH | Dot and dash style.
wxSTIPPLE | Use the stipple bitmap.
wxUSER_DASH | Use the user dashes: see wxPen::SetDashes.
wxBDIAGONAL_HATCH | Backward diagonal hatch.
wxCROSSDIAG_HATCH | Cross-diagonal hatch.
wxFDIAGONAL_HATCH | Forward diagonal hatch.
wxCROSS_HATCH | Cross hatch.
wxHORIZONTAL_HATCH | Horizontal hatch.
wxVERTICAL_HATCH | Vertical hatch.

Remarks

Different versions of Windows and different versions of other platforms support very different subsets of the styles above - there is no similarity even between Windows95 and Windows98 - so handle with care.

If the named colour form is used, an appropriate wxColour structure is found in the colour database.

See also

wxPen::SetStyle, wxPen::SetColour, wxPen::SetWidth, wxPen::SetStipple

wxPerl note: Constructors supported by wxPerl are:

- Wx::Pen->new( colour, width, style )
- Wx::Pen->new( colourName, width, style )
- Wx::Pen->new( stipple, width )

### wxPen::~wxPen

~wxPen()

Destructor. See reference-counted object destruction for more info.

Remarks

Although all remaining pens are deleted when the application exits, the application should try to clean up all pens itself. This is because wxWidgets cannot know if a pointer to the pen object is stored in an application data structure, and there is a risk of double deletion.

### wxPen::GetCap

int GetCap() const

Returns the pen cap style, which may be one of wxCAP_ROUND, wxCAP_PROJECTING and wxCAP_BUTT. The default is wxCAP_ROUND.

See also

wxPen::SetCap

### wxPen::GetColour

wxColour& GetColour() const

Returns a reference to the pen colour.

See also

wxPen::SetColour

### wxPen::GetDashes

int GetDashes(wxDash** dashes) const

Gets an array of dashes (defined as char in X, DWORD under Windows). dashes is a pointer to the internal array. Do not deallocate or store this pointer. The function returns the number of dashes associated with this pen.

See also

wxPen::SetDashes

### wxPen::GetJoin

int GetJoin() const

Returns the pen join style, which may be one of wxJOIN_BEVEL, wxJOIN_ROUND and wxJOIN_MITER. The default is wxJOIN_ROUND.

See also

wxPen::SetJoin

### wxPen::GetStipple

wxBitmap* GetStipple() const

Gets a pointer to the stipple bitmap.

See also

wxPen::SetStipple

### wxPen::GetStyle

int GetStyle() const

Returns the pen style.

See also

wxPen::wxPen, wxPen::SetStyle

### wxPen::GetWidth

int GetWidth() const

Returns the pen width.

See also

wxPen::SetWidth

### wxPen::IsOk

bool IsOk() const

Returns true if the pen is initialised.

### wxPen::SetCap

void SetCap(int capStyle)

Sets the pen cap style, which may be one of wxCAP_ROUND, wxCAP_PROJECTING and wxCAP_BUTT. The default is wxCAP_ROUND.

See also

wxPen::GetCap

### wxPen::SetColour

void SetColour(wxColour& colour)

void SetColour(const wxString& colourName)

void SetColour(unsigned char red, unsigned char green, unsigned char blue)

The pen's colour is changed to the given colour.

See also

wxPen::GetColour

### wxPen::SetDashes

void SetDashes(int n, wxDash* dashes)

Associates an array of pointers to dashes (defined as char in X, DWORD under Windows) with the pen. The array is not deallocated by wxPen, but neither must it be deallocated by the calling application until the pen is deleted or this function is called with a NULL array.

See also

wxPen::GetDashes

### wxPen::SetJoin

void SetJoin(int join_style)

Sets the pen join style, which may be one of wxJOIN_BEVEL, wxJOIN_ROUND and wxJOIN_MITER. The default is wxJOIN_ROUND.

See also

wxPen::GetJoin

### wxPen::SetStipple

void SetStipple(wxBitmap* stipple)

Sets the bitmap for stippling.

See also

wxPen::GetStipple

### wxPen::SetStyle

void SetStyle(int style)

Set the pen style.

See also

wxPen::wxPen

### wxPen::SetWidth

void SetWidth(int width)

Sets the pen width.

See also

wxPen::GetWidth

### wxPen::operator =

wxPen& operator =(const wxPen& pen)

Assignment operator, using reference counting.

### wxPen::operator ==

bool operator ==(const wxPen& pen)

Equality operator. See reference-counted object comparison for more info.

### wxPen::operator !=

bool operator !=(const wxPen& pen)

Inequality operator. See reference-counted object comparison for more info.