使用windows窗体时,以编程方式单击一个"保存文件";按钮,打开保存文件对话框。
如何访问此保存文件对话框?例如,放置位置、文件名等
是否有任何方法可以获得已经打开的保存文件对话框,就像我们对表单应用程序所做的那样。OpenForms
我不认为您可以将SaveFileDialog作为控件,但有一种方法可以访问;另存为";窗口,并访问您想要的信息。
使用hwnd-窗口句柄。窗口的OR句柄。
以下是获取窗口的方法,如果保存文件对话框在前面,则可以使用:
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
public static string GetActiveWindowTitle(IntPtr handle)
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
public static void YourMethod()
{
IntPtr handle = GetForegroundWindow();
var name = GetActiveWindowTitle(handle);
if (handle != IntPtr.Zero && name == "Save As")
{
// Do your work here
}
}
此外,您可以进入子窗口,访问FileName、Save按钮等信息…
例如,如果您想访问保存按钮,请在现有中添加以下内容
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr handleParent, IntPtr handleChild, string className, string WindowName);
保存按钮的类名将是">按钮";
您可以使用以下方法获得此按钮的句柄:
IntPtr edithWnd = FindWindowEx(handle, IntPtr.Zero, "Button", "&Save");
其中,handle-直接父句柄
"按钮"-类名
"amp;保存"-按钮窗口的标题。。。
希望这会有所帮助!!
问题的答案很简单!CCD_ 1得到了CCD_。
官方文档:SaveFileDialog类(System.Windows.Form(