运行下面代码后桌面一真有文字,一直到关闭程序,有没方法可以清除掉。其中有一个 “g.Dispose();”和“g.Clear();”但不知道怎么用。希望高手帮帮忙。谢谢
       [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr GetDC(IntPtr hWnd);
        IntPtr hdc = GetDC(IntPtr.Zero);
        public Form1()
        {
            string show = "显示这行字符在桌面";
            InitializeComponent();
            Graphics g = Graphics.FromHdc(hdc);
            g.DrawString(show, new Font("宋体 ", 36), new SolidBrush(Color.Red), new Point(430, 330));  
        }

解决方案 »

  1.   

    顯示 背景顏色的話 , 在 new SolidBrush(Color.Red)   Color. 里面改不就可以了?
         楼主说的清除是什么意思?
      

  2.   

       如果 LZ 想用g.Clear();  这方法 清除 。。
       感觉  有点 。。    
      需要的话就
    g.Clear(Color.White);  
         用这个自己试试 。。
      
      

  3.   


    g.Clear(Color.White); 
    整个桌面都白完去,我晕了
      

  4.   

    我是要清除桌面上,显示的那几个字。代码如下string show = "显示这行字符在桌面";
      InitializeComponent();
      Graphics g = Graphics.FromHdc(hdc);
      g.DrawString(show, new Font("宋体 ", 36), new SolidBrush(Color.Red), new Point(430, 330));   
      

  5.   

    桌面么?
    那你一定得到桌面的句柄了。
    [DllImport("user32.dll", EntryPoint="InvalidateRect")]
    public static extern int InvalidateRect (
    IntPtr hwnd,
    Rectangle lpRect,
    bool bErase
    );
    [DllImport("user32.dll", EntryPoint="GetDesktopWindow")]
    public static extern IntPtr GetDesktopWindow ();//调用
    InvalidateRect(GetDesktopWindow(),System.Windows.Forms.Screen.PrimaryScreen.Bounds,true);
      

  6.   


        清除那几个字?    这是什么用意呢?
      如果LZ 想 当什么事件需要那些字, 调用它的 g.DrawString(); 方法 不就可以了?
      

  7.   

    我再调用它的 g.DrawString(); 方法时,原来的字也在,再二次调用的也在。如 string show1 = "显示这行字符在桌面 一";
       string show2 = "显示这行字符在桌面 二";

    g.DrawString(show1, new Font("宋体 ", 36), new SolidBrush(Color.Red), new Point(430, 330));   
    g.DrawString(show2, new Font("宋体 ", 36), new SolidBrush(Color.Red), new Point(430, 330));   我想在第二次调用时清除前面的内容只显示第二次的内容。
      

  8.   

    重绘桌面:
    C# code
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetDesktopWindow();[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern bool RedrawWindow(IntPtr hwnd, RECT rcUpdate, IntPtr hrgnUpdate, int flags);RedrawWindow(GetDesktopWindow(), null, IntPtr.Zero, 0x85);
      

  9.   


    RECT rcUpdate  这个要引入哪个命名空间的。
      

  10.   

    找不到类型或命名空间名称“RECT”(是否缺少 using 指令或程序集引用
      

  11.   

    public class RECT 

    public   RECT() 


    public   CRECT(int   left,   int   top,   int   right,   int   bottom) 


    }
      

  12.   

    都写给你很多正确答案了。你自己视而不见怪谁?
    RECT  你可以直接替换用Rectangle类。如梦那个修改一下类型[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetDesktopWindow();[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern bool RedrawWindow(IntPtr hwnd, Rectangle rcUpdate, IntPtr hrgnUpdate, int flags);RedrawWindow(GetDesktopWindow(), null, IntPtr.Zero, 0x85);
      

  13.   

    这个也不行
    错误 2 参数“2”: 无法从“<null>”转换为“System.Drawing.Rectangle” E:\BakUpData\C# 视频教程\C# 项目\动态密保卡\动态密保卡\BaseForm.cs 494 50 动态密保卡
      

  14.   

    新建控制台程序。粘贴:[DllImport("user32.dll", EntryPoint = "InvalidateRect")]
    public static extern int InvalidateRect(
        IntPtr hwnd,
        IntPtr lpRect,
        bool bErase
    );
    [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
    public static extern IntPtr GetDesktopWindow();[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern bool RedrawWindow(IntPtr hwnd, ref Rectangle rcUpdate, IntPtr hrgnUpdate, int flags);static void Main(string[] args)
    {
        Thread.Sleep(2000);
        Console.WriteLine("现在开始绘制屏幕");
        Graphics g = Graphics.FromHwnd(IntPtr.Zero);
        g.DrawLine(Pens.Yellow, new Point(0, 0), new Point(500, 800));
        g.Dispose(); 
        Thread.Sleep(2000);
        Console.WriteLine("现在开始清除屏幕");
        Rectangle rect = Screen.PrimaryScreen.Bounds;
        RedrawWindow(GetDesktopWindow(), ref rect, IntPtr.Zero, 0x85);
        Console.ReadKey();
    }
      

  15.   

     GetDC 记得一定要与DeleteDC 配套使用 设备上下文资源是非常有限的 如果用完不立刻释放下次再申请 不要2分钟你的系统就资源耗尽~~~
      

  16.   

    Rectangle rect = Screen.PrimaryScreen.Bounds;
    这个 Screen 是哪来的呀?
    系统提示:错误 1 当前上下文中不存在名称“Screen”
      

  17.   

    项目添加程序集引用:
    System.Drawing.dll
    System.Windows.Forms.dll
    文件中添加命名空间引用:
    using System.Drawing;
    using System.Windows.Forms;
      

  18.   

    现在可以了。要引入 using System.Windows.Forms;
      

  19.   

    这个有点像字幕哦。我试了一下楼上朋友的代码,完全不行,全叠在一起了。
    测试代码如下:
                for (int i = 0; i < 40; i++)
                {
                    Rectangle rect = Screen.PrimaryScreen.Bounds;
                    Graphics g = Graphics.FromHwnd(IntPtr.Zero);                RedrawWindow(GetDesktopWindow(), ref rect, IntPtr.Zero, 0x85);
                    Thread.Sleep(2000);                g.DrawString((new Random()).Next().ToString(),
                        new Font( "宋体", 15, FontStyle.Bold | FontStyle.Italic ),
                        new SolidBrush(Color.Blue), new PointF(Screen.GetWorkingArea(this).Width / 2,
                            Screen.GetWorkingArea(this).Height / 2));
                    g.Dispose();
                    Thread.Sleep(2000);
                }