In Windows Forms you might want to create some shapes and other graphical objects. For that you can use the graphic elements of Windows Forms that helps to create a visual application. In Windows Forms GDI+ is used to create graphics, draw text and manipulate graphical images.
The core of GDI+ functionality is the Graphics class. GDI+ has a set of base classes that can be used to do custom drawing on the screen. The set of such classes are collectively called the GDI+ managed class interfaces. This class is the part of .NET Framework. It provides an environment for creating, deploying and running XML web services and other applications.
The GDI+ managed class interfaces consist of about 60 classes, 50 enumerations and 8 structures. You can group these class into various namespaces. Some of them are:
- System.Drawing
- System.Drawing.Drawing.2D
- System.Drawing.Imaging
- System.Drawing.Printing
- System.Drawing.Text
- System.Drawing.Design
The following describes the simple Windows Forms application.
Make your design page like this:
Here in this case when the user clicks on the first button it will get the shape that we made in the code behind file using GDI.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
-
- namespace WindowsFormsApplication1
- {
- publicpartialclassForm1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- privatevoid button1_Click(object sender, EventArgs e)
- {
- Graphics g1 = CreateGraphics();
- Pen p = newPen(Color.Blue, 2);
- g1.DrawEllipse(p, 150, 20, 50, 50);
- g1.DrawEllipse(p, 160, 35, 10, 10);
- g1.DrawEllipse(p, 180, 35, 10, 10);
- g1.DrawArc(p, 150, 35, 50, 30, 45, 90);
- g1.DrawRectangle(p, 150, 75, 50, 100);
- g1.DrawLine(p, 175, 175, 150, 250);
- g1.DrawLine(p, 175, 175,200 , 250);
-
- }
-
- privatevoid button2_Click(object sender, EventArgs e)
- {
- Graphics g2 = CreateGraphics();
- SolidBrush sbr = newSolidBrush(Color.Chocolate);
- g2.FillEllipse(sbr, 150, 25, 50, 50);
- sbr.Color = Color.Gray;
- g2.FillEllipse(sbr, 160, 35, 10, 10);
- g2.FillEllipse(sbr, 180, 35, 10, 10);
- sbr.Color = Color.Black;
- g2.FillPie(sbr, 150, 35, 50, 30, 45, 90);
- sbr.Color = Color.Bisque;
- g2.FillRectangle(sbr, 150, 75, 50, 100);
-
-
- }
- }
- }
OutputI hope you like it. Thank you for reading. Have a good day.