65.9K
CodeProject 正在变化。 阅读更多。
Home

远程控制 Microsoft FileDialog 类 (OpenFileDialog)

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (3投票s)

2009年2月3日

CPOL

1分钟阅读

viewsIcon

44214

downloadIcon

803

远程控制 Microsoft FileDialog 类 (OpenFileDialog)

引言

如果您在程序中经常使用包含大量文件的标准文件打开对话框,您很快就会发现缺少一些默认参数,这些参数可用于打开包含详细信息并按日期排序的文件。

背景

此时,如果您不想在 Visual Studio(2005、2008)中重写文件对话框,您就开始寻找解决此问题的简单方法。我发现此类解决方案的一些好的示例是

但我没有找到如何按日期或大小排序!!!!!!

因此,这是我针对此问题的少量解决方案。

Using the Code

为此,我找到了如何远程控制列。包含列列表的类是“ListViewRemote”。

要调用此类,您需要知道FileOpenDialog内部的句柄。以下是一小段代码,您可以将其输入任何表单以帮助您查找句柄。

/// <summary> 
/// this part of code, i took from FileDialogExtender.aspx 
/// </summary> 
/// <param name="m"></param> 
protected override void WndProc(ref Message m)
{
  base.WndProc(ref m);
  if (m.Msg == 289) //Notify of message loop 
  {
    uint dialogHandle = (uint)m.LParam; //handle of the file dialog 
    if (dialogHandle != _lastDialogHandle) //only when not already changed 
    {
      //get handle of the listview 
      IntPtr SHELLDLL_DefView = Win32.FindWindowEx
			((IntPtr)dialogHandle, 0, "SHELLDLL_DefView", "");
      //send message to SHELLDLL_DefView 
      IntPtr result = Win32.SendMessage(SHELLDLL_DefView, 
			(int)Win32.WM_COMMAND, (int)DialogView, 0);

现在,当您获得句柄后,就可以调用ListViewRemote了。

      if (DialogView == DialogViewTypes.Details)
      {
        ListViewRemote lvr = new ListViewRemote(SHELLDLL_DefView);
        lvr.SetSortBy((string)sortByComboBox.SelectedItem, 
			(ColumnSort)sortToComboBox.SelectedItem);
      }
  //remember last handle 
  _lastDialogHandle = dialogHandle;
}
}
}

此解决方案的主要思想是捕获ListView的标题……

base.AssignHandle(Win32.FindWindowEx
	(SHELLDLL_DefView, 0, "SysListView32", "FolderView"));

IntPtr listViewHeader = Win32.FindWindowEx(listViewHandle, 0, "SysHeader32", "");

……并使用HDITEM作为远程控制项(有时只是生成鼠标点击)。

我在本文中提供的代码示例只是解决 Visual Studio 文件对话框问题的第一步。

历史

  • 2009年2月2日:第一个版本
    当前:按升序和降序对listview的任何列进行排序,并选择所需的listview类型

哇,我做到了。:) 找不到如此重要问题的解决方案真是令人恼火!

© . All rights reserved.