Пример #1
0
 private void UnregisterClass()
 {
     if (this.registered && System.Windows.Forms.UnsafeNativeMethods.UnregisterClass(this.windowClassName, new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null))))
     {
         this.windowProc = null;
         this.registered = false;
     }
 }
Пример #2
0
 protected override bool RunDialog(IntPtr hwndOwner)
 {
     System.Windows.Forms.IntSecurity.SafePrinting.Demand();
     System.Windows.Forms.NativeMethods.WndProc hookProcPtr = new System.Windows.Forms.NativeMethods.WndProc(this.HookProc);
     if ((!this.UseEXDialog || (Environment.OSVersion.Platform != PlatformID.Win32NT)) || (Environment.OSVersion.Version.Major < 5))
     {
         System.Windows.Forms.NativeMethods.PRINTDLG printdlg = CreatePRINTDLG();
         return(this.ShowPrintDialog(hwndOwner, hookProcPtr, printdlg));
     }
     System.Windows.Forms.NativeMethods.PRINTDLGEX data = CreatePRINTDLGEX();
     return(this.ShowPrintDialog(hwndOwner, data));
 }
Пример #3
0
 internal void AssignHandle(IntPtr handle, bool assignUniqueID)
 {
     lock (this)
     {
         this.CheckReleased();
         this.handle = handle;
         if (userDefWindowProc == IntPtr.Zero)
         {
             string lpProcName = (Marshal.SystemDefaultCharSize == 1) ? "DefWindowProcA" : "DefWindowProcW";
             userDefWindowProc = System.Windows.Forms.UnsafeNativeMethods.GetProcAddress(new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle("user32.dll")), lpProcName);
             if (userDefWindowProc == IntPtr.Zero)
             {
                 throw new Win32Exception();
             }
         }
         this.defWindowProc = System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), -4);
         if (WndProcShouldBeDebuggable)
         {
             this.windowProc = new System.Windows.Forms.NativeMethods.WndProc(this.DebuggableCallback);
         }
         else
         {
             this.windowProc = new System.Windows.Forms.NativeMethods.WndProc(this.Callback);
         }
         AddWindowToTable(handle, this);
         System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(new HandleRef(this, handle), -4, this.windowProc);
         this.windowProcPtr = System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), -4);
         if ((assignUniqueID && ((((int)((long)System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), -16))) & 0x40000000) != 0)) && (((int)((long)System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), -12))) == 0))
         {
             System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(new HandleRef(this, handle), -12, new HandleRef(this, handle));
         }
         if (this.suppressedGC)
         {
             new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
             try
             {
                 GC.ReRegisterForFinalize(this);
             }
             finally
             {
                 CodeAccessPermission.RevertAssert();
             }
             this.suppressedGC = false;
         }
         this.OnHandleChange();
     }
 }
