Click here to Skip to main content
11,647,810 members (48,392 online)
Click here to Skip to main content

Extending the save file dialog class in .NET

, 24 Aug 2004 185.8K 3.3K 59
Rate this:
Please Sign up or sign in to vote.
Using the GetSaveFileName function to extend the Save File dialog class

Introduction

Sometimes programming is like doing a jigsaw puzzle with a missing corner.

I work at Red Gate Software, the company that produces SQL Compare, which is written in C#. Although C# and the .NET framework enable us to write good code quickly, the combination has its drawbacks. More often than not I find that even though .NET covers 95 percent of the native Windows API, the five percent that’s missing is the five percent I need. This article describes one of these situations.

C# is very good at handling international character support. This means that SQL Compare can handle databases that use diverse character sets. The problem comes when we need to get the T-SQL scripts out of SQL Compare and into other third-party tools.

Tangled web of encoding

There are a number of different ways of saving Unicode data to disk. ASCII encoding doesn’t handle international characters at all – it will only cope with seven-bit values from 0 to 127. UTF-8 encoding is the same as ANSI encoding for seven-bit characters such as A, B, C and D. Other, more exotic, characters are stored as two-, three- or four-byte combinations. Unicode, or UTF-16, encoding normally stores each character as two bytes, although it can occasionally use four bytes. In addition, each encoding type allows a preamble. This consists of two (for UTF-16) or three (UTF-8) bytes written at the start of a file to indicate the encoding of the file.

Not all tools support all encodings. SQL Query Analyzer, for example, supports UTF-16, but not UTF-8 encoded files; if you ask it to open a UTF-8 file it will treat it as ASCII and treat a two-byte international character as two separate characters. If, however, you save files as UTF-16, then editors that are not Unicode-aware will display each two-byte character as two single characters, l i k e t h i s.

Simple solution?

The lack of universal support means that we need to let our users choose the type of encoding they want when saving files to disk. This is where the tricky part starts. .NET provides a SaveFileDialog class that you can normally use to get a filename. Unfortunately, this dialog does not provide a way of asking the user for an encoding. I thought adding this functionality would be a simple matter of inheriting from the SaveFileDialog class and then adding a couple of controls to the dialog. This class is not, however, a simple Windows form. Instead, it uses the Windows GetSaveFileName function to display the save file dialog.

This made things a bit harder. I looked at the MSDN help for the SaveFileDialog class and noticed that there is a protected HookProc method. This lets you hook into the dialog creation process. You should be able to inherit from the class, intercept the appropriate windows messages, and then add the controls onto the form. Unfortunately, the SaveFileDialog class is sealed. This means that you cannot inherit from the class and override the HookProc method.

At this point I did a search through MSDN and the newsgroups. I found a couple of interesting articles by Dino Esposito and some newsgroup postings by Nicholas Paladino that suggested a good approach.

I decided to use the GetSaveFileNamefrom the Windows API. This takes an OPENFILENAME struct. One of the fields of this struct is a hook that expects a function pointer. One of the neat things about .NET interop is the way you can use a managed C# delegate in place of an unmanaged C++ function pointer:

.
.
private delegate int OFNHookProcDelegate(int hdlg, int msg, 
                                         int wParam, int lParam);
.
.
ofn.lpfnHook = new OFNHookProcDelegate(HookProc);
.
.
GetSaveFileName(ref ofn)
.
.
private int HookProc(int hdlg, int msg, int wParam, int lParam)
{
      //function body
}
.
.

When GetSaveFileName is called, the HookProc receives notification messages from the save file dialog box.

I needed to hook into the WM_INITDIALOG message. In the hook I found the location of the standard Save as type:label and combobox and added a new label and combo box for the encoding choices using CreateWindowEx:

int fileTypeWindow=GetDlgItem(parent, 0x441);
RECT aboveRect = new RECT();
GetWindowRect(fileTypeWindow, ref aboveRect);

POINT point=new POINT();
point.X=aboveRect.Left;
point.Y=aboveRect.Bottom;
ScreenToClient(parent, ref point);

int labelHandle=CreateWindowEx(0, "STATIC", "mylabel", 
                               WS_VISIBLE | WS_CHILD | WS_TABSTOP, 
                               point.X, point.Y + 12, 200, 100, parent, 
                               0, 0, 0);
SetWindowText(labelHandle, "&Encoding:");

