Change font color Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
Ambush
Knows some wx things
Knows some wx things
Posts: 27
Joined: Mon Jun 12, 2017 6:08 pm

Change font color

Post by Ambush »

hello again,

I have added a check box to my basic window and have changed the font and font size accordingly,
however i am struggling to change the color, i have tried a number of different ways to do it, but
still no luck, can you show me an example of how to do it please, i will post my code below.

Code: Select all

#include "main.h"

IMPLEMENT_APP(MyApp)

bool MyApp::OnInit()
{
	wxInitAllImageHandlers();

	wxFont font(wxFontInfo(14).FaceName("segoe ui semilight"));

	mainframe = new wxFrame(NULL, wxID_ANY, wxString("wxProgen"), wxPoint(wxDefaultPosition), wxSize(458, 596), wxBORDER_SIMPLE);

	sizer = new wxBoxSizer(wxHORIZONTAL);
	
	optionbox = new wxCheckBox(mainframe, wxID_ANY, wxString("hello"), wxPoint(wxDefaultPosition), wxSize(90,20));
	optionbox->SetFont(font);

	sizer->Add(optionbox);

	mainframe->SetSizerAndFit(sizer);

	mainframe->SetBackgroundColour(wxColor(45, 45, 48));

	mainframe->Center();
	mainframe->Show();

	return true;
}
main.h

Code: Select all

#include <wx/wx.h>

class MyApp : public wxApp
{
public:
	virtual bool OnInit();

	wxFrame *mainframe;

	wxBoxSizer *sizer;
	
	wxCheckBox *optionbox;



};
DECLARE_APP(MyApp)
I have tried different combinations of setbackgroundcolor(), setforegroundcolor(), setfont(), wxcolor() and none seem to work,
im not really sure why.

tnx

msw server2012r2
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Change font color

Post by doublemax »

You want to change the color of the text of the checkbox? Then "optionbox-SetForegroundColour( wxColour("#ff0000") );" should work. I just tested this under Windows 7 and 10.

Beware that this is not a guarantee it will work on all platforms. Sometimes (especially under OSX) the OS just doesn't allow to change the color of some GUI elements.
Use the source, Luke!
User avatar
Ambush
Knows some wx things
Knows some wx things
Posts: 27
Joined: Mon Jun 12, 2017 6:08 pm

Re: Change font color

Post by Ambush »

hi doublemax, you are right, i just want to change for the optionbox, at this point.

I was looking at the possibility of creating custom controls using the custom controls template posted at the page below,
mainly because one project i am trying to do will require the same look and feel on all platforms, its a game cummunity thing tbh

http://docs.wxwidgets.org/trunk/overvie ... dgets.html

but that looks way too much for me at the moment, although i will def try and have a go at least.

anyway thanks for your help !
Noodles
In need of some credit
In need of some credit
Posts: 2
Joined: Thu Aug 30, 2018 1:31 pm

Re: Change font color

Post by Noodles »

doublemax wrote:You want to change the color of the text of the checkbox? Then "optionbox-SetForegroundColour( wxColour("#ff0000") );" should work. I just tested this under Windows 7 and 10.
Hello, this code indeed worked on Win7; but surprisingly the text is a bit shifted to the right, and the last letter is truncated:
Image

Have you an idea of why and/or a workaround please?
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Change font color

Post by doublemax »

If you call SetForegroundColour on a CheckBox (and possible a few other controls), the control will become "ownerdrawn". This means that wxWidgets draws the control itself. Apparently the drawing code in wxWidgets does not generate the exact same look.

But i don't know a good solution for this. As for the spacing, you could call SetForegroundColour() on all checkboxes. That way they would at least look all the same. But the clipped text is bug that would have to be fixed in wxWidgets.
Use the source, Luke!
Noodles
In need of some credit
In need of some credit
Posts: 2
Joined: Thu Aug 30, 2018 1:31 pm

Re: Change font color

Post by Noodles »

Ok, thank you for the explaination! (So by the way, a hack to have same look on different OSes could be to set a foreground color ? Interesting :wink: )
Yes, setting same foreground is a workaround, as well as simply adding a space at end ...
Post Reply