0

我正在尝试将鼠标左键单击发送到 c# 中的 sopcast 静音按钮,但没有结果。这是我所做的:

  const int BN_CLICKED = 245;  
public static void mute_sopcast() 
{ 

    IntPtr hwnd = FindWindow("#32770", null); 
    IntPtr chwnd1 = FindWindowEx(hwnd, IntPtr.Zero, "AfxOleControl70su", null); 
    IntPtr chwnd2 = FindWindowEx(chwnd1, IntPtr.Zero, "#32770", null); 
    IntPtr chwnd3 = FindWindowEx(chwnd2, IntPtr.Zero, "Button", "Mute"); 

    PostMessage(chwnd3, BN_CLICKED, 0, (int)IntPtr.Zero); 

    //The code bellow I made it to see if chwnd… is zero or not.

    if (chwnd3 == new IntPtr(0)) 
    { 
        MessageBox.Show("IntPtr(0)"); 
    } 
    else if (chwnd3 == IntPtr.Zero) 
    { 
        MessageBox.Show("IntPtr.Zero"); 
    } 
    else 
    { 
        MessageBox.Show("IntPtr.Zero not empty"); 
    } 


} 

Winspector Spy 给了我这个:

000E036E: #32770
    …
    00070406:AfxOleControl70su
        002C06A2:#32770
            …
            000F03A4:“静音”按钮

有人能告诉我哪里错了吗?非常感谢你。

4

3 回答 3

1

你有没有尝试过:

SendMessage(chwnd4, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
SendMessage(chwnd4, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero); 
于 2012-07-08T11:54:54.743 回答
1

BN_CLICKED不是窗口消息 - 它是作为WPARAMWM_COMMAND 消息的一部分发送的通知代码。WM_COMMAND 消息也需要外部不可用的控件 ID。WM_COMMAND 消息也被发送到控件的父窗口,而不是控件本身。

发送这个的正确方法是:

const int WM_COMMAND = 0x111;
const int BN_CLICKED = 245; 

int controlID = ??; // Something you don't know
int wParam = BN_CLICKED >> 16 | controlID;

PostMessage(chwnd3, WM_COMMAND, wParam , (int)chwnd4);

当然,这假设代码正在使用 WM_COMMAND 消息,而不是直接侦听按钮向上/向下消息。

更好的选择是将焦点设置为按钮,然后发送空格键以使用SendKeys执行按钮单击。

于 2012-07-08T12:04:12.810 回答
0

使用系统;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace automouse
{
    public partial class Form1 : Form
    {
        public const int WM_LBUTTONDOWN = 0x0201;
        public const int WM_LBUTTONUP = 0x0202;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendMessage(button2.Handle, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
            SendMessage(button2.Handle, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Button2 clicked!");
        }
    }
}
于 2012-07-21T21:50:25.553 回答