Ejemplo n.º 1
0
        private void UpdateFont(System.Windows.Forms.NativeMethods.LOGFONT lf)
        {
            IntPtr dC = System.Windows.Forms.UnsafeNativeMethods.GetDC(System.Windows.Forms.NativeMethods.NullHandleRef);

            try
            {
                using (System.Drawing.Font font = null)
                {
                    System.Windows.Forms.IntSecurity.UnmanagedCode.Assert();
                    try
                    {
                        font = System.Drawing.Font.FromLogFont(lf, dC);
                    }
                    finally
                    {
                        CodeAccessPermission.RevertAssert();
                    }
                    this.font = ControlPaint.FontInPoints(font);
                }
            }
            finally
            {
                System.Windows.Forms.UnsafeNativeMethods.ReleaseDC(System.Windows.Forms.NativeMethods.NullHandleRef, new HandleRef(null, dC));
            }
        }
        protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
        {
            switch (msg)
            {
                case 0x110:
                    if (!this.showColor)
                    {
                        IntPtr dlgItem = System.Windows.Forms.UnsafeNativeMethods.GetDlgItem(new HandleRef(null, hWnd), 0x473);
                        System.Windows.Forms.SafeNativeMethods.ShowWindow(new HandleRef(null, dlgItem), 0);
                        dlgItem = System.Windows.Forms.UnsafeNativeMethods.GetDlgItem(new HandleRef(null, hWnd), 0x443);
                        System.Windows.Forms.SafeNativeMethods.ShowWindow(new HandleRef(null, dlgItem), 0);
                    }
                    break;

                case 0x111:
                    if (((int) wparam) == 0x402)
                    {
                        System.Windows.Forms.NativeMethods.LOGFONT lParam = new System.Windows.Forms.NativeMethods.LOGFONT();
                        System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(null, hWnd), 0x401, 0, lParam);
                        this.UpdateFont(lParam);
                        int num = (int) System.Windows.Forms.UnsafeNativeMethods.SendDlgItemMessage(new HandleRef(null, hWnd), 0x473, 0x147, IntPtr.Zero, IntPtr.Zero);
                        if (num != -1)
                        {
                            this.UpdateColor((int) System.Windows.Forms.UnsafeNativeMethods.SendDlgItemMessage(new HandleRef(null, hWnd), 0x473, 0x150, (IntPtr) num, IntPtr.Zero));
                        }
                        if (NativeWindow.WndProcShouldBeDebuggable)
                        {
                            this.OnApply(EventArgs.Empty);
                        }
                        else
                        {
                            try
                            {
                                this.OnApply(EventArgs.Empty);
                            }
                            catch (Exception exception)
                            {
                                Application.OnThreadException(exception);
                            }
                        }
                    }
                    break;
            }
            return base.HookProc(hWnd, msg, wparam, lparam);
        }
Ejemplo n.º 3
0
        protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
        {
            switch (msg)
            {
            case 0x110:
                if (!this.showColor)
                {
                    IntPtr dlgItem = System.Windows.Forms.UnsafeNativeMethods.GetDlgItem(new HandleRef(null, hWnd), 0x473);
                    System.Windows.Forms.SafeNativeMethods.ShowWindow(new HandleRef(null, dlgItem), 0);
                    dlgItem = System.Windows.Forms.UnsafeNativeMethods.GetDlgItem(new HandleRef(null, hWnd), 0x443);
                    System.Windows.Forms.SafeNativeMethods.ShowWindow(new HandleRef(null, dlgItem), 0);
                }
                break;

            case 0x111:
                if (((int)wparam) == 0x402)
                {
                    System.Windows.Forms.NativeMethods.LOGFONT lParam = new System.Windows.Forms.NativeMethods.LOGFONT();
                    System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(null, hWnd), 0x401, 0, lParam);
                    this.UpdateFont(lParam);
                    int num = (int)System.Windows.Forms.UnsafeNativeMethods.SendDlgItemMessage(new HandleRef(null, hWnd), 0x473, 0x147, IntPtr.Zero, IntPtr.Zero);
                    if (num != -1)
                    {
                        this.UpdateColor((int)System.Windows.Forms.UnsafeNativeMethods.SendDlgItemMessage(new HandleRef(null, hWnd), 0x473, 0x150, (IntPtr)num, IntPtr.Zero));
                    }
                    if (NativeWindow.WndProcShouldBeDebuggable)
                    {
                        this.OnApply(EventArgs.Empty);
                    }
                    else
                    {
                        try
                        {
                            this.OnApply(EventArgs.Empty);
                        }
                        catch (Exception exception)
                        {
                            Application.OnThreadException(exception);
                        }
                    }
                }
                break;
            }
            return(base.HookProc(hWnd, msg, wparam, lparam));
        }
