获取句柄
using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
// 获取窗口标题
[DllImport( " user32 " , SetLastError = true )]
public static extern int GetWindowText(
IntPtr hWnd, // 窗口句柄
StringBuilder lpString, // 标题
int nMaxCount // 最大值
);
// 获取类的名字
[DllImport( " user32.dll " )]
private static extern int GetClassName(
IntPtr hWnd, // 句柄
StringBuilder lpString, // 类名
int nMaxCount // 最大值
);
// 根据坐标获取窗口句柄
[DllImport( " user32 " )]
private static extern IntPtr WindowFromPoint(
Point Point // 坐标
);
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e)
{
}
private void timer1_Tick( object sender, EventArgs e)
{
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
Point p = new Point(x, y);
IntPtr formHandle = WindowFromPoint(p); // 得到窗口句柄
StringBuilder title = new StringBuilder( 256 );
GetWindowText(formHandle, title, title.Capacity); // 得到窗口的标题
StringBuilder className = new StringBuilder( 256 );
GetClassName(formHandle, className, className.Capacity); // 得到窗口的句柄
this .textBox1.Text = title.ToString(); // 窗口标题
this .textBox2.Text = formHandle.ToString(); //
this .textBox3.Text = className.ToString();
}
private void button1_Click( object sender, EventArgs e)
{
timer1.Enabled = true ;
}
private void button2_Click( object sender, EventArgs e)
{
timer1.Enabled = false ;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
// 获取窗口标题
[DllImport( " user32 " , SetLastError = true )]
public static extern int GetWindowText(
IntPtr hWnd, // 窗口句柄
StringBuilder lpString, // 标题
int nMaxCount // 最大值
);
// 获取类的名字
[DllImport( " user32.dll " )]
private static extern int GetClassName(
IntPtr hWnd, // 句柄
StringBuilder lpString, // 类名
int nMaxCount // 最大值
);
// 根据坐标获取窗口句柄
[DllImport( " user32 " )]
private static extern IntPtr WindowFromPoint(
Point Point // 坐标
);
public Form1()
{
InitializeComponent();
}
private void Form1_Load( object sender, EventArgs e)
{
}
private void timer1_Tick( object sender, EventArgs e)
{
int x = Cursor.Position.X;
int y = Cursor.Position.Y;
Point p = new Point(x, y);
IntPtr formHandle = WindowFromPoint(p); // 得到窗口句柄
StringBuilder title = new StringBuilder( 256 );
GetWindowText(formHandle, title, title.Capacity); // 得到窗口的标题
StringBuilder className = new StringBuilder( 256 );
GetClassName(formHandle, className, className.Capacity); // 得到窗口的句柄
this .textBox1.Text = title.ToString(); // 窗口标题
this .textBox2.Text = formHandle.ToString(); //
this .textBox3.Text = className.ToString();
}
private void button1_Click( object sender, EventArgs e)
{
timer1.Enabled = true ;
}
private void button2_Click( object sender, EventArgs e)
{
timer1.Enabled = false ;
}
}
}
调用如下:
例如:
QQ登陆框句柄传输QQ号码代码如下:
首先是HotKeys类定义
using
System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class HotKeys
{
int keyid = 10 ;
Dictionary < int , HotKeyCallBackHanlder > keymap = new Dictionary < int , HotKeyCallBackHanlder > ();
public delegate void HotKeyCallBackHanlder();
public enum HotkeyModifiers
{
Alt = 1 ,
Control = 2 ,
Shift = 4 ,
Win = 8
}
[DllImport( " user32.dll " )]
static extern bool RegisterHotKey(IntPtr hWnd, int id, int modifiers, Keys vk);
[DllImport( " user32.dll " )]
static extern bool UnregisterHotKey(IntPtr hWnd, int id);
/// <summary>
/// 注册快捷键
/// </summary>
/// <param name="hWnd"> 持有快捷键窗口的句柄 </param>
/// <param name="fsModifiers"> 组合键 </param>
/// <param name="vk"> 快捷键的虚拟键码 </param>
/// <param name="callBack"> 回调函数(按下快捷键时被调用的方法) </param>
public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack)
{
int id = keyid ++ ;
if ( ! RegisterHotKey(hWnd, id, modifiers, vk))
throw new Exception( " 注册失败! " );
keymap[id] = callBack;
}
/// <summary>
/// 注销快捷键
/// </summary>
/// <param name="hWnd"> 持有快捷键窗口的句柄 </param>
/// <param name="callBack"> 回调函数 </param>
public void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack)
{
foreach (KeyValuePair < int , HotKeyCallBackHanlder > var in keymap)
{
if (var.Value == callBack)
UnregisterHotKey(hWnd, var.Key);
}
}
/// <summary>
/// 快捷键消息处理
/// </summary>
public void ProcessHotKey(Message m)
{
if (m.Msg == 0x312 )
{
int id = m.WParam.ToInt32();
HotKeyCallBackHanlder callback;
if (keymap.TryGetValue(id, out callback))
callback();
}
}
}
using System.Collections.Generic;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class HotKeys
{
int keyid = 10 ;
Dictionary < int , HotKeyCallBackHanlder > keymap = new Dictionary < int , HotKeyCallBackHanlder > ();
public delegate void HotKeyCallBackHanlder();
public enum HotkeyModifiers
{
Alt = 1 ,
Control = 2 ,
Shift = 4 ,
Win = 8
}
[DllImport( " user32.dll " )]
static extern bool RegisterHotKey(IntPtr hWnd, int id, int modifiers, Keys vk);
[DllImport( " user32.dll " )]
static extern bool UnregisterHotKey(IntPtr hWnd, int id);
/// <summary>
/// 注册快捷键
/// </summary>
/// <param name="hWnd"> 持有快捷键窗口的句柄 </param>
/// <param name="fsModifiers"> 组合键 </param>
/// <param name="vk"> 快捷键的虚拟键码 </param>
/// <param name="callBack"> 回调函数(按下快捷键时被调用的方法) </param>
public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack)
{
int id = keyid ++ ;
if ( ! RegisterHotKey(hWnd, id, modifiers, vk))
throw new Exception( " 注册失败! " );
keymap[id] = callBack;
}
/// <summary>
/// 注销快捷键
/// </summary>
/// <param name="hWnd"> 持有快捷键窗口的句柄 </param>
/// <param name="callBack"> 回调函数 </param>
public void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack)
{
foreach (KeyValuePair < int , HotKeyCallBackHanlder > var in keymap)
{
if (var.Value == callBack)
UnregisterHotKey(hWnd, var.Key);
}
}
/// <summary>
/// 快捷键消息处理
/// </summary>
public void ProcessHotKey(Message m)
{
if (m.Msg == 0x312 )
{
int id = m.WParam.ToInt32();
HotKeyCallBackHanlder callback;
if (keymap.TryGetValue(id, out callback))
callback();
}
}
}
发送代码如下:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
// 第一个与第三个是用于查找窗口句柄的
[DllImport( " user32.dll " )]
public static extern IntPtr FindWindow( string lpClassName, string lpWindowName);
[DllImport( " User32.dll " , EntryPoint = " SendMessage " )]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
// [DllImport("user32.dll", EntryPoint = "FindWindow")]
// private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport( " User32.dll " )]
public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);
[DllImport( " USER32.DLL " )]
public static extern bool SetForegroundWindow(IntPtr hWnd);
bool a = true ;
public Form1()
{
InitializeComponent();
}
HotKeys h = new HotKeys();
// 重载WndProc函数
protected override void WndProc( ref Message m)
{
h.ProcessHotKey(m); // 快捷键消息处理
base .WndProc( ref m);
}
private void Form1_Load( object sender, EventArgs e)
{
serialPort1.PortName = " COM5 " ;
serialPort1.BaudRate = 9600 ;
serialPort1.Open();
h.Regist( this .Handle, ( int )HotKeys.HotkeyModifiers.Control, Keys.E, CallBack);
}
private void button2_Click( object sender, EventArgs e)
{
// 串口发送
// serialPort1.WriteLine(textBox1.Text);
// 模拟键盘,句柄发送
Test();
// MessageBox.Show("数据发送成功!", "系统提示");
}
// 跨越程序输入
public void Test()
{
const int WM_SETTEXT = 0x000C ;
// IntPtr hwnd = FindWindow(null, "无标题 - 记事本");
IntPtr hwnd = FindWindow( null , " QQ2010 " );
// IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null);
IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, " ATL:30A441A8 " , null );
// IntPtr htextbox2 = FindWindowEx(hwnd, htextbox, "EDIT", null); // 填上次获得的句柄,可以得到下一个的句柄。
SendMessage(htextbox, WM_SETTEXT, IntPtr.Zero, this .textBox1.Text);
}
// 按下快捷键时被调用的方法
public void CallBack()
{
Test();
}
private void DoUpdate( object s, EventArgs e)
{
textBox1.Text = serialPort1.ReadExisting();
MessageBox.Show(serialPort1.PortName);
}
private void serialPort1_DataReceived( object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
this .Invoke( new EventHandler(DoUpdate));
}
}
}
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
// 第一个与第三个是用于查找窗口句柄的
[DllImport( " user32.dll " )]
public static extern IntPtr FindWindow( string lpClassName, string lpWindowName);
[DllImport( " User32.dll " , EntryPoint = " SendMessage " )]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
// [DllImport("user32.dll", EntryPoint = "FindWindow")]
// private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport( " User32.dll " )]
public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);
[DllImport( " USER32.DLL " )]
public static extern bool SetForegroundWindow(IntPtr hWnd);
bool a = true ;
public Form1()
{
InitializeComponent();
}
HotKeys h = new HotKeys();
// 重载WndProc函数
protected override void WndProc( ref Message m)
{
h.ProcessHotKey(m); // 快捷键消息处理
base .WndProc( ref m);
}
private void Form1_Load( object sender, EventArgs e)
{
serialPort1.PortName = " COM5 " ;
serialPort1.BaudRate = 9600 ;
serialPort1.Open();
h.Regist( this .Handle, ( int )HotKeys.HotkeyModifiers.Control, Keys.E, CallBack);
}
private void button2_Click( object sender, EventArgs e)
{
// 串口发送
// serialPort1.WriteLine(textBox1.Text);
// 模拟键盘,句柄发送
Test();
// MessageBox.Show("数据发送成功!", "系统提示");
}
// 跨越程序输入
public void Test()
{
const int WM_SETTEXT = 0x000C ;
// IntPtr hwnd = FindWindow(null, "无标题 - 记事本");
IntPtr hwnd = FindWindow( null , " QQ2010 " );
// IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null);
IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, " ATL:30A441A8 " , null );
// IntPtr htextbox2 = FindWindowEx(hwnd, htextbox, "EDIT", null); // 填上次获得的句柄,可以得到下一个的句柄。
SendMessage(htextbox, WM_SETTEXT, IntPtr.Zero, this .textBox1.Text);
}
// 按下快捷键时被调用的方法
public void CallBack()
{
Test();
}
private void DoUpdate( object s, EventArgs e)
{
textBox1.Text = serialPort1.ReadExisting();
MessageBox.Show(serialPort1.PortName);
}
private void serialPort1_DataReceived( object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
this .Invoke( new EventHandler(DoUpdate));
}
}
}
模拟邮件登录
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;
using System.Diagnostics;
using System.Threading;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport( " user32.dll " )]
static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport( " user32.dll " )]
[ return : MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e)
{
Process proc = Process.Start( " IExplore.exe " , " http://mail.szjm.edu.cn/ " );
SetActiveWindow(proc.MainWindowHandle); // 激活窗口
SetForegroundWindow(proc.MainWindowHandle); // 将窗口放置到前端显示
Thread.Sleep( 1000 ); // 等待1000毫秒(1秒),用于等待网页完全打开
SendKeys.SendWait( " 用户名@szjm.edu.cn " ); // 自动录入邮箱账号
SendKeys.SendWait( " {tab} " ); // 自动录入TAB键,用于录入账号后,切换至密码框
SendKeys.SendWait( " 密码 " ); // 自动录入密码
SendKeys.SendWait( " {ENTER} " ); // 输入回车键,提交数据登录
}
}
}
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;
using System.Diagnostics;
using System.Threading;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport( " user32.dll " )]
static extern IntPtr SetActiveWindow(IntPtr hWnd);
[DllImport( " user32.dll " )]
[ return : MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void button1_Click( object sender, EventArgs e)
{
Process proc = Process.Start( " IExplore.exe " , " http://mail.szjm.edu.cn/ " );
SetActiveWindow(proc.MainWindowHandle); // 激活窗口
SetForegroundWindow(proc.MainWindowHandle); // 将窗口放置到前端显示
Thread.Sleep( 1000 ); // 等待1000毫秒(1秒),用于等待网页完全打开
SendKeys.SendWait( " 用户名@szjm.edu.cn " ); // 自动录入邮箱账号
SendKeys.SendWait( " {tab} " ); // 自动录入TAB键,用于录入账号后,切换至密码框
SendKeys.SendWait( " 密码 " ); // 自动录入密码
SendKeys.SendWait( " {ENTER} " ); // 输入回车键,提交数据登录
}
}
}
如下代码,描述,有一程序,窗体名称是test_title,窗体里面有2个textbox控件,标题分别是aa和bb,向他们发送2个变量,然后模拟键盘,点击确认按钮,打开这个程序的主窗体。
const
int
WM_SETTEXT
=
0x000C
;
// IntPtr hwnd = FindWindow(null, "无标题 - 记事本");
IntPtr hwnd = FindWindow( null , " test_title " );
SetForegroundWindow(hwnd);
// IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null);0O
// IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, "WindowsForms10.EDIT.app.0.378734a", null);
IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, null , " aa " );
// IntPtr htextbox = new IntPtr(73396);
// IntPtr htextbox2 = FindWindowEx(hwnd, htextbox, "Edit", null); // 填上次获得的句柄,可以得到下一个的句柄。
SendMessage(htextbox, WM_SETTEXT, IntPtr.Zero, textBox1.Text);
IntPtr htextbox1 = FindWindowEx(hwnd, IntPtr.Zero, null , " bb " );
SendMessage(htextbox1, WM_SETTEXT, IntPtr.Zero, " 19791225 " );
// SendKeys.SendWait("{tab}"); // 自动录入TAB键,用于录入账号后,切换至密码框
SendKeys.SendWait( " {ENTER} " ); // 输入回车键,提交数据登录
// IntPtr htextbox1= new IntPtr(73398);
// SendMessage(htextbox1, WM_SETTEXT, IntPtr.Zero,"19791225");
// IntPtr hwnd = FindWindow(null, "无标题 - 记事本");
IntPtr hwnd = FindWindow( null , " test_title " );
SetForegroundWindow(hwnd);
// IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null);0O
// IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, "WindowsForms10.EDIT.app.0.378734a", null);
IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, null , " aa " );
// IntPtr htextbox = new IntPtr(73396);
// IntPtr htextbox2 = FindWindowEx(hwnd, htextbox, "Edit", null); // 填上次获得的句柄,可以得到下一个的句柄。
SendMessage(htextbox, WM_SETTEXT, IntPtr.Zero, textBox1.Text);
IntPtr htextbox1 = FindWindowEx(hwnd, IntPtr.Zero, null , " bb " );
SendMessage(htextbox1, WM_SETTEXT, IntPtr.Zero, " 19791225 " );
// SendKeys.SendWait("{tab}"); // 自动录入TAB键,用于录入账号后,切换至密码框
SendKeys.SendWait( " {ENTER} " ); // 输入回车键,提交数据登录
// IntPtr htextbox1= new IntPtr(73398);
// SendMessage(htextbox1, WM_SETTEXT, IntPtr.Zero,"19791225");