画圆画线
首先来个画背景的,写上偶的名字咧!嘿嘿
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawString("yellowyu", new Font("Verdana", 14), Brushes.Black, new PointF(100, 100));
}
接着画圆画方
Graphics g = this.CreateGraphics();
//消除锯齿,控制图像质量
g.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rect = new Rectangle(20, 20, 100, 100);
Pen redPen = new Pen(Color.Red, 3);
Pen blackPen = Pens.Black;
//单色画笔,填充形状
SolidBrush greenBrush = new SolidBrush(Color.Green);
//画一个矩形
g.DrawRectangle(redPen, rect);
//填充一个矩形里的椭圆
g.FillEllipse(greenBrush, rect);
//画一条横线
g.DrawLine(blackPen, 0, 250, this.Width, 250);
//画一系列圆形
g.FillEllipse(Brushes.Blue, 70, 220, 30, 30);
g.FillEllipse(Brushes.SkyBlue, 100, 210, 40, 40);
g.FillEllipse(Brushes.Green, 140, 200, 50, 50);
g.FillEllipse(Brushes.Yellow, 190, 190, 60, 60);
g.FillEllipse(Brushes.Violet, 250, 180, 70, 70);
g.FillEllipse(Brushes.Red, 320, 170, 80, 80);
//通过两点定一条直线
Point pt = new Point(150, 150);
Point newpt = Point.Empty;
newpt.X = 200;
newpt.Y = 300;
Pen pn = new Pen(Color.Blue, 10);
g.DrawLine(pn, pt, newpt);
StringFormat drawFormat = new StringFormat();
//文本cuizi对齐
string DrawText = "Draw GDI+ BY YELLOWYU";
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
g.DrawString(DrawText, new Font("Verdana", 14), Brushes.Aquamarine, 200.0F, 20.0F, drawFormat);
g.DrawString(DrawText, new Font("Verdana", 14), Brushes.Aquamarine, 220.0F, 20.0F);
greenBrush.Dispose();
redPen.Dispose();
g.Dispose();
接着画坐标,还有通过鼠标连线
Graphics g = this.CreateGraphics();
Font VertFont = new Font(strFontStyle, 10, FontStyle.Bold);
Font horzFont = new Font(strFontStyle, 10, FontStyle.Bold);
SolidBrush vertBrush = new SolidBrush(Color.Black);
SolidBrush horzBrush = new SolidBrush(Color.Blue);
Pen BlackPen = new Pen(Color.Black, 2);
Pen BluePen = new Pen(Color.Blue, 2);
g.DrawLine(BlackPen, 350, 520, 350, 225);
g.DrawLine(BluePen, 350, 520, 550, 520);
for (int i = 0; i < 11; i++)
{
g.DrawString(i.ToString(), horzFont, horzBrush, 350 + i * 20, 520);
}
//绘制文本
StringFormat vertStrFormat = new StringFormat();
vertStrFormat.FormatFlags = StringFormatFlags.DirectionVertical;
for (int i = 1; i < 11; i++)
{
g.DrawString("-", horzFont, horzBrush, 350 + i * 20, 512, vertStrFormat);
x = 350 + i * 20;
}
for (int i = 0; i < 10; i++)
{
g.DrawString((10 * (10 - i)).ToString() + "-", VertFont, vertBrush, 325, 310 + 20 * (i % 11));
if (i == 0)
{
y = 310 + 20 * (i % 11);
}
}
Console.WriteLine("X:{0},Y:{1}", new object[] { x, y });
点击两点连线
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
Graphics g1 = this.CreateGraphics();
Pen linePen = new Pen(Color.Green, 1);
Pen EllipsePen = new Pen(Color.Red, 1);
startPoint = endPoint;
endPoint = new Point(e.X, e.Y);
g1.DrawLine(linePen, startPoint, endPoint);
if (checkBox1.Checked)
{
g1.DrawRectangle(EllipsePen, e.X - 2, e.Y - 2, 4, 4);
}
else
{
g1.DrawEllipse(EllipsePen, e.X - 2, e.Y - 2, 4, 4);
}
linePen.Dispose();
EllipsePen.Dispose();
g1.Dispose();
}
直接上效果图
最后即是刷新,其实这里面有个技巧,就是在刷的时候先把图取下来,刷完再往上盖
而Invalidate函数是使某块区域无效
比如吧!一个LISTVIEW切换一项,系统会先取得那一项的位置,还有改变后的位置,把这两个地方INVALIDATE掉,再画上该画的东西
startPoint = new Point(350, 520);
endPoint = new Point(350, 520);
t = new Rectangle(370, 310, 100, 100);
this.Invalidate(t);
而这里面有太多性能上的问题,改天再说喽!打球去喽!夏天的上海,真热,还有一张饼图的,改天再补上
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawString("yellowyu", new Font("Verdana", 14), Brushes.Black, new PointF(100, 100));
}
接着画圆画方
Graphics g = this.CreateGraphics();
//消除锯齿,控制图像质量
g.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rect = new Rectangle(20, 20, 100, 100);
Pen redPen = new Pen(Color.Red, 3);
Pen blackPen = Pens.Black;
//单色画笔,填充形状
SolidBrush greenBrush = new SolidBrush(Color.Green);
//画一个矩形
g.DrawRectangle(redPen, rect);
//填充一个矩形里的椭圆
g.FillEllipse(greenBrush, rect);
//画一条横线
g.DrawLine(blackPen, 0, 250, this.Width, 250);
//画一系列圆形
g.FillEllipse(Brushes.Blue, 70, 220, 30, 30);
g.FillEllipse(Brushes.SkyBlue, 100, 210, 40, 40);
g.FillEllipse(Brushes.Green, 140, 200, 50, 50);
g.FillEllipse(Brushes.Yellow, 190, 190, 60, 60);
g.FillEllipse(Brushes.Violet, 250, 180, 70, 70);
g.FillEllipse(Brushes.Red, 320, 170, 80, 80);
//通过两点定一条直线
Point pt = new Point(150, 150);
Point newpt = Point.Empty;
newpt.X = 200;
newpt.Y = 300;
Pen pn = new Pen(Color.Blue, 10);
g.DrawLine(pn, pt, newpt);
StringFormat drawFormat = new StringFormat();
//文本cuizi对齐
string DrawText = "Draw GDI+ BY YELLOWYU";
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
g.DrawString(DrawText, new Font("Verdana", 14), Brushes.Aquamarine, 200.0F, 20.0F, drawFormat);
g.DrawString(DrawText, new Font("Verdana", 14), Brushes.Aquamarine, 220.0F, 20.0F);
greenBrush.Dispose();
redPen.Dispose();
g.Dispose();
接着画坐标,还有通过鼠标连线
Graphics g = this.CreateGraphics();
Font VertFont = new Font(strFontStyle, 10, FontStyle.Bold);
Font horzFont = new Font(strFontStyle, 10, FontStyle.Bold);
SolidBrush vertBrush = new SolidBrush(Color.Black);
SolidBrush horzBrush = new SolidBrush(Color.Blue);
Pen BlackPen = new Pen(Color.Black, 2);
Pen BluePen = new Pen(Color.Blue, 2);
g.DrawLine(BlackPen, 350, 520, 350, 225);
g.DrawLine(BluePen, 350, 520, 550, 520);
for (int i = 0; i < 11; i++)
{
g.DrawString(i.ToString(), horzFont, horzBrush, 350 + i * 20, 520);
}
//绘制文本
StringFormat vertStrFormat = new StringFormat();
vertStrFormat.FormatFlags = StringFormatFlags.DirectionVertical;
for (int i = 1; i < 11; i++)
{
g.DrawString("-", horzFont, horzBrush, 350 + i * 20, 512, vertStrFormat);
x = 350 + i * 20;
}
for (int i = 0; i < 10; i++)
{
g.DrawString((10 * (10 - i)).ToString() + "-", VertFont, vertBrush, 325, 310 + 20 * (i % 11));
if (i == 0)
{
y = 310 + 20 * (i % 11);
}
}
Console.WriteLine("X:{0},Y:{1}", new object[] { x, y });
点击两点连线
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
Graphics g1 = this.CreateGraphics();
Pen linePen = new Pen(Color.Green, 1);
Pen EllipsePen = new Pen(Color.Red, 1);
startPoint = endPoint;
endPoint = new Point(e.X, e.Y);
g1.DrawLine(linePen, startPoint, endPoint);
if (checkBox1.Checked)
{
g1.DrawRectangle(EllipsePen, e.X - 2, e.Y - 2, 4, 4);
}
else
{
g1.DrawEllipse(EllipsePen, e.X - 2, e.Y - 2, 4, 4);
}
linePen.Dispose();
EllipsePen.Dispose();
g1.Dispose();
}
直接上效果图
最后即是刷新,其实这里面有个技巧,就是在刷的时候先把图取下来,刷完再往上盖
而Invalidate函数是使某块区域无效
比如吧!一个LISTVIEW切换一项,系统会先取得那一项的位置,还有改变后的位置,把这两个地方INVALIDATE掉,再画上该画的东西
startPoint = new Point(350, 520);
endPoint = new Point(350, 520);
t = new Rectangle(370, 310, 100, 100);
this.Invalidate(t);
而这里面有太多性能上的问题,改天再说喽!打球去喽!夏天的上海,真热,还有一张饼图的,改天再补上