Exemplo n.º 1
0
        protected override IntPtr HookProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam)
        {
            IntPtr result = IntPtr.Zero;

            if (msg == 78)
            {
                this._hwndFileDialog = UnsafeNativeMethods.GetParent(new HandleRef(this, hwnd));
                NativeMethods.OFNOTIFY ofnotify = (NativeMethods.OFNOTIFY)UnsafeNativeMethods.PtrToStructure(lParam, typeof(NativeMethods.OFNOTIFY));
                switch (ofnotify.hdr_code)
                {
                case -606:
                    if (this._ignoreSecondFileOkNotification)
                    {
                        if (this._fileOkNotificationCount != 0)
                        {
                            this._ignoreSecondFileOkNotification = false;
                            UnsafeNativeMethods.CriticalSetWindowLong(new HandleRef(this, hwnd), 0, NativeMethods.InvalidIntPtr);
                            result = NativeMethods.InvalidIntPtr;
                            break;
                        }
                        this._fileOkNotificationCount = 1;
                    }
                    if (!this.DoFileOk(ofnotify.lpOFN))
                    {
                        UnsafeNativeMethods.CriticalSetWindowLong(new HandleRef(this, hwnd), 0, NativeMethods.InvalidIntPtr);
                        result = NativeMethods.InvalidIntPtr;
                    }
                    break;

                case -604:
                    this._ignoreSecondFileOkNotification = true;
                    this._fileOkNotificationCount        = 0;
                    break;

                case -602:
                {
                    NativeMethods.OPENFILENAME_I openfilename_I = (NativeMethods.OPENFILENAME_I)UnsafeNativeMethods.PtrToStructure(ofnotify.lpOFN, typeof(NativeMethods.OPENFILENAME_I));
                    int num = (int)UnsafeNativeMethods.UnsafeSendMessage(this._hwndFileDialog, (WindowMessage)1124, IntPtr.Zero, IntPtr.Zero);
                    if (num > openfilename_I.nMaxFile)
                    {
                        int num2 = num + 2048;
                        NativeMethods.CharBuffer charBuffer = NativeMethods.CharBuffer.CreateBuffer(num2);
                        IntPtr lpstrFile = charBuffer.AllocCoTaskMem();
                        Marshal.FreeCoTaskMem(openfilename_I.lpstrFile);
                        openfilename_I.lpstrFile = lpstrFile;
                        openfilename_I.nMaxFile  = num2;
                        this._charBuffer         = charBuffer;
                        Marshal.StructureToPtr(openfilename_I, ofnotify.lpOFN, true);
                        Marshal.StructureToPtr(ofnotify, lParam, true);
                    }
                    break;
                }

                case -601:
                    base.MoveToScreenCenter(new HandleRef(this, this._hwndFileDialog));
                    break;
                }
            }
            else
            {
                result = base.HookProc(hwnd, msg, wParam, lParam);
            }
            return(result);
        }