コード例 #1
0
        private void SetFontFormatProperties(Interop.CHARFORMAT2 charFormat, Format format)
        {
            if ((charFormat.dwMask & RichEditConstants.CFM_FACE) == 0)
            {
                return;
            }

            string _familyName = charFormat.lfFaceName;
            float  _emSize     = 13f;

            if ((charFormat.dwMask & RichEditConstants.CFM_CHARSET) != 0)
            {
                _emSize = ((charFormat.yHeight) / 20f);

                if ((_emSize == 0f) && (charFormat.yHeight > 0))
                {
                    _emSize = 1f;
                }
            }

            FontStyle _style = FontStyle.Regular;

            if (((charFormat.dwMask & RichEditConstants.CFM_BOLD) != 0) &&
                ((charFormat.dwEffects & RichEditConstants.CFE_BOLD) != 0))
            {
                _style |= FontStyle.Bold;
            }

            if (((charFormat.dwMask & RichEditConstants.CFM_ITALIC) != 0) &&
                ((charFormat.dwEffects & RichEditConstants.CFE_ITALIC) != 0))
            {
                _style |= FontStyle.Italic;
            }

            if (((charFormat.dwMask & RichEditConstants.CFM_STRIKEOUT) != 0) &&
                ((charFormat.dwEffects & RichEditConstants.CFE_STRIKEOUT) != 0))
            {
                _style |= FontStyle.Strikeout;
            }

            if (((charFormat.dwMask & RichEditConstants.CFM_UNDERLINE) != 0) &&
                ((charFormat.dwEffects & RichEditConstants.CFE_UNDERLINE) != 0))
            {
                _style |= FontStyle.Underline;
            }

            try
            {
                format.Font = new Font(_familyName, _emSize, _style, GraphicsUnit.Point, charFormat.bCharSet);
            }
            catch
            {
            }
        }
コード例 #2
0
        private Format GetSelectionFormat()
        {
            Format _format = new Format();

            Interop.CHARFORMAT2 _charFormat = new Interop.CHARFORMAT2();
            _charFormat.cbSize = Marshal.SizeOf(_charFormat);

            Interop.SendMessage(Handle, RichEditConstants.EM_GETCHARFORMAT, RichEditConstants.SCF_SELECTION,
                                ref _charFormat);

            if ((_charFormat.dwMask & RichEditConstants.CFM_BACKCOLOR) != 0)
            {
                _format.BackColor = ColorTranslator.FromWin32(_charFormat.crBackColor);
            }

            if ((_charFormat.dwMask & RichEditConstants.CFM_COLOR) != 0)
            {
                _format.ForeColor = ColorTranslator.FromWin32(_charFormat.crTextColor);
            }

            SetFontFormatProperties(_charFormat, _format);

            if ((_charFormat.dwMask & RichEditConstants.CFM_BOLD) != 0)
            {
                _format.Bold = (_charFormat.dwEffects & RichEditConstants.CFE_BOLD) != 0;
            }

            if ((_charFormat.dwMask & RichEditConstants.CFM_ITALIC) != 0)
            {
                _format.Italic = (_charFormat.dwEffects & RichEditConstants.CFE_ITALIC) != 0;
            }

            if ((_charFormat.dwMask & RichEditConstants.CFM_STRIKEOUT) != 0)
            {
                _format.Strikethrough = (_charFormat.dwEffects & RichEditConstants.CFE_STRIKEOUT) != 0;
            }

            if ((_charFormat.dwMask & RichEditConstants.CFM_PROTECTED) != 0)
            {
                _format.Protected = (_charFormat.dwEffects & RichEditConstants.CFE_PROTECTED) != 0;
            }

            if ((_charFormat.dwMask & RichEditConstants.CFM_HIDDEN) != 0)
            {
                _format.Protected = (_charFormat.dwEffects & RichEditConstants.CFE_HIDDEN) != 0;
            }

            if ((_charFormat.dwMask & RichEditConstants.CFM_UNDERLINETYPE) != 0)
            {
                _format.UnderlineFormat = UnderlineTypeToUnderlineFormat(_charFormat.bUnderlineType);
            }

            return(_format);
        }
コード例 #3
0
        private void SetFontFormatProperties(Format format, ref Interop.CHARFORMAT2 charFormat)
        {
            Font _font = format.Font;

            Interop.LOGFONT logfont = new Interop.LOGFONT();
            _font.ToLogFont(logfont);

            uint _mask = RichEditConstants.CFM_SIZE | RichEditConstants.CFM_FACE |
                         RichEditConstants.CFM_BOLD | RichEditConstants.CFM_ITALIC | RichEditConstants.CFM_UNDERLINE |
                         RichEditConstants.CFM_STRIKEOUT;

            int _effects = 0;

            if (_font.Bold)
            {
                _effects |= RichEditConstants.CFE_BOLD;
            }

            if (_font.Italic)
            {
                _effects |= RichEditConstants.CFE_ITALIC;
            }

            if (_font.Strikeout)
            {
                _effects |= RichEditConstants.CFE_STRIKEOUT;
            }

            if (_font.Underline)
            {
                _effects |= RichEditConstants.CFE_UNDERLINE;
            }

            charFormat.dwMask          = _mask;
            charFormat.dwEffects       = (uint)_effects;
            charFormat.lfFaceName      = logfont.lfFaceName;
            charFormat.yHeight         = ((int)(_font.SizeInPoints * 20f));
            charFormat.bCharSet        = logfont.lfCharSet;
            charFormat.bPitchAndFamily = logfont.lfPitchAndFamily;
        }