Ejemplo n.º 4
0
        public Font GetFont(IDeviceContext dc, FontProperty prop)
        {
            if (dc == null)
            {
                throw new ArgumentNullException("dc");
            }
            if (!System.Windows.Forms.ClientUtils.IsEnumValid(prop, (int)prop, 0xa29, 0xa29))
            {
                throw new InvalidEnumArgumentException("prop", (int)prop, typeof(FontProperty));
            }
            System.Windows.Forms.NativeMethods.LOGFONT pFont = new System.Windows.Forms.NativeMethods.LOGFONT();
            using (WindowsGraphicsWrapper wrapper = new WindowsGraphicsWrapper(dc, TextFormatFlags.PreserveGraphicsTranslateTransform | TextFormatFlags.PreserveGraphicsClipping))
            {
                HandleRef hdc = new HandleRef(wrapper, wrapper.WindowsGraphics.DeviceContext.Hdc);
                this.lastHResult = System.Windows.Forms.SafeNativeMethods.GetThemeFont(new HandleRef(this, this.Handle), hdc, this.part, this.state, (int)prop, pFont);
            }
            Font font = null;

            if (!System.Windows.Forms.NativeMethods.Succeeded(this.lastHResult))
            {
                return(font);
            }
            System.Windows.Forms.IntSecurity.ObjectFromWin32Handle.Assert();
            try
            {
                return(Font.FromLogFont(pFont));
            }
            catch (Exception exception)
            {
                if (System.Windows.Forms.ClientUtils.IsSecurityOrCriticalException(exception))
                {
                    throw;
                }
                return(null);
            }
        }
 internal static bool FontToIFont(Font source, System.Windows.Forms.UnsafeNativeMethods.IFont target)
 {
     bool flag = false;
     string name = target.GetName();
     if (!source.Name.Equals(name))
     {
         target.SetName(source.Name);
         flag = true;
     }
     float num = ((float) target.GetSize()) / 10000f;
     float sizeInPoints = source.SizeInPoints;
     if (sizeInPoints != num)
     {
         target.SetSize((long) (sizeInPoints * 10000f));
         flag = true;
     }
     System.Windows.Forms.NativeMethods.LOGFONT logFont = new System.Windows.Forms.NativeMethods.LOGFONT();
     System.Windows.Forms.IntSecurity.ObjectFromWin32Handle.Assert();
     try
     {
         source.ToLogFont(logFont);
     }
     finally
     {
         CodeAccessPermission.RevertAssert();
     }
     if (target.GetWeight() != logFont.lfWeight)
     {
         target.SetWeight((short) logFont.lfWeight);
         flag = true;
     }
     if (target.GetBold() != (logFont.lfWeight >= 700))
     {
         target.SetBold(logFont.lfWeight >= 700);
         flag = true;
     }
     if (target.GetItalic() != (0 != logFont.lfItalic))
     {
         target.SetItalic(0 != logFont.lfItalic);
         flag = true;
     }
     if (target.GetUnderline() != (0 != logFont.lfUnderline))
     {
         target.SetUnderline(0 != logFont.lfUnderline);
         flag = true;
     }
     if (target.GetStrikethrough() != (0 != logFont.lfStrikeOut))
     {
         target.SetStrikethrough(0 != logFont.lfStrikeOut);
         flag = true;
     }
     if (target.GetCharset() != logFont.lfCharSet)
     {
         target.SetCharset(logFont.lfCharSet);
         flag = true;
     }
     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;
 }
 private static System.Windows.Forms.NativeMethods.FONTDESC GetFONTDESCFromFont(System.Drawing.Font font)
 {
     System.Windows.Forms.NativeMethods.FONTDESC fontdesc = null;
     if (fontTable == null)
     {
         fontTable = new Hashtable();
     }
     else
     {
         fontdesc = (System.Windows.Forms.NativeMethods.FONTDESC) fontTable[font];
     }
     if (fontdesc == null)
     {
         fontdesc = new System.Windows.Forms.NativeMethods.FONTDESC {
             lpstrName = font.Name,
             cySize = (long) (font.SizeInPoints * 10000f)
         };
         System.Windows.Forms.NativeMethods.LOGFONT logFont = new System.Windows.Forms.NativeMethods.LOGFONT();
         font.ToLogFont(logFont);
         fontdesc.sWeight = (short) logFont.lfWeight;
         fontdesc.sCharset = logFont.lfCharSet;
         fontdesc.fItalic = font.Italic;
         fontdesc.fUnderline = font.Underline;
         fontdesc.fStrikethrough = font.Strikeout;
         fontTable[font] = fontdesc;
     }
     return fontdesc;
 }
 public Font GetFont(IDeviceContext dc, FontProperty prop)
 {
     if (dc == null)
     {
         throw new ArgumentNullException("dc");
     }
     if (!System.Windows.Forms.ClientUtils.IsEnumValid(prop, (int) prop, 0xa29, 0xa29))
     {
         throw new InvalidEnumArgumentException("prop", (int) prop, typeof(FontProperty));
     }
     System.Windows.Forms.NativeMethods.LOGFONT pFont = new System.Windows.Forms.NativeMethods.LOGFONT();
     using (WindowsGraphicsWrapper wrapper = new WindowsGraphicsWrapper(dc, TextFormatFlags.PreserveGraphicsTranslateTransform | TextFormatFlags.PreserveGraphicsClipping))
     {
         HandleRef hdc = new HandleRef(wrapper, wrapper.WindowsGraphics.DeviceContext.Hdc);
         this.lastHResult = System.Windows.Forms.SafeNativeMethods.GetThemeFont(new HandleRef(this, this.Handle), hdc, this.part, this.state, (int) prop, pFont);
     }
     Font font = null;
     if (!System.Windows.Forms.NativeMethods.Succeeded(this.lastHResult))
     {
         return font;
     }
     System.Windows.Forms.IntSecurity.ObjectFromWin32Handle.Assert();
     try
     {
         return Font.FromLogFont(pFont);
     }
     catch (Exception exception)
     {
         if (System.Windows.Forms.ClientUtils.IsSecurityOrCriticalException(exception))
         {
             throw;
         }
         return null;
     }
 }
