运行下面代码后桌面一真有文字,一直到关闭程序,有没方法可以清除掉。其中有一个 “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));
}
[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));
}
楼主说的清除是什么意思?
感觉 有点 。。
需要的话就
g.Clear(Color.White);
用这个自己试试 。。
g.Clear(Color.White);
整个桌面都白完去,我晕了
InitializeComponent();
Graphics g = Graphics.FromHdc(hdc);
g.DrawString(show, new Font("宋体 ", 36), new SolidBrush(Color.Red), new Point(430, 330));
那你一定得到桌面的句柄了。
[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);
清除那几个字? 这是什么用意呢?
如果LZ 想 当什么事件需要那些字, 调用它的 g.DrawString(); 方法 不就可以了?
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)); 我想在第二次调用时清除前面的内容只显示第二次的内容。
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);
RECT rcUpdate 这个要引入哪个命名空间的。
{
public RECT()
{
}
public CRECT(int left, int top, int right, int bottom)
{
}
}
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);
错误 2 参数“2”: 无法从“<null>”转换为“System.Drawing.Rectangle” E:\BakUpData\C# 视频教程\C# 项目\动态密保卡\动态密保卡\BaseForm.cs 494 50 动态密保卡
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();
}
这个 Screen 是哪来的呀?
系统提示:错误 1 当前上下文中不存在名称“Screen”
System.Drawing.dll
System.Windows.Forms.dll
文件中添加命名空间引用:
using System.Drawing;
using System.Windows.Forms;
测试代码如下:
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);
}