Basics: How to Draw on a Control Using GDI

The comments are in English, and they are perfectly clear:

C#
//These attributes do not need to be here, it works just as well without them
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.LinkDemand, Flags = System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    // Get handle to device context.
    IntPtr hdc = e.Graphics.GetHdc();
    // Create new graphics object using handle to device context.
    Graphics newGraphics = Graphics.FromHdc(hdc);
    // Draw rectangle to screen.
    newGraphics.DrawRectangle(new Pen(Color.Red, 3), 0, 0, 200, 100);
    // Release handle to device context and dispose of the Graphics object
    e.Graphics.ReleaseHdc(hdc);
    newGraphics.Dispose();
}

Hopefully I will find the time one day to expand this article so that it teaches you how to take screen captures while seeing what you are capturing.

Categories

Leave a Reply