private int OpenFileSubClass
        (
            IntPtr hWnd,
            uint uMsg,
            IntPtr wParam,
            IntPtr lParam,
            IntPtr uIdSubclass,
            uint dwRefData
        )
        {
            switch (uMsg)
            {
            case InteropUtil.WM_PARENTNOTIFY:
            {
                unchecked
                {
                    int id = InteropUtil.GetDlgCtrlID(lParam);

                    if (LOW((uint)wParam) == InteropUtil.WM_CREATE &&
                        (id == InteropUtil.ID_FileList || id == 0))
                    {
                        InteropUtil.SetWindowSubclass(lParam, m_defViewSubClassDelegate, 0, 0);
                    }
                }
                break;
            }
            }
            return(InteropUtil.DefSubclassProc(hWnd, uMsg, wParam, lParam));
        }
        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);
                }
            }
        }
        private void PositionButton(IntPtr hWnd, int right, int width)
        {
            InteropUtil.AssumeNonZero(hWnd);
            int id = InteropUtil.GetDlgCtrlID(hWnd);

            //hWnd.BringWindowToTop();

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

            buttonLoc.Right = right;
            buttonLoc.Left  = buttonLoc.Right - width;
            InteropUtil.SetWindowPlacement(hWnd, ref buttonLoc);
            InteropUtil.InvalidateRect(hWnd, IntPtr.Zero, true);
        }