我在另一个窗口上覆盖图形时遇到问题

时间:2014-02-17 21:09:37

标签: 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 < 1000000000; i++) {
                WindowHandler.testOverlay();
            }
        }
    }
}

WindowHandler.cs:

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;
        }
    }
}

我不确定我做错了什么。我正在编写一个扫雷解算器,我正在尝试在扫雷窗口上叠加图形以提供调试信息。不幸的是,它似乎根本不起作用,因为我在屏幕上看不到任何变化。我在Program.cs中循环了1000000000次,以防它在每次刷新帧时擦除我的叠加层。我宁愿不尝试挂钩DirectX。

0 个答案:

没有答案