Пример #4
0
        private bool RunDialogOld(IntPtr hWndOwner)
        {
            bool flag;

            System.Windows.Forms.NativeMethods.WndProc        proc = new System.Windows.Forms.NativeMethods.WndProc(this.HookProc);
            System.Windows.Forms.NativeMethods.OPENFILENAME_I ofn  = new System.Windows.Forms.NativeMethods.OPENFILENAME_I();
            try
            {
                this.charBuffer = System.Windows.Forms.UnsafeNativeMethods.CharBuffer.CreateBuffer(0x2000);
                if (this.fileNames != null)
                {
                    this.charBuffer.PutString(this.fileNames[0]);
                }
                ofn.lStructSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.OPENFILENAME_I));
                if ((Environment.OSVersion.Platform != PlatformID.Win32NT) || (Environment.OSVersion.Version.Major < 5))
                {
                    ofn.lStructSize = 0x4c;
                }
                ofn.hwndOwner       = hWndOwner;
                ofn.hInstance       = this.Instance;
                ofn.lpstrFilter     = MakeFilterString(this.filter, this.DereferenceLinks);
                ofn.nFilterIndex    = this.filterIndex;
                ofn.lpstrFile       = this.charBuffer.AllocCoTaskMem();
                ofn.nMaxFile        = 0x2000;
                ofn.lpstrInitialDir = this.initialDir;
                ofn.lpstrTitle      = this.title;
                ofn.Flags           = this.Options | 0x880020;
                ofn.lpfnHook        = proc;
                ofn.FlagsEx         = 0x1000000;
                if ((this.defaultExt != null) && this.AddExtension)
                {
                    ofn.lpstrDefExt = this.defaultExt;
                }
                flag = this.RunFileDialog(ofn);
            }
            finally
            {
                this.charBuffer = null;
                if (ofn.lpstrFile != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(ofn.lpstrFile);
                }
            }
            return(flag);
        }
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            bool flag;

            System.Windows.Forms.NativeMethods.WndProc     proc = new System.Windows.Forms.NativeMethods.WndProc(this.HookProc);
            System.Windows.Forms.NativeMethods.CHOOSECOLOR cc   = new System.Windows.Forms.NativeMethods.CHOOSECOLOR();
            IntPtr destination = Marshal.AllocCoTaskMem(0x40);

            try
            {
                Marshal.Copy(this.customColors, 0, destination, 0x10);
                cc.hwndOwner    = hwndOwner;
                cc.hInstance    = this.Instance;
                cc.rgbResult    = ColorTranslator.ToWin32(this.color);
                cc.lpCustColors = destination;
                int num = this.Options | 0x11;
                if (!this.AllowFullOpen)
                {
                    num &= -3;
                }
                cc.Flags    = num;
                cc.lpfnHook = proc;
                if (!System.Windows.Forms.SafeNativeMethods.ChooseColor(cc))
                {
                    return(false);
                }
                if (cc.rgbResult != ColorTranslator.ToWin32(this.color))
                {
                    this.color = ColorTranslator.FromOle(cc.rgbResult);
                }
                Marshal.Copy(destination, this.customColors, 0, 0x10);
                flag = true;
            }
            finally
            {
                Marshal.FreeCoTaskMem(destination);
            }
            return(flag);
        }
 protected override bool RunDialog(IntPtr hwndOwner)
 {
     bool flag;
     System.Windows.Forms.NativeMethods.WndProc proc = new System.Windows.Forms.NativeMethods.WndProc(this.HookProc);
     System.Windows.Forms.NativeMethods.CHOOSECOLOR cc = new System.Windows.Forms.NativeMethods.CHOOSECOLOR();
     IntPtr destination = Marshal.AllocCoTaskMem(0x40);
     try
     {
         Marshal.Copy(this.customColors, 0, destination, 0x10);
         cc.hwndOwner = hwndOwner;
         cc.hInstance = this.Instance;
         cc.rgbResult = ColorTranslator.ToWin32(this.color);
         cc.lpCustColors = destination;
         int num = this.Options | 0x11;
         if (!this.AllowFullOpen)
         {
             num &= -3;
         }
         cc.Flags = num;
         cc.lpfnHook = proc;
         if (!System.Windows.Forms.SafeNativeMethods.ChooseColor(cc))
         {
             return false;
         }
         if (cc.rgbResult != ColorTranslator.ToWin32(this.color))
         {
             this.color = ColorTranslator.FromOle(cc.rgbResult);
         }
         Marshal.Copy(destination, this.customColors, 0, 0x10);
         flag = true;
     }
     finally
     {
         Marshal.FreeCoTaskMem(destination);
     }
     return flag;
 }
 private void RegisterClass()
 {
     string className;
     System.Windows.Forms.NativeMethods.WNDCLASS_D wc = new System.Windows.Forms.NativeMethods.WNDCLASS_D();
     if (NativeWindow.userDefWindowProc == IntPtr.Zero)
     {
         string lpProcName = (Marshal.SystemDefaultCharSize == 1) ? "DefWindowProcA" : "DefWindowProcW";
         NativeWindow.userDefWindowProc = System.Windows.Forms.UnsafeNativeMethods.GetProcAddress(new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle("user32.dll")), lpProcName);
         if (NativeWindow.userDefWindowProc == IntPtr.Zero)
         {
             throw new Win32Exception();
         }
     }
     if (this.className == null)
     {
         wc.hbrBackground = System.Windows.Forms.UnsafeNativeMethods.GetStockObject(5);
         wc.style = this.classStyle;
         this.defWindowProc = NativeWindow.userDefWindowProc;
         className = "Window." + Convert.ToString(this.classStyle, 0x10);
         this.hashCode = 0;
     }
     else
     {
         System.Windows.Forms.NativeMethods.WNDCLASS_I wndclass_i = new System.Windows.Forms.NativeMethods.WNDCLASS_I();
         bool flag = System.Windows.Forms.UnsafeNativeMethods.GetClassInfo(System.Windows.Forms.NativeMethods.NullHandleRef, this.className, wndclass_i);
         int error = Marshal.GetLastWin32Error();
         if (!flag)
         {
             throw new Win32Exception(error, System.Windows.Forms.SR.GetString("InvalidWndClsName"));
         }
         wc.style = wndclass_i.style;
         wc.cbClsExtra = wndclass_i.cbClsExtra;
         wc.cbWndExtra = wndclass_i.cbWndExtra;
         wc.hIcon = wndclass_i.hIcon;
         wc.hCursor = wndclass_i.hCursor;
         wc.hbrBackground = wndclass_i.hbrBackground;
         wc.lpszMenuName = Marshal.PtrToStringAuto(wndclass_i.lpszMenuName);
         className = this.className;
         this.defWindowProc = wndclass_i.lpfnWndProc;
         this.hashCode = this.className.GetHashCode();
     }
     this.windowClassName = this.GetFullClassName(className);
     this.windowProc = new System.Windows.Forms.NativeMethods.WndProc(this.Callback);
     wc.lpfnWndProc = this.windowProc;
     wc.hInstance = System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null);
     wc.lpszClassName = this.windowClassName;
     short num2 = System.Windows.Forms.UnsafeNativeMethods.RegisterClass(wc);
     if (num2 == 0)
     {
         int num3 = Marshal.GetLastWin32Error();
         if (num3 == 0x582)
         {
             System.Windows.Forms.NativeMethods.WNDCLASS_I wndclass_i2 = new System.Windows.Forms.NativeMethods.WNDCLASS_I();
             if (System.Windows.Forms.UnsafeNativeMethods.GetClassInfo(new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null)), this.windowClassName, wndclass_i2) && (wndclass_i2.lpfnWndProc == NativeWindow.UserDefindowProc))
             {
                 if (System.Windows.Forms.UnsafeNativeMethods.UnregisterClass(this.windowClassName, new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null))))
                 {
                     num2 = System.Windows.Forms.UnsafeNativeMethods.RegisterClass(wc);
                 }
                 else
                 {
                     do
                     {
                         domainQualifier++;
                         this.windowClassName = this.GetFullClassName(className);
                         wc.lpszClassName = this.windowClassName;
                         num2 = System.Windows.Forms.UnsafeNativeMethods.RegisterClass(wc);
                     }
                     while ((num2 == 0) && (Marshal.GetLastWin32Error() == 0x582));
                 }
             }
         }
         if (num2 == 0)
         {
             this.windowProc = null;
             throw new Win32Exception(num3);
         }
     }
     this.registered = true;
 }
