C# 在没有表格的情况下在屏幕上绘图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14385838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw on the screen without a form
提问by user1988147
Is it possible to create a big white rectangle on the screen, without using a Forms Application?
是否可以在不使用表单应用程序的情况下在屏幕上创建一个大的白色矩形?
It should cover the whole screen if possible.
I know I have to use the System.Drawing
and tried several steps, but none have actually printed anything to my screen!
如果可能,它应该覆盖整个屏幕。我知道我必须使用System.Drawing
并尝试了几个步骤,但实际上没有人在我的屏幕上打印任何内容!
采纳答案by Tharwen
Method 1: Call the Windows API
方法一:调用Windows API
You need System.Drawing
and System.Runtime.InteropServices
. You may need to add project references to them.
你需要System.Drawing
和System.Runtime.InteropServices
。您可能需要向它们添加项目引用。
using System.Runtime.InteropServices;
using System.Drawing;
Add the methods to your class with P/Invoke
使用 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
object for the entire screen and draw a rectangle with it:
获取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);
The problem with this method is that if the screen refreshes at all, the rectangle will be overwritten, making it useless for most practical applications.
这种方法的问题在于,如果屏幕完全刷新,矩形将被覆盖,使其对大多数实际应用程序无用。
Method 2: Create a borderless form
方法二:创建无边框表单
As before, you need a project reference. This time to System.Windows.Forms
. You'll also need System.Drawing
again:
和以前一样,您需要一个项目参考。这次到System.Windows.Forms
. 您还需要System.Drawing
再次:
using System.Drawing;
using System.Windows.Forms;
Make the new form, remove its borders, fill the screen with it, and put it on top of the taskbar:
创建新表单,移除其边框,用它填充屏幕,然后将其放在任务栏的顶部:
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);
A possible issue with this is that the user can just alt+tab away from the window. If you want to do any more complicated graphics, you'll need to write some drawing code like this. To make the form background transparent, set its TransparentKey
to the same as its Backolor
.
一个可能的问题是用户只能 alt+tab 离开窗口。如果你想做更复杂的图形,你需要像这样编写一些绘图代码。要使表单背景透明,请将其设置TransparentKey
为与其Backolor
.
I've just tested both of these in .NET 4.5 and Windows 7, so it may be different for earlier versions. More information hereand here.
回答by Paul Ruane
Yes it is possible to draw to the screen but it may be easier to use a topmost, borderless form.
是的,可以绘制到屏幕上,但使用最顶层的无边框形式可能更容易。
You can also do this from a console application, if you must, providing you reference the necessary assemblies but this would result in a console window remaining on screen for the life of the application.
如果必须,您也可以从控制台应用程序执行此操作,前提是您引用了必要的程序集,但这会导致控制台窗口在应用程序的整个生命周期内都保留在屏幕上。
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
Alternatively I believe you can create an instance of Window
and call Show
on that.
或者,我相信您可以创建一个实例Window
并调用Show
它。
This answer to a different questionexplains how to use GDI+ calls to draw directly to the screen.
This answer to a different question解释了如何使用 GDI+ 调用直接绘制到屏幕上。
回答by 500 - Internal Server Error
The way I understand the question, that's not really possible in any meaningful way. I suppose you could platform invoke out to user32.dll and gdi32.dll and implement a classical Windows UI application that way, with message loop, and so on, but it would be a lot simpler to just use Forms. What's the reason you can't do that?
我理解这个问题的方式,这在任何有意义的方式中都是不可能的。我想您可以通过平台调用 user32.dll 和 gdi32.dll 并通过消息循环等方式实现经典的 Windows UI 应用程序,但仅使用 Forms 会简单得多。你不能这样做的原因是什么?