/*整個Windows編程的基礎。一個句柄是指使用的一個唯一的整數值,即一個4字節(64位程序中為8字節)長的數值,來標識應用程序中的不同對象和同類中的不同的實例,諸如,一個窗口,按鈕,圖標,滾動條,輸出設備,控件或者文件等。應用程序能夠通過句柄訪問相應的對象的信息,但是句柄不是指針,程序不能利用句柄來直接閱讀文件中的信息。如果句柄不在I/O文件中,它是毫無用處的。 句柄是Windows用來標志應用程序中建立的或是使用的唯一整數,Windows大量使用了句柄來標識對象。*/
[DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lpClassName,string lpWindowName); [DllImport("user32.dll", EntryPoint = "FindWindowEx",SetLastError = true)] 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); const int WM_GETTEXT = 0x000D; const int WM_SETTEXT = 0x000C; const int WM_CLICK = 0x00F5; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int retval = 0; //增加一個返回值用來判斷操作是否成功 //string lpszParentClass = "#32770"; //整個窗口的類名 string lpszParentWindow = "Form1"; //窗口標題 string lpszClass = "WindowsForms10.EDIT.app.0.b7ab7b"; //需要查找的子窗口的類名,也就是輸入框 //string lpszClass = "Edit"; string lpszClass_Submit = "WindowsForms10.BUTTON.app.0.b7ab7b"; //需要查找的Button的類名 //string lpszClass_Submit = "Button"; string lpszName_Submit = "確定"; //需要查找的Button的標題 string text = ""; IntPtr ParenthWnd = new IntPtr(0); IntPtr EdithWnd = new IntPtr(0); //查到窗體,得到整個窗體 ParenthWnd = FindWindow(null, lpszParentWindow); //判斷這個窗體是否有效 if (!ParenthWnd.Equals(IntPtr.Zero)) { //得到Form1這個子窗體的文本框,並設置其內容 EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, ""); [color=#FF0000]這里獲取到的EdithWnd始終為0;[/color] if (!EdithWnd.Equals(IntPtr.Zero)) { text = "test1"; //調用SendMessage方法設置其內容 SendMessage(EdithWnd, WM_SETTEXT, IntPtr.Zero, text); retval++; } //得到Button這個子窗體,並觸發它的Click事件 EdithWnd = FindWindowEx(ParenthWnd, (IntPtr)0, lpszClass_Submit, lpszName_Submit); if (!EdithWnd.Equals(IntPtr.Zero)) { SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0"); retval++; } } }
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx",SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, stringlpszClass, string lpszWindow);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd,int Msg, IntPtr wParam, string lParam);
const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int retval = 0; //增加一個返回值用來判斷操作是否成功
//string lpszParentClass = "#32770"; //整個窗口的類名
string lpszParentWindow = "Form1"; //窗口標題
string lpszClass = "WindowsForms10.EDIT.app.0.b7ab7b"; //需要查找的子窗口的類名,也就是輸入框
//string lpszClass = "Edit";
string lpszClass_Submit = "WindowsForms10.BUTTON.app.0.b7ab7b"; //需要查找的Button的類名
//string lpszClass_Submit = "Button";
string lpszName_Submit = "確定"; //需要查找的Button的標題
string text = "";
IntPtr ParenthWnd = new IntPtr(0);
IntPtr EdithWnd = new IntPtr(0);
//查到窗體,得到整個窗體
ParenthWnd = FindWindow(null, lpszParentWindow);
//判斷這個窗體是否有效
if (!ParenthWnd.Equals(IntPtr.Zero))
{
//得到Form1這個子窗體的文本框,並設置其內容
EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, ""); [color=#FF0000]這里獲取到的EdithWnd始終為0;[/color]
if (!EdithWnd.Equals(IntPtr.Zero))
{
text = "test1";
//調用SendMessage方法設置其內容
SendMessage(EdithWnd, WM_SETTEXT, IntPtr.Zero, text);
retval++;
}
//得到Button這個子窗體,並觸發它的Click事件
EdithWnd = FindWindowEx(ParenthWnd,
(IntPtr)0, lpszClass_Submit, lpszName_Submit);
if (!EdithWnd.Equals(IntPtr.Zero))
{
SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");
retval++;
}
}
}