使用System.Drawing在桌面上绘制十字准线? [英] Using System.Drawing to draw a crosshair on the desktop?

查看:149
本文介绍了使用System.Drawing在桌面上绘制十字准线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个非常小的c#实用程序应用程序,该应用程序将利用System.Drawing在我的桌面上绘制全屏,静态,固定的十字准线,以便我可以将一些桌面项与相关的屏幕中心对齐.

I'm trying to create a very small c# utility application that will utilize System.Drawing to draw a full screen, static, fixed cross-hair on my desktop so that I can align some desktop items to the relevant screen center.

我尝试查找一些示例,但并没有提出很多建议,并且想知道是否有人在这方面有任何经验.

I tried looking up a few examples but didn't come up with a whole lot and was wondering if anyone had any experience in this area.

如果可能的话,我宁愿不制作透明的全屏窗口来完成这项任务.

I would prefer not making a transparent full screen window to accomplish this feat if possible.

推荐答案

尝试此方法时会出现大量问题.一方面,您没有桌面的所有权,因此永远无法完全控制它的失效时间.

Loads of issues come up when you try this. For one thing, you don't own the desktop, so you never have complete control of it as to when it gets invalidated.

[DllImport("User32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);

[DllImport("User32.dll")]
static extern void ReleaseDC(IntPtr dc);

private void DrawDeskTop()
{
  IntPtr desk = GetDC(IntPtr.Zero);
  using (Graphics g = Graphics.FromHdc(desk))
  {
    g.FillRectangle(Brushes.Red, new Rectangle((SystemInformation.WorkingArea.Width / 2) - 4, (SystemInformation.WorkingArea.Height / 2) - 20, 8, 40));
    g.FillRectangle(Brushes.Red, new Rectangle((SystemInformation.WorkingArea.Width / 2) - 20, (SystemInformation.WorkingArea.Height / 2) - 4, 40, 8));
  }
  ReleaseDC(desk);
}

运行时,它将看起来很棒.但是,一旦您将表单移到中心上方或移动其他窗口,加号就会消失,因此您将不得不一次又一次地绘制它.

When this runs, it will look great. But as soon as you move your form over the center or move other windows, the plus sign will disappear, so you will have to draw it again, and again, and again.

这篇关于使用System.Drawing在桌面上绘制十字准线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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