Пример #1
0
        /// <summary></summary>
        /// <param name="control"></param>
        /// <param name="font"></param>
        /// <returns></returns>
        public static PointF GetFontMetricsByFont(Control control, Font font)
        {
            PointF retPoint;

            lock (_metricsCache)
            {
                if (!_metricsCache.TryGetValue(font, out retPoint))
                {
#if PocketPC
                    // In PocketPC, Panels don't have CreateGraphics. We go up the parents
                    // tree until we find a non-panel one.
                    while (control is Panel)
                    {
                        control = control.Parent;
                    }
#endif
                    using (Graphics g = control.CreateGraphics())
                    {
                        IntPtr hFont = FontHandlesCache.GetInstance().Get(font).FontHandle;
                        GetTextMetrics(g, hFont, font.Name, (int)font.Size, out retPoint);
                    }

                    _metricsCache.Add(font, retPoint);
                }
            }

            return(retPoint);
        }
Пример #2
0
        /// <summary>return the size of the string according to the font</summary>
        /// <param name="font"></param>
        /// <param name="str"></param>
        /// <param name="control"></param>
        /// <returns></returns>
        public static Size GetTextExt(Font font, String str, Control control)
        {
            Size size = new Size();

            if (control != null && str != null)
            {
#if PocketPC
                Form form = (Form)((control is Form)? control : control.TopLevelControl);
                using (Graphics g = form.CreateGraphics())
                {
                    // Mobile does not support GetTextExtentPoint32 or similar functions
                    SizeF sizef = g.MeasureString(str, font);
                    size.Width  = (int)Math.Ceiling(sizef.Width);
                    size.Height = (int)Math.Ceiling(sizef.Height);
                }
#else
                using (Graphics g = control.CreateGraphics())
                {
                    IntPtr hdc      = g.GetHdc();
                    IntPtr hFont    = FontHandlesCache.GetInstance().Get(font).FontHandle;
                    IntPtr hFontOld = (IntPtr)NativeWindowCommon.SelectObject(hdc, hFont);
                    NativeWindowCommon.SIZE nativeSize = new NativeWindowCommon.SIZE();
                    NativeWindowCommon.GetTextExtentPoint32(hdc, str, str.Length, out nativeSize);
                    size.Width  = nativeSize.cx; //(int)Math.Ceiling(size.cx);
                    size.Height = nativeSize.cy; //(int)Math.Ceiling(size.cy);
                    NativeWindowCommon.SelectObject(hdc, hFontOld);
                    g.ReleaseHdc(hdc);
                }
#endif
            }

            return(size);
        }
Пример #3
0
 public FontDescription(LOGFONT logFont)
 {
     this.LogFont        = logFont;
     fontHandleContainer = FontHandlesCache.GetInstance().Get(logFont);
 }
Пример #4
0
 public FontDescription(Font font)
 {
     LogFont = new LOGFONT();
     font.ToLogFont(LogFont);
     fontHandleContainer = FontHandlesCache.GetInstance().Get(font);
 }