简体   繁体   English

如何在C#中直接在屏幕上绘图? (示例导致PInvokeStackImbalance)

[英]How to draw directly on the screen in C#? (Example causes PInvokeStackImbalance)

I want to draw directly on the desktop ( As wallpaper or over all, I don't mind ) in C#, but I have not been able to find how. 我想在C#中直接在桌面上画画(如壁纸或者我不介意),但是我还是找不到。 I found this: 我找到了这个:

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

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

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

    static void Main(string[] args) {
        IntPtr desktop = GetDC(IntPtr.Zero);
        using (Graphics g = Graphics.FromHdc(desktop)) {
            g.FillRectangle(Brushes.Red, 0, 0, 100, 100);
        }
        ReleaseDC(desktop);
    }
}

It actually draws a red rectangle but within 1 second, Visual Studio gives me this error: 它实际上绘制了一个红色矩形但在1秒内,Visual Studio给了我这个错误:

PInvokeStackImbalance was detected 检测到PInvokeStackImbalance

Any help is appreciated, Thanks 任何帮助表示赞赏,谢谢

you can not draw directly to the desktop with C#. 你不能用C#直接绘制到桌面。 Basically you are going to have to get the bitmap that is being used as the wallpaper, draw on that, then save that to disk and set that as the active wallpaper. 基本上你将不得不得到用作壁纸的位图,在上面绘制,然后将其保存到磁盘并将其设置为活动壁纸。

look at this SO question for pointers 看看这个SO问题的指针

You could create a top level window that does not have any chrome and then push it to the back of the z-order for the current set of top level windows. 您可以创建一个没有任何镶边的顶级窗口,然后将其推送到当前顶级窗口集的z顺序的后面。 Then you would draw into the client area of that window just like any other C# window. 然后,您将像任何其他C#窗口一样绘制到该窗口的客户区域。

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

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