方法一:调用Windows API
你需要System.Drawing
and System.Runtime.InteropServices
。您可能需要向它们添加项目引用。
using System.Runtime.InteropServices;
using System.Drawing;
使用 P/Invoke 将方法添加到您的类中
[DllImport("User32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern void ReleaseDC(IntPtr hwnd, IntPtr dc);
Get a Graphics
整个屏幕的对象并用它绘制一个矩形:
IntPtr desktopPtr = GetDC(IntPtr.Zero);
Graphics g = Graphics.FromHdc(desktopPtr);
SolidBrush b = new SolidBrush(Color.White);
g.FillRectangle(b, new Rectangle(0, 0, 1920, 1080));
g.Dispose();
ReleaseDC(IntPtr.Zero, desktopPtr);
这种方法的问题是,如果屏幕完全刷新,矩形将被覆盖,使其对于大多数实际应用来说毫无用处。
方法2:创建无边框表单
和以前一样,您需要一个项目参考。这次到System.Windows.Forms
。你还需要System.Drawing
again:
using System.Drawing;
using System.Windows.Forms;
制作新表单,删除其边框,用它填充屏幕,然后将其放在任务栏顶部:
Form f = new Form();
f.BackColor = Color.White;
f.FormBorderStyle = FormBorderStyle.None;
f.Bounds = Screen.PrimaryScreen.Bounds;
f.TopMost = true;
Application.EnableVisualStyles();
Application.Run(f);
一个可能的问题是用户只需使用 Alt+Tab 即可离开窗口。如果你想做任何更复杂的图形,你需要编写一些绘图代码,例如this http://msdn.microsoft.com/en-us/library/55aa1t17%28v=VS.90%29.aspx。要使表单背景透明,请将其设置为TransparentKey
与其相同Backolor
.
我刚刚在 .NET 4.5 和 Windows 7 中测试了这两个版本,因此早期版本可能会有所不同。更多信息here https://stackoverflow.com/a/3379369/626796 and here https://stackoverflow.com/a/277776/626796.