public DialogResult ShowDialog() { //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 = Form.ActiveForm.Handle; //we need to find out the active screen so the dialog box is //centred on the correct display m_ActiveScreen = Screen.FromControl(Form.ActiveForm); //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); }
private static extern bool GetSaveFileName(ref OpenFileName lpofn);