B
blah
Hello everyone, Ive been trying to get my application to "click" on a
button in another application using SendMessage, Ive gotten this far
but Im not sure whats wrong with this code, here is the whole
application (its small for testing purposes) and it seems that window
wraps the text, at least when I preview this post:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace test
{
public partial class Form1 : Form
{
[DllImport("user32", EntryPoint = "GetNextWindow")]
public static extern int
GetWindow(IntPtr hwnd, int wFlag);
public Form1()
{
InitializeComponent();
}
public class Win32
{
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
public const int BM_CLICK = 0xF5;
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClass, string
lpWindow);
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg,
int wParam, int lParam);
}
private void button1_Click(object sender, EventArgs e)
{
int hWnd;
//Thread.Sleep(5000);
if ((txtWindowCaption.Text == "") && (txtClassName.Text ==
""))
{
hWnd = Win32API.FindWindowAny(0, 0);
}
else if ((txtWindowCaption.Text == "") &&
(txtClassName.Text != ""))
{
hWnd =
Win32API.FindWindowNullWindowCaption(txtClassName.Text, 0);
}
else if ((txtWindowCaption.Text != "") &&
(txtClassName.Text == ""))
{
hWnd = Win32API.FindWindowNullClassName(0,
txtWindowCaption.Text);
}
else
{
hWnd = Win32API.FindWindow(txtClassName.Text,
txtWindowCaption.Text);
}
if (hWnd == 0)
{
MessageBox.Show("Specified window is not running.",
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
// Set the window foreground.
Win32API.SetForegroundWindow(hWnd);
// if window is minimized, simply restore, otherwise
show it. Notice the
// declaration of Win32API.IsIconic defines the return
value bool
// allowing .NET to marshall the integer value to a
bool.
if (Win32API.IsIconic(hWnd))
{
Win32API.ShowWindow(hWnd, Win32API.SW_RESTORE);
}
else
{
Win32API.ShowWindow(hWnd, Win32API.SW_SHOW);
}
int a = Win32.FindWindow(txtClassName2.Text,
Title.Text);
int b = Win32.SendMessage(a, Win32.BM_CLICK, 0, 0);
}
}
}
}
button in another application using SendMessage, Ive gotten this far
but Im not sure whats wrong with this code, here is the whole
application (its small for testing purposes) and it seems that window
wraps the text, at least when I preview this post:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace test
{
public partial class Form1 : Form
{
[DllImport("user32", EntryPoint = "GetNextWindow")]
public static extern int
GetWindow(IntPtr hwnd, int wFlag);
public Form1()
{
InitializeComponent();
}
public class Win32
{
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
public const int BM_CLICK = 0xF5;
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClass, string
lpWindow);
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, uint Msg,
int wParam, int lParam);
}
private void button1_Click(object sender, EventArgs e)
{
int hWnd;
//Thread.Sleep(5000);
if ((txtWindowCaption.Text == "") && (txtClassName.Text ==
""))
{
hWnd = Win32API.FindWindowAny(0, 0);
}
else if ((txtWindowCaption.Text == "") &&
(txtClassName.Text != ""))
{
hWnd =
Win32API.FindWindowNullWindowCaption(txtClassName.Text, 0);
}
else if ((txtWindowCaption.Text != "") &&
(txtClassName.Text == ""))
{
hWnd = Win32API.FindWindowNullClassName(0,
txtWindowCaption.Text);
}
else
{
hWnd = Win32API.FindWindow(txtClassName.Text,
txtWindowCaption.Text);
}
if (hWnd == 0)
{
MessageBox.Show("Specified window is not running.",
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
// Set the window foreground.
Win32API.SetForegroundWindow(hWnd);
// if window is minimized, simply restore, otherwise
show it. Notice the
// declaration of Win32API.IsIconic defines the return
value bool
// allowing .NET to marshall the integer value to a
bool.
if (Win32API.IsIconic(hWnd))
{
Win32API.ShowWindow(hWnd, Win32API.SW_RESTORE);
}
else
{
Win32API.ShowWindow(hWnd, Win32API.SW_SHOW);
}
int a = Win32.FindWindow(txtClassName2.Text,
Title.Text);
int b = Win32.SendMessage(a, Win32.BM_CLICK, 0, 0);
}
}
}
}