C# 桌面上直接绘制/在屏幕上绘图和清除

1、名字空间 

using System.Runtime.InteropServices;

2、API函数申明

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();//该函数返回桌面窗口的句柄。
[DllImport("user32.dll", EntryPoint = "GetDCEx", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags); //获取显示设备上下文环境的句柄

3、绘制代码

 private void button4_Click(object sender, EventArgs e)
 {
     IntPtr desk = GetDesktopWindow();
     IntPtr deskDC = GetDCEx(desk, IntPtr.Zero, 0x403);
     Graphics g = Graphics.FromHdc(deskDC);
     g.DrawString("测试", new Font("宋体", 50, FontStyle.Bold), Brushes.Red, new PointF(100, 100));
 }
我注意到绘图使用
Graphics g = Graphics.FromHwnd(form.Handle);

在其控制下在表单背景上绘制。这是你想要完成的吗?

// draw the rectangle
Brush b = new SolidBrush(Color.FromArgb(20, 0, 0, 255));
g.FillRectangle(b, new Rectangle(5, 5, 200, 200));

// clear the rectangle
g.Clear(this.BackColor);

如果我直接在屏幕上绘图,则:

Graphics g = Graphics.FromHwnd(IntPtr.Zero);

Windows 刷新屏幕后,该矩形立即消失。

还有第三种选择,这并不是很简单。

不绘制矩形,而是创建一个降低不透明度、将 TopMost 属性设置为 true 且没有边框的表单。然后使其对事件透明:

protected override void WndProc(ref Message m)
{
    const int WM_NCHITTEST = 0x0084;
    const int HTTRANSPARENT = (-1);

    if (m.Msg == WM_NCHITTEST)
    {
        m.Result = (IntPtr)HTTRANSPARENT;
    }
    else
    {
        base.WndProc(ref m);
    }
}

之后您唯一需要注意的是此表单的 Visible、Location 和 Size 属性。

bool change = false;
private void timer1_Tick(object sender, EventArgs e)
{
    try
    {
        if (change)
        {
            InvalidateRect(IntPtr.Zero, IntPtr.Zero, true);
            change = false;
        }
        else
        {
            PaintRectangleToScreen();
            change = true;
        }


    }
    catch (System.Exception caught)
    {

        MessageBox.Show(caught.Message);
    }
}

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 = 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();

 

posted @   多见多闻  阅读(2204)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2022-08-15 Unity做360度的全景照片
点击右上角即可分享
微信分享提示