Beispiel #1
0
        ///  GetTextMargins - checks to see if we have cached information about the current font,
        ///  returns info about it.
        ///  An MRU of Font margins was considered, but seems like overhead.
        internal static Interop.User32.DRAWTEXTPARAMS GetTextMargins(WindowsGraphics wg, WindowsFont font)
        {
            // PERF: operate on a local reference rather than party directly on the thread static one.
            CachedInfo currentCachedInfo = cachedMeasurementDCInfo;

            if (currentCachedInfo != null && currentCachedInfo.LeftTextMargin > 0 && currentCachedInfo.RightTextMargin > 0 && font == currentCachedInfo.LastUsedFont)
            {
                // we have to return clones as DrawTextEx will modify this struct
                return(new Interop.User32.DRAWTEXTPARAMS
                {
                    iLeftMargin = currentCachedInfo.LeftTextMargin,
                    iRightMargin = currentCachedInfo.RightTextMargin
                });
            }
            else if (currentCachedInfo == null)
            {
                currentCachedInfo       = new CachedInfo();
                cachedMeasurementDCInfo = currentCachedInfo;
            }
            Interop.User32.DRAWTEXTPARAMS drawTextParams = wg.GetTextMargins(font);
            currentCachedInfo.LeftTextMargin  = drawTextParams.iLeftMargin;
            currentCachedInfo.RightTextMargin = drawTextParams.iRightMargin;

            // returning a copy here to be consistent with the return value from the cache.
            return(new Interop.User32.DRAWTEXTPARAMS
            {
                iLeftMargin = currentCachedInfo.LeftTextMargin,
                iRightMargin = currentCachedInfo.RightTextMargin
            });
        }
Beispiel #2
0
        private int GetLeadingTextPaddingFromTextFormatFlags()
        {
            int iLeftMargin;

            if (!base.IsHandleCreated)
            {
                return(0);
            }
            if (this.UseCompatibleTextRendering && (this.FlatStyle != System.Windows.Forms.FlatStyle.System))
            {
                return(0);
            }
            using (WindowsGraphics graphics = WindowsGraphics.FromHwnd(base.Handle))
            {
                TextFormatFlags flags = this.CreateTextFormatFlags();
                if ((flags & TextFormatFlags.NoPadding) == TextFormatFlags.NoPadding)
                {
                    graphics.TextPadding = TextPaddingOptions.NoPadding;
                }
                else if ((flags & TextFormatFlags.LeftAndRightPadding) == TextFormatFlags.LeftAndRightPadding)
                {
                    graphics.TextPadding = TextPaddingOptions.LeftAndRightPadding;
                }
                using (WindowsFont font = WindowsGraphicsCacheManager.GetWindowsFont(this.Font))
                {
                    iLeftMargin = graphics.GetTextMargins(font).iLeftMargin;
                }
            }
            return(iLeftMargin);
        }
Beispiel #3
0
        public void Font_GetTextMargins(string family, float size, int left, int right)
        {
            using Font font = new Font(family, size);
            if (font.Name != family)
            {
                // Not installed on this machine
                return;
            }

            using WindowsFont windowsFont = WindowsFont.FromFont(font, Gdi32.QUALITY.CLEARTYPE);
            WindowsGraphics graphics = WindowsGraphicsCacheManager.MeasurementGraphics;

            User32.DRAWTEXTPARAMS margins = graphics.GetTextMargins(windowsFont);
            Assert.Equal(left, margins.iLeftMargin);
            Assert.Equal(right, margins.iRightMargin);
        }
Beispiel #4
0
       /// GetTextMargins - checks to see if we have cached information about the current font,
       /// returns info about it.
       /// An MRU of Font margins was considered, but seems like overhead.
       internal static IntNativeMethods.DRAWTEXTPARAMS GetTextMargins(WindowsGraphics wg, WindowsFont font) {

            // PERF: operate on a local reference rather than party directly on the thread static one.
            CachedInfo currentCachedInfo = cachedMeasurementDCInfo;
            
            if (currentCachedInfo != null && currentCachedInfo.LeftTextMargin >0 && currentCachedInfo.RightTextMargin >0 && font == currentCachedInfo.LastUsedFont) 
            {
                // we have to return clones as DrawTextEx will modify this struct
                return new IntNativeMethods.DRAWTEXTPARAMS(currentCachedInfo.LeftTextMargin,currentCachedInfo.RightTextMargin);                    
            }
            else if (currentCachedInfo == null) 
            {
                currentCachedInfo = new CachedInfo();
                cachedMeasurementDCInfo = currentCachedInfo;
            }
            IntNativeMethods.DRAWTEXTPARAMS drawTextParams = wg.GetTextMargins(font);
            currentCachedInfo.LeftTextMargin = drawTextParams.iLeftMargin;
            currentCachedInfo.RightTextMargin = drawTextParams.iRightMargin;
            
            // returning a copy here to be consistent with the return value from the cache.
            return new IntNativeMethods.DRAWTEXTPARAMS(currentCachedInfo.LeftTextMargin,currentCachedInfo.RightTextMargin);
            
       }