Cetra
Cetra

Reputation: 2621

How do i specify a bold version of the theme's default font?

I'm having problems with cross theme compatibility in windows forms. If you don't set the font for a control on a windows form, it will use the system font with correct typeface and size. If you want to make the font bold, it hard codes in the rest of the system font values for the current theme you're programming with. For instance:

System::Windows::Forms::Label^ label1 = gcnew System::Windows::Forms::Label();

this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(9, 12);
this->label1->Name = L"lblExample";
this->label1->Size = System::Drawing::Size(44, 13);
this->label1->TabIndex = 3;
this->label1->Text = L"Example Text";

If I then change the properties of this via the properties editor so that bold = true, it adds in this line:

this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0)));

Is there a way of using the default font, but making it bold?

Further yet, is there a way of using the system font, but making the size 3 or 4 points larger?

Upvotes: 1

Views: 465

Answers (2)

Corey Ross
Corey Ross

Reputation: 2015

You can put the modified font initialization directly after the InitializeComponent call in your constructor.

Also, you can you one of the many, many constructors to change the size.

InitializeComponent();

label1->Font = gcnew System::Drawing::Font(
    label1->Font->FontFamily, 
    label1->Font->SizeInPoints + 4, 
    FontStyle::Bold,
    GraphicsUnit::Point);

This will keep the design view from getting confused... but you also won't be able to see it in design view.

Upvotes: 1

Cetra
Cetra

Reputation: 2621

Ah, I think I've found the answer:

this->label1->Font = gcnew System::Drawing::Font(this->label1->Font, FontStyle::Bold);

But this now breaks the designer view :(

Upvotes: 1

Related Questions