スタイルを変更せずにRichTextBox.SelectionFont FontFamilyを設定するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1632951

  •  06-07-2019
  •  | 
  •  

質問

アプリケーションのコントロールの1つは、ユーザーがテキストのフォントスタイル(B、I、U)と色のみを変更できるように制限しています。この目的のためにRichTextBoxから継承するカスタムコントロールを作成しました。 CTRL-Vをインターセプトし、貼り付けたテキストのフォントを SystemFonts.DefaultFont に設定できます。私が現在直面している問題は、貼り付けられたテキストに、たとえば、太字の半分と通常のスタイルが含まれている場合です-太字は失われます。

つまり" Foo バー" 「Foo Bar」として貼り付けます。

現在、私の唯一のアイデアは、文字単位でテキストを(非常に遅い)通過し、次のようなことをすることです:

public class MyRichTextBox : RichTextBox
{

private RichTextBox hiddenBuffer = new RichTextBox();

/// <summary>
/// This paste will strip the font size, family and alignment from the text being pasted.
/// </summary>
public void PasteUnformatted()
{
    this.hiddenBuffer.Clear();
    this.hiddenBuffer.Paste();

    for (int x = 0; x < this.hiddenBuffer.TextLength; x++)
    {
        // select the next character
        this.hiddenBuffer.Select(x, 1);

        // Set the font family and size to default
        this.hiddenBuffer.SelectionFont = new Font(SystemFonts.DefaultFont.FontFamily, SystemFonts.DefaultFont.Size, this.hiddenBuffer.SelectionFont.Style);
    }

    // Reset the alignment
    this.hiddenBuffer.SelectionAlignment = HorizontalAlignment.Left;

    base.SelectedRtf = this.hiddenBuffer.SelectedRtf;
    this.hiddenBuffer.Clear();
}

}

誰もがよりクリーンな(そしてより高速な)ソリューションを思い浮かべますか?

役に立ちましたか?

解決

MSDNフォーラムの「nobugz」がこれに答えてくれました(すぐに答えが必要だったので、SOからのタンブルウィードのほぼ1日後、他の場所を見る必要がありました-私を判断しないでください!):

using System.Runtime.InteropServices;
...
        public static bool SetRtbFace(RichTextBox rtb, Font font, bool selectionOnly) {
            CHARFORMATW fmt = new CHARFORMATW();
            fmt.cbSize = Marshal.SizeOf(fmt);
            fmt.szFaceName = font.FontFamily.Name;
            fmt.dwMask = 0x20000000;  // CFM_FACE
            return IntPtr.Zero != SendMessage(rtb.Handle, 0x444, (IntPtr)(selectionOnly ? 1 : 4), fmt);
        }
        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        private class CHARFORMATW {
            public int cbSize;
            public int dwMask;
            public int dwEffects;
            public int yHeight;
            public int yOffset;
            public int crTextColor;
            public byte bCharSet;
            public byte bPitchAndFamily;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x40)]
            public string szFaceName;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, CHARFORMATW lParam);

他のヒント

Delphiの回答が必要な場合は、基本的なアイデアを得るための抜粋:

using RichEdit; //reqd. for the constants and types

var
  chformat : TCharFormat2;
  fontname : string;

begin
  FillChar(chformat,sizeof(chformat),0);
  chformat.cbSize := sizeof(chformat);
  //only modify the szFaceName field, height etc. left alone
  chformat.dwMask :=  CFM_FACE; 
  //get the fontname set by the user
  fontname := AdvFontSelector1.Text;
  strpcopy(chformat.szFaceName,fontname);
  RichEdit1.Perform(EM_SETCHARFORMAT, SCF_SELECTION, lparam(@chformat));
end;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top