C# 在屏幕上画图效果的经典例子
本文导语: C#实现在屏幕上画图的效果。 //DllImport所在的名字空间 using System.Runtime.InteropServices; [DllImport("User32.dll")] public extern static System.IntPtr GetDC(System.IntPtr hWnd); private void button19_Click(object sender, EventArgs e) { System.IntPtr DesktopHandle = Get...
C#实现在屏幕上画图的效果。
using System.Runtime.InteropServices;
[DllImport("User32.dll")]
public extern static System.IntPtr GetDC(System.IntPtr hWnd);
private void button19_Click(object sender, EventArgs e)
{
System.IntPtr DesktopHandle = GetDC(System.IntPtr.Zero);
Graphics g = Graphics.FromHdc(DesktopHandle);
g.DrawRectangle(new Pen(Color.Red),new Rectangle(10,10,100,100));
}
附,c#画图的经典小例子
1.位图上绘制点和线
System.Drawing.Image MyImage=new System.Drawing.Bitmap (w,h);//申请位图对象
System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(MyImage); //申请画图对象
//用白色C清出除
Color C=Color.FromArgb(255,255,255);
g.Clear(C);
//画线
g.DrawLine(Pens.Black,x1,y1,x2,y2);
//画一个象素的点
//MyBitmap.SetPixel(x, y, Color.White);
g.DrawImage(MyImage,0,0,h,w); //pictureBox1在(0,0)到(h,w)范围画点
pictureBox1.Image=MyImage; //这个图片贴到pictureBox1控件上
2.在窗体上绘制图形
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);//画笔
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);//画刷
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new Rectangle(0,0,100,200));//画实心椭圆
formGraphics.DrawEllipse(myPen, new Rectangle(0,0,100,200));//空心圆
formGraphics.FillRectangle(myBrush, new Rectangle(0,0,100,200));//画实心方
formGraphics.DrawRectangle(myPen, new Rectangle(0,0,100,200));//空心矩形
formGraphics.DrawLine(myPen, 0, 0, 200, 200);//画线
formGraphics.DrawPie(myPen,90,80,140,40,120,100); //画馅饼图形
//画多边形
formGraphics.DrawPolygon(myPen,new Point[]{ new Point(30,140), new Point(270,250), new Point(110,240), new Point
(200,170), new Point(70,350), new Point(50,200)});
//清理使用的资源
myPen.Dispose();
myBrush.Dispose();
formGraphics.Dispose();
3.在窗体上绘制文本
//在窗体上绘制竖向文本
System.Drawing.Graphics formGraphics = this.CreateGraphics();
string drawString = "Text";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
float x = 150.0f;
float y = 50.0f;
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);//文本
//垂直
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y);//绘制文本
formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);//绘制垂直文本
//清理使用的资源
drawFont.Dispose();
drawBrush.Dispose();
formGraphics.Dispose();
4.其他画笔
//该画刷的HatchStyle有DiagonalCross、 ForwardDiagonal、Horizontal、 Vertical、 Solid等不同风格
HatchStyle hs = HatchStyle.Cross; //十字
HatchBrush sb = new HatchBrush(hs,Color.Blue,Color.Red);
g.FillRectangle(sb,50,50,150,150);
Rectangle r = new Rectangle(500, 300, 100, 100);
LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal);
g.FillRectangle(lb, r);
//PathGradientBrush路径渐变,LinearGradientBrush线性渐变
Image bgimage = new Bitmap("E:2065215396.jpg");
brush = new TextureBrush(bgimage); //易一幅图作椭圆的背景
g.FillEllipse(brush,50,50,500,300);
5.其他技巧
//申请画图区域
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
//创建笔
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
myPen.DashStyle=DashStyle.Dash //DashStyle有Dash、DashDot、DashDotDot、Dot、Solid等风格
//创建单色画刷
System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
//画图函数
protected override void OnPaint ( System.Windows.Forms.PaintEventArgs e )
{
}
//颜色对话框
ColorDialog c = new ColorDialog();
c.ShowDialog();
textBox1.BackColor = c.Color;
//字体对话框
FontDialog f = new FontDialog();
f.ShowDialog();
textBox1.Font = flg.Font;
//RGB的使用
int Red=(int)SelfPlaceKey.GetValue("Red");
int Green=(int)SelfPlaceKey.GetValue("Green");
int Blue=(int)SelfPlaceKey.GetValue("Blue");
BackColor=Color.FromArgb(Red,Green,Blue);
//字体
Font aFont=new Font("Arial",16,FontStyle.Bold|FontStyle.Italic);
rect=new Rectangle(0,y,400,aFont.Height);
g.DrawRectangle(Pens.Blue,rect);
StringFormat sf=new StringFormat();
sf.Alignment=StringAlignment.Far;
g.DrawString("This text is right justified.",aFont,Brushes.Blue,rect,sf);
y+=aFont.Height+20;
aFont.Dispose();