Пример #8
0
        public DialogResult ShowDialog(IWin32Window owner)
        {
            System.Windows.Forms.IntSecurity.SafeSubWindows.Demand();
            if (!SystemInformation.UserInteractive)
            {
                throw new InvalidOperationException(System.Windows.Forms.SR.GetString("CantShowModalOnNonInteractive"));
            }
            NativeWindow window = null;
            IntPtr       zero   = IntPtr.Zero;
            DialogResult cancel = DialogResult.Cancel;

            try
            {
                if (owner != null)
                {
                    zero = Control.GetSafeHandle(owner);
                }
                if (zero == IntPtr.Zero)
                {
                    zero = System.Windows.Forms.UnsafeNativeMethods.GetActiveWindow();
                }
                if (zero == IntPtr.Zero)
                {
                    window = new NativeWindow();
                    window.CreateHandle(new CreateParams());
                    zero = window.Handle;
                }
                if (helpMsg == 0)
                {
                    helpMsg = System.Windows.Forms.SafeNativeMethods.RegisterWindowMessage("commdlg_help");
                }
                System.Windows.Forms.NativeMethods.WndProc d = new System.Windows.Forms.NativeMethods.WndProc(this.OwnerWndProc);
                this.hookedWndProc = Marshal.GetFunctionPointerForDelegate(d);
                IntPtr userCookie = IntPtr.Zero;
                try
                {
                    this.defOwnerWndProc = System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(new HandleRef(this, zero), -4, d);
                    if (Application.UseVisualStyles)
                    {
                        userCookie = System.Windows.Forms.UnsafeNativeMethods.ThemingScope.Activate();
                    }
                    Application.BeginModalMessageLoop();
                    try
                    {
                        cancel = this.RunDialog(zero) ? DialogResult.OK : DialogResult.Cancel;
                    }
                    finally
                    {
                        Application.EndModalMessageLoop();
                    }
                    return(cancel);
                }
                finally
                {
                    IntPtr windowLong = System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, zero), -4);
                    if ((IntPtr.Zero != this.defOwnerWndProc) || (windowLong != this.hookedWndProc))
                    {
                        System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(new HandleRef(this, zero), -4, new HandleRef(this, this.defOwnerWndProc));
                    }
                    System.Windows.Forms.UnsafeNativeMethods.ThemingScope.Deactivate(userCookie);
                    this.defOwnerWndProc = IntPtr.Zero;
                    this.hookedWndProc   = IntPtr.Zero;
                    GC.KeepAlive(d);
                }
            }
            finally
            {
                if (window != null)
                {
                    window.DestroyHandle();
                }
            }
            return(cancel);
        }
 protected override bool RunDialog(IntPtr hwndOwner)
 {
     System.Windows.Forms.NativeMethods.PAGESETUPDLG pagesetupdlg;
     bool flag2;
     System.Windows.Forms.IntSecurity.SafePrinting.Demand();
     System.Windows.Forms.NativeMethods.WndProc proc = new System.Windows.Forms.NativeMethods.WndProc(this.HookProc);
     if (this.pageSettings == null)
     {
         throw new ArgumentException(System.Windows.Forms.SR.GetString("PSDcantShowWithoutPage"));
     }
     pagesetupdlg = new System.Windows.Forms.NativeMethods.PAGESETUPDLG {
         lStructSize = Marshal.SizeOf(pagesetupdlg),
         Flags = this.GetFlags(),
         hwndOwner = hwndOwner,
         lpfnPageSetupHook = proc
     };
     PrinterUnit thousandthsOfAnInch = PrinterUnit.ThousandthsOfAnInch;
     if (this.EnableMetric)
     {
         StringBuilder lpLCData = new StringBuilder(2);
         if ((UnsafeNativeMethods.GetLocaleInfo(System.Windows.Forms.NativeMethods.LOCALE_USER_DEFAULT, 13, lpLCData, lpLCData.Capacity) > 0) && (int.Parse(lpLCData.ToString(), CultureInfo.InvariantCulture) == 0))
         {
             thousandthsOfAnInch = PrinterUnit.HundredthsOfAMillimeter;
         }
     }
     if (this.MinMargins != null)
     {
         Margins margins = PrinterUnitConvert.Convert(this.MinMargins, PrinterUnit.Display, thousandthsOfAnInch);
         pagesetupdlg.minMarginLeft = margins.Left;
         pagesetupdlg.minMarginTop = margins.Top;
         pagesetupdlg.minMarginRight = margins.Right;
         pagesetupdlg.minMarginBottom = margins.Bottom;
     }
     if (this.pageSettings.Margins != null)
     {
         Margins margins2 = PrinterUnitConvert.Convert(this.pageSettings.Margins, PrinterUnit.Display, thousandthsOfAnInch);
         pagesetupdlg.marginLeft = margins2.Left;
         pagesetupdlg.marginTop = margins2.Top;
         pagesetupdlg.marginRight = margins2.Right;
         pagesetupdlg.marginBottom = margins2.Bottom;
     }
     pagesetupdlg.marginLeft = Math.Max(pagesetupdlg.marginLeft, pagesetupdlg.minMarginLeft);
     pagesetupdlg.marginTop = Math.Max(pagesetupdlg.marginTop, pagesetupdlg.minMarginTop);
     pagesetupdlg.marginRight = Math.Max(pagesetupdlg.marginRight, pagesetupdlg.minMarginRight);
     pagesetupdlg.marginBottom = Math.Max(pagesetupdlg.marginBottom, pagesetupdlg.minMarginBottom);
     System.Drawing.Printing.PrinterSettings settings = (this.printerSettings == null) ? this.pageSettings.PrinterSettings : this.printerSettings;
     System.Windows.Forms.IntSecurity.AllPrintingAndUnmanagedCode.Assert();
     try
     {
         pagesetupdlg.hDevMode = settings.GetHdevmode(this.pageSettings);
         pagesetupdlg.hDevNames = settings.GetHdevnames();
     }
     finally
     {
         CodeAccessPermission.RevertAssert();
     }
     try
     {
         if (!UnsafeNativeMethods.PageSetupDlg(pagesetupdlg))
         {
             return false;
         }
         UpdateSettings(pagesetupdlg, this.pageSettings, this.printerSettings);
         flag2 = true;
     }
     finally
     {
         UnsafeNativeMethods.GlobalFree(new HandleRef(pagesetupdlg, pagesetupdlg.hDevMode));
         UnsafeNativeMethods.GlobalFree(new HandleRef(pagesetupdlg, pagesetupdlg.hDevNames));
     }
     return flag2;
 }
