[erlang-questions] wxStaticText resizing

Dan Gudmundsson <>
Thu Mar 8 08:54:27 CET 2012


You want the actual font size to change?

If so you will have to write that yourself there is no such
functionality in wx that I'm aware of.

Something like this:

go() ->
    wx:new(),
    Parent = wxFrame:new(wx:null(), ?wxID_ANY, "Foobar"),
    %% wxFrame:connect(Parent, close_window),
    Panel = wxPanel:new(Parent, []),

    %% Setup sizers
    TextSizer = wxBoxSizer:new(?wxVERTICAL),
    ClockText = wxStaticText:new(Panel,1,"00 : 00 :
00",[{style,?wxALIGN_CENTER}]),

    %% Add to sizers
    wxSizer:add(TextSizer,ClockText,[{flag,?wxEXPAND},{proportion,0}]),
    DrawArea = wxPanel:new(Panel, [{style, ?wxFULL_REPAINT_ON_RESIZE}]),
    wxSizer:add(TextSizer,DrawArea,[{flag,?wxEXPAND},{proportion,1}]),
    Fonts = create_fonts(),
    wxPanel:connect(DrawArea, paint, [{callback, fun(Ev, _) ->
draw(Ev, Fonts) end}]),
    wxPanel:setSizer(Panel,TextSizer),
    wxFrame:show(Parent).

create_fonts() ->
    [wxFont:new(Sz, ?wxFONTFAMILY_DEFAULT, ?wxFONTSTYLE_NORMAL,
?wxFONTWEIGHT_NORMAL) ||
	Sz <- lists:reverse([8, 10, 12, 14, 16, 18, 24, 28, 40, 48, 60])].

draw(#wx{obj=Panel}, Fonts) ->
    DC = wxPaintDC:new(Panel),
    Sz = {W,H} = wxWindow:getClientSize(Panel),
    wxDC:drawRectangle(DC, {3,3,W-6, H-6}),
    Str = "00 : 00 : 00",
    Pos = set_font(Fonts, Str, DC, Sz),
    wxDC:drawText(DC, Str, Pos),
    wxPaintDC:destroy(DC).

set_font([Font|Fs], Str, DC, Sz = {MaxW, MaxH}) ->
    wxDC:setFont(DC, Font),
    case wxDC:getTextExtent(DC, Str) of
	{W,H} when W < MaxW, H < MaxH ->
	    {(MaxW - W) div 2, (MaxH - H) div 2};
	_ ->
	    set_font(Fs, Str, DC, Sz)
    end;
set_font([], _, _, _) ->
    {0,0}.

/Dan

On Wed, Mar 7, 2012 at 5:03 PM, eigenfunction <> wrote:
> I have a static text centered in a panel. When i resize the panel, i
> would like the text to resize as well. This is my code:
>
>    wxFrame:connect(Parent, close_window),
>    Panel = wxPanel:new(Parent, []),
>
>    %% Setup sizers
>    TextSizer = wxBoxSizer:new(?wxVERTICAL),
>    ClockText = wxStaticText:new(Panel,1,"00 : 00 : 00",[{style,?
> wxALIGN_CENTER}]),
>
>    %% Add to sizers
>    wxSizer:add(TextSizer,ClockText,[{flag,?wxEXPAND},{proportion,
> 1}]),
>
>    wxPanel:setSizer(Panel,TextSizer),
>    wxFrame:show(Parent)
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list