C# FindWindowEx函数,可以获取得到文件另存为窗口的保存,取消按钮的句柄。但是获取不了
C#FindWindowEx函数,可以获取得到文件另存为窗口的保存,取消按钮的句柄。但是获取不了输入文件名的文本框的句柄,怎么回事?...
C# FindWindowEx函数,可以获取得到文件另存为窗口的保存,取消按钮的句柄。但是获取不了输入文件名的文本框的句柄,怎么回事?
展开
展开全部
可以这样写:
var hWnd = IntPtr.Zero;
var hChild = IntPtr.Zero;
// Find Save File Dialog Box
while (hWnd == IntPtr.Zero)
{
Thread.Sleep(500);
hWnd = FindWindow("#32770", "文件另存为");
}
if (hWnd == IntPtr.Zero) return;
// Enter fileName
EnumChildWindows(hWnd, (handle, s) =>
{
//####取标题
//StringBuilder title = new StringBuilder(100);
//GetWindowText(handle, title, 100);//取标题
//if (title.ToString() == s)
//{
// hChild = handle;
// return false;
//}
//return true;
//####取类型
StringBuilder type = new StringBuilder(100);
GetClassName(handle, type, 100);//取类型
if (type.ToString() == s)
{
hChild = handle;
return false;
}
return true;
}, "Edit");
SendMessage(hChild, WM_SETTEXT, null, "c:\\文件名.xps");
// Press Save button
hChild = FindWindowEx(hWnd, IntPtr.Zero, "Button", "保存(&S)");
PostMessage(hChild, WM_IME_KEYDOWN, (int)Keys.S, 0);
PostMessage(hChild, WM_IME_KEYUP, (int)Keys.S, 0);
这些是上面用的函数:
[DllImport("User32.dll")]
public static extern int GetWindowText(IntPtr WinHandle, StringBuilder Title, int size);
[DllImport("user32.dll")]
public static extern int GetClassName(IntPtr WinHandle, StringBuilder Type, int size);
public delegate bool EnumChildWindow(IntPtr WindowHandle, string num);
[DllImport("User32.dll")]
public static extern int EnumChildWindows(IntPtr WinHandle, EnumChildWindow ecw, string name);
[DllImport("User32.dll", EntryPoint = "PostMessage")]
private static extern int PostMessage(IntPtr hWnd, int Msg, string wParam, string lParam);
[DllImport("User32.dll", EntryPoint = "SendMessage", SetLastError = true)]
private static extern int SendMessage(IntPtr hWnd, int WM_CHAR, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "SendMessage", SetLastError = true)]
private static extern int SendMessage(IntPtr hWnd, int WM_CHAR, string wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private extern static 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 = "PostMessage")]
private static extern int PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
这些是用到的常量:
private const int WM_KEYDOWN = 0x0100;
private const int WM_IME_KEYDOWN = 0x0290;
private const int WM_IME_KEYUP = 0x0291;
private const int WM_SETTEXT = 0x000C;
我这里测试已经通过了,你可以试试。
更多追问追答
追问
谢谢,我也测试通过了。我顺便测试不一下注释掉的GetWindowText()为什么获取不到另存为窗口的默认文件名呢?
追答
GetWindowText() 函数不能返回在其他应用程序中的编辑控件的文本。就是说文本编辑器的文本不能获取。其他的 窗口标题、label 、groupbox等控件的文本是可以获取的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询