font italic and bold

Hi,

I'm having trouble changing fonts to bold nad/or italics. I have the following which works for font type,size and color:

System::Drawing::Font *drawFont = new System::Drawing::Font(font_type,font_size);

Color myColor = Color::FromArgb((int)r,(int)g,(int)b);

SolidBrush *drawBrush = new SolidBrush(myColor);

ev->Graphics->DrawString(text_data.c_str(),drawFont,drawBrush, x, y);

However I cannot change to bold or italics.

Any suggestions will be appreciated.

Thanks


Answer this question

font italic and bold

  • Arcadian

    You'll probably kick yourself, because the answer seems so simple...

    System::Drawing::Font ^drawFont = gcnew System::Drawing::Font(_T("Arial"), 10, System::Drawing::FontStyle::Italic | System::Drawing::FontStyle::Bold);

    (sorry about the example being in C++/CLI).



  • vineeth2005

    Thanks a lot. It works for Bold or Italic individually. However the logial OR doesn't work on the font class. Am I doing something wrong

    Thanks

  • shakhawat adnan

    Thanks again. It works now. I had to cast it to (System::Drawing::FontStyle) type.



  • Alex118

    I don't know how it is for /clr:OldSyntax, but it works for C++/CLI. Try casting them to ints and see if that works.

  • alberich

    Code Snippet

    Public Class Form1
    Dim R As New Rectangle(50, 50, 200, 30)
    Dim CB1 As New CheckBox
    Dim CB2 As New CheckBox
    Dim F As New Font("Arial", 18, FontStyle.Regular)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Controls.Add(CB1)
    Me.Controls.Add(CB2)
    AddHandler CB1.CheckedChanged, AddressOf DrawText
    AddHandler CB2.CheckedChanged, AddressOf DrawText
    CB1.SetBounds(50, 100, 100, 100)
    CB2.SetBounds(150, 100, 100, 100)
    CB1.Text = "Make Bold"
    CB2.Text = "Make Italic"
    End Sub
    Private Sub DrawText(ByVal sender As Object, ByVal e As System.EventArgs)
    If CB1.Checked And Not CB2.Checked Then
    F = New Font("Arial", 18, FontStyle.Bold)
    ElseIf CB2.Checked And Not CB1.Checked Then
    F = New Font("Arial", 18, FontStyle.Italic)
    ElseIf CB1.Checked And CB2.Checked Then
    F = New Font("Arial", 18, FontStyle.Italic Or FontStyle.Bold)
    Else
    F = New Font("Arial", 18, FontStyle.Regular)
    End If
    Invalidate(R)
    End Sub
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    If Me.Visible = False Then Exit Sub
    e.Graphics.DrawString("This is a test", F, Brushes.Black, R)
    End Sub
    End Class

    Tthe sub drawtext should give u an idea on how to get bold with italic.

  • font italic and bold