简体   繁体   English

C#应用程序外部的图形对象-可行吗?

[英]Graphic object outside of C# app - is it doable?

I have already asked here but the only answer was related to positioning. 我已经在这里问过,但唯一的答案与定位有关。 I would need to make an app which would produce an image always following the mouse, outside of the app. 我需要制作一个应用程序,该应用程序始终会在应用程序外部跟随鼠标生成图像。 I know now how to hook mouse position, but I have no clue how to let the object (image) be on the top of other apps. 我现在知道如何勾勒鼠标的位置,但是我不知道如何让对象(图像)位于其他应用程序的顶部。 Anyway I dont know if it is even possible in .NET. 无论如何,我什至不知道在.NET中是否可能。 Thanks! 谢谢!

One of the best and easiest ways to accomplish such a thing is to have a topmost window, with no border and not showing in the taskbar. 完成此操作的最佳和最简便的方法之一是拥有一个最顶部的窗口,该窗口没有边框且不在任务栏中显示。 That would allow you to basically draw whatever you want wherever you want it. 这样一来,您就可以随心所欲地绘制所需的任何内容。

using System.Windows.Forms;

Form overlay = new Form();
overlay.FormBorderStyle = FormBorderStyle.None;
overlay.ShowInTaskbar = false;
overlay.TopMost = true;

For bonus points, you could even apply the toolbar-property and revoke it's right to be activated. 对于奖励积分,您甚至可以应用工具栏属性并取消激活它的权利。 That will yield a form which does not behave like a window, but like fe a tooltip. 这将产生一种形式,它的行为不像窗口,而是像工具提示一样。 For that you need to subclass the Form, like this: 为此,您需要子类化Form,如下所示:

public class PassiveForm : Form
{
    public PassiveForm()
    {
        InitializeComponent();
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams parameter = base.CreateParams;
            parameter.ExStyle |= 0x80; // Apply toolbar-property
            parameter.ExStyle |= 0x8000000; // Revoke acitvation
            return parameter;
        }
    }

    protected override bool ShowWithoutActivation
    {
        get
        {
            return true;
        }
    }
}

Following the mouse around is a little bit harder, but pooling the mouse location should work well enough. 跟踪鼠标会有些困难,但是合并鼠标位置应该可以很好地工作。

You might try Graphics.FromHdc(IntPtr.Zero) . 您可以尝试Graphics.FromHdc(IntPtr.Zero) In plain old GDI this allowed you to draw directly on the desktop on top of everything. 在普通的旧GDI中,这使您可以直接在桌面上绘制所有内容。 I'm not sure though if this still works in .NET. 我不确定这是否仍然可以在.NET中使用。

Added: That said, I'd still go for the trasparent-background-window approach mentioned in another answer. 补充:就是说,我仍然会继续尝试另一个答案中提到的透明背景窗口方法。 Drawing directly on desktop is waaaay too messy and error-prone. 直接在桌面上绘图太乱而且容易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM