首先新建WinAPIuser32類(API聲明類)
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace WinAPITest
{
public class WinAPIuser32
{
/// <summary>
/// 根據(jù)句柄得到控件ID
/// </summary>
/// <param
name="hwndCtl"></param>
/// <returns></returns>
[DllImport("user32.dll")]
public static extern
int GetDlgCtrlID(IntPtr hwndCtl);
/// <summary>
/// 由控件ID獲得控件窗口句柄
/// </summary>
/// <param
name="hDlg"></param>
/// <param
name="nIDDlgItem"></param>
/// <returns></returns>
[DllImport("user32.dll ", EntryPoint = "GetDlgItem")]
public static extern
IntPtr GetDlgItem(
IntPtr hDlg,
int nIDDlgItem
);
/// <summary>
///
查找句柄
/// </summary>
/// <param
name="lpClassName"></param>
/// <param
name="lpWindowName"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern
IntPtr FindWindow(string lpClassName, string lpWindowName);
/// <summary>
/// 查找句柄
/// </summary>
/// <param
name="hwndParent"></param>
/// <param
name="hwndChildAfter"></param>
/// <param
name="lpszClass">類名</param>
/// <param
name="lpszWindow">標(biāo)題</param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
public static extern
IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
public static extern
int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
/// <summary>
/// 發(fā)送消息
/// </summary>
/// <param
name="hwnd"></param>
/// <param
name="wMsg"></param>
/// <param
name="wParam"></param>
/// <param
name="lParam"></param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
public static extern
int SendMessage(IntPtr hwnd, uint
wMsg, int wParam, int lParam);
[DllImport("User32.dll")]
public static extern
int SendMessage(int hWnd,
int Msg, int
wParam, int lParam);
/// <summary>
/// 發(fā)送文本消息
/// </summary>
/// <param
name="hWnd"></param>
/// <param
name="Msg"></param>
/// <param
name="wParam"></param>
/// <param
name="lParam"></param>
/// <returns></returns>
[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
public static extern
int SendTextMessage(
IntPtr hWnd, // handle
to destination window
int Msg, //
message
int wParam, // first
message parameter
string lParam
// int lParam // second
message parameter
);
/// <summary>
/// 獲取焦點(diǎn)
/// </summary>
/// <param
name="hwnd"></param>
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
public static extern
void SetForegroundWindow(IntPtr hwnd);
[DllImport("user32.dll")]
public static extern
int SetWindowText(int hwnd,
string lpString);
[DllImport("user32.dll")]
public static extern
int SetFocus(int hWnd);
[DllImport("user32.dll")]
public static extern
int EnumChildWindows(int hWndParent, CallBack lpfn, int
lParam);
/// <summary>
/// 回調(diào)業(yè)務(wù)
/// </summary>
public delegate void
CallBusiness(IntPtr hwnd);
public delegate bool
CallBack(IntPtr hwnd, int
lParam);
/// <summary>
/// 遍歷子窗體的父窗體句柄
/// </summary>
public static CallBack callBackEnumChildWindows = new CallBack(ChildWindowProcess);
/// <summary>
/// 委托業(yè)務(wù),需要客戶端添加
/// </summary>
public static CallBusiness CallFuntion;
/// <summary>
/// 遍歷子窗體或控件
/// </summary>
/// <param
name="hWnd"></param>
/// <param
name="lParam"></param>
/// <returns></returns>
public static bool
EnumChildWindows(IntPtr hWnd, int
lParam)
{
EnumChildWindows(hWnd.ToInt32(), callBackEnumChildWindows, 0);
return true;
}
/// <summary>
/// 獲取類名字
/// </summary>
/// <param
name="hwnd">需要獲取類名的句柄</param>
/// <param
name="lpClassName">類名(執(zhí)行完成以后查看)</param>
/// <param
name="nMaxCount">緩沖區(qū)</param>
/// <returns></returns>
[DllImport("user32.dll", EntryPoint = "GetClassName")]
public static extern
int GetClassName(
IntPtr hwnd,
StringBuilder lpClassName,
int nMaxCount
);
/// <summary>
/// 遍歷子控件
/// </summary>
/// <param
name="hwnd"></param>
/// <param
name="lParam"></param>
/// <returns></returns>
public static bool
ChildWindowProcess(IntPtr hwnd, int
lParam)
{
if (CallFuntion != null)
{
CallFuntion(hwnd);
}
return true;
}
[DllImport("user32.dll", EntryPoint = "PostMessage")]
public static extern
int PostMessage(IntPtr hwnd, int
wMsg, int wParam, int lParam);
/// <summary>
/// 最大化窗口(3),最小化窗口(2),正常大小窗口(1)第二個(gè)參數(shù);
/// </summary>
[DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
public static extern
int ShowWindow(IntPtr hwnd, int
nCmdShow);
/// <summary>
/// 移動(dòng)窗體
/// </summary>
[DllImport("user32.dll", EntryPoint = "MoveWindow")]
public static extern
int MoveWindow(IntPtr hwnd, int
x, int y,
int nWidth, int nHeight, int bRepaint);
/// <summary>
/// 鼠標(biāo)移動(dòng)
/// </summary>
[DllImport("user32.dll", EntryPoint = "SetCursorPos")]
public static extern
int SetCursorPos(int x,
int y);
//[DllImport("user32.dll", EntryPoint =
"SendMessage")]
//public static extern int
SendMessage(IntPtr hwnd, int wMsg, uint wParam, uint
lParam);
//[DllImport("user32.dll", EntryPoint =
"SendMessage", SetLastError = true, CharSet =
CharSet.Auto)]
//private static extern int
SendMessage(IntPtr hwnd, uint wMsg, int wParam, int
lParam);
}
}
再建一個(gè)API消息編碼類
using System;
using System.Collections.Generic;
using System.Text;
namespace WinAPITest
{
/// <summary>
/// 消息編碼
/// </summary>
public class MSCODE
{
/// <summary>
/// 鼠標(biāo)左鍵按下
/// </summary>
public const int
WM_LBUTTONDOWN =
0x201;
/// <summary>
/// 鼠標(biāo)左鍵抬起
/// </summary>
public const int
WM_LBUTTONUP =
0x202;
/// <summary>
/// 設(shè)置文本
/// </summary>
public const int
WM_SETTEXT = 0xC;
public const int
VK_TAB = 0x9;
public const int
WM_KEYDOWN = 0x100;
public const int
WM_KEYUP = 0x101;
public const int
WM_CHAR = 0x102;
public const int
WM_SETFOCUS = 0x007;
/// <summary>
/// 回車鍵
/// </summary>
public const int
VK_RETURN = 13;
/// <summary>
/// 回車鍵
/// </summary>
public const int
VK_RETURN1 = 0x0D;
/// <summary>
///字母K
/// </summary>
public const int
VK_K = 0x4B;
/// <summary>
///F5
/// </summary>
public const int
VK_F5 = 0x74;
/// <summary>
///單擊事件
/// </summary>
public const int
BM_CLICK = 0xF5;
}
}
新建WINFORM和WINFORM1
WINFORM1的窗體名稱為Form1aa//隨便拖幾個(gè)控件
然后再單擊事件中寫代碼
private void button1_Click(object sender, EventArgs e)
{
Random rand = new
Random();
IntPtr hwndCalc = WinAPIuser32.FindWindow(null, "Form1aa");//獲取窗體的句柄
//WinAPIuser32.ShowWindow(hwndCalc,
1);//最小化窗體
IntPtr hwndThree = WinAPIuser32.FindWindowEx(hwndCalc, 0, null, "123");
WinAPIuser32.SendMessage(hwndThree, MSCODE.BM_CLICK, 0, 0);//單擊Form1aa中的123按鈕
//WinAPIuser32.PostMessage(hwndThree,
MSCODE.BM_CLICK, 0, 0);//
//WinAPIuser32.PostMessage(hwndThree,
MSCODE.WM_LBUTTONDOWN,0 , 0);//鼠標(biāo)按下
//WinAPIuser32.PostMessage(hwndThree,
MSCODE.WM_LBUTTONUP, 0, 0);//鼠標(biāo)臺(tái)起
組成單擊事件
//int x =
rand.Next(800);
//int y =
rand.Next(600);
//int nWidth = rand.Next(50,
500);
//int nHeight = rand.Next(50,
500);
//WinAPIuser32.MoveWindow(hwndThree, x,
y, nWidth, nHeight, 1);//移動(dòng)窗體
//WinAPIuser32.SetCursorPos(x,
y);//移動(dòng)鼠標(biāo)
if (hwndCalc != IntPtr.Zero)
{
List<IntPtr>
list = new List<IntPtr>();//存儲(chǔ)所有控件的緩沖區(qū)
WinAPIuser32.CallFuntion = delegate(IntPtr enumIntPtr)//定義委托業(yè)務(wù)
{
StringBuilder s = new
StringBuilder(2000);
WinAPIuser32.GetClassName(enumIntPtr, s, 255);
list.Add(enumIntPtr);
};
WinAPIuser32.EnumChildWindows(hwndCalc, 0);//把獲取窗體控件的句柄寫入緩沖區(qū)
WinAPIuser32.CallFuntion = null;
foreach (IntPtr a in
list)
{
WinAPIuser32.SendTextMessage(a, MSCODE.WM_SETTEXT, 0, "mextb1860第二個(gè)文本框");
}//寫入所有文本框的
//WinAPIuser32.PostMessage(list[2],
MSCODE.WM_KEYDOWN, MSCODE.VK_RETURN,
0);//發(fā)送回車鍵
//WinAPIuser32.PostMessage(list[2],
MSCODE.WM_KEYDOWN, MSCODE.VK_K, 0);//發(fā)送K建
//WinAPIuser32.PostMessage(list[3],
MSCODE.WM_KEYDOWN, MSCODE.VK_RETURN1, 0);//
//第2個(gè)文本框
//IntPtr hwndThree =
WinAPIuser32.FindWindowEx(hwndCalc, 0, null,
"確定");
// WinAPIuser32.SendMessage(hwndCalc,
MSCODE.BM_CLICK, 0, 0);
//第3個(gè)文本框
//WinAPIuser32.SendTextMessage(list[1],
MSCODE.WM_SETTEXT, 0, "mextb1860第三個(gè)文本框");//發(fā)送文本
}
}
代碼粗略~!有問題留言
|