Rendering with Graphics
This tutorial will teach you how to create a custom Control, which draws itself on the screen using Graphics class.
Create a new AlterNET UI Application project, name it
DrawingContextTutorial. For step-by-step guidance on how to create a new AlterNET UI project, see "Hello, World" Tutorial.Add a new empty class named
DrawingControlto the project. Make the classpublic, and derive it from Control:using Alternet.UI; namespace DrawingContextTutorial { public class DrawingControl : Control { } }Open
MainWindow.uixml. Add the reference to the local namespace, and add aDrawingControlto the window:<Window xmlns="http://schemas.alternetsoft.com/ui/2021" xmlns:x="http://schemas.alternetsoft.com/ui/2021/uixml" x:Class="DrawingContextTutorial.MainWindow" Title="DrawingContextTutorial" xmlns:local="clr-namespace:DrawingContextTutorial;assembly=DrawingContextTutorial"> <local:DrawingControl /> </Window>Compile and run the application. An empty window will appear. This is because
DrawingControldoes not paint itself yet.In the
DrawingControlclass, add a default constructor. In its body, set UserPaint property totrue. Also, override the OnPaint method:using Alternet.Drawing; using Alternet.UI; namespace DrawingContextTutorial { public class DrawingControl : Control { public DrawingControl() { UserPaint = true; } protected override void OnPaint(PaintEventArgs e) { } } }In the overridden
OnPaintmethod, add the following Graphics.FillRectangle call to paint the control's background LightBlue:using Alternet.Drawing; using Alternet.UI; namespace DrawingContextTutorial { public class DrawingControl : Control { public DrawingControl() { UserPaint = true; } protected override void OnPaint(PaintEventArgs e) { e.DrawingContext.FillRectangle(Brushes.LightBlue, e.Bounds); } } }Build and run the application. The displayed window will have a light blue background:

In the overridden
OnPaintmethod, add the following two lines of code to paint a red circular pattern:using Alternet.Drawing; using Alternet.UI; namespace DrawingContextTutorial { public class DrawingControl : Control { public DrawingControl() { UserPaint = true; } protected override void OnPaint(PaintEventArgs e) { e.DrawingContext.FillRectangle(Brushes.LightBlue, e.Bounds); for (int size = 10; size < 200; size += 10) e.DrawingContext.DrawEllipse(Pens.Red, new(10, 10, size, size)); } } }Build and run the application. The displayed window will look like the following:

Now, let's draw a simple line of text. To do that, we will create a cached Font instance, and draw a text line using the Graphics.DrawText method:
using Alternet.Drawing; using Alternet.UI; namespace DrawingContextTutorial { public class DrawingControl : Control { public DrawingControl() { UserPaint = true; } private static Font font = new Font(FontFamily.GenericSerif, 15); protected override void OnPaint(PaintEventArgs e) { e.DrawingContext.FillRectangle(Brushes.LightBlue, e.Bounds); for (int size = 10; size < 200; size += 10) e.DrawingContext.DrawEllipse(Pens.Red, new(10, 10, size, size)); e.DrawingContext.DrawText("Hello!", font, Brushes.Black, new PointF(10, 220)); } } }Build and run the application. The displayed window will look like the following:

To demonstrate how Graphics.MeasureText works, let's draw the names of the three spring months one under another:
using Alternet.Drawing; using Alternet.UI; namespace DrawingContextTutorial { public class DrawingControl : Control { public DrawingControl() { UserPaint = true; } private static Font font = new Font(FontFamily.GenericSerif, 15); protected override void OnPaint(PaintEventArgs e) { e.DrawingContext.FillRectangle(Brushes.LightBlue, e.Bounds); for (int size = 10; size < 200; size += 10) e.DrawingContext.DrawEllipse(Pens.Red, new(10, 10, size, size)); float y = 210; for (int month = 3; month <= 5; month++) { var text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month); var textSize = e.DrawingContext.MeasureText(text, font); e.DrawingContext.DrawText(text, font, Brushes.Black, new(0, y, textSize.Width, textSize.Height)); y += textSize.Height + 10; } } } }Build and run the application. The displayed window will look like the following:

Congratulations, you have completed the Rendering Graphics with DrawingContext tutorial.