Ejemplo n.º 9
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);
        }
 public static extern int GetThemeFont(HandleRef hTheme, HandleRef hdc, int iPartId, int iStateId, int iPropId, System.Windows.Forms.NativeMethods.LOGFONT pFont);
 private void SetCharFormatFont(bool selectionOnly, System.Drawing.Font value)
 {
     byte[] bytes;
     this.ForceHandleCreate();
     System.Windows.Forms.NativeMethods.LOGFONT logfont = new System.Windows.Forms.NativeMethods.LOGFONT();
     FontToLogFont(value, logfont);
     int num = -1476394993;
     int num2 = 0;
     if (value.Bold)
     {
         num2 |= 1;
     }
     if (value.Italic)
     {
         num2 |= 2;
     }
     if (value.Strikeout)
     {
         num2 |= 8;
     }
     if (value.Underline)
     {
         num2 |= 4;
     }
     if (Marshal.SystemDefaultCharSize == 1)
     {
         bytes = Encoding.Default.GetBytes(logfont.lfFaceName);
         System.Windows.Forms.NativeMethods.CHARFORMATA lParam = new System.Windows.Forms.NativeMethods.CHARFORMATA();
         for (int i = 0; i < bytes.Length; i++)
         {
             lParam.szFaceName[i] = bytes[i];
         }
         lParam.dwMask = num;
         lParam.dwEffects = num2;
         lParam.yHeight = (int) (value.SizeInPoints * 20f);
         lParam.bCharSet = logfont.lfCharSet;
         lParam.bPitchAndFamily = logfont.lfPitchAndFamily;
         System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, base.Handle), 0x444, selectionOnly ? 1 : 4, lParam);
     }
     else
     {
         bytes = Encoding.Unicode.GetBytes(logfont.lfFaceName);
         System.Windows.Forms.NativeMethods.CHARFORMATW charformatw = new System.Windows.Forms.NativeMethods.CHARFORMATW();
         for (int j = 0; j < bytes.Length; j++)
         {
             charformatw.szFaceName[j] = bytes[j];
         }
         charformatw.dwMask = num;
         charformatw.dwEffects = num2;
         charformatw.yHeight = (int) (value.SizeInPoints * 20f);
         charformatw.bCharSet = logfont.lfCharSet;
         charformatw.bPitchAndFamily = logfont.lfPitchAndFamily;
         System.Windows.Forms.UnsafeNativeMethods.SendMessage(new HandleRef(this, base.Handle), 0x444, selectionOnly ? 1 : 4, charformatw);
     }
 }