【问题标题】:Simulate click into a hidden window模拟点击进入隐藏窗口
【发布时间】:2012-05-04 00:30:49
【问题描述】:

我遇到了一个 C# 问题,

我可以模拟对当前窗口的点击,但如果窗口被最小化或隐藏,我想这样做。

有什么想法吗?

【问题讨论】:

标签: c# click hidden simulate sendinput


【解决方案1】:

这是一个完整工作的 sn-p,它将为您提供窗口句柄、目标子窗口并将消息发布到该子窗口。

#include "TCHAR.h"
#include "Windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
    HWND hwndWindowTarget;
    HWND hwndWindowNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if (hwndWindowNotepad)
    {
        // Find the target Edit window within Notepad.
        hwndWindowTarget = FindWindowEx(hwndWindowNotepad, NULL, L"Edit", NULL);
        if (hwndWindowTarget) {
            PostMessage(hwndWindowTarget, WM_CHAR, 'G', 0);
        }
    }

    return 0;
}

目前它将G字符发送到记事本“无标题”(打开一个新记事本,什么都不做。

你可以使用Visual Studio自带的spy++找到子窗口。

下面是一个使用 SendInput 发送鼠标事件的例子:

#include "TCHAR.h"
#include "Windows.h"

int _tmain(int argc, _TCHAR* argv[])
{
    POINT pt;
    pt.x = 300;
    pt.y = 300;

    HWND hwndWindowTarget;
    HWND hwndWindowNotepad = FindWindow(NULL, L"Untitled - Notepad");
    if (hwndWindowNotepad)
    {
        // Find the target Edit window within Notepad.
        hwndWindowTarget = FindWindowEx(hwndWindowNotepad, NULL, L"Edit", NULL);
        if (hwndWindowTarget) {
            PostMessage ( hwndWindowTarget, WM_RBUTTONDOWN, 0, (pt.x) & (( pt.y) << 16) );
            PostMessage ( hwndWindowTarget, WM_RBUTTONUP, 0, (pt.x ) & (( pt.y) << 16) );
        }
    }

    return 0;
}

抱歉,拖了这么久,这里是 C# 版本:

using System;
using System.Runtime.InteropServices;

namespace Sandbox
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);

        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        static extern IntPtr FindWindowEx(IntPtr hWnd, string lpClassName, string lpWindowName, string lParam);

        public const Int32 WM_CHAR = 0x0102;
        public const Int32 WM_KEYDOWN = 0x0100;
        public const Int32 WM_KEYUP = 0x0101;
        public const Int32 VK_RETURN = 0x0D;

        public const string windowName = "Untitled - Notepad";

        static void Main(string[] args)
        {
            Console.WriteLine($"Finding {windowName}");
            var hwndWindowNotepad = FindWindow(null, "Untitled - Notepad");

            if (hwndWindowNotepad != 0)
            {
                // Find the target Edit window within Notepad.
                var hwndWindowTarget = FindWindowEx(hwndWindowNotepad, null, "Edit", null);
                if (hwndWindowTarget != 0)
                {
                    Console.WriteLine($"Sending Char to {windowName}");
                    PostMessage(hwndWindowTarget, WM_CHAR, 'G', 0);
                }
            }
        }
    }
}

【讨论】:

  • 这很有帮助,鼠标左下和上的代码是什么来与 PostMessage 一起使用? :)
  • 如果你愿意,我实际上有一个使用 SendInput 的 C# 代码示例,我会发布它。
  • 在 C# 中会很棒 :) 可以吗?但是用鼠标键
  • 抱歉,使用 mouse_event 的旧代码已被取代。不过,我会努力将两者都转换为 C#,但这可能需要一点时间:)
  • 不管怎样,在这里找到一个非常不错的鼠标输入模拟器stackoverflow.com/questions/5094398/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多