OpenFileDialog in beliebiger Ansicht öffnen  
Frank Dzaebel, erstellt am: 23.01.07, zuletzt geändert:  23.01.07
Kategorie: Shell Interaktion, .NET-Version: 2.0, [Download]

Die Ansicht des OpenFileDialog's von .NET ist nicht direkt über verwaltetete .NET-Methoden beeinflussbar. Eine Möglichkeit ist, spezielle bekannte Klassennamen der Windows-Dialog-Fenster mit PInvoke zu erkennen und diesen eine spezielle Meldung zu senden. Dieses Wissen ist durch Tools wie Spy++ aus den Visual Studio Tools-Menü herauszufinden. Getestet unter XP und Vista.

Weiterführende Links: Customizing OpenFileDialog in .NET

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace OpenFileThumbnail
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      Button btn = new Button(); btn.Text = "Öffnen";
      btn.Click +=new EventHandler(btn_Click);
      this.Controls.Add(btn);
    }

    private void btn_Click(object sender, EventArgs e)
    {
      OpenFileDialog ofd = new OpenFileDialog();
      ofd.Title = "Bilder öffnen"; ofdTitle = ofd.Title;
      new Thread(new ThreadStart(OfnModify)).Start();
      ofd.ShowDialog();
    }

    string ofdTitle;
    const int WM_COMMAND = 0x111;

    void OfnModify()
    {
      IntPtr ofnWindow = FindWindowWait(ofdTitle);
      if (ofnWindow == IntPtr.Zero) return;
      IntPtr child = FindWindowEx(ofnWindow, IntPtr.Zero, shellDll_defView, null);
      SendMessage(child, WM_COMMAND, SHELL_VIEW.THUMBNAIL, IntPtr.Zero);
    }

    const string shellDll_defView = "SHELLDLL_DefView";
    const int WaitForShell_Milliseconds = 500; 

    public static IntPtr FindWindowWait(string windowName)
    {
      Application.DoEvents();
      IntPtr hWnd = FindWindow(null, windowName);
      while (hWnd == IntPtr.Zero)
      {
        Application.DoEvents(); Thread.Sleep(WaitForShell_Milliseconds);
        hWnd = FindWindow(null, windowName);
      }
      return hWnd;
    }

    // http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/findwindowex.asp
    [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern IntPtr FindWindowEx(
      IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern int SendMessage(IntPtr hWnd, int wMsg, SHELL_VIEW wParam, IntPtr lParam);

    // http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/findwindow.asp
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    enum SHELL_VIEW
    {
      LARGEICON = 0x7029,
      SMALLICON = 0x702A,
      LIST      = 0x702B,
      REPORT    = 0x702C,
      THUMBNAIL = 0x702D,  // ab XP
      TILE      = 0x702E,  // ab XP
    }
  }
}