在屏幕上绘制字符串 C#

发布于 2024-09-30 05:37:05 字数 122 浏览 2 评论 0原文

我想简单地在屏幕上(在特定位置)绘制一个字符串(如果可能的话,以特定字体和大小)。我在 C# Windows 窗体应用程序中。不幸的是,我在网上找不到任何关于如何执行此操作的提示。

请帮忙!

基督教

I would like to simply draw a string (if possible in a specific font and size) on the screen (at a specific location). I am within a C# windows forms application. Unfortunately, I could not found any hint on how to do this in the web.

Please help!

Christian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

往日 2024-10-07 05:37:05

要在窗口外绘制字符串,您必须创建一个新窗口,将其蒙版设置为某种颜色(例如洋红色),然后在其上绘制文本 - 您可以在此处使用简单的标签。

将窗口边框样式设置为“无”,然后就可以了。

换句话说,没有附加窗口就无法显示“自由文本”。

对于遮罩颜色,请使用“透明颜色”或类似属性(我稍后会查找它 - 手头没有 VS)

To draw a string outside of your window, you'll have to CREATE a new window, set it's mask to some color (say magenta) and then draw text onto it - you can use simple label here.

Set your window border style to None, and there you go.

In other words, there is no way of displaying 'free text' without window attached.

For masking color, use 'transparency color' or similar property (I will look up into it later - have no VS at hand)

烟花易冷人易散 2024-10-07 05:37:05

并不真正建议您执行您要求的操作,请参阅 链接

如果你真的想做这样的事情;这是一种令人毛骨悚然的方法:

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

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

    protected override void OnPaint(PaintEventArgs e)
    {
        IntPtr desktopDC = GetDC(IntPtr.Zero);

        Graphics g = Graphics.FromHdc(desktopDC);

        g.DrawString("Test", new Font(FontFamily.GenericSerif, 12), Brushes.Blue, 300, 300);
        g.Dispose();

        ReleaseDC(desktopDC);
    }

请注意,我不建议任何人这样做,因为我认为应用程序不应该做这样的事情。如果您想画一些东西,您应该在自己的表单/控件上进行。

doing what you are asking for is not really recommended, see e.g. Link

If you really want to do something like this; here is a creepy way to do it:

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

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

    protected override void OnPaint(PaintEventArgs e)
    {
        IntPtr desktopDC = GetDC(IntPtr.Zero);

        Graphics g = Graphics.FromHdc(desktopDC);

        g.DrawString("Test", new Font(FontFamily.GenericSerif, 12), Brushes.Blue, 300, 300);
        g.Dispose();

        ReleaseDC(desktopDC);
    }

Please note that I DON'T recommend anyone doing this as I don't think applications should be doing stuff like this. If you want to draw something you should do it on your own form/controls.

飘然心甜 2024-10-07 05:37:05

检查这个

或者您可能正在寻找 DrawString 方法

希望这会有所帮助

Check this out.

Or may be you are looking for DrawString method

Hope this will help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文