Exemple #1
0
 public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, ref NativeMethods.CHARFORMATW lParam);
        private void SetCharFormatFont(bool selectionOnly, Font value) {
            ForceHandleCreate();
            NativeMethods.LOGFONT logfont = new NativeMethods.LOGFONT();

            FontToLogFont(value, logfont);

            byte[] bytesFaceName;

            int dwMask = RichTextBoxConstants.CFM_FACE | RichTextBoxConstants.CFM_SIZE | RichTextBoxConstants.CFM_BOLD |
                RichTextBoxConstants.CFM_ITALIC | RichTextBoxConstants.CFM_STRIKEOUT | RichTextBoxConstants.CFM_UNDERLINE |
                RichTextBoxConstants.CFM_CHARSET;

            int dwEffects = 0;
            if (value.Bold) dwEffects |= RichTextBoxConstants.CFE_BOLD;
            if (value.Italic) dwEffects |= RichTextBoxConstants.CFE_ITALIC;
            if (value.Strikeout) dwEffects |= RichTextBoxConstants.CFE_STRIKEOUT;
            if (value.Underline) dwEffects |= RichTextBoxConstants.CFE_UNDERLINE;

            if (Marshal.SystemDefaultCharSize == 1)            
            {
                bytesFaceName = Encoding.Default.GetBytes(logfont.lfFaceName);

                NativeMethods.CHARFORMATA cfA = new NativeMethods.CHARFORMATA();
                for (int i=0; i<bytesFaceName.Length; i++) cfA.szFaceName[i] = bytesFaceName[i];
                cfA.dwMask = dwMask;
                cfA.dwEffects = dwEffects;
                cfA.yHeight = (int) (value.SizeInPoints * 20);
                cfA.bCharSet = logfont.lfCharSet;
                cfA.bPitchAndFamily = logfont.lfPitchAndFamily;

                UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), RichTextBoxConstants.EM_SETCHARFORMAT, selectionOnly ? RichTextBoxConstants.SCF_SELECTION : RichTextBoxConstants.SCF_ALL, cfA);
            }
            else
            {
                bytesFaceName = Encoding.Unicode.GetBytes(logfont.lfFaceName);

                NativeMethods.CHARFORMATW cfW = new NativeMethods.CHARFORMATW();
                for (int i=0; i<bytesFaceName.Length; i++) cfW.szFaceName[i] = bytesFaceName[i];
                cfW.dwMask = dwMask;
                cfW.dwEffects = dwEffects;
                cfW.yHeight = (int) (value.SizeInPoints * 20);
                cfW.bCharSet = logfont.lfCharSet;
                cfW.bPitchAndFamily = logfont.lfPitchAndFamily;

                UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), RichTextBoxConstants.EM_SETCHARFORMAT, selectionOnly ? RichTextBoxConstants.SCF_SELECTION : RichTextBoxConstants.SCF_ALL, cfW);
            }
        }