[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
private static extern void SetForegroundWindow(IntPtr hwnd);
[DllImport("user32.dll")]
public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);
/// <summary>
/// 查找表單上控制元件句柄
/// </summary>
/// <param name="hwnd">父表單句柄</param>
/// <param name="lpszWindow">控制元件標題(Text)</param>
/// <param name="bChild">設定是否在子表單中查找</param>
/// <returns>控制元件句柄,沒找到回傳IntPtr.Zero</returns>
public static IntPtr FindWindowExMy(IntPtr hwnd, string lpszWindow, bool bChild)
{
IntPtr iResult = IntPtr.Zero;
// 首先在父表單上查找控制元件
iResult = FindWindowEx(hwnd, 0, lpszClass, "");
// 如果找到直接回傳控制元件句柄
if (iResult != IntPtr.Zero)
return iResult;
// 如果設定了不在子表單中查找
if (!bChild)
return iResult;
// 列舉子表單,查找控制元件句柄
int i = EnumChildWindows(hwnd,(h, l) =>
{
IntPtr f1 = FindWindowEx(h, 0, lpszClass, lpszWindow);
if (f1 == IntPtr.Zero)
return true;
else
{
StringBuilder title = new StringBuilder(200);
int len;
len = GetWindowText(hwnd, title, 200);
iResult = f1;
return false;
}
},
0);
// 回傳查找結果
return iResult;
}
/// <summary>
/// 設定文本框內容
/// </summary>
/// <param name="taskName">行程名</param>
/// <param name="Text">內容</param>
public static void SetText(string taskName,string Text)
{
IntPtr hwndPhoto = FindWindow(null, taskName); //查找拍照程式的句柄【任務管理器中的應用程式名稱】
if (hwndPhoto != IntPtr.Zero)
{
bool isCur = false;
IntPtr hwndText = new IntPtr();
hwndText = FindWindowExMy(hwndPhoto, "", true);
SetForegroundWindow(hwndPhoto); //將UcDemo程式設為當前活動視窗
if (hwndText != IntPtr.Zero)
{
SendMessage(hwndText, WM_SETTEXT, IntPtr.Zero, "123");
}
Rectangle rs = new Rectangle();
GetClientRect(hwndText, ref rs);
}
}
以上代碼的作用是向第三方界面文本框輸入想要的值,我的第三方界面上有兩個文本框,第一次用“”空字串帶入方法可以正常獲取句柄,并向改控制元件輸入文本,但是第二次使用方法是用“”字串帶入方法或得的還是第一次得到的句柄,此時這個句柄所在的文本框已經有值,我的設想是第二次可以得到另一個空著的文本框的句柄,但是怎么做都沒辦法得到
uj5u.com熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/114935.html
標籤:C#
上一篇:js時間戳轉換
下一篇:c# 編程
