【问题标题】:Send click to background window relative position in panel with C#使用C#将点击发送到面板中的背景窗口相对位置
【发布时间】:2020-11-20 13:45:21
【问题描述】:

我使用 C#,我有一个带有 button1、button2 和 panel1 的应用程序,如果我点击 button1,它会打开一个到 panel1 的外部程序,然后如果我点击 button2,它会向窗口发送背景点击,工作正常. 但问题是这个点击位置不是相对于面板,也不是去正确的地方。它发送点击实际光标在哪里。

我使用的代码片段:

PerformRightClick(proc.MainWindowHandle, new Point(54, 42));
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);
}
[System.Runtime.InteropServices.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));

【问题讨论】:

    标签: c# visual-studio mouse background-process sendmessage


    【解决方案1】:

    根据我的测试,当我们想要发送点击到面板时,我们需要使用panel.Handle

    这是一个代码示例,您可以参考。

    private const Int32 WM_MOUSEMOVE = 0x0200;
    
            private const Int32 WM_RBUTTONDOWN = 0x0204;
            private const Int32 WM_RBUTTONUP = 0x0205;
            [System.Runtime.InteropServices.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));
    
            private void button1_Click(object sender, EventArgs e)
            {
                PerformRightClick(panel1.Handle, new Point(20, 30));
            }
    
            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);
            }
    
            private void panel1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Panel is clicked");
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                PerformRightClick(this.Handle, new Point(20, 30));
            }
    
            private void Form1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Form is clicked");
            }
    

    图片:

    如上图,当我们点击button1时,点击panel,点击button2时点击Form。

    【讨论】:

    • 亲爱的杰克,首先感谢您的时间和测试!正如我用我糟糕的英语所说的那样,剪裁还可以。程序发送点击但位置不好。它总是将点击发送到当前鼠标位置而不是 lparam 设置的位置,也许 lparam 是错误的或者我不知道。
    • @Laszlo,你如何检查位置是否不好?你检查过我的代码吗?请先试试上面的代码,然后你可以告诉我你有什么问题。
    • 杰克,我在我的代码中测试了:private void panel1_MouseUp(object sender, MouseEventArgs e) { MessageBox.Show(string.Format("X: {0} Y: {1}", e.X, e.Y)); } 然后我在你的源代码上使用了这段代码,我得到了很好的坐标,所以它可以工作。我做错了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多