private void ResizeCustomControl(IntPtr hWnd, InteropUtil.RECT rect, params IntPtr[] buttons)
        {
            InteropUtil.Assume(buttons != null && buttons.Length > 0);

            InteropUtil.AssumeNonZero(hWnd);

            InteropUtil.WINDOWPLACEMENT wndLoc = InteropUtil.GetWindowPlacement(hWnd);

            wndLoc.Right = rect.right;
            InteropUtil.SetWindowPlacement(hWnd, ref wndLoc);

            foreach (IntPtr hBtn in buttons)
            {
                int btnRight, btnWidth;

                m_calcPosMap[InteropUtil.GetDlgCtrlID(hBtn)](this, rect.right, out btnRight, out btnWidth);

                PositionButton(hBtn, btnRight, btnWidth);
            }

            //see bug # 844
            //We clip hWnd to only draw in the rectangle around our custom buttons.
            //When we supply a custom dialog template to GetOpenFileName(), it adds
            //an extra HWND to the open file dialog, and then sticks all the controls
            //in the dialog //template inside the HWND. It then resizes the control
            //to stretch from the top of the open file dialog to the bottom of the
            //window, extending the bottom of the window large enough to include the
            //additional height of the dialog template. This ends up sticking our custom
            //buttons at the bottom of the window, which is what we want.
            //
            //However, the fact that the parent window extends from the top of the open
            //file dialog was causing some painting problems on Windows XP SP 3 systems.
            //Basically, because the window was covering the predefined controls on the
            //open file dialog, they were not getting painted. This results in a blank
            //window. I tried setting an extended WS_EX_TRANSPARENT style on the dialog,
            //but that didn't help.
            //
            //So, to fix the problem I setup a window region for the synthetic HWND.
            //This clips the drawing of the window to only within the region containing
            //the custom buttons, and thus avoids the problem.
            //
            //I'm not sure why this wasn't an issue on Vista.
            IntPtr hRgn = InteropUtil.CreateRectRgnIndirect(ref rect);

            try
            {
                if (InteropUtil.SetWindowRgn(hWnd, hRgn, true) == 0)
                {
                    //setting the region failed, so we need to delete the region we created above.
                    InteropUtil.DeleteObject(hRgn);
                }
            }
            catch
            {
                if (hRgn != IntPtr.Zero)
                {
                    InteropUtil.DeleteObject(hRgn);
                }
            }
        }
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            InteropUtil.Assume(Marshal.SystemDefaultCharSize == 2, "The character size should be 2");

            IntPtr nativeBuffer = Marshal.AllocCoTaskMem(InteropUtil.NumberOfFileChars * 2);
            IntPtr filterBuffer = IntPtr.Zero;

            _selected = new List <string>();

            try
            {
                var openFileName = new InteropUtil.OpenFileName();
                openFileName.Initialize();
                openFileName.hwndOwner = hwndOwner;

                var chars = new char[InteropUtil.NumberOfFileChars];

                try
                {
                    if (File.Exists(Path))
                    {
                        if (AcceptFiles)
                        {
                            string fileName = System.IO.Path.GetFileName(Path);
                            int    length   = Math.Min(fileName.Length, InteropUtil.NumberOfFileChars);
                            fileName.CopyTo(0, chars, 0, length);
                            openFileName.lpstrInitialDir = System.IO.Path.GetDirectoryName(Path);
                        }
                        else
                        {
                            openFileName.lpstrInitialDir = System.IO.Path.GetDirectoryName(Path);
                        }
                    }
                    else if (Directory.Exists(Path))
                    {
                        openFileName.lpstrInitialDir = Path;
                    }
                    else
                    {
                        //the path does not exist.
                        //We don't just want to throw it away, however.
                        //The initial path we get is most likely provided by the user in some way.
                        //It could be what they typed into a text box before clicking a browse button,
                        //or it could be a value they had entered previously that used to be valid, but now no longer exists.
                        //In any case, we don't want to throw out the user's text. So, we find the first parent
                        //directory of Path that exists on disk.
                        //We will set the initial directory to that path, and then set the initial file to
                        //the rest of the path. The user will get an error if the click "OK"m saying that the selected path
                        //doesn't exist, but that's ok. If we didn't do this, and showed the path, if they clicked
                        //OK without making changes we would actually change the selected path, which would be bad.
                        //This way, if the users want's to change the folder, he actually has to change something.
                        string pathToShow;
                        InitializePathDNE(Path, out openFileName.lpstrInitialDir, out pathToShow);
                        pathToShow = pathToShow ?? String.Empty;
                        int length = Math.Min(pathToShow.Length, InteropUtil.NumberOfFileChars);
                        pathToShow.CopyTo(0, chars, 0, length);
                    }
                }
                catch
                {
                }

                Marshal.Copy(chars, 0, nativeBuffer, chars.Length);

                openFileName.lpstrFile = nativeBuffer;

                if (!AcceptFiles)
                {
                    string str = string.Format("Folders\0*.{0}-{1}\0\0", Guid.NewGuid().ToString("N"),
                                               Guid.NewGuid().ToString("N"));
                    filterBuffer = openFileName.lpstrFilter = Marshal.StringToCoTaskMemUni(str);
                }
                else
                {
                    openFileName.lpstrFilter = IntPtr.Zero;
                }

                openFileName.nMaxCustFilter = 0;
                openFileName.nFilterIndex   = 0;
                openFileName.nMaxFile       = InteropUtil.NumberOfFileChars;
                openFileName.nMaxFileTitle  = 0;
                openFileName.lpstrTitle     = Title;
                openFileName.lpfnHook       = m_hookDelegate;
                openFileName.templateID     = InteropUtil.IDD_CustomOpenDialog;
                openFileName.hInstance      = Marshal.GetHINSTANCE(typeof(SelectFileAndFolderDialog).Module);
                openFileName.Flags          =
                    InteropUtil.OFN_DONTADDTORECENT |
                    InteropUtil.OFN_ENABLEHOOK |
                    InteropUtil.OFN_ENABLESIZING |
                    InteropUtil.OFN_NOTESTFILECREATE |
                    InteropUtil.OFN_ALLOWMULTISELECT |
                    InteropUtil.OFN_EXPLORER |
                    InteropUtil.OFN_FILEMUSTEXIST |
                    InteropUtil.OFN_PATHMUSTEXIST |
                    InteropUtil.OFN_NODEREFERENCELINKS |
                    InteropUtil.OFN_ENABLETEMPLATE |
                    (ShowReadOnly ? 0 : InteropUtil.OFN_HIDEREADONLY);

                m_useCurrentDir = false;
                bool hideFileExtSettingChanged = false;

                try
                {
                    if (GetHideFileExtensionSetting())
                    {
                        HideFileExtension(false);
                        hideFileExtSettingChanged = true;
                    }

                    bool ret = false;
                    try
                    {
                        ret = InteropUtil.GetOpenFileNameW(ref openFileName);
                    }
                    catch (Exception e)
                    {
                        IntPtr hParent = InteropUtil.AssumeNonZero(InteropUtil.GetParent(m_hWnd));
                        InteropUtil.SendMessage(hParent, InteropUtil.WM_CLOSE, 0, 0);
                        throw e;
                    }
                    //var extErrpr = InteropUtil.CommDlgExtendedError();
                    //InteropUtil.CheckForWin32Error();

                    if (m_useCurrentDir)
                    {
                        Path = m_currentFolder;
                        return(true);
                    }
                    else if (ret)
                    {
                        Marshal.Copy(nativeBuffer, chars, 0, chars.Length);
                        int firstZeroTerm = ((IList)chars).IndexOf('\0');
                        if (firstZeroTerm >= 0 && firstZeroTerm <= chars.Length - 1)
                        {
                            Path = new string(chars, 0, firstZeroTerm);
                        }
                    }
                    return(ret);
                }
                finally
                {
                    //revert registry setting
                    if (hideFileExtSettingChanged)
                    {
                        HideFileExtension(true);
                    }
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(nativeBuffer);
                if (filterBuffer != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(filterBuffer);
                }
            }
        }