- 爱易网页
-
C#教程
- c# api 模拟按钮点击有关问题,
日期:2014-05-18 浏览次数:21000 次
c# api 模拟按钮点击问题,高手请进!
[DllImport("user32", EntryPoint = "SetParent", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern int SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32", EntryPoint = "FindWindowA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SendMessageA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd,int Msg, IntPtr wParam, string lParam);
private const int WM_GETTEXT = 0x000D;
private const int WM_SETTEXT = 0x000C;
private const int WM_CLICK = 0x00F5;
[DllImport("shell32.dll", EntryPoint = "ShellExecuteA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int ShellExecute(int hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirectory, int nShowCmd);
private const int WM_SYSCOMMAND = 0x112;
private const int SC_MAXIMIZE = 0xF030;
private const int SC_MINIMIZE = 0xF020;
private const int SC_RESTORE = 0xF120;
public const int SW_HIDE = 0;
public const int SW_SHOW = 5;
[DllImport("user32.dll ", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int ShowWindow(int hwnd, int nCmdShow);
private void timer1_Tick(object sender, EventArgs e)
{
IntPtr h = FindWindow("#32770", "东北证券通达信V6.03");
SetParent(h, this.panel1.Handle); //嵌套到panel1内
SendMessage(h.ToInt32(), WM_SYSCOMMAND, SC_MAXIMIZE, 0);
ShowWindow(h.ToInt32(), SW_SHOW);
timer1.Enabled = false;
//得到控件
IntPtr MIMA = new IntPtr(0);
IntPtr YZMA = new IntPtr(0);
IntPtr DL = new IntPtr(0);
if (!h.Equals(IntPtr.Zero))
{
//得到控件子窗体,并设置其内容
MIMA = FindWindowEx(h, MIMA, "SafeEdit", null);
if (!MIMA.Equals(IntPtr.Zero))
{
//调用SendMessage方法设置其内容
SendMessage(MIMA, WM_SETTEXT, h, "123456");
}
YZMA = FindWindowEx(h, MIMA, "SafeEdit", null);
if (!YZMA.Equals(IntPtr.Zero))
{
//调用SendMessage方法设置其内容
SendMessage(YZMA, WM_SETTEXT, h, "123456");
}
//按钮点击
DL = FindWindowEx(h, DL, "AfxWnd42", null);
if (!DL.Equals(IntPtr.Zero))
{
SendMessage(DL, WM_CLICK, h, "0");