public Font GetFont(IDeviceContext dc, FontProperty prop)
        {
            if( dc == null ){
                throw new ArgumentNullException("dc");
            }
            
            //valid values are 0xa29 to 0xa29
            if (!ClientUtils.IsEnumValid(prop, (int)prop, (int)FontProperty.GlyphFont, (int)FontProperty.GlyphFont))
            {
                throw new InvalidEnumArgumentException("prop", (int)prop, typeof(FontProperty));
            }

            NativeMethods.LOGFONT logfont = new NativeMethods.LOGFONT();

            using( WindowsGraphicsWrapper wgr = new WindowsGraphicsWrapper( dc, AllGraphicsProperties ) ) {
                HandleRef hdc = new HandleRef( wgr, wgr.WindowsGraphics.DeviceContext.Hdc );
                lastHResult = SafeNativeMethods.GetThemeFont( new HandleRef( this, Handle ), hdc, part, state, (int) prop, logfont );
            }

            Font font = null;

            //check for a failed HR.
            if (NativeMethods.Succeeded(lastHResult)) {


                // SECREVIEW: Safe to assert here, since the logfont comes from a native api and not from the 
                //            caller of this method.  The system creates the font handle.
                //
                IntSecurity.ObjectFromWin32Handle.Assert();
                try {
                    font = Font.FromLogFont(logfont);
                }
                catch (Exception e) {
                    if (ClientUtils.IsSecurityOrCriticalException(e)) {
                        throw;
                    }

                    //Looks like the font was not true type
                    font = null;
                }
            }

            return font;
        }
Ejemplo n.º 2
0
        private static NativeMethods.FONTDESC GetFONTDESCFromFont(Font font) {
            NativeMethods.FONTDESC fdesc = null;

            if (fontTable == null) {
                fontTable = new Hashtable();
            }
            else {
                fdesc = (NativeMethods.FONTDESC)fontTable[font];
            }

            if (fdesc == null) {
                fdesc = new NativeMethods.FONTDESC();
                fdesc.lpstrName = font.Name;
                fdesc.cySize = (long)(font.SizeInPoints * 10000);
                NativeMethods.LOGFONT logfont = new NativeMethods.LOGFONT();
                font.ToLogFont(logfont);
                fdesc.sWeight = (short) logfont.lfWeight;
                fdesc.sCharset = logfont.lfCharSet;
                fdesc.fItalic = font.Italic;
                fdesc.fUnderline = font.Underline;
                fdesc.fStrikethrough = font.Strikeout;

                fontTable[font] = fdesc;
            }

            return fdesc;
        }
Ejemplo n.º 3
0
        /// <include file='doc\FontDialog.uex' path='docs/doc[@for="FontDialog.RunDialog"]/*' />
        /// <internalonly/>
        /// <devdoc>
        ///    <para>
        ///       The actual implementation of running the dialog. Inheriting classes
        ///       should override this if they want to add more functionality, and call
        ///       base.runDialog() if necessary
        ///       
        ///    </para>
        /// </devdoc>
        protected override bool RunDialog(IntPtr hWndOwner) {
            NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(this.HookProc);
            NativeMethods.CHOOSEFONT cf = new NativeMethods.CHOOSEFONT();
            IntPtr screenDC = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef);
            NativeMethods.LOGFONT lf = new NativeMethods.LOGFONT();

            Graphics graphics = Graphics.FromHdcInternal(screenDC);

            // SECREVIEW : The Font.ToLogFont method is marked with the 'unsafe' modifier cause it needs to write bytes into the logFont struct,
            //             here we are passing that struct so the assert is safe.
            //
            IntSecurity.ObjectFromWin32Handle.Assert();
            try {
                Font.ToLogFont(lf, graphics);
            }
            finally {
                CodeAccessPermission.RevertAssert();
                graphics.Dispose();
            }
            UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, screenDC));

            IntPtr logFontPtr = IntPtr.Zero;
            try {
                logFontPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(NativeMethods.LOGFONT)));
                Marshal.StructureToPtr(lf, logFontPtr, false);

                cf.lStructSize = Marshal.SizeOf(typeof(NativeMethods.CHOOSEFONT));
                cf.hwndOwner = hWndOwner;
                cf.hDC = IntPtr.Zero;
                cf.lpLogFont = logFontPtr;
                cf.Flags = Options | NativeMethods.CF_INITTOLOGFONTSTRUCT | NativeMethods.CF_ENABLEHOOK;
                if (minSize > 0 || maxSize > 0) {
                    cf.Flags |= NativeMethods.CF_LIMITSIZE;
                }

                //if ShowColor=true then try to draw the sample text in color,
                //if ShowEffects=false then we will draw the sample text in standard control text color regardless.
                //(limitation of windows control)
                //
                if (ShowColor || ShowEffects) {
                    cf.rgbColors = ColorTranslator.ToWin32(color);
                }
                else {
                    cf.rgbColors = ColorTranslator.ToWin32(SystemColors.ControlText);
                }

                cf.lpfnHook = hookProcPtr;
                cf.hInstance = UnsafeNativeMethods.GetModuleHandle(null);
                cf.nSizeMin = minSize;
                if (maxSize == 0) {
                    cf.nSizeMax = Int32.MaxValue;
                }
                else {
                    cf.nSizeMax = maxSize;
                }
                Debug.Assert(cf.nSizeMin <= cf.nSizeMax, "min and max font sizes are the wrong way around");
                if (!SafeNativeMethods.ChooseFont(cf)) return false;


                NativeMethods.LOGFONT lfReturned = null;
                lfReturned = (NativeMethods.LOGFONT)UnsafeNativeMethods.PtrToStructure(logFontPtr, typeof(NativeMethods.LOGFONT));

                if (lfReturned.lfFaceName != null && lfReturned.lfFaceName.Length > 0) {
                    lf = lfReturned;
                    UpdateFont(lf);
                    UpdateColor(cf.rgbColors);
                }

                return true;
            }
            finally {
                if (logFontPtr != IntPtr.Zero)
                    Marshal.FreeCoTaskMem(logFontPtr);
            }
        }
