winforms-rendering

Implements custom GDI and GDI+ painting for WinForms controls with flicker-free rendering.

1|1|Updated Jun 12, 2022
One-click install
npx skills add https://github.com/KlausLoeffelmann/WinFormsDevTools --skill winforms-rendering-klausloeffelmann
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: winforms-rendering
Source: https://github.com/KlausLoeffelmann/WinFormsDevTools/tree/main/.github/skills/winforms-rendering
Command: npx skills add https://github.com/KlausLoeffelmann/WinFormsDevTools --skill winforms-rendering-klausloeffelmann

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Custom WinForms painting often produces flicker, resize artifacts, memory leaks, blurry text, and incorrect dark-mode colors. This Skill provides the patterns and rules needed to write correct, performant OnPaint code using GDI+ and GDI. ## Core Features & Use Cases - Flicker and Artifact Prevention: Configure DoubleBuffered, ResizeRedraw, and manual BufferedGraphics for smooth custom control painting. - Resource Management: Apply correct disposal and caching rules for Graphics, Pen, Brush, Font, and Image objects, including the never-dispose rule for SystemBrushes and SystemPens. - Text and Quality Control: Use StringFormat.GenericTypographic for precise measurement, choose between TextRenderer and DrawString, and set SmoothingMode, TextRenderingHint, and InterpolationMode correctly. - Use Case: You are building a custom owner-drawn control that must render crisp text, scale across DPI settings, and support .NET 9 dark mode. This Skill gives you the setup checklist, pen-width boundary math, and dark-mode color handling to ship it correctly. ## Quick Start Ask the AI to help you implement a custom OnPaint method for a WinForms control that avoids flicker, disposes GDI resources correctly, and supports dark mode.

Frequently Asked Questions about winforms-rendering

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I prevent flicker in a custom WinForms control?▼

Set DoubleBuffered = true in the control constructor, or use SetStyle with OptimizedDoubleBuffer, AllPaintingInWmPaint, and UserPaint for more control. For complex scenarios, render to a BufferedGraphics offscreen buffer and call Render in OnPaint.

Should I use TextRenderer or Graphics.DrawString in WinForms?▼

Use TextRenderer (GDI) for large amounts of text, UI text matching system appearance, and performance-critical rendering. Use Graphics.DrawString (GDI+) for rotated text, effects like shadows or gradients, and alpha blending. Never mix both for the same rendering task.

Why does my custom control show paint artifacts when resizing?▼

Artifacts occur because the control does not fully repaint during resize. Call SetStyle(ControlStyles.ResizeRedraw, true) in the constructor so the control repaints completely on every resize, eliminating smearing at a small performance cost.

Can I dispose SystemBrushes or the Graphics object from PaintEventArgs?▼

No. SystemBrushes, SystemPens, and SystemColors are shared framework-managed resources, and disposing them breaks the application. PaintEventArgs.Graphics is owned by the framework; only dispose Graphics objects you create via CreateGraphics, FromImage, or FromHwnd.

How do I support dark mode in WinForms custom painting?▼

Check Application.IsDarkModeEnabled in .NET 9+ and use SystemColors, which adapt automatically to dark mode. Absolute custom colors require manual handling with separate light and dark values, and note that ControlDark becomes a lighter complementary color in dark mode.

Why is my MeasureString result inaccurate for monospace fonts?▼

The default MeasureString includes extra padding that causes cumulative layout errors. Pass StringFormat.GenericTypographic to MeasureString for precise measurements, which is critical for monospace fonts, mixed-style text runs, and custom text controls.