The following code snippet show you how to draw a line and rectangle in C#.
Here is the output of the project.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DrawOnWinForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Drawing.Pen bounderyPen;
System.Drawing.Pen OxOyPen;
System.Drawing.Pen ABPen;
System.Drawing.Pen CPen;
System.Drawing.Brush ACMBrush = Brushes.LightGray;
System.Drawing.Brush BCMpBrush = Brushes.LightCyan;
System.Drawing.Brush TextBrush = Brushes.Black;
Font textFont = new Font("Arial", 10);
Point TOPLEFT = new Point(0, 0);
Point BASEO = new Point(400, 400);
Rectangle Boundery = new Rectangle();
double xO = 0, yO = 0;
private void Form1_Load(object sender, EventArgs e)
{
}
int zoomX = 50;
int margin = 10;
int zoomY = 50;
System.Drawing.Brush brush = Brushes.White;
private void Form1_Paint(object sender, PaintEventArgs e)
{
Clear();
System.Drawing.Graphics formGraphics = this.CreateGraphics();
Boundery.X = margin;
Boundery.Y = margin;
Boundery.Width = this.Width - 4 * margin;
Boundery.Height = this.Height - Boundery.Y - margin * 5;
bounderyPen = new System.Drawing.Pen(System.Drawing.Color.Brown);
bounderyPen.Width = 2;
formGraphics.DrawRectangle(bounderyPen, Boundery);
BASEO.X = (int)(this.Width / 2);
BASEO.Y = this.Height - margin * 5 - 100;
OxOyPen = new System.Drawing.Pen(System.Drawing.Color.Blue);
OxOyPen.Width = 2;
formGraphics.DrawLine(OxOyPen, margin, BASEO.Y, Boundery.X + Boundery.Width - margin * 0, BASEO.Y);
formGraphics.DrawLine(OxOyPen, Boundery.X + Boundery.Width - 10 - margin * 0, BASEO.Y - 5, Boundery.X + Boundery.Width - margin * 0, BASEO.Y);
formGraphics.DrawLine(OxOyPen, Boundery.X + Boundery.Width - 10 - margin * 0, BASEO.Y + 5, Boundery.X + Boundery.Width - margin * 0, BASEO.Y);
string textTrucX = "x";
formGraphics.DrawString(textTrucX, textFont, TextBrush, new PointF(Boundery.X + Boundery.Width - 10 - margin * 0, BASEO.Y + 2));
formGraphics.DrawLine(OxOyPen, BASEO.X, this.Height - margin * 5, BASEO.X, this.Height - margin * 5 - Boundery.Height);
formGraphics.DrawLine(OxOyPen, BASEO.X - 5, this.Height - margin * 5 - Boundery.Height + 10, BASEO.X, this.Height - margin * 5 - Boundery.Height);
formGraphics.DrawLine(OxOyPen, BASEO.X + 5, this.Height - margin * 5 - Boundery.Height + 10, BASEO.X, this.Height - margin * 5 - Boundery.Height);
string textTrucY = "y";
formGraphics.DrawString(textTrucY, textFont, TextBrush, new PointF(BASEO.X - 15, this.Height - margin * 5 - Boundery.Height));
string textO = "O(" + xO + "," + yO + ")";
formGraphics.DrawString(textO, textFont, TextBrush, new PointF(ToaDoFormXZoomY(xO), ToaDoFormYZoomY(yO)));
}
private void Clear()
{
System.Drawing.Graphics formGraphics = this.CreateGraphics();
#region Ve Boundery
Rectangle screen = new Rectangle();
screen.X = 0;
screen.Y = 0;
screen.Width = this.Width - 1;
screen.Height = this.Height;
formGraphics.FillRectangle(brush, screen);
#endregion
formGraphics.Dispose();
}
private int ToaDoFormXZoomY(double x)
{
return (int)(BASEO.X + x * zoomY);
}
private int ToaDoFormYZoomY(double y)
{
return (int)(BASEO.Y - y * zoomY);
}
private int ToaDoFormXZoomX(double x)
{
return (int)(BASEO.X + x * zoomX);
}
private int ToaDoFormYZoomX(double y)
{
return (int)(BASEO.X - y * zoomX);
}
}
}
Here is the output of the project.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.