Пример #10
0
        protected override bool RunDialog(IntPtr hWndOwner)
        {
            bool flag;

            System.Windows.Forms.NativeMethods.WndProc    proc = new System.Windows.Forms.NativeMethods.WndProc(this.HookProc);
            System.Windows.Forms.NativeMethods.CHOOSEFONT cf   = new System.Windows.Forms.NativeMethods.CHOOSEFONT();
            IntPtr dC = System.Windows.Forms.UnsafeNativeMethods.GetDC(System.Windows.Forms.NativeMethods.NullHandleRef);

            System.Windows.Forms.NativeMethods.LOGFONT logFont = new System.Windows.Forms.NativeMethods.LOGFONT();
            Graphics graphics = Graphics.FromHdcInternal(dC);

            System.Windows.Forms.IntSecurity.ObjectFromWin32Handle.Assert();
            try
            {
                this.Font.ToLogFont(logFont, graphics);
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
                graphics.Dispose();
            }
            System.Windows.Forms.UnsafeNativeMethods.ReleaseDC(System.Windows.Forms.NativeMethods.NullHandleRef, new HandleRef(null, dC));
            IntPtr zero = IntPtr.Zero;

            try
            {
                zero = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.LOGFONT)));
                Marshal.StructureToPtr(logFont, zero, false);
                cf.lStructSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.CHOOSEFONT));
                cf.hwndOwner   = hWndOwner;
                cf.hDC         = IntPtr.Zero;
                cf.lpLogFont   = zero;
                cf.Flags       = (this.Options | 0x40) | 8;
                if ((this.minSize > 0) || (this.maxSize > 0))
                {
                    cf.Flags |= 0x2000;
                }
                if (this.ShowColor || this.ShowEffects)
                {
                    cf.rgbColors = ColorTranslator.ToWin32(this.color);
                }
                else
                {
                    cf.rgbColors = ColorTranslator.ToWin32(System.Drawing.Color.Black);
                }
                cf.lpfnHook  = proc;
                cf.hInstance = System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null);
                cf.nSizeMin  = this.minSize;
                if (this.maxSize == 0)
                {
                    cf.nSizeMax = 0x7fffffff;
                }
                else
                {
                    cf.nSizeMax = this.maxSize;
                }
                if (!System.Windows.Forms.SafeNativeMethods.ChooseFont(cf))
                {
                    return(false);
                }
                System.Windows.Forms.NativeMethods.LOGFONT logfont2 = null;
                logfont2 = (System.Windows.Forms.NativeMethods.LOGFONT)System.Windows.Forms.UnsafeNativeMethods.PtrToStructure(zero, typeof(System.Windows.Forms.NativeMethods.LOGFONT));
                if ((logfont2.lfFaceName != null) && (logfont2.lfFaceName.Length > 0))
                {
                    logFont = logfont2;
                    this.UpdateFont(logFont);
                    this.UpdateColor(cf.rgbColors);
                }
                flag = true;
            }
            finally
            {
                if (zero != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(zero);
                }
            }
            return(flag);
        }
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            System.Windows.Forms.NativeMethods.PAGESETUPDLG pagesetupdlg;
            bool flag2;

            System.Windows.Forms.IntSecurity.SafePrinting.Demand();
            System.Windows.Forms.NativeMethods.WndProc proc = new System.Windows.Forms.NativeMethods.WndProc(this.HookProc);
            if (this.pageSettings == null)
            {
                throw new ArgumentException(System.Windows.Forms.SR.GetString("PSDcantShowWithoutPage"));
            }
            pagesetupdlg = new System.Windows.Forms.NativeMethods.PAGESETUPDLG {
                lStructSize       = Marshal.SizeOf(pagesetupdlg),
                Flags             = this.GetFlags(),
                hwndOwner         = hwndOwner,
                lpfnPageSetupHook = proc
            };
            PrinterUnit thousandthsOfAnInch = PrinterUnit.ThousandthsOfAnInch;

            if (this.EnableMetric)
            {
                StringBuilder lpLCData = new StringBuilder(2);
                if ((UnsafeNativeMethods.GetLocaleInfo(System.Windows.Forms.NativeMethods.LOCALE_USER_DEFAULT, 13, lpLCData, lpLCData.Capacity) > 0) && (int.Parse(lpLCData.ToString(), CultureInfo.InvariantCulture) == 0))
                {
                    thousandthsOfAnInch = PrinterUnit.HundredthsOfAMillimeter;
                }
            }
            if (this.MinMargins != null)
            {
                Margins margins = PrinterUnitConvert.Convert(this.MinMargins, PrinterUnit.Display, thousandthsOfAnInch);
                pagesetupdlg.minMarginLeft   = margins.Left;
                pagesetupdlg.minMarginTop    = margins.Top;
                pagesetupdlg.minMarginRight  = margins.Right;
                pagesetupdlg.minMarginBottom = margins.Bottom;
            }
            if (this.pageSettings.Margins != null)
            {
                Margins margins2 = PrinterUnitConvert.Convert(this.pageSettings.Margins, PrinterUnit.Display, thousandthsOfAnInch);
                pagesetupdlg.marginLeft   = margins2.Left;
                pagesetupdlg.marginTop    = margins2.Top;
                pagesetupdlg.marginRight  = margins2.Right;
                pagesetupdlg.marginBottom = margins2.Bottom;
            }
            pagesetupdlg.marginLeft   = Math.Max(pagesetupdlg.marginLeft, pagesetupdlg.minMarginLeft);
            pagesetupdlg.marginTop    = Math.Max(pagesetupdlg.marginTop, pagesetupdlg.minMarginTop);
            pagesetupdlg.marginRight  = Math.Max(pagesetupdlg.marginRight, pagesetupdlg.minMarginRight);
            pagesetupdlg.marginBottom = Math.Max(pagesetupdlg.marginBottom, pagesetupdlg.minMarginBottom);
            System.Drawing.Printing.PrinterSettings settings = (this.printerSettings == null) ? this.pageSettings.PrinterSettings : this.printerSettings;
            System.Windows.Forms.IntSecurity.AllPrintingAndUnmanagedCode.Assert();
            try
            {
                pagesetupdlg.hDevMode  = settings.GetHdevmode(this.pageSettings);
                pagesetupdlg.hDevNames = settings.GetHdevnames();
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            try
            {
                if (!UnsafeNativeMethods.PageSetupDlg(pagesetupdlg))
                {
                    return(false);
                }
                UpdateSettings(pagesetupdlg, this.pageSettings, this.printerSettings);
                flag2 = true;
            }
            finally
            {
                UnsafeNativeMethods.GlobalFree(new HandleRef(pagesetupdlg, pagesetupdlg.hDevMode));
                UnsafeNativeMethods.GlobalFree(new HandleRef(pagesetupdlg, pagesetupdlg.hDevNames));
            }
            return(flag2);
        }
 protected override bool RunDialog(IntPtr hwndOwner)
 {
     System.Windows.Forms.IntSecurity.SafePrinting.Demand();
     System.Windows.Forms.NativeMethods.WndProc hookProcPtr = new System.Windows.Forms.NativeMethods.WndProc(this.HookProc);
     if ((!this.UseEXDialog || (Environment.OSVersion.Platform != PlatformID.Win32NT)) || (Environment.OSVersion.Version.Major < 5))
     {
         System.Windows.Forms.NativeMethods.PRINTDLG printdlg = CreatePRINTDLG();
         return this.ShowPrintDialog(hwndOwner, hookProcPtr, printdlg);
     }
     System.Windows.Forms.NativeMethods.PRINTDLGEX data = CreatePRINTDLGEX();
     return this.ShowPrintDialog(hwndOwner, data);
 }
 public DialogResult ShowDialog(IWin32Window owner)
 {
     System.Windows.Forms.IntSecurity.SafeSubWindows.Demand();
     if (!SystemInformation.UserInteractive)
     {
         throw new InvalidOperationException(System.Windows.Forms.SR.GetString("CantShowModalOnNonInteractive"));
     }
     NativeWindow window = null;
     IntPtr zero = IntPtr.Zero;
     DialogResult cancel = DialogResult.Cancel;
     try
     {
         if (owner != null)
         {
             zero = Control.GetSafeHandle(owner);
         }
         if (zero == IntPtr.Zero)
         {
             zero = System.Windows.Forms.UnsafeNativeMethods.GetActiveWindow();
         }
         if (zero == IntPtr.Zero)
         {
             window = new NativeWindow();
             window.CreateHandle(new CreateParams());
             zero = window.Handle;
         }
         if (helpMsg == 0)
         {
             helpMsg = System.Windows.Forms.SafeNativeMethods.RegisterWindowMessage("commdlg_help");
         }
         System.Windows.Forms.NativeMethods.WndProc d = new System.Windows.Forms.NativeMethods.WndProc(this.OwnerWndProc);
         this.hookedWndProc = Marshal.GetFunctionPointerForDelegate(d);
         IntPtr userCookie = IntPtr.Zero;
         try
         {
             this.defOwnerWndProc = System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(new HandleRef(this, zero), -4, d);
             if (Application.UseVisualStyles)
             {
                 userCookie = System.Windows.Forms.UnsafeNativeMethods.ThemingScope.Activate();
             }
             Application.BeginModalMessageLoop();
             try
             {
                 cancel = this.RunDialog(zero) ? DialogResult.OK : DialogResult.Cancel;
             }
             finally
             {
                 Application.EndModalMessageLoop();
             }
             return cancel;
         }
         finally
         {
             IntPtr windowLong = System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, zero), -4);
             if ((IntPtr.Zero != this.defOwnerWndProc) || (windowLong != this.hookedWndProc))
             {
                 System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(new HandleRef(this, zero), -4, new HandleRef(this, this.defOwnerWndProc));
             }
             System.Windows.Forms.UnsafeNativeMethods.ThemingScope.Deactivate(userCookie);
             this.defOwnerWndProc = IntPtr.Zero;
             this.hookedWndProc = IntPtr.Zero;
             GC.KeepAlive(d);
         }
     }
     finally
     {
         if (window != null)
         {
             window.DestroyHandle();
         }
     }
     return cancel;
 }
