TPen
Fuente: source/classes/pen.prg
Standalone class (no inheritance)
TPen encapsulates a Windows GDI pen object for drawing lines, borders, and outlines. Pens define the color, width, and style (solid, dashed, dotted, etc.) of lines drawn on any device context. TPen is used internally by many FiveWin controls and is also available for direct use in custom painting routines.
Key DATA Members
| DATA | Type | Description |
|---|---|---|
hPen | Numeric (Handle) | Windows GDI pen handle |
nStyle | Numeric | Pen style (PS_SOLID, PS_DASH, PS_DOT, PS_DASHDOT, etc.) |
nWidth | Numeric | Pen width in logical units (pixels) |
nColor | Numeric | Pen color as RGB value |
oDevice | Object | Optional device context owner for DPI-aware width scaling |
Methods
| Method | Description |
|---|---|
New( nStyle, nWidth, nColor, oDevice ) | Create a new pen. Defaults: PS_SOLID, width 1, CLR_BLACK. When oDevice is provided, width is scaled by DPI. |
End() | Delete the GDI pen object and release resources. Alias for Release(). |
Pen Styles
| Constant | Value | Description |
|---|---|---|
PS_SOLID | 0 | Solid line (default) |
PS_DASH | 1 | Dashed line (valid only when width is 1) |
PS_DOT | 2 | Dotted line (valid only when width is 1) |
PS_DASHDOT | 3 | Alternating dash-dot pattern |
PS_DASHDOTDOT | 4 | Dash-dot-dot pattern |
PS_NULL | 5 | Invisible pen |
PS_INSIDEFRAME | 6 | Solid pen that draws inside the bounding frame |
Example
#include "FiveWin.ch"
function Main()
local oWnd, oPen
DEFINE WINDOW oWnd TITLE "TPen Demo" SIZE 400, 300
oWnd:bPainted = {| hDC |
// Create a red pen with width 2
DEFINE PEN oPen WIDTH 2 COLOR CLR_RED
// Draw a line from top-left to bottom-right
MoveTo( hDC, 50, 50 )
LineTo( hDC, 350, 250, oPen:hPen )
oPen:End()
}
ACTIVATE WINDOW oWnd CENTERED
return nil
Notes
- Always call
End()orRelease()when the pen is no longer needed to avoid GDI resource leaks. - When specifying an
oDevice(window, printer, etc.), the pen width is automatically scaled based on the device's logical pixels per inch, making the visual width consistent across different DPI settings. - The
DEFINE PENcommand provides a convenient syntax alternative to callingTPen():New()directly. - TPen is used extensively by FiveWin controls internally. Once a pen is selected into a device context via
SelectObject(), it affects all subsequent drawing operations until another pen is selected.