-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveFileDialogWithEncoding.cs
346 lines (262 loc) · 10.2 KB
/
SaveFileDialogWithEncoding.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using System;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
public enum EncodingType {
//UTF8=0,
//UTF8WithPreamble,
//Unicode,
//Ansi
ANSI,
BigEndianUnicode,
Unicode,
UTF8,
Shift_JIS
};
public class SaveFileDialogWithEncoding {
private delegate int OFNHookProcDelegate(int hdlg, int msg, int wParam, int lParam);
private int m_LabelHandle=0;
private int m_ComboHandle=0;
private string m_Filter="";
private string m_DefaultExt="";
private string m_FileName="";
private EncodingType m_EncodingType;
private Screen m_ActiveScreen;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
private struct OPENFILENAME {
public int lStructSize;
public IntPtr hwndOwner;
public int hInstance;
[MarshalAs(UnmanagedType.LPTStr)] public string lpstrFilter;
[MarshalAs(UnmanagedType.LPTStr)] public string lpstrCustomFilter;
public int nMaxCustFilter;
public int nFilterIndex;
[MarshalAs(UnmanagedType.LPTStr)] public string lpstrFile;
public int nMaxFile;
[MarshalAs(UnmanagedType.LPTStr)] public string lpstrFileTitle;
public int nMaxFileTitle;
[MarshalAs(UnmanagedType.LPTStr)] public string lpstrInitialDir;
[MarshalAs(UnmanagedType.LPTStr)] public string lpstrTitle;
public int Flags;
public short nFileOffset;
public short nFileExtension;
[MarshalAs(UnmanagedType.LPTStr)] public string lpstrDefExt;
public int lCustData;
public OFNHookProcDelegate lpfnHook;
[MarshalAs(UnmanagedType.LPTStr)] public string lpTemplateName;
//only if on nt 5.0 or higher
public int pvReserved;
public int dwReserved;
public int FlagsEx;
}
[DllImport("Comdlg32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern bool GetSaveFileName(ref OPENFILENAME lpofn);
[DllImport("Comdlg32.dll")]
private static extern int CommDlgExtendedError();
[DllImport("user32.dll")]
private static extern bool SetWindowPos(int hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private struct RECT {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
private struct POINT {
public int X;
public int Y;
}
private struct NMHDR {
public int HwndFrom;
public int IdFrom;
public int Code;
}
[DllImport("user32.dll")]
private static extern bool GetWindowRect(int hWnd, ref RECT lpRect);
[DllImport("user32.dll")]
private static extern int GetParent(int hWnd);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern bool SetWindowText(int hWnd, string lpString);
[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern int SendMessage(int hWnd, int Msg, int wParam, string lParam);
[DllImport("user32.dll")]
private static extern bool DestroyWindow(int hwnd);
private const int OFN_ENABLEHOOK=0x00000020;
private const int OFN_EXPLORER=0x00080000;
private const int OFN_FILEMUSTEXIST=0x00001000;
private const int OFN_HIDEREADONLY=0x00000004;
private const int OFN_CREATEPROMPT=0x00002000;
private const int OFN_NOTESTFILECREATE=0x00010000;
private const int OFN_OVERWRITEPROMPT=0x00000002;
private const int OFN_PATHMUSTEXIST=0x00000800;
private const int SWP_NOSIZE=0x0001;
private const int SWP_NOMOVE=0x0002;
private const int SWP_NOZORDER=0x0004;
private const int WM_INITDIALOG=0x110;
private const int WM_DESTROY=0x2;
private const int WM_SETFONT=0x0030;
private const int WM_GETFONT=0x0031;
private const int CBS_DROPDOWNLIST=0x0003;
private const int CBS_HASSTRINGS=0x0200;
private const int CB_ADDSTRING=0x0143;
private const int CB_SETCURSEL=0x014E;
private const int CB_GETCURSEL=0x0147;
private const uint WS_VISIBLE=0x10000000;
private const uint WS_CHILD=0x40000000;
private const uint WS_TABSTOP=0x00010000;
private const int CDN_FILEOK=-606;
private const int WM_NOTIFY=0x004E;
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern int GetDlgItem(int hDlg, int nIDDlgItem);
[DllImport("user32.dll", CharSet=CharSet.Auto)]
private static extern int CreateWindowEx(int dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, int x, int y, int nWidth, int nHeight, int hWndParent, int hMenu, int hInstance, int lpParam);
[DllImport("user32.dll")]
private static extern bool ScreenToClient(int hWnd, ref POINT lpPoint);
private int HookProc(int hdlg, int msg, int wParam, int lParam) {
switch (msg) {
case WM_INITDIALOG:
//we need to centre the dialog
Rectangle sr=m_ActiveScreen.Bounds;
RECT cr=new RECT();
int parent=GetParent(hdlg);
GetWindowRect(parent, ref cr);
int x=(sr.Right + sr.Left - (cr.Right-cr.Left))/2;
int y=(sr.Bottom + sr.Top - (cr.Bottom-cr.Top))/2;
SetWindowPos(parent, 0, x, y, cr.Right-cr.Left, cr.Bottom - cr.Top + 32, SWP_NOZORDER);
//we need to find the label to position our new label under
int fileTypeWindow=GetDlgItem(parent, 0x441);
RECT aboveRect=new RECT();
GetWindowRect(fileTypeWindow, ref aboveRect);
//now convert the label's screen co-ordinates to client co-ordinates
POINT point=new POINT();
point.X=aboveRect.Left;
point.Y=aboveRect.Bottom;
ScreenToClient(parent, ref point);
//create the label
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:");
int fontHandle=SendMessage(fileTypeWindow, WM_GETFONT, 0, 0);
SendMessage(labelHandle, WM_SETFONT, fontHandle, 0);
//we now need to find the combo-box to position the new combo-box under
int fileComboWindow=GetDlgItem(parent, 0x470);
aboveRect=new RECT();
GetWindowRect(fileComboWindow, ref aboveRect);
point=new POINT();
point.X=aboveRect.Left;
point.Y=aboveRect.Bottom;
ScreenToClient(parent, ref point);
POINT rightPoint=new POINT();
rightPoint.X=aboveRect.Right;
rightPoint.Y=aboveRect.Top;
ScreenToClient(parent, ref rightPoint);
//we create the new combobox
int comboHandle=CreateWindowEx(0, "ComboBox", "mycombobox", WS_VISIBLE | WS_CHILD | CBS_HASSTRINGS | CBS_DROPDOWNLIST | WS_TABSTOP, point.X, point.Y + 8, rightPoint.X-point.X, 100, parent, 0, 0, 0);
SendMessage(comboHandle, WM_SETFONT, fontHandle, 0);
//and add the encodings we want to offer
SendMessage(comboHandle, CB_ADDSTRING, 0, "ANSI");
SendMessage(comboHandle, CB_ADDSTRING, 0, "BigEndianUnicode");
SendMessage(comboHandle, CB_ADDSTRING, 0, "Unicode");
SendMessage(comboHandle, CB_ADDSTRING, 0, "UTF-8");
SendMessage(comboHandle, CB_ADDSTRING, 0, "SJIS");
SendMessage(comboHandle, CB_SETCURSEL, (int)m_EncodingType, 0);
//remember the handles of the controls we have created so we can destroy them after
m_LabelHandle=labelHandle;
m_ComboHandle=comboHandle;
break;
case WM_DESTROY:
//destroy the handles we have created
if (m_ComboHandle!=0) {
DestroyWindow(m_ComboHandle);
}
if (m_LabelHandle!=0) {
DestroyWindow(m_LabelHandle);
}
break;
case WM_NOTIFY:
//we need to intercept the CDN_FILEOK message
//which is sent when the user selects a filename
NMHDR nmhdr=(NMHDR)Marshal.PtrToStructure(new IntPtr(lParam), typeof(NMHDR));
if (nmhdr.Code==CDN_FILEOK) {
//a file has been selected
//we need to get the encoding
m_EncodingType=(EncodingType)SendMessage(m_ComboHandle, CB_GETCURSEL, 0, 0);
}
break;
}
return 0;
}
public string DefaultExt {
get { return m_DefaultExt; }
set { m_DefaultExt=value; }
}
public string Filter {
get { return m_Filter; }
set { m_Filter=value; }
}
public string FileName {
get { return m_FileName; }
set { m_FileName=value; }
}
public EncodingType EncodingType {
get { return m_EncodingType; }
set { m_EncodingType=value; }
}
public DialogResult ShowDialog(IntPtr ownerhwnd, Screen ownerscreen) {
return ShowDialog(ownerhwnd,ownerscreen,"Unicode");
}
public DialogResult ShowDialog(IntPtr ownerhwnd, Screen ownerscreen, string encoding) {
//m_EncodingType = stringToEncodingType(encoding);
//set up the struct and populate it
OPENFILENAME ofn=new OPENFILENAME();
ofn.lStructSize= Marshal.SizeOf( ofn );
ofn.lpstrFilter= m_Filter.Replace('|', '\0') + '\0';
ofn.lpstrFile = m_FileName + new string(' ', 512);
ofn.nMaxFile= ofn.lpstrFile.Length;
ofn.lpstrFileTitle= System.IO.Path.GetFileName(m_FileName) + new string(' ', 512);
ofn.nMaxFileTitle = ofn.lpstrFileTitle.Length;
ofn.lpstrTitle= "Save file as";
ofn.lpstrDefExt=m_DefaultExt;
//position the dialog above the active window
ofn.hwndOwner=ownerhwnd;
//we need to find out the active screen so the dialog box is
//centred on the correct display
m_ActiveScreen=ownerscreen;
//set up some sensible flags
ofn.Flags=OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_NOTESTFILECREATE | OFN_ENABLEHOOK | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
//this is where the hook is set. Note that we can use a C# delegate in place of a C function pointer
ofn.lpfnHook=new OFNHookProcDelegate(HookProc);
//if we're running on Windows 98/ME then the struct is smaller
if (System.Environment.OSVersion.Platform!=PlatformID.Win32NT) {
ofn.lStructSize-=12;
}
//show the dialog
if (!GetSaveFileName(ref ofn)) {
int ret=CommDlgExtendedError();
if (ret!=0) {
throw new ApplicationException("Couldn't show file open dialog - " + ret.ToString());
}
return DialogResult.Cancel;
}
m_FileName=ofn.lpstrFile;
return DialogResult.OK;
}
public static System.Text.Encoding stringToEncodingType(string input) {
unsafe {
switch (input) {
case "ASCII" : return System.Text.Encoding.ASCII;
case "ANSI" : return System.Text.Encoding.GetEncoding(1252);
case "BigEndianUnicode" : return System.Text.Encoding.BigEndianUnicode;
case "SJIS" :
case "Shift_JIS" : return System.Text.Encoding.GetEncoding("shift_jis");
case "UTF8" :
case "UTF-8" : return System.Text.Encoding.UTF8;
case "Unicode" :
default : return System.Text.Encoding.Unicode;
}
}
}
}