Пример #14
0
        private bool ShowPrintDialog(IntPtr hwndOwner, System.Windows.Forms.NativeMethods.WndProc hookProcPtr, System.Windows.Forms.NativeMethods.PRINTDLG data)
        {
            bool flag;

            data.Flags         = this.GetFlags();
            data.nCopies       = this.PrinterSettings.Copies;
            data.hwndOwner     = hwndOwner;
            data.lpfnPrintHook = hookProcPtr;
            System.Windows.Forms.IntSecurity.AllPrintingAndUnmanagedCode.Assert();
            try
            {
                if (this.PageSettings == null)
                {
                    data.hDevMode = this.PrinterSettings.GetHdevmode();
                }
                else
                {
                    data.hDevMode = this.PrinterSettings.GetHdevmode(this.PageSettings);
                }
                data.hDevNames = this.PrinterSettings.GetHdevnames();
            }
            catch (InvalidPrinterException)
            {
                data.hDevMode  = IntPtr.Zero;
                data.hDevNames = IntPtr.Zero;
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            try
            {
                if (this.AllowSomePages)
                {
                    if ((this.PrinterSettings.FromPage < this.PrinterSettings.MinimumPage) || (this.PrinterSettings.FromPage > this.PrinterSettings.MaximumPage))
                    {
                        throw new ArgumentException(System.Windows.Forms.SR.GetString("PDpageOutOfRange", new object[] { "FromPage" }));
                    }
                    if ((this.PrinterSettings.ToPage < this.PrinterSettings.MinimumPage) || (this.PrinterSettings.ToPage > this.PrinterSettings.MaximumPage))
                    {
                        throw new ArgumentException(System.Windows.Forms.SR.GetString("PDpageOutOfRange", new object[] { "ToPage" }));
                    }
                    if (this.PrinterSettings.ToPage < this.PrinterSettings.FromPage)
                    {
                        throw new ArgumentException(System.Windows.Forms.SR.GetString("PDpageOutOfRange", new object[] { "FromPage" }));
                    }
                    data.nFromPage = (short)this.PrinterSettings.FromPage;
                    data.nToPage   = (short)this.PrinterSettings.ToPage;
                    data.nMinPage  = (short)this.PrinterSettings.MinimumPage;
                    data.nMaxPage  = (short)this.PrinterSettings.MaximumPage;
                }
                if (!UnsafeNativeMethods.PrintDlg(data))
                {
                    return(false);
                }
                System.Windows.Forms.IntSecurity.AllPrintingAndUnmanagedCode.Assert();
                try
                {
                    UpdatePrinterSettings(data.hDevMode, data.hDevNames, data.nCopies, data.Flags, this.settings, this.PageSettings);
                }
                finally
                {
                    CodeAccessPermission.RevertAssert();
                }
                this.PrintToFile = (data.Flags & 0x20) != 0;
                this.PrinterSettings.PrintToFile = this.PrintToFile;
                if (this.AllowSomePages)
                {
                    this.PrinterSettings.FromPage = data.nFromPage;
                    this.PrinterSettings.ToPage   = data.nToPage;
                }
                if (((data.Flags & 0x40000) == 0) && (Environment.OSVersion.Version.Major >= 6))
                {
                    this.PrinterSettings.Copies  = data.nCopies;
                    this.PrinterSettings.Collate = (data.Flags & 0x10) == 0x10;
                }
                flag = true;
            }
            finally
            {
                UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevMode));
                UnsafeNativeMethods.GlobalFree(new HandleRef(data, data.hDevNames));
            }
            return(flag);
        }
 private void UnregisterClass()
 {
     if (this.registered && System.Windows.Forms.UnsafeNativeMethods.UnregisterClass(this.windowClassName, new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null))))
     {
         this.windowProc = null;
         this.registered = false;
     }
 }
 internal void AssignHandle(IntPtr handle, bool assignUniqueID)
 {
     lock (this)
     {
         this.CheckReleased();
         this.handle = handle;
         if (userDefWindowProc == IntPtr.Zero)
         {
             string lpProcName = (Marshal.SystemDefaultCharSize == 1) ? "DefWindowProcA" : "DefWindowProcW";
             userDefWindowProc = System.Windows.Forms.UnsafeNativeMethods.GetProcAddress(new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle("user32.dll")), lpProcName);
             if (userDefWindowProc == IntPtr.Zero)
             {
                 throw new Win32Exception();
             }
         }
         this.defWindowProc = System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), -4);
         if (WndProcShouldBeDebuggable)
         {
             this.windowProc = new System.Windows.Forms.NativeMethods.WndProc(this.DebuggableCallback);
         }
         else
         {
             this.windowProc = new System.Windows.Forms.NativeMethods.WndProc(this.Callback);
         }
         AddWindowToTable(handle, this);
         System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(new HandleRef(this, handle), -4, this.windowProc);
         this.windowProcPtr = System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), -4);
         if ((assignUniqueID && ((((int) ((long) System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), -16))) & 0x40000000) != 0)) && (((int) ((long) System.Windows.Forms.UnsafeNativeMethods.GetWindowLong(new HandleRef(this, handle), -12))) == 0))
         {
             System.Windows.Forms.UnsafeNativeMethods.SetWindowLong(new HandleRef(this, handle), -12, new HandleRef(this, handle));
         }
         if (this.suppressedGC)
         {
             new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
             try
             {
                 GC.ReRegisterForFinalize(this);
             }
             finally
             {
                 CodeAccessPermission.RevertAssert();
             }
             this.suppressedGC = false;
         }
         this.OnHandleChange();
     }
 }
 private bool RunDialogOld(IntPtr hWndOwner)
 {
     bool flag;
     System.Windows.Forms.NativeMethods.WndProc proc = new System.Windows.Forms.NativeMethods.WndProc(this.HookProc);
     System.Windows.Forms.NativeMethods.OPENFILENAME_I ofn = new System.Windows.Forms.NativeMethods.OPENFILENAME_I();
     try
     {
         this.charBuffer = System.Windows.Forms.UnsafeNativeMethods.CharBuffer.CreateBuffer(0x2000);
         if (this.fileNames != null)
         {
             this.charBuffer.PutString(this.fileNames[0]);
         }
         ofn.lStructSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.OPENFILENAME_I));
         if ((Environment.OSVersion.Platform != PlatformID.Win32NT) || (Environment.OSVersion.Version.Major < 5))
         {
             ofn.lStructSize = 0x4c;
         }
         ofn.hwndOwner = hWndOwner;
         ofn.hInstance = this.Instance;
         ofn.lpstrFilter = MakeFilterString(this.filter, this.DereferenceLinks);
         ofn.nFilterIndex = this.filterIndex;
         ofn.lpstrFile = this.charBuffer.AllocCoTaskMem();
         ofn.nMaxFile = 0x2000;
         ofn.lpstrInitialDir = this.initialDir;
         ofn.lpstrTitle = this.title;
         ofn.Flags = this.Options | 0x880020;
         ofn.lpfnHook = proc;
         ofn.FlagsEx = 0x1000000;
         if ((this.defaultExt != null) && this.AddExtension)
         {
             ofn.lpstrDefExt = this.defaultExt;
         }
         flag = this.RunFileDialog(ofn);
     }
     finally
     {
         this.charBuffer = null;
         if (ofn.lpstrFile != IntPtr.Zero)
         {
             Marshal.FreeCoTaskMem(ofn.lpstrFile);
         }
     }
     return flag;
 }
 protected override bool RunDialog(IntPtr hWndOwner)
 {
     bool flag;
     System.Windows.Forms.NativeMethods.WndProc proc = new System.Windows.Forms.NativeMethods.WndProc(this.HookProc);
     System.Windows.Forms.NativeMethods.CHOOSEFONT cf = new System.Windows.Forms.NativeMethods.CHOOSEFONT();
     IntPtr dC = System.Windows.Forms.UnsafeNativeMethods.GetDC(System.Windows.Forms.NativeMethods.NullHandleRef);
     System.Windows.Forms.NativeMethods.LOGFONT logFont = new System.Windows.Forms.NativeMethods.LOGFONT();
     Graphics graphics = Graphics.FromHdcInternal(dC);
     System.Windows.Forms.IntSecurity.ObjectFromWin32Handle.Assert();
     try
     {
         this.Font.ToLogFont(logFont, graphics);
     }
     finally
     {
         CodeAccessPermission.RevertAssert();
         graphics.Dispose();
     }
     System.Windows.Forms.UnsafeNativeMethods.ReleaseDC(System.Windows.Forms.NativeMethods.NullHandleRef, new HandleRef(null, dC));
     IntPtr zero = IntPtr.Zero;
     try
     {
         zero = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.LOGFONT)));
         Marshal.StructureToPtr(logFont, zero, false);
         cf.lStructSize = Marshal.SizeOf(typeof(System.Windows.Forms.NativeMethods.CHOOSEFONT));
         cf.hwndOwner = hWndOwner;
         cf.hDC = IntPtr.Zero;
         cf.lpLogFont = zero;
         cf.Flags = (this.Options | 0x40) | 8;
         if ((this.minSize > 0) || (this.maxSize > 0))
         {
             cf.Flags |= 0x2000;
         }
         if (this.ShowColor || this.ShowEffects)
         {
             cf.rgbColors = ColorTranslator.ToWin32(this.color);
         }
         else
         {
             cf.rgbColors = ColorTranslator.ToWin32(System.Drawing.Color.Black);
         }
         cf.lpfnHook = proc;
         cf.hInstance = System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null);
         cf.nSizeMin = this.minSize;
         if (this.maxSize == 0)
         {
             cf.nSizeMax = 0x7fffffff;
         }
         else
         {
             cf.nSizeMax = this.maxSize;
         }
         if (!System.Windows.Forms.SafeNativeMethods.ChooseFont(cf))
         {
             return false;
         }
         System.Windows.Forms.NativeMethods.LOGFONT logfont2 = null;
         logfont2 = (System.Windows.Forms.NativeMethods.LOGFONT) System.Windows.Forms.UnsafeNativeMethods.PtrToStructure(zero, typeof(System.Windows.Forms.NativeMethods.LOGFONT));
         if ((logfont2.lfFaceName != null) && (logfont2.lfFaceName.Length > 0))
         {
             logFont = logfont2;
             this.UpdateFont(logFont);
             this.UpdateColor(cf.rgbColors);
         }
         flag = true;
     }
     finally
     {
         if (zero != IntPtr.Zero)
         {
             Marshal.FreeCoTaskMem(zero);
         }
     }
     return flag;
 }
