Repository navigation
SysWin GdiPlus
Home › All Categories › sys/win › sys/win/gdiplus
Draws shapes, gradients, text and images with GDI+, and loads and saves image files.
GDI+ is the Windows graphics library that sits between GDI and Direct2D: it draws with
antialiasing and transparency, fills with gradients and textures, transforms coordinates, renders
text with any installed font and reads and writes BMP, PNG, JPEG, GIF and TIFF files. It runs
on the processor and draws onto a window, a device context, a printer or an image in memory.
This module wraps it in classes whose names start with CGp, and adds procedures that turn image
files into icons and bitmaps for the rest of a Windows program.
The model:
-
A drawing surface and the tools you pass to it. A CGpGraphics draws; pens,
brushes, fonts, string formats, paths, regions, matrices and images are separate objects that
you create once and pass by address (
@pen) to its methods. -
Each object owns one GDI+ object and deletes it in its destructor. Never copy one by value
(no
=from another object, nobyvalparameter): both copies would delete the same GDI+ object. Copy with the pointer constructor (dim b as CGpPen = @a) orClone. -
GDI+ starts and stops by itself. The first
CGp*object created starts GDI+ and the last one destroyed shuts it down. To keep it running for the whole program, callgdiplus.Init()at start-up andgdiplus.Uninit( token )at the end, after every object is gone, as the example does; see GDI+ procedures and types. -
Colours are
ARGB:&hAARRGGBB, alpha first, which is not the byte order of a GDICOLORREF. Use theARGB_*constants of Colours orGDIP_ARGB( a, r, g, b ). -
Status. The methods return a
GpStatus,Ok(0) on success, and store it; every object'sGetLastStatus()returns the last one, including the outcome of its constructor.
The whole GDI+ flat API (GdipCreateFromHDC, GdipDrawLine …) is declared as well, and every
object keeps its GDI+ handle in a public field (m_pGraphics, m_pImage, m_pPen …), so the
classes and the flat functions can be mixed.
#import "sys/win"
#import "sys/win/gdiplus"
using win, gdiplusThe ARGB_* colour constants come with the module. A program that creates windows imports
sys/win/gui too.
A window painted with a vertical gradient, a dashed, antialiased ellipse and a caption centred in it, all sized for the display's DPI:
#import "sys/win"
#import "sys/win/gui"
#import "sys/win/gdiplus"
using win, gui, gdiplus
declare function WndProc( byval hwnd as HWND, byval uMsg as UINT, byval wParam as WPARAM, byval lParam as LPARAM ) as LRESULT
function main() as long
SetProcessDpiAwareness( PROCESS_SYSTEM_DPI_AWARE )
dim token as ULONG_PTR = gdiplus.Init()
dim pWindow as CWindow = "GdiPlusDemo"
pWindow.Create( NULL, "sys/win/gdiplus", @WndProc )
pWindow.SetClientSize( 400, 220 )
pWindow.Center
dim nResult as long = pWindow.DoEvents( SW_NORMAL )
gdiplus.Uninit( token )
return nResult
end function
function WndProc( byval hwnd as HWND, byval uMsg as UINT, byval wParam as WPARAM, byval lParam as LPARAM ) as LRESULT
select case uMsg
case WM_PAINT
dim ps as PAINTSTRUCT
dim hdc as HDC = BeginPaint( hwnd, @ps )
scope
dim g as CGpGraphics = hdc
g.ScaleTransformForDpi()
g.SetSmoothingMode( SmoothingModeAntiAlias )
g.SetTextRenderingHint( TextRenderingHintAntiAlias )
' background: a gradient from white to light blue
dim rcAll as GpRectF = GDIP_RECTF( 0, 0, 400, 220 )
dim sky as CGpLinearGradientBrush = CGpLinearGradientBrush( @rcAll, ARGB_White, ARGB_LightSkyBlue, LinearGradientModeVertical )
g.FillRectangle( @sky, @rcAll )
' a translucent ellipse with a dashed outline
dim rcEllipse as GpRectF = GDIP_RECTF( 50, 30, 300, 160 )
dim glass as CGpSolidBrush = CGpSolidBrush( GDIP_ARGB_SetAlpha( ARGB_White, 120 ) )
g.FillEllipse( @glass, @rcEllipse )
dim outline as CGpPen = CGpPen( ARGB_SteelBlue, 3 )
outline.SetDashStyle( DashStyleDash )
g.DrawEllipse( @outline, @rcEllipse )
' a caption centred in the ellipse
dim fnt as CGpFont = CGpFont( "Segoe UI", 22, FontStyleBold )
dim ink as CGpSolidBrush = CGpSolidBrush( ARGB_MidnightBlue )
dim centred as CGpStringFormat
centred.SetAlignment( StringAlignmentCenter )
centred.SetLineAlignment( StringAlignmentCenter )
g.DrawString( "Hello, GDI+", -1, @fnt, @rcEllipse, @centred, @ink )
end scope
EndPaint( hwnd, @ps )
return 0
case WM_DESTROY
PostQuitMessage( 0 )
return 0
end select
return DefWindowProcW( hwnd, uMsg, wParam, lParam )
end function
end main()The scope block destroys the graphics object and the tools before EndPaint releases the
device context. More programs, one per class, are in the toolchain's
examples/win64/GdiPlus Examples/ folder.
| Page | Covers |
|---|---|
| CGpGraphics | The drawing surface: lines, curves, shapes, text and images on a window, a DC or an image; transformations, clipping, quality settings, containers. |
| Graphics paths |
CGpGraphicsPath, shapes built from lines, curves, figures and text, and CGpGraphicsPathIterator, which walks their points. |
| CGpRegion | Areas made from rectangles and paths and combined, for filling, clipping and hit-testing. |
| CGpMatrix | The affine matrix that moves, scales, rotates and shears points, used by graphics objects, pens, brushes and paths. |
| GDI+ images |
CGpImage, CGpBitmap, CGpMetafile and CGpCachedBitmap: loading, creating, editing pixels, frames, metadata, thumbnails and saving. |
| CGpImageAttributes | How an image's colours are changed as it is drawn: colour matrices, gamma, thresholds, colour keys and remap tables. |
| GDI+ effects |
CGpEffect and the effect classes (blur, sharpen, brightness and contrast, hue, levels, tint, colour balance, curves, lookup tables, red-eye) applied to bitmaps. |
| CGpPen | Pens: colour or brush, width, alignment, dash patterns, joins, caps and transformations. |
| GDI+ brushes |
CGpBrush and its kinds: solid, hatch, texture, linear gradient and path gradient. |
| GDI+ line caps |
CGpCustomLineCap and CGpAdjustableArrowCap: arrow heads and shapes of your own at the ends of lines. |
| GDI+ fonts |
CGpFont, CGpFontFamily and the installed and private font collections. |
| CGpStringFormat | Text layout: alignment, wrapping, trimming, tab stops, hot-key prefixes and measurable ranges. |
| GDI+ procedures and types | Starting GDI+, colour and rectangle helpers, image files to icons and bitmaps, saving and printing, OpenGL textures; CGpBase; the structures, enumerations, status codes, pixel formats and GUIDs. |
- Category: sys/win
-
Colours for the
ARGB_*constants - sys/win/direct2d, hardware-accelerated drawing
- Bitmaps for GDI bitmaps and DIB sections
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing