在不同位置保存文件时出现的hwnd窗口句柄问题

问题描述

Windows窗体/窗口句柄的新功能

尝试将文件保存在不同的位置。

SaveFileDialog图像:

enter image description here

我可以在“保存文件”对话框中获取文件名编辑框的编辑框句柄。 可以粘贴路径。

enter image description here

private const int WM_SETTEXT = 0x000C;

IntPtr edithWnd = IntPtr.Zero;
edithWnd = findwindowex(edithWnd,IntPtr.Zero,"Edit",null);
SendMessage(edithWnd,WM_SETTEXT,"D:\mine\Folder1\file");

以上代码文件名文本框中设置文件路径。

现在,通过获取其句柄并发送点击来单击“保存”按钮。

private const int BM_CLICK = 0x00F5;

IntPtr handle = GetForegroundWindow(); // Save As dialog
IntPtr edithWnd = findwindowex(handle,"Button","&Save");           
SendMessage(edithWnd,BM_CLICK,null);

它工作正常,但是在循环中使用此代码将多个文件保存在不同位置时,它无法正常工作,它仅将所有文件保存在一个位置中

例如,文件保存在“ D:\ mine \ Folder1 \ file”

文件1不会保存在“ D:\ mine \ 文件夹2 \ file1”中,而是会保存在“ D:\ mine \ 文件夹1 \ file1”中

file2未保存在“ D:\ mine \ 文件夹3 \ file2”中,而是保存在“ D:\ mine \ 文件夹1 \ file2”中

无论路径是什么,它似乎都仅指向第一个位置。

解决方法

为什么不先进入所需路径然后保存文件。

为我工作。

SendMessage(edithWnd,WM_SETTEXT,IntPtr.Zero,"D:\Mine\Folder1\");
// Hit enter
SendKeys.SendWait("{ENTER}");
// Now click the save button,file name will be there already,in case if it is 
// not paste the file name first and then perform save button click

您可能需要先对文件路径进行聚焦,然后再按“ Enter”将设置焦点,而按“ Enter”将使您进入文件夹路径。