5,696,576 members and growing! (15,306 online)
Email Password   helpLost your password?
Desktop Development » Dialogs and Windows » General     Intermediate License: The Code Project Open License (CPOL)

FileDialogExtender

By Robert Rohde

Describes an easy way to change the initial view state of the listview in the Open/SaveFileDialog.
C#.NET 1.1, Win2K, WinXP, Windows, .NET, WinForms, Visual Studio, VS.NET2003, Dev

Posted: 4 Dec 2004
Updated: 4 Dec 2004
Views: 81,294
Bookmarked: 49 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
37 votes for this Article.
Popularity: 7.19 Rating: 4.59 out of 5
0 votes, 0.0%
1
2 votes, 5.4%
2
3 votes, 8.1%
3
6 votes, 16.2%
4
26 votes, 70.3%
5

Sample Image - article.jpg

Introduction

The .NET framework is in my opinion an impressive piece of work with many useful classes. But I think every developer has at least once faced the problem that a class or function doesn't exactly fit his needs. Normally, one would derive from it and extend the functionality. The OpenFileDialog and SaveFileDialog are sealed and thus there is no direct way of extending them. This article explains how you can extend them anyway without the need to make everything yourself.

Background

Once upon a time, in a country far far away...

... a customer had the request that in an OpenFileDialog - where he could open an image - the default view state of the contained listview should be the thumbnail view. No one in my company really thought about it, and so we just said: "No problem!".

After looking at the given dialog classes, I realized that this wasn't as easy as just setting the appropriate property. I searched the forums and several search engines, but all I could find was listings which showed how to create the dialogs directly via the Windows APIs which needed dozen of structs with dozen of fields and cryptic constants. This was just too much overhead for me just to change the initial view state. So I began to think about other solutions.

The idea which came to my mind was that the parent form somehow must get to know when and which modal dialog is displayed. So I wrote a form overriding the WndProc and traced the messages coming while opening a file dialog. After some minutes, I had the message I needed. It has the number 289 and fires in the message loop of the modal dialog. This message also contains the handle to the dialog. It is important that the dialog is fully created at this time, because otherwise the following steps wouldn't work.

Phew! No, I had the handle to the dialog and knew it was shown (I could just have looked at the screen to realize this). Now, I needed a way to change the style of the listview. So I started Spy++ and searched for the dialog (with the handle at hand, this was easy) and quickly found the child window which was containing the listview.

SampleImage - spy1.jpg

I opened the messages dialog of Spy++ and began changing the listview's view state, and after some filtering, I got the message which was changing the style. The appropriate parameters for the view types were also shown by Spy++.

SampleImage - spy2.jpg

Now, I just had to add two small API calls to change my dialog. I used FindWindowEx to search for the handle of the window holding the listview. After retrieving it, I called SendMessage to send the message delivered by Spy++ to the window.

Using the code

I tried to stuff everything into a component. The problem is that the only one who has access to WndProc of a System.Windows.Form is the form itself. Thus, the form that will show the file dialog needs to override WndProc and make a call to this component. The minimal code to show an extended dialog would be:

public class TestForm : System.Windows.Forms.Form
{
    private System.Windows.Forms.Button _btnOpen;
    private FileDialogExtender _extender = 
        new FileDialogExtender();

    public TestForm()
    {
        this._btnOpen = 
            new System.Windows.Forms.Button();
        this._btnOpen.Location = 
            new System.Drawing.Point(8, 8);
        this._btnOpen.Name = "_btnOpen";
        this._btnOpen.Size = 
            new System.Drawing.Size(96, 23);
        this._btnOpen.Text = "Open";
        this._btnOpen.Click += 
            new System.EventHandler(this.ShowOpenDialog);
        this.Controls.Add(this._btnOpen);
        this.FormBorderStyle = 
            FormBorderStyle.FixedDialog;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "TestForm";
        this.Text = "Test";
    }

    [STAThread]
    static void Main() 
    {
        Application.Run(new TestForm());
    }

    private void ShowOpenDialog(object sender, 
        System.EventArgs e)
    {
        new OpenFileDialog().ShowDialog();
    }

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        _extender.WndProc(ref m);
    }
}

A better sample project is included in one of the download links.

Points of Interest

I hope I could show you how you can extend system dialogs by finding the needed handles to remote control their behavior.

There is much more one could do this way without throwing away the benefits of using the managed dialog classes. As I have no further need for extensions, I will now leave it at this stage.

If anyone has an idea on how to achieve this functionality without overriding WndProc or adding other API calls or hooks, please let me know.

History

  • 2004-12-04 - Initial release.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Robert Rohde



Occupation: Web Developer
Location: Germany Germany

Other popular Dialogs and Windows articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 39 (Total in Forum: 39) (Refresh)FirstPrevNext
GeneralRewritten for VB2005memberMember 42577422:35 25 Jan '08  
GeneralYou are my hero!memberMarakai18:03 29 Apr '07  
QuestionChange the sizememberEsben Sundgaard1:58 20 Oct '06  
AnswerRe: Change the sizememberRobert Rohde22:41 20 Oct '06  
GeneralRe: Change the sizememberEsben Sundgaard11:02 21 Oct '06  
Generalmultiple files open dialog box.memberjeevaggan21:32 21 Jun '06  
GeneralRe: multiple files open dialog box.memberRobert Rohde0:10 22 Jun '06  
GeneralAnother implementation without WndProcmemberstancrm4:27 24 Apr '06  
GeneralRe: Another implementation without WndProcmemberRobert Rohde11:02 26 Apr '06  
GeneralLike the code below you don't have to call StartWatchingmemberhenrik_stromberg4:25 21 Sep '07  
GeneralI agree ... you rockmembermikecline15:40 7 Mar '06  
GeneralYou Rock Man [modified]memberLightingToGo14:32 1 Jan '06  
GeneralDose not Work In child form of MDI Form?membermajidbhutta6:47 14 Nov '05  
GeneralRe: Dose not Work In child form of MDI Form?memberRobert Rohde8:43 14 Nov '05  
GeneralRe: Dose not Work In child form of MDI Form?memberRobert Rohde15:23 19 Nov '05  
GeneralImplementation seems to be OS dependentsussCristianC106:41 7 Jul '05  
Generalcontactmembermanatarms2415:15 3 Jul '05  
GeneralUsing the FileDialog to select folders as wellsussDoron Nesher11:38 12 Mar '05  
GeneralRe: Using the FileDialog to select folders as wellmemberRobert Rohde22:02 12 Mar '05  
GeneralRe: Using the FileDialog to select folders as wellmemberEytan Herksovic2:10 13 Mar '05  
GeneralRe: Using the FileDialog to select folders as wellmemberRobert Rohde3:32 13 Mar '05  
GeneralRe: Using the FileDialog to select folders as wellmemberEytan Herksovic5:07 13 Mar '05  
GeneralRe: Using the FileDialog to select folders as wellmemberThe_Mega_ZZTer11:09 15 Sep '05  
AnswerRe: Using the FileDialog to select folders as wellmembersoftplanner6:30 6 Dec '05  
GeneralRe: Using the FileDialog to select folders as wellmemberEytan Herksovic22:21 24 Dec '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 4 Dec 2004
Editor: Smitha Vijayan
Copyright 2004 by Robert Rohde
Everything else Copyright © CodeProject, 1999-2008
Web17 | Advertise on the Code Project