示例#1
0
        public IntPtr SelectFont(WindowsFont font)
        {
            if (font.Equals(this.Font))
            {
                return(IntPtr.Zero);
            }
            IntPtr      ptr          = this.SelectObject(font.Hfont, GdiObjectType.Font);
            WindowsFont selectedFont = this.selectedFont;

            this.selectedFont = font;
            this.hCurrentFont = font.Hfont;
            if ((selectedFont != null) && MeasurementDCInfo.IsMeasurementDC(this))
            {
                selectedFont.Dispose();
            }
            if (MeasurementDCInfo.IsMeasurementDC(this))
            {
                if (ptr != IntPtr.Zero)
                {
                    MeasurementDCInfo.LastUsedFont = font;
                    return(ptr);
                }
                MeasurementDCInfo.Reset();
            }
            return(ptr);
        }
示例#2
0
        /// <summary>
        ///  Selects the specified object into the dc.  If the specified object is the same as the one currently selected
        ///  in the dc, the object is not set and a null value is returned.
        /// </summary>
        public IntPtr SelectFont(WindowsFont font)
        {
            // Fonts are one of the most expensive objects to select in an hdc and in many cases we are passed a Font that is the
            // same as the one already selected in the dc so to avoid a perf hit we get the hdc font's log font and compare it
            // with the one passed in before selecting it in the hdc.
            // Also, we avoid performing GDI operations that if done on an enhanced metafile DC would add an entry to it, hence
            // reducing the size of the metafile.
            if (font.Equals(Font))
            {
                return(IntPtr.Zero);
            }
            IntPtr result = SelectObject(font.Hfont, GdiObjectType.Font);

            WindowsFont previousFont = selectedFont;

            selectedFont = font;
            hCurrentFont = font.Hfont;

            // the measurement DC always leaves fonts selected for pref reasons.
            // in this case, we need to diposse the font since the original
            // creator didn't fully dispose.
            if (previousFont != null)
            {
                if (MeasurementDCInfo.IsMeasurementDC(this))
                {
                    previousFont.Dispose();
                }
            }

#if OPTIMIZED_MEASUREMENTDC
            // once we've changed the font, update the last used font.
            if (MeasurementDCInfo.IsMeasurementDC(this))
            {
                if (result != IntPtr.Zero)
                {
                    MeasurementDCInfo.LastUsedFont = font;
                }
                else
                {
                    // there was an error selecting the Font into the DC, we dont know anything about it.
                    MeasurementDCInfo.Reset();
                }
            }
#endif
            return(result);
        }
 public IntPtr SelectFont(WindowsFont font)
 {
     if (font.Equals(this.Font))
     {
         return IntPtr.Zero;
     }
     IntPtr ptr = this.SelectObject(font.Hfont, GdiObjectType.Font);
     WindowsFont selectedFont = this.selectedFont;
     this.selectedFont = font;
     this.hCurrentFont = font.Hfont;
     if ((selectedFont != null) && MeasurementDCInfo.IsMeasurementDC(this))
     {
         selectedFont.Dispose();
     }
     if (MeasurementDCInfo.IsMeasurementDC(this))
     {
         if (ptr != IntPtr.Zero)
         {
             MeasurementDCInfo.LastUsedFont = font;
             return ptr;
         }
         MeasurementDCInfo.Reset();
     }
     return ptr;
 }