【问题标题】:Customizing OpenFileDialog自定义 OpenFileDialog
【发布时间】:2011-08-30 21:12:49
【问题描述】:

我正在使用 C# 开发 winforms 应用程序。我想要实现的是从我使用以下代码的用户那里获取一个文件:

OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
    string sFileName = dlg.FileName;
    //my code goes here
}

现在,一切正常,但我想在同一个对话框中放置 3 个单选按钮,这意味着我现在将从该对话框中得到两件事

string sFileName = dlg.FileName; //same as in case of traditional dialog box
//some thing like this which tells which radio button is selected:
dlg.rbTypes.Selected

我如何做到这一点?

【问题讨论】:

  • +1 表示问题和答案。至少现在我意识到我不想移动那么多奶酪。我会找到另一种不会破坏用户对 Windows 正常功能的期望的方法。

标签: c# winforms openfiledialog customizing


【解决方案1】:

是的,这是可能的,我成功地使用SaveFileDialog 进行了同样的自定义,这很有趣。

点击以下链接:

http://www.codeproject.com/KB/dialog/OpenFileDialogEx.aspx

http://www.codeproject.com/KB/cs/getsavefilename.aspx

http://www.codeproject.com/KB/dialog/CustomizeFileDialog.aspx

我自己的问题也会对你有所帮助:

Change default arrangement of Save and Cancel buttons in SaveFileDialog

How to stop overwriteprompt when creating SaveFileDialog using GetSaveFileName

为此,您必须使用WinAPI,并且您需要自己编写ShowDialog 方法,调用其中的GetOpenFileName windows 函数,而不是调用.net 的OpenFileDialogGetOpenFileName 将创建窗口 OpenFileDialog。 (请参阅http://msdn.microsoft.com/en-us/library/ms646927%28v=vs.85%29.aspx)。这与编写 HookProc 过程并在其中捕获诸如 WM_INITDIALOG, CDN_INITDONE 之类的事件一起将帮助您做您想做的事情。

要添加单选按钮等,必须调用windows函数如CreateWindowExSendMessage....

第二个链接有自定义的确切方向...

要求任何澄清...

【讨论】:

  • 可惜所有代码项目链接都死了:(
【解决方案2】:

在 XP 上,您需要使用挂钩过程方法和 GetOpenFileName API。在 Vista 和更高版本上,这将导致一个看起来很可怕的文件对话框,但实用程序有限,例如没有搜索。在 Vista 上,您应该使用 IFileDialog 并自定义您需要 IFileDialogCustomize 接口的对话框。因为新的 Vista 对话框作为 COM 接口公开,所以它们在 .net 中很容易使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-03
    • 2015-09-19
    • 2013-08-29
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多