I'm trying to send a mouse left click to a sopcast mute button, in c#, but with no result. Here is what I did:
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 gives me this:
000E036E: #32770 … 00070406: AfxOleControl70su 002C06A2: #32770 … 000F03A4: Button “Mute”
Can someone tell me where I'm wrong? Thank you very much.
Have you tried:
SendMessage(chwnd4, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
SendMessage(chwnd4, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
BN_CLICKED is not a window message - it is a notification code send as part of the WPARAM
of a WM_COMMAND message. The WM_COMMAND message requires the control ID as well which isn't available externally. Also WM_COMMAND message are send to the parent window the control, not to the control itself.
The correct way to send this would be:
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);
Of course this assumes that the code is using WM_COMMAND messages and not directly listening for button up/down messages.
A better bet would be to set focus to the button and then send a spacebar press to using SendKeys to perform the button click.
using System;
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!");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.