窗口 交互 窗口的方法
比如- -我有一个窗口 a 要控制 窗口b中的按钮.
首先必须确定一个窗口的确句柄= =他是唯一的,还要确定要控制的按钮名称.这些都可以利用
SPY++ 得到非常简单
然后头痛的是- -每次编译net程序的时候他的窗口句柄都变化,经测试编译是发布版本的化就将
变化缩小到两个- -都记住就行了
Code
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.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;
[DllImport("User32.dll",EntryPoint="FindWindow")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll",EntryPoint="FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll",EntryPoint="SendMessage")]
private static extern int SendMessage(IntPtr hWnd,int Msg, IntPtr wParam, string lParam);
public enum nCmdShow : uint
{
SW_FORCEMINIMIZE = 0x0,
SW_HIDE = 0x1,
SW_MAXIMIZE = 0x2,
SW_MINIMIZE = 0x3,
SW_RESTORE = 0x4,
SW_SHOW = 0x5,
SW_SHOWDEFAULT = 0x6,
SW_SHOWMAXIMIZED = 0x7,
SW_SHOWMINIMIZED = 0x8,
SW_SHOWMINNOACTIVE = 0x9,
SW_SHOWNA = 0xA,
SW_SHOWNOACTIVATE = 0xB,
SW_SHOWNORMAL = 0xC,
WM_CLOSE = 0x10,
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool t = true;
while (t)
{
//---FindWindow-函数要找的是主要被调用窗体
IntPtr ParenthWnd = FindWindow("g", "gg");
if (!ParenthWnd.Equals(IntPtr.Zero))
{
System.Threading.Thread.Sleep(100);
IntPtr e1 = new IntPtr(0);
//--在这个被调用的窗体中在查找控件
IntPtr TPanel = FindWindowEx(ParenthWnd, e1, "窗体句柄", "");
//-在TPanel这个控件上查找textbox
IntPtr Paw = FindWindowEx(TPanel, e1, "窗体句柄", "");
//--控制给Paw发送消息
SendMessage(Paw, WM_SETTEXT, (IntPtr)0, "这是我发送的消息");
//--控制在TPanel查找textbox user
IntPtr user = FindWindowEx(TPanel, Paw, "窗体句柄", "");
//--控制给user发送消息
SendMessage(user, WM_SETTEXT, (IntPtr)0, "窗体句柄");
//--这里奉送一个搞的我疯狂的小知识
//--当一个窗体内有两个控件都是同一类形-那么窗体句柄是一样的
//--第一查找是最后一个,
//--第二次查找一第一次查找的窗体句柄为基础在查-是倒数第二个
//- -具体你可以测试因为本人用的是net2008不知道其他的如何
//--寻找按钮
IntPtr button = FindWindowEx(
ParenthWnd, e1, "窗体句柄", "");
//--触发按钮的CLICK事件
SendMessage(button, WM_CLICK, (IntPtr)0, "0");
t = false;
}
}
}
}
}
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.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;
[DllImport("User32.dll",EntryPoint="FindWindow")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll",EntryPoint="FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll",EntryPoint="SendMessage")]
private static extern int SendMessage(IntPtr hWnd,int Msg, IntPtr wParam, string lParam);
public enum nCmdShow : uint
{
SW_FORCEMINIMIZE = 0x0,
SW_HIDE = 0x1,
SW_MAXIMIZE = 0x2,
SW_MINIMIZE = 0x3,
SW_RESTORE = 0x4,
SW_SHOW = 0x5,
SW_SHOWDEFAULT = 0x6,
SW_SHOWMAXIMIZED = 0x7,
SW_SHOWMINIMIZED = 0x8,
SW_SHOWMINNOACTIVE = 0x9,
SW_SHOWNA = 0xA,
SW_SHOWNOACTIVATE = 0xB,
SW_SHOWNORMAL = 0xC,
WM_CLOSE = 0x10,
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
bool t = true;
while (t)
{
//---FindWindow-函数要找的是主要被调用窗体
IntPtr ParenthWnd = FindWindow("g", "gg");
if (!ParenthWnd.Equals(IntPtr.Zero))
{
System.Threading.Thread.Sleep(100);
IntPtr e1 = new IntPtr(0);
//--在这个被调用的窗体中在查找控件
IntPtr TPanel = FindWindowEx(ParenthWnd, e1, "窗体句柄", "");
//-在TPanel这个控件上查找textbox
IntPtr Paw = FindWindowEx(TPanel, e1, "窗体句柄", "");
//--控制给Paw发送消息
SendMessage(Paw, WM_SETTEXT, (IntPtr)0, "这是我发送的消息");
//--控制在TPanel查找textbox user
IntPtr user = FindWindowEx(TPanel, Paw, "窗体句柄", "");
//--控制给user发送消息
SendMessage(user, WM_SETTEXT, (IntPtr)0, "窗体句柄");
//--这里奉送一个搞的我疯狂的小知识
//--当一个窗体内有两个控件都是同一类形-那么窗体句柄是一样的
//--第一查找是最后一个,
//--第二次查找一第一次查找的窗体句柄为基础在查-是倒数第二个
//- -具体你可以测试因为本人用的是net2008不知道其他的如何
//--寻找按钮
IntPtr button = FindWindowEx(
ParenthWnd, e1, "窗体句柄", "");
//--触发按钮的CLICK事件
SendMessage(button, WM_CLICK, (IntPtr)0, "0");
t = false;
}
}
}
}
}
还是比较简单的哦