Customize Save file dialog to be similar to underlying OS save dialog C#

后端 未结 1 1096
孤独总比滥情好
孤独总比滥情好 2021-02-14 19:05

I have been using this example to customize the save dialog,

http://www.codeproject.com/Articles/19566/Extend-OpenFileDialog-and-SaveFileDialog-the-easy

This wor

相关标签:
1条回答
  • 2021-02-14 19:34

    The suggested solution works as described:

    The Save As file dialog (used in this example) is associated to a User Control, called CustomSaveFileDialog. It has the advantage that it is present in the Toolbox, and that it implements automatically the IDisposable interface. However, it could have also been a simple C# class.

    This control has a constructor accepting an arbitrary application specific User Control hosting all the elements which are to show up in the File Dialog. When I got the question right, that is what is required.

    The CustomSaveFileDialog has the following properties:

    • Accepting arbitrary User Controls Docked to the bottom of the File Dialog, i.e. they follow the resizing of the File Dialog
    • No special behaviour for the additional elements (buttons, images, checkboxes etc) is necessary. They act quite normally, as in other windows.

    This is the code of the described class:

    using System;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    
    namespace customSaveFileDialog
    {
        public partial class CustomSaveFileDialog : UserControl
        {
            //https://stackoverflow.com/questions/9665579/setting-up-hook-on-windows-messages
            delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType,
                IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
    
            const uint WINEVENT_OUTOFCONTEXT = 0;
    
            [DllImport("user32.dll")]
            private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr
               hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess,
               uint idThread, uint dwFlags);
    
            [DllImport("user32.dll")]
            private static extern bool UnhookWinEvent(IntPtr hWinEventHook);
    
            [DllImport("user32.dll")]
            private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int w, int h, bool repaint);
            private struct RECT { public int Left; public int Top; public int Right; public int Bottom; }
    
            [DllImport("user32.dll")]
            private static extern bool GetClientRect(IntPtr hWnd, out RECT rc);
    
            [DllImport("kernel32.dll")]
            private static extern uint GetLastError();
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern IntPtr SetParent(IntPtr hwndChild, IntPtr hwndNewParent);
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern IntPtr GetParent(IntPtr hWnd);
    
            private IntPtr hDlg;        // Save As dialog handle
            private IntPtr hHook;       // Event hook
            private IntPtr hCtrl;       // App. specific user control handle
    
            UserControl ctrl;           // App. specific user control
    
            //Static variable containing the instance object
            private static CustomSaveFileDialog customSaveFileDialog;
    
            //public property for the user
            //theSaveFileDialog has been added to the control in the designer from the Toolbox
            public SaveFileDialog Dlg { get { return theSaveFileDialog; } }
    
            //Event hook delegate
            private static WinEventDelegate procDelegate = new WinEventDelegate(WinEventProc);
    
            /// <summary>
            /// Constructor
            /// </summary>
            /// <param name="ctrl">The User Control to be displayed in the file dialog</param>
            public CustomSaveFileDialog(UserControl ctrl)
            {
                InitializeComponent();
    
                customSaveFileDialog = this;
                this.ctrl = ctrl;
                hCtrl = ctrl.Handle;
    
                //Setup Hook; for simplicity, hook all possible events from the current process
                hHook = SetWinEventHook(1, 0x7fffffff, IntPtr.Zero,
                        procDelegate, (uint)Process.GetCurrentProcess().Id, 0, WINEVENT_OUTOFCONTEXT);
            }
    
    
            // Hook function
            static void WinEventProc(IntPtr hWinEventHook, uint eventType,
                IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
            {
                CustomSaveFileDialog csfdg = customSaveFileDialog;
                if (csfdg.hDlg == IntPtr.Zero)
                    csfdg.hDlg = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Save As");
    
                if (hwnd == csfdg.hDlg) 
                {
                    IntPtr hParent = GetParent(csfdg.hCtrl);
    
                    //this is done only once
                    if (!(hParent == csfdg.hDlg))
                        SetParent(csfdg.hCtrl, csfdg.hDlg);   //Bind the user control to the Common Dialog
    
                    RECT cliRect;
                    GetClientRect(csfdg.hDlg, out cliRect);
    
                    //Position the button in the file dialog
                    MoveWindow(csfdg.hCtrl, cliRect.Left + 130, cliRect.Bottom - 55, 500, 60, true);
                }
            }
        }
    }
    

    The essential part is the hooking of the windows events. This has been taken from that post.

    It may be noted that the "FindWindowEx" function (in the WinEventProc) finds all Common Dialogs (and probably more) with a title of "Save As". If this should be a problem, more filtering would be necessary, e.g by searching in the current thread only. Such a search function may be found here.

    Additionally (not shown in the above code) the "Dispose" method in CustormSaveFileDialog.desinger.cs contains the Unhook function with the hHook handle as the parameter.

    The software has been tested in Windows7 in Debug mode. As a test, a simple Forms window with a button has been implemented:

            //Test for the customized "Save As" dialog
            private void button1_Click(object sender, EventArgs e)
            {
                //Arbitrary User Control
                myUserControl ctrl = new myUserControl();
    
                using (CustomSaveFileDialog csfdg = new CustomSaveFileDialog(ctrl))
                {
                    csfdg.Dlg.FileName = "test";
    
                    //Show the Save As dialog associated to the CustomFileDialog control
                    DialogResult res = csfdg.Dlg.ShowDialog();
                    if (res == System.Windows.Forms.DialogResult.OK)
                        MessageBox.Show("Save Dialog Finished");
                }
            }
    

    And - as well as a test - the applicatioin specific user control handles the following events:

    using System;
    using System.Windows.Forms;
    
    namespace CustomFile
    {
        public partial class myUserControl : UserControl
        {
            public myUserControl()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Button Clicked");
            }
    
            private void pictureBox1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Image Clicked");
    
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                if (!checkBox1.Checked)
                    pictureBox1.Visible = false;
                else
                    pictureBox1.Visible = true;
            }
        }
    }
    

    The following output is produced:

    enter image description here

    The next picture shows another screenshot, File Dialog resized, and the checkbox to display the image is unchecked.

    enter image description here

    0 讨论(0)
提交回复
热议问题