In addition to intercepting the WM_INITDIALOGmessage, I needed to intercept the WM_DESTROYand WM_NOTIFYmessages to destroy the label and combo box, and to intercept the selection of a file to work out the chosen encoding.

The final dialog box looks like this:

Download the source code

You can download the complete source code for this sample by following the link at the top of this article. Although the sample doesn’t provide all the functionality of the SaveFileDialog class, it should provide a good starting point for you to implement the functionality you need.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Neil Davidson
United Kingdom United Kingdom
Neil Davidson is technical director of Red Gate Software (www.red-gate.com), a company that provides simple tools for Microsoft developers, testers and DBAs. He can be reached at neil.davidson@red-gate.com.

You may also be interested in...

Comments and Discussions

 
GeneralNMHDR.Code has changed in Windows Vista Pin
malac130-Jul-10 2:06
membermalac130-Jul-10 2:06 
GeneralGet File Name control in SaveAs dialog Pin
Sameer Abhang10-Mar-10 20:49
memberSameer Abhang10-Mar-10 20:49 
GeneralRe: Get File Name control in SaveAs dialog Pin
malac130-Jul-10 1:58
membermalac130-Jul-10 1:58 
GeneralRe: Get File Name control in SaveAs dialog Pin
dmihailescu17-Nov-10 6:36
memberdmihailescu17-Nov-10 6:36 
QuestionHow css can be applied in files stored in Memory Pin
sushilabhanvar1-Apr-09 22:16
membersushilabhanvar1-Apr-09 22:16 
GeneralThank you - I have a question Pin
loumarco8-Dec-08 5:14
memberloumarco8-Dec-08 5:14 
GeneralApplying this to all applications Pin
scoulton8912-Oct-08 12:24
memberscoulton8912-Oct-08 12:24 
Questionhow can i find a dialog item id needed by GetDlgItem()? Pin
Ajithevn4-Mar-08 21:58
memberAjithevn4-Mar-08 21:58 
GeneralAwesome Pin
viperjason29-Feb-08 13:08
memberviperjason29-Feb-08 13:08 
QuestionGood stuff, I have a question Pin
Zardoz17-Nov-07 2:48
memberZardoz17-Nov-07 2:48 
QuestionAny resource usage considerations? Pin
flipdoubt9-Nov-07 4:35
memberflipdoubt9-Nov-07 4:35 
QuestionRe: Any resource usage considerations? Pin
flipdoubt9-Nov-07 5:13
memberflipdoubt9-Nov-07 5:13 
Generalresizable file dialog Pin
tsaik9917-Jun-07 22:07
membertsaik9917-Jun-07 22:07 
GeneralRe: resizable file dialog Pin
Giorgi Dalakishvili17-Jun-07 22:39
memberGiorgi Dalakishvili17-Jun-07 22:39 
GeneralDialog items Pin
juliusf15-May-07 5:49
memberjuliusf15-May-07 5:49 
GeneralReplase "|" with #0, not #92 Pin
George Birbilis25-Mar-07 10:02
memberGeorge Birbilis25-Mar-07 10:02 
GeneralVB.net version with FileOK event [modified] Pin
George Birbilis20-Mar-07 11:31
memberGeorge Birbilis20-Mar-07 11:31 
GeneralGood stuff, but I have a question. Pin
roybrew27-Oct-06 5:02
memberroybrew27-Oct-06 5:02 
GeneralRe: Good stuff, but I have a question. Pin
roybrew27-Oct-06 7:40
memberroybrew27-Oct-06 7:40 
Questionvery nice Pin
chandu.sanka8-Sep-06 1:30
memberchandu.sanka8-Sep-06 1:30 
GeneralControl Id Pin
KynanHaas31-May-06 17:16
memberKynanHaas31-May-06 17:16 
GeneralRe: Control Id Pin
George Birbilis20-Mar-07 6:34
memberGeorge Birbilis20-Mar-07 6:34 
QuestionC# Control in dialog Pin
Olaf Gramkow26-May-06 5:42
memberOlaf Gramkow26-May-06 5:42 
AnswerRe: C# Control in dialog Pin
Stuart Carnie7-Feb-07 19:57
memberStuart Carnie7-Feb-07 19:57 
QuestionRe: C# Control in dialog Pin
flipdoubt27-Oct-07 4:24
memberflipdoubt27-Oct-07 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150804.4 | Last Updated 25 Aug 2004
Article Copyright 2004 by Neil Davidson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid