字体样式代码c#到c++

FontStyle code c# to c++

本文关键字:c++ 代码 样式 字体      更新时间:2023-10-16

我想让我的粗体,斜体,下划线按钮在richtextbox中相互协作。我找到了一个例子,但它是用c#写的。请帮助,它如何在c++中工作:

if(e.Button==toolBarButton1)
{
    int fStyle = (int)FontStyle.Bold + (int)FontStyle.Italic;
    FontStyle f = (FontStyle)fStyle;
    richTextBox1.SelectionFont = 
        new Font(richTextBox1.Font.Name, richTextBox1.Font.Size, f);
}

我的版本,我想重制它

     if ( RichTextBox1->SelectionFont != nullptr )
   {
      System::Drawing::Font^ currentFont = RichTextBox1->SelectionFont;
      System::Drawing::FontStyle newFontStyle;
      if ( RichTextBox1->SelectionFont->Bold == true )
      {
         newFontStyle = FontStyle::Regular;
      }
      else
      {
         newFontStyle = FontStyle::Bold;
      }
      RichTextBox1->SelectionFont = gcnew System::Drawing::Font( currentFont->FontFamily,currentFont->Size,newFontStyle );

如果您想直接将c#转换为c++/CLI,只需使用:

if (e->Button == toolBarButton1)
{
    int fStyle = safe_cast<int>(FontStyle::Bold) + safe_cast<int>(FontStyle::Italic);
    FontStyle ^f = safe_cast<FontStyle^>(fStyle);
    richTextBox1->SelectionFont = gcnew Font(richTextBox1::Font->Name, richTextBox1::Font::Size, f);
}