Пример #19
0
            private void RegisterClass()
            {
                string className;

                System.Windows.Forms.NativeMethods.WNDCLASS_D wc = new System.Windows.Forms.NativeMethods.WNDCLASS_D();
                if (NativeWindow.userDefWindowProc == IntPtr.Zero)
                {
                    string lpProcName = (Marshal.SystemDefaultCharSize == 1) ? "DefWindowProcA" : "DefWindowProcW";
                    NativeWindow.userDefWindowProc = System.Windows.Forms.UnsafeNativeMethods.GetProcAddress(new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle("user32.dll")), lpProcName);
                    if (NativeWindow.userDefWindowProc == IntPtr.Zero)
                    {
                        throw new Win32Exception();
                    }
                }
                if (this.className == null)
                {
                    wc.hbrBackground   = System.Windows.Forms.UnsafeNativeMethods.GetStockObject(5);
                    wc.style           = this.classStyle;
                    this.defWindowProc = NativeWindow.userDefWindowProc;
                    className          = "Window." + Convert.ToString(this.classStyle, 0x10);
                    this.hashCode      = 0;
                }
                else
                {
                    System.Windows.Forms.NativeMethods.WNDCLASS_I wndclass_i = new System.Windows.Forms.NativeMethods.WNDCLASS_I();
                    bool flag  = System.Windows.Forms.UnsafeNativeMethods.GetClassInfo(System.Windows.Forms.NativeMethods.NullHandleRef, this.className, wndclass_i);
                    int  error = Marshal.GetLastWin32Error();
                    if (!flag)
                    {
                        throw new Win32Exception(error, System.Windows.Forms.SR.GetString("InvalidWndClsName"));
                    }
                    wc.style           = wndclass_i.style;
                    wc.cbClsExtra      = wndclass_i.cbClsExtra;
                    wc.cbWndExtra      = wndclass_i.cbWndExtra;
                    wc.hIcon           = wndclass_i.hIcon;
                    wc.hCursor         = wndclass_i.hCursor;
                    wc.hbrBackground   = wndclass_i.hbrBackground;
                    wc.lpszMenuName    = Marshal.PtrToStringAuto(wndclass_i.lpszMenuName);
                    className          = this.className;
                    this.defWindowProc = wndclass_i.lpfnWndProc;
                    this.hashCode      = this.className.GetHashCode();
                }
                this.windowClassName = this.GetFullClassName(className);
                this.windowProc      = new System.Windows.Forms.NativeMethods.WndProc(this.Callback);
                wc.lpfnWndProc       = this.windowProc;
                wc.hInstance         = System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null);
                wc.lpszClassName     = this.windowClassName;
                short num2 = System.Windows.Forms.UnsafeNativeMethods.RegisterClass(wc);

                if (num2 == 0)
                {
                    int num3 = Marshal.GetLastWin32Error();
                    if (num3 == 0x582)
                    {
                        System.Windows.Forms.NativeMethods.WNDCLASS_I wndclass_i2 = new System.Windows.Forms.NativeMethods.WNDCLASS_I();
                        if (System.Windows.Forms.UnsafeNativeMethods.GetClassInfo(new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null)), this.windowClassName, wndclass_i2) && (wndclass_i2.lpfnWndProc == NativeWindow.UserDefindowProc))
                        {
                            if (System.Windows.Forms.UnsafeNativeMethods.UnregisterClass(this.windowClassName, new HandleRef(null, System.Windows.Forms.UnsafeNativeMethods.GetModuleHandle(null))))
                            {
                                num2 = System.Windows.Forms.UnsafeNativeMethods.RegisterClass(wc);
                            }
                            else
                            {
                                do
                                {
                                    domainQualifier++;
                                    this.windowClassName = this.GetFullClassName(className);
                                    wc.lpszClassName     = this.windowClassName;
                                    num2 = System.Windows.Forms.UnsafeNativeMethods.RegisterClass(wc);
                                }while ((num2 == 0) && (Marshal.GetLastWin32Error() == 0x582));
                            }
                        }
                    }
                    if (num2 == 0)
                    {
                        this.windowProc = null;
                        throw new Win32Exception(num3);
                    }
                }
                this.registered = true;
            }