コード例 #1
0
        /// <summary>
        ///  Creates the font handle.
        /// </summary>
        private unsafe WindowsFont(NativeMethods.LOGFONTW logFont, FontStyle style, bool createHandle)
        {
            Debug.Assert(Hfont == IntPtr.Zero, "hFont is not null, this will generate a handle leak.");

            _logFont = logFont;
            if (_logFont.FaceName.Length == 0)
            {
                _logFont.FaceName = DefaultFaceName;
            }
            Style = style;

            if (createHandle)
            {
                Hfont = IntUnsafeNativeMethods.CreateFontIndirectW(ref _logFont);

                if (Hfont == IntPtr.Zero)
                {
                    _logFont.FaceName       = DefaultFaceName;
                    _logFont.lfOutPrecision = IntNativeMethods.OUT_TT_ONLY_PRECIS; // TrueType only.

                    Hfont = IntUnsafeNativeMethods.CreateFontIndirectW(ref _logFont);
                }

                // Update logFont height and other adjusted parameters.
                IntUnsafeNativeMethods.GetObjectW(new HandleRef(this, Hfont), sizeof(NativeMethods.LOGFONTW), ref _logFont);

                // We created the hFont, we will delete it on dispose.
                _ownHandle = true;
            }
        }