[英]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.