http://www.perlmonks.org?node_id=1025611


in reply to Re^2: Wx::Perl: How to change/set font and size of Wx::ListCtrl column headings?
in thread Wx::Perl: How to change/set font and size of Wx::ListCtrl column headings?

Found the following hint in the wxWidgets Wiki

Using Specific Colours for Each Item

Trying to set specific item colours for a wxListCtrl doesn't have any effect with wxMSW 2.4.0 standard release. You have to enable it. Indeed, it is not set as default in the downloadable releases so you will have to recompile the library with _WIN32_IE at least defined as 0x300. To do so, just insert the -D_WIN32_IE=0x300 compile flag when making. See the Guides & Tutorials for further details about building wx. Then, just use wxListItem's methods to set the text colour, background colour and font of each item. For example:

MyListCtrl::AppendColouredItem(const wxString& Label) { wxListItem NewItem; NewItem.SetMask(wxLIST_MASK_TEXT); NewItem.SetId(GetItemCount()); NewItem.SetText(sLabel); NewItem.SetFont(*wxITALIC_FONT); NewItem.SetTextColour(wxColour(*wxRED)); NewItem.SetBackgroundColour(wxColour(235, 235, 235)); InsertItem(NewItem); }

This piece of code appends a new line (item) to MyListCtrl with the label of sLabel's value, an italic font, a red text colour and a light grey background.

Important part of this mess:

NOTE: It seems that these methods still don't work with columns so the tip is to set the global text/background colours and font of MyListCtrl and then, when you append an item, set its colours/font to the proper values.

Try playing around with my attempt at the above suggestion.

#! /home/xxxx/CitrusPerl/perl/bin/perl # Change header 2.pl: following James: http://www.perlmonks.org/?node_ +id=1025552 use strict; use warnings; use Wx; use 5.014; use autodie; use Carp; use Carp qw {cluck}; # use Carp::Always; package MyFrame; use Wx ':everything'; use Wx ':listctrl'; use Wx::Event 'EVT_BUTTON'; use parent -norequire, 'Wx::Frame'; use Data::Dumper; sub new { #1 MyFrame:: my ($class, $parent, $title) = @_; my $self = $class->SUPER::new( $parent, -1, # parent window; ID -1 means any $title, # title [150, 150 ], # position [ 350, 400 ], # size ); my $panel = Wx::Panel->new($self); $self->{list_control} = Wx::ListCtrl->new($panel, -1, wxDefaultP +osition, [340,100], wxLC_REPORT); $self->{list_control}->SetBackgroundColour(wxWHITE); $self->{list_control}->SetTextColour(wxBLUE); $self->{list_control}->SetFont(Wx::Font->new(14, wxFONTFAMILY_RO +MAN, wxNORMAL, wxNORMAL)); my $itemCol = Wx::ListItem->new; $itemCol->SetText('Column 1'); $itemCol->SetWidth(100); $self->{list_control}->InsertColumn(0, $itemCol); $itemCol->SetText('Column 2'); $itemCol->SetWidth(135); $self->{list_control}->InsertColumn(1, $itemCol); $itemCol->SetText('Column 3'); $itemCol->SetWidth(100); $self->{list_control}->InsertColumn(2, $itemCol); $self->{list_control}->InsertStringItem( 0, 'Data 1' ); $self->{list_control}->SetItem( 0, 1, 'Data 3'); my $f = Wx::Font->new(12, wxFONTFAMILY_ROMAN, wxNORMAL +, wxBOLD); my $item = $self->{list_control}->GetItem(0); $item->SetTextColour(wxRED); $item->SetFont($f); $self->{list_control}->SetItem($item); $self->{list_control}->InsertStringItem( 1, 'Data 2' ); $self->{list_control}->SetItem( 1, 1, 'Data 4'); $item = $self->{list_control}->GetItem(1); $item->SetTextColour(wxRED); $item->SetFont($f); $self->{list_control}->SetItem($item); my $btn_header = Wx::Button->new($panel, -1, 'Change header', +wxDefaultPosition, wxDefaultSize); my $btn_item = Wx::Button->new($panel, -1, 'Change item font', + wxDefaultPosition, wxDefaultSize); my $btn_header2 = Wx::Button->new($panel, -1, 'Change header2 +font', wxDefaultPosition, wxDefaultSize); EVT_BUTTON ($self, $btn_header, sub { $self->{list_control}->M +yFrame::on_header }); EVT_BUTTON ($self, $btn_item, sub { $self->{list_control}->MyF +rame::on_item }); EVT_BUTTON ($self, $btn_header2, sub { $self->{list_control}-> +MyFrame::on_header2 }); my $sizer = Wx::BoxSizer->new(wxVERTICAL); $sizer->Add($self->{list_control}, 0, wxALL, 10); $sizer->Add($btn_header, 0, wxALL, 10); $sizer->Add($btn_item, 0, wxALL, 10); $sizer->Add($btn_header2, 0, wxALL, 10); $panel->SetSizer($sizer); $panel->Layout(); return $self; } #1 end sub new MyFrame:: sub on_header { my $this = shift; my $column = $this->GetColumn(1); $column->SetText('NEW TITLE'); $this->SetColumn(1, $column); } #1 end sub on_header sub on_item { my $this = shift; my $item = $this->GetItem(1); $item->SetTextColour(wxGREEN); my $f = Wx::Font->new(12, wxFONTFAMILY_ROMAN, wxNORMAL, wxBOLD +); $item->SetFont($f); $this->SetItem($item); } #1 end sub on_item sub on_header2 { my $this = shift; my $itemCol = Wx::ListItem->new; $itemCol->SetText('Column 2'); $this->SetColumn(1, $itemCol); } #1 end sub on_header2 # end package MyFrame:: package main; my $frame = MyFrame->new(undef, 'Setting headers'); $frame->Show(1); my $app = Wx::SimpleApp->new; $app->MainLoop; 1;

When you move to a virtual control I think you will have to also start using Wx::ListItemAttr to hold the color/font info for the virtual row items.

James

There's never enough time to do it right, but always enough time to do it over...