You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C#实现检测屏幕绿色像素并自动点击SAP弹窗?

实现屏幕绿色像素检测与自动点击(C# WPF)

可以通过屏幕像素采样+Windows API模拟鼠标操作实现需求,以下是具体实现方案:

核心步骤

  1. 截取屏幕指定区域的像素,检测目标绿色是否出现
  2. 当检测到绿色像素时,调用Windows API模拟鼠标点击操作

代码实现

1. 引用必要命名空间

需要添加System.Drawing程序集引用(WPF项目右键→添加引用→程序集→勾选System.Drawing),并引入以下命名空间:

using System;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using System.Runtime.InteropServices;

2. 模拟鼠标点击的Windows API声明

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

private const uint MOUSEEVENTF_LEFTDOWN = 0x02;
private const uint MOUSEEVENTF_LEFTUP = 0x04;

/// <summary>
/// 在指定坐标执行鼠标左键点击
/// </summary>
/// <param name="x">屏幕X坐标</param>
/// <param name="y">屏幕Y坐标</param>
public static void ClickAt(int x, int y)
{
    Cursor.Position = new Point(x, y);
    mouse_event(MOUSEEVENTF_LEFTDOWN, (uint)x, (uint)y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, (uint)x, (uint)y, 0, 0);
}

3. 绿色像素检测逻辑

/// <summary>
/// 检测指定坐标的像素是否为目标绿色(允许颜色误差)
/// </summary>
/// <param name="x">屏幕X坐标</param>
/// <param name="y">屏幕Y坐标</param>
/// <param name="tolerance">颜色误差范围</param>
/// <returns></returns>
public static bool IsTargetGreen(int x, int y, int tolerance = 10)
{
    // 替换为你实际获取的SAP绿色钩子的RGB值(用取色工具提取)
    const int targetR = 0;
    const int targetG = 255;
    const int targetB = 0;

    using (Bitmap screenBmp = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))
    {
        using (Graphics g = Graphics.FromImage(screenBmp))
        {
            g.CopyFromScreen(0, 0, 0, 0, screenBmp.Size);
        }
        Color pixelColor = screenBmp.GetPixel(x, y);
        
        // 对比RGB值,允许一定误差
        return Math.Abs(pixelColor.R - targetR) <= tolerance &&
               Math.Abs(pixelColor.G - targetG) <= tolerance &&
               Math.Abs(pixelColor.B - targetB) <= tolerance;
    }
}

4. 后台循环检测与触发点击

在WPF窗口的加载事件中启动后台检测线程,避免阻塞UI:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Task.Run(() =>
    {
        // 限定检测区域(根据SAP消息框的实际位置调整,比如屏幕底部中间)
        int checkStartX = 800;
        int checkEndX = 1000;
        int checkStartY = (int)SystemParameters.PrimaryScreenHeight - 100;
        int checkEndY = (int)SystemParameters.PrimaryScreenHeight;

        while (true)
        {
            bool greenDetected = false;
            int clickX = 0;
            int clickY = 0;

            // 遍历检测区域内的像素
            for (int x = checkStartX; x <= checkEndX; x++)
            {
                for (int y = checkStartY; y <= checkEndY; y++)
                {
                    if (IsTargetGreen(x, y))
                    {
                        greenDetected = true;
                        clickX = x;
                        clickY = y;
                        break;
                    }
                }
                if (greenDetected) break;
            }

            if (greenDetected)
            {
                // 切换到UI线程执行点击操作
                Dispatcher.Invoke(() => ClickAt(clickX, clickY));
                // 点击后暂停2秒,避免重复触发
                Thread.Sleep(2000);
            }

            // 每500毫秒检测一次,降低CPU占用
            Thread.Sleep(500);
        }
    });
}

关键注意事项

  • 颜色值校准:用取色工具(如Snipaste、系统画图工具)获取SAP绿色钩子的真实RGB值,替换代码中的targetR/targetG/targetB。
  • 权限问题:Windows 10/11下需以管理员权限运行程序,否则可能无法正常截取屏幕或模拟鼠标操作。
  • 检测区域优化:尽量缩小检测范围,避免全屏幕遍历,提升性能。
  • 线程安全:WPF中操作UI相关内容(如Cursor.Position)需通过Dispatcher.Invoke切换到UI线程执行。

内容的提问来源于stack exchange,提问作者user20202784

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 18:31:08