gpt4 book ai didi

c# - 将右键单击发送到窗口

转载 作者:行者123 更新时间:2023-12-04 10:56:09 28 4
gpt4 key购买 nike

我正在尝试将鼠标右键单击发送到指定坐标的窗口。

我测试了 2 个代码

代码 1:

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);

public struct POINT
{
public int x;
public int y;
}

var client = Process.GetProcessesByName("client_dx");
var whandle = client.MainWindowHandle;

POINT point = new POINT();
point.x = 1836;
point.y = 325;
ScreenToClient(whandle, ref point);
int lparm = (point.x << 16) + point.y;
int lngResult = SendMessage(whandle, 0x0204, 0, lparm);
int lngResult2 = SendMessage(whandle, 0x0205, 0, lparm);

代码 2:

[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);

public struct POINT
{
public int x;
public int y;
}

public int MakeLParam(int LoWord, int HiWord)
{
return (int)((HiWord << 16) | (LoWord & 0xFFFF));
}

var client = Process.GetProcessesByName("client_dx");
var whandle = client.MainWindowHandle;

POINT point = new POINT();
point.x = 1836;
point.y = 325;

ScreenToClient(whandle, ref point);

int lparm = MakeLParam(point.x, point.y);
int lngResult = SendMessage(whandle, 0x0204, 0, lparm);
int lngResult2 = SendMessage(whandle, 0x0205, 0, lparm);

它发送了一个右键单击但没有发送到正确的坐标,似乎它忽略了我在 LPARAM 中指定的坐标,因为如果我在窗口周围移动鼠标,它会在我放置鼠标指针但不在坐标中的任何地方单击我指定。

我测试了更改代码 2 中的这一行:

int lparm = MakeLParam(point.x, point.y);

给这个:

int lparm = (point.x << 16) + point.y;

但是没有用,我得到了相同的结果...

最佳答案

您可以使用 SendMessagemouse_eventSendInput执行鼠标操作。在这里,我将分享有关前两个的一些细节和示例。

使用 SendMessage

  • SendMessage 无需移动光标即可执行鼠标操作。
  • SendMessage 需要窗口句柄来发送消息。
  • SendMessage 将鼠标消息发送到窗口内的相对位置。该坐标应相对于窗口客户区的左上角。
  • 如果您知道要发送点击的客户坐标中的哪个点,则只需使用客户坐标即可。通常是这样。
  • 如果您有屏幕位置并且想将其转换为客户端相对位置,您可以使用 ScreenToClient。但是由于您通常知道要单击的相对位置,因此通常不需要 ScreenToClient .
  • 传递参数给MakeLParam低位字指定 x 坐标,高位字指定光标的 y 坐标。为了减少混淆,请使用以下函数:

    IntPtr MakeLParam(int x, int y) => (IntPtr)((y << 16) | (x & 0xFFFF));
  • 附带说明一下,如果您想将消息发送到窗口并返回而不等待线程处理该消息,您可以使用PostMessage。 .

使用 mouse_event

  • mouse_event 在当前光标位置执行鼠标操作。
  • 在调用 mouse_event 之前,您需要将光标移动到屏幕上您要执行点击的位置。
  • 要获取窗口客户点的屏幕位置,可以使用ClientToScreen。方法。
  • 要移动光标你可以设置Cursor.Position到屏幕位置。
  • 建议改用SendInput函数。

示例 1 - 发送消息

使用SendMessage,您可以点击指定窗口的指定相对位置。在下面的示例中,我在 notepad 主窗口中找到了 edit 控件,然后右键单击 (20,20) 编辑控件的客户端矩形内的坐标:

//using System;
//using System.Diagnostics;
//using System.Drawing;
//using System.Linq;
//using System.Runtime.InteropServices;
//using System.Windows.Forms;
const int WM_RBUTTONDOWN = 0x0204;
const int WM_RBUTTONUP = 0x0205;
const int WM_MOUSEMOVE = 0x0200;
[DllImport("User32.DLL")]
static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

IntPtr MakeLParam(int x, int y) => (IntPtr)((y << 16) | (x & 0xFFFF));

[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,
string lpszClass, string lpszWindow);

void PerformRightClick(IntPtr hwnd, Point point)
{
var pointPtr = MakeLParam(point.X, point.Y);
SendMessage(hwnd, WM_MOUSEMOVE, IntPtr.Zero, pointPtr);
SendMessage(hwnd, WM_RBUTTONDOWN, IntPtr.Zero, pointPtr);
SendMessage(hwnd, WM_RBUTTONUP, IntPtr.Zero, pointPtr);
}
void button1_Click(object sender, EventArgs e)
{
var notepad = Process.GetProcessesByName("notepad").FirstOrDefault();
if (notepad != null)
{
var edit = FindWindowEx(notepad.MainWindowHandle, IntPtr.Zero, "Edit", null);
PerformRightClick(edit, new Point(20, 20));
}
}

示例 2 - mouse_event

使用 mouse_event 您可以点击当前的鼠标位置。这意味着您需要将鼠标移动到所需的位置。在下面的示例中,我在 notepad 主窗口中找到了 edit 控件,然后右键单击 (20,20) 编辑控件的客户端矩形内的坐标:

//using System;
//using System.Diagnostics;
//using System.Drawing;
//using System.Linq;
//using System.Runtime.InteropServices;
//using System.Windows.Forms;
const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
const int MOUSEEVENTF_RIGHTUP = 0x0010;
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy,
uint cButtons, uint dwExtraInfo);

[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter,
string lpszClass, string lpszWindow);

[StructLayout(LayoutKind.Sequential)]
struct POINT { public int X; public int Y; }

[DllImport("user32.dll")]
static extern bool ClientToScreen(IntPtr hWnd, ref POINT lpPoint);

void PerformRightClick(IntPtr hwnd, Point p)
{
POINT point = new POINT() { X = p.X, Y = p.Y };
ClientToScreen(hwnd, ref point);
Cursor.Position = new Point(point.X, point.Y);
uint X = (uint)Cursor.Position.X;
uint Y = (uint)Cursor.Position.Y;
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, X, Y, 0, 0);
}
void button1_Click(object sender, EventArgs e)
{
var notepad = Process.GetProcessesByName("notepad").FirstOrDefault();
if (notepad != null)
{
var edit = FindWindowEx(notepad.MainWindowHandle, IntPtr.Zero, "Edit", null);
PerformRightClick(edit, new Point(20, 20));
}
}

关于c# - 将右键单击发送到窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59180107/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com