Home > Back-end >  TListView custom painting
TListView custom painting

Time:10-06

I want to custom painting ListView in DrawItem event, first, the second column shows the text, the third column display images,

But when change the column width text ghosting, excuse me how to go to custom painting?

CodePudding user response:

Ask demon elder brother to code

CodePudding user response:

About VCL control since the painting code previously ccrun hair a lot, such as TListBox TCheckListbox, TComboBox, TMenuItem etc., the following code is custom painting TListItem, namely ListView Item from the picture, when ViewStyle to vsReport renderings below:


First define a few we need to use the color of the article here with Office 2003 menu highlighting color:

# define MYCOLOR_BACK TColor (0 x00cfb9b1)
# define MYCOLOR_BORDER clHighlight
# define MYCOLOR_ROW TColor (RGB (240, 240, 240))

VCL provides, in general, since the draw function controls have similar OnDrawXX or OnCustomDrawXX events, ListView has four similar event, in this case we use OnDrawItem events,

Explain in Form of header files, since the drawing function, can be put inside Form __published section of a class, it is important to note that the IDE automatically generated ListViewOnDrawItem since the draw function parameters and we declare this is slightly different, so on the design, the function are invisible to the ListView OnDrawItem,

Private://User declarations
Void __fastcall CrnDrawListViewItem (TCustomListView * Sender,
TListItem * Item, TRect & amp; The Rect, TOwnerDrawState State);

Here is the drawing function of specific implementation code:
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//since the draw ListView Item
//by ccrun (old demon)
info#ccrun.com//welcome to c + + Builder study - http://www.ccrun.com
But//, reproduced to hold
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Void __fastcall TForm1: : CrnDrawListViewItem (TCustomListView * Sender,
TListItem * Item, TRect & amp; The Rect, TOwnerDrawState State)
{
TListView * lv=(TListView *) Sender;
//the Rect
TRect RCT (the Rect. Left + 1, the Rect. Top, the Rect. The Width () - 1, the Rect, Bottom);
//the Fill background
If (State. The Contains (odFocused) | | State. The Contains (odSelected))
{
//With focus
Louis vuitton - & gt; Canvas - & gt; Brush - & gt; Color=MYCOLOR_BACK;
Louis vuitton - & gt; Canvas - & gt; FillRect (RCT);
Louis vuitton - & gt; Canvas - & gt; Pen - & gt; Color=MYCOLOR_BORDER;
Louis vuitton - & gt; Canvas - & gt; A Rectangle (RCT);
}
The else
{
Louis vuitton - & gt; Canvas - & gt; Brush - & gt; Color=lv - & gt; Color;
Louis vuitton - & gt; Canvas - & gt; FillRect (the Rect);
}
Int nLeftOffset (0);
//With the CheckBox?
//63 63 72 75 6 2 63 6 f e e d
If (lv - & gt; Checkboxes)
{
Louis vuitton - & gt; Canvas - & gt; Pen - & gt; Color=clBlack;
Louis vuitton - & gt; Canvas - & gt; Pen - & gt; Width=2;
//Draw the CheckBox the Rect
Louis vuitton - & gt; Canvas - & gt; A Rectangle (the Rect. Left + 4, the Rect. Top + 3, the Rect. Left + 16, the Rect. The Bottom - 2);
NLeftOffset=16;
If (Item - & gt; Checked)
{
//draw the CheckBox hook
Louis vuitton - & gt; Canvas - & gt; MoveTo (the Rect. Left + 6, the Rect. Top + 6);
Louis vuitton - & gt; Canvas - & gt; LineTo (the Rect. Left + 8, the Rect. Top + 11);
Louis vuitton - & gt; Canvas - & gt; The LineTo (the Rect. Left + 13, the Rect. Top + 5);
}
Louis vuitton - & gt; Canvas - & gt; Pen - & gt; Width=1;
}
//the Draw small Icon
If (lv - & gt; SmallImages & amp; & Item - & gt; ImageIndex!=1)
{
Louis vuitton - & gt; SmallImages - & gt; The Draw (lv - & gt; Canvas, nLeftOffset + the Rect. Left + 2,
The Rect. Top + (the Rect. Height () - lv - & gt; SmallImages - & gt; Height)/2 + 1,
//this article from the c + + Builder research - http://www.ccrun.com/article.asp? I=656 & amp; D=qdc7kg
Item - & gt; ImageIndex, true);
NLeftOffset +=lv - & gt; SmallImages - & gt; Width;
}
//the Draw Text
Louis vuitton - & gt; Canvas - & gt; The Font - & gt; Color=clBlack;
Louis vuitton - & gt; Canvas - & gt; TextOutA (the Rect. Left + 4 + nLeftOffset,
The Rect. Top + (the Rect. Height () - lv - & gt; Canvas - & gt; TextHeight (" A "))/2,
Item - & gt; Caption);
//the Draw SubItem Text
Int nColOffset (0);
for(int i=0; i{
NColOffset +=lv - & gt; The Column [I] - & gt; Width;
Louis vuitton - & gt; Canvas - & gt; TextOutA (nColOffset + the Rect. Left + 4,
The Rect. Top + (the Rect. Height () - lv - & gt; Canvas - & gt; TextHeight (" A "))/2,
Item - & gt; SubItems - & gt; Strings [0]);
}
}
//since the end of drawing code

Because of this since the draw function is different from the IDE automatically generated, so you need to dynamically specify the ListView OnDrawItem, at the same time, the ListView need some Settings, of course, you can directly in the design of change in the properties,
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
__fastcall TForm1: : TForm1 (TComponent * Owner)
: TForm (the Owner)
{
ListView1 - & gt; OwnerDraw=true;
ListView1 - & gt; The RowSelect=true;
ListView1 - & gt; ReadOnly=true;
ListView1 - & gt; OnDrawItem=(TLVDrawItemEvent) & amp; CrnDrawListViewItem;
}

So basic to OK, interested friends can test,

DFM file content (View as Text)
=============================================================================
The object Form1: TForm1
Left=196
Top=131
BorderStyle=bsDialog
Caption='ListItem from painting demo'
ClientHeight=168
ClientWidth=231
Color=clBtnFace
The Font. The Charset=GB2312_CHARSET
The Font, Color=clWindowText
12. The Font Height=
The Font. The Name='song typeface'
The Font, Style=[]
OldCreateOrder=False
The Position=poScreenCenter
The Visible=True
PixelsPerInch=96
TextHeight=12
The object Label1: TLabel
Left=40
Top=8
Width=144
Height=12
Caption='by ccrun (old demon) QQ: 165332'
End
The object Label2: TLabel
Left=24
Top=144
Width=186
Height=12
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related