在屏幕上画出无形体 [英] draw on screen without form

查看:105
本文介绍了在屏幕上画出无形体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在屏幕上创建一个大的白色矩形只是用C#控制台,而不是一个C#窗体应用程序?

Is it possible to create a big white rectangle on the screen with just using the C# console and not an C# Forms Application?

它应该覆盖整个屏幕,如果可能的。
我知道我必须使用System.Drawing中,并试图几个步骤,但没有人会实际上我的屏幕上打印出任何东西!

It should cover the whole Screen if possible. I know I have to use the System.Drawing and tried several steps, but none will actually print anything on my screen!

推荐答案

您需要 System.Drawing中 System.Runtime.InteropServices 。您可能需要项目引用添加到它们。

Method 1: Call the Windows API

You need System.Drawing and System.Runtime.InteropServices. You may need to add project references to them.

using System.Runtime.InteropServices;
using System.Drawing;

添加方法,您的类的P / Invoke

Add the methods to your class with 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:

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.

和以前一样,你需要一个项目引用。此番 System.Windows.Forms的。您还需要 System.Drawing中又说:

As before, you need a project reference. This time to System.Windows.Forms. You'll also need System.Drawing again:

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

这个可能的问题是,用户也可以用Alt + Tab键离开了窗口。如果你想做任何更复杂的图形,你需要写一些绘图code一样的这个。为了使窗体背景透明,设置其 TransparentKey 来与它的 Backolor

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.

我刚刚测试过这两个在.NET 4.5和Windows 7,因此它可能是早期版本不同。更多信息 href=\"http://stackoverflow.com/a/3379369/626796\">这里

I've just tested both of these in .NET 4.5 and Windows 7, so it may be different for earlier versions. More information here and here.

这篇关于在屏幕上画出无形体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