Ejemplo n.º 4
0
 protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam) {
     
     switch (msg) {
         case NativeMethods.WM_COMMAND:
             if ((int)wparam == 0x402) {
                 NativeMethods.LOGFONT lf = new NativeMethods.LOGFONT();
                 UnsafeNativeMethods.SendMessage(new HandleRef(null, hWnd), NativeMethods.WM_CHOOSEFONT_GETLOGFONT, 0, lf);
                 UpdateFont(lf);
                 int index = (int)UnsafeNativeMethods.SendDlgItemMessage(new HandleRef(null, hWnd), 0x473, NativeMethods.CB_GETCURSEL, IntPtr.Zero, IntPtr.Zero);
                 if (index != NativeMethods.CB_ERR) {
                     UpdateColor((int)UnsafeNativeMethods.SendDlgItemMessage(new HandleRef(null, hWnd), 0x473,
                                                                  NativeMethods.CB_GETITEMDATA, (IntPtr) index, IntPtr.Zero));
                 }
                 if (NativeWindow.WndProcShouldBeDebuggable) {
                     OnApply(EventArgs.Empty);
                 }
                 else {
                     try
                     {
                         OnApply(EventArgs.Empty);
                     }
                     catch (Exception e)
                     {
                         Application.OnThreadException(e);
                     }
                 }
             }
             break;
         case NativeMethods.WM_INITDIALOG:
             if (!showColor) {
                 IntPtr hWndCtl = UnsafeNativeMethods.GetDlgItem(new HandleRef(null, hWnd), NativeMethods.cmb4);
                 SafeNativeMethods.ShowWindow(new HandleRef(null, hWndCtl), NativeMethods.SW_HIDE);
                 hWndCtl = UnsafeNativeMethods.GetDlgItem(new HandleRef(null, hWnd), NativeMethods.stc4);
                 SafeNativeMethods.ShowWindow(new HandleRef(null, hWndCtl), NativeMethods.SW_HIDE);
             }
             break;
     }
     
     return base.HookProc(hWnd, msg, wparam, lparam);
 }
