Кликнуть на кнопку в стороннем приложении - C#
Формулировка задачи:
int hwnd=0;
IntPtr hwndChild=IntPtr.Zero;
//Get a handle for the Calculator Application main window
hwnd=FindWindow(null,"Calculator");
if(hwnd == 0)
//Get a handle for the "1" button
hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","1");
//send BN_CLICKED message
SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);Пожалуйста, не нужно отсылать меня в похожие темы, я неоднократно натыкался на них в гугле, в них авторам предложено пройти точно также по другим темам, в третьих темах тоже самое и так до бесконечности.
Решение задачи: «Кликнуть на кнопку в стороннем приложении»
textual
Листинг программы
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowTextLength(IntPtr hWnd);
List<IntPtr> ListHandles = new List<IntPtr>();
private void button1_Click(object sender, EventArgs e)
{
EnumWindows((hWnd, lParam) =>
{
if ((IsWindowVisible(hWnd) && GetWindowTextLength(hWnd) != 0) && GetWindowText(hWnd).StartsWith("Calculator"))
{
ListHandles.Add(hWnd);
listBox1.Items.Add(hWnd.ToString());
}
return true;
}, IntPtr.Zero);
}
string GetWindowText(IntPtr hWnd)
{
int len = GetWindowTextLength(hWnd) + 1;
StringBuilder sb = new StringBuilder(len);
len = GetWindowText(hWnd, sb, len);
return sb.ToString(0, len);
}
}