C# 在另一个窗口上叠加图形时遇到问题

C# 在另一个窗口上叠加图形时遇到问题,c#,.net,graphics,overlay,C#,.net,Graphics,Overlay,Program.cs: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Test { class Program { static void Main(string[] args) { for (int i = 0; i < 1000000

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test {
    class Program {
        static void Main(string[] args) {
            for (int i = 0; i < 1000000000; i++) {
                WindowHandler.testOverlay();
            }
        }
    }
}

我不确定我做错了什么。我正在编写一个“扫雷舰”解算器,并试图在“扫雷舰”窗口上覆盖图形,以提供调试信息。不幸的是,它似乎根本不起作用,因为我在屏幕上看不到任何变化。我正在Program.cs中循环100000000次,以防每次刷新帧时它都会删除我的覆盖。我不想挂接DirectX。

我猜Windows窗体控件会覆盖您的覆盖:)。。。尝试覆盖表单类上的OnPaint事件,并将您自己的图形放在基础后面?我不知道,我最终做了
Graphics g=Graphics.FromHwnd(IntPtr.Zero)这似乎是可行的。好的,似乎是用IntPtr初始化图形。Zero获取桌面图形上下文。。。我不知道你用的是windows自带的扫雷程序。。。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test {
    static class WindowHandler {
        private const String WINDOW_TITLE = "Minesweeper";
        private static IntPtr windowHandle = IntPtr.Zero;

        public static void testOverlay() {
            if (windowHandle == IntPtr.Zero) {
                windowHandle = getWindowHandle();
            }

            Graphics g = Graphics.FromHwnd(windowHandle);
            g.FillRectangle(new SolidBrush(Color.White), 0, 0, 10000, 10000);
        }

        private static IntPtr getWindowHandle() {
            foreach (Process proc in Process.GetProcesses()) {
                if (proc.MainWindowTitle == WINDOW_TITLE) {
                    return proc.MainWindowHandle;
                }
            }

            MessageBox.Show("Error: Unable to find window.");
            return IntPtr.Zero;
        }
    }
}