[英]Send a mouse left click to a sopcast mute button, in c#
我试图用鼠标左键单击一个sopcast静音按钮,在c#中,但没有结果。 这是我做的:
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: Button “Mute”
谁能告诉我哪里错了? 非常感谢你。
你有没有尝试过:
SendMessage(chwnd4, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
SendMessage(chwnd4, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
BN_CLICKED不是窗口消息 - 它是作为WM_COMMAND消息的WPARAM
的一部分发送的通知代码。 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执行按钮单击。
使用系统;
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!");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.