Ejemplo n.º 5
0
        // Returns whether or not target was changed
        internal static bool FontToIFont(Font source, UnsafeNativeMethods.IFont target) {
            bool changed = false; 

            // we need to go through all the pain of the diff here because
            // it looks like setting them all has different results based on the
            // order and each individual IFont implementor...
            //
            string fontName = target.GetName();
            if (!source.Name.Equals(fontName)) {
                target.SetName(source.Name);
                changed = true;
            }

            // [....], Review: this always seems to come back as
            // the point size * 10000 (HIMETRIC?), regardless
            // or ratio or mapping mode, and despite what
            // the documentation says...
            //
            // Either figure out what's going on here or
            // do the process that the windows forms FONT object does here
            // or, worse case, just create another Font object
            // from the handle, but that's pretty heavy...
            //
            float fontSize = (float)target.GetSize() / 10000;

            // size must be in points
            float winformsSize = source.SizeInPoints;
            if (winformsSize != fontSize) {
                target.SetSize((long)(winformsSize * 10000));
                changed = true;
            }

            NativeMethods.LOGFONT logfont = new NativeMethods.LOGFONT();

            IntSecurity.ObjectFromWin32Handle.Assert();
            try {
                source.ToLogFont(logfont);
            }
            finally {
                CodeAccessPermission.RevertAssert();
            }

            short fontWeight = target.GetWeight();
            if (fontWeight != logfont.lfWeight) {
                target.SetWeight((short)logfont.lfWeight);
                changed = true;
            }

            bool fontBold = target.GetBold();
            if (fontBold != (logfont.lfWeight >= 700)) {
                target.SetBold(logfont.lfWeight >= 700);
                changed = true;
            }

            bool fontItalic = target.GetItalic();
            if (fontItalic != (0 != logfont.lfItalic)) {
                target.SetItalic(0 != logfont.lfItalic);
                changed = true;
            }

            bool fontUnderline = target.GetUnderline();
            if (fontUnderline != (0 != logfont.lfUnderline)) {
                target.SetUnderline(0 != logfont.lfUnderline);
                changed = true;
            }

            bool fontStrike = target.GetStrikethrough();
            if (fontStrike != (0 != logfont.lfStrikeOut)) {
                target.SetStrikethrough(0 != logfont.lfStrikeOut);
                changed = true;
            }

            short fontCharset = target.GetCharset();
            if (fontCharset != logfont.lfCharSet) {
                target.SetCharset(logfont.lfCharSet);
                changed = true;
            }

            return changed;
        }
Ejemplo n.º 6
0
        private void SetCharFormatFont(bool selectionOnly, Font value) {
            ForceHandleCreate();
            NativeMethods.LOGFONT logfont = new NativeMethods.LOGFONT();

            FontToLogFont(value, logfont);

            byte[] bytesFaceName;

            int dwMask = RichTextBoxConstants.CFM_FACE | RichTextBoxConstants.CFM_SIZE | RichTextBoxConstants.CFM_BOLD |
                RichTextBoxConstants.CFM_ITALIC | RichTextBoxConstants.CFM_STRIKEOUT | RichTextBoxConstants.CFM_UNDERLINE |
                RichTextBoxConstants.CFM_CHARSET;

            int dwEffects = 0;
            if (value.Bold) dwEffects |= RichTextBoxConstants.CFE_BOLD;
            if (value.Italic) dwEffects |= RichTextBoxConstants.CFE_ITALIC;
            if (value.Strikeout) dwEffects |= RichTextBoxConstants.CFE_STRIKEOUT;
            if (value.Underline) dwEffects |= RichTextBoxConstants.CFE_UNDERLINE;

            if (Marshal.SystemDefaultCharSize == 1)            
            {
                bytesFaceName = Encoding.Default.GetBytes(logfont.lfFaceName);

                NativeMethods.CHARFORMATA cfA = new NativeMethods.CHARFORMATA();
                for (int i=0; i<bytesFaceName.Length; i++) cfA.szFaceName[i] = bytesFaceName[i];
                cfA.dwMask = dwMask;
                cfA.dwEffects = dwEffects;
                cfA.yHeight = (int) (value.SizeInPoints * 20);
                cfA.bCharSet = logfont.lfCharSet;
                cfA.bPitchAndFamily = logfont.lfPitchAndFamily;

                UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), RichTextBoxConstants.EM_SETCHARFORMAT, selectionOnly ? RichTextBoxConstants.SCF_SELECTION : RichTextBoxConstants.SCF_ALL, cfA);
            }
            else
            {
                bytesFaceName = Encoding.Unicode.GetBytes(logfont.lfFaceName);

                NativeMethods.CHARFORMATW cfW = new NativeMethods.CHARFORMATW();
                for (int i=0; i<bytesFaceName.Length; i++) cfW.szFaceName[i] = bytesFaceName[i];
                cfW.dwMask = dwMask;
                cfW.dwEffects = dwEffects;
                cfW.yHeight = (int) (value.SizeInPoints * 20);
                cfW.bCharSet = logfont.lfCharSet;
                cfW.bPitchAndFamily = logfont.lfPitchAndFamily;

                UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), RichTextBoxConstants.EM_SETCHARFORMAT, selectionOnly ? RichTextBoxConstants.SCF_SELECTION : RichTextBoxConstants.SCF_ALL, cfW);
            }
        }