views:

276

answers:

2

Hi Guys,

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?

A: 

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 :(

Cetra
+1  A: 

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.

Corey Ross