为了达到的最终效果。
CPP文件
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "windef.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "panelcheck"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
pnlchck1->Parent=lv1; //Panel 添加到listview item中...
pnlchck1->DoubleBuffered=true;
pnlchck2->Parent=lv1; //Panel 添加到listview item中...
pnlchck2->DoubleBuffered=true;
lv1->DoubleBuffered=true;
lv2->DoubleBuffered=true;
TListItem *Item;
for (int i =0;i<50;i++) //随便初始化
{
Item=lv1->Items->Insert(0);
Item->Caption="Caption"+IntToStr(i);
int col=1;
Item->SubItems->Add("Col"+IntToStr(col++));//报警时间
Item->SubItems->Add("Col"+IntToStr(col++)); //分路名称
Item->SubItems->Add("Col"+IntToStr(col++)); //
Item->SubItems->Add("Col"+IntToStr(col++)); //
Item->SubItems->Add("Col"+IntToStr(col++));
Item->ImageIndex=i;
// Item->
String str=Item->SubItems->Strings[1];
}
for (int i =0;i<50;i++)
{
Item=lv2->Items->Insert(0);
Item->Caption="Caption"+IntToStr(i);
int col=1;
Item->SubItems->Add("Col"+IntToStr(col++));//报警时间
/*
Item->SubItems->Add("Col"+IntToStr(col++)); //分路名称
Item->SubItems->Add("Col"+IntToStr(col++)); //
Item->SubItems->Add("Col"+IntToStr(col++)); //
Item->SubItems->Add("Col"+IntToStr(col++));
Item->ImageIndex=0;
String str=Item->SubItems->Strings[1];
*/
}
for (int i =0;i<50;i++)
{
Item=lv3->Items->Insert(0);
Item->Caption="Caption"+IntToStr(i);
int col=1;
Item->SubItems->Add("Col"+IntToStr(col++));//报警时间
//*
Item->SubItems->Add("Col"+IntToStr(col++)); //分路名称
Item->SubItems->Add("Col"+IntToStr(col++)); //
Item->SubItems->Add("Col"+IntToStr(col++)); //
Item->SubItems->Add("Col"+IntToStr(col++));
Item->ImageIndex=0;
String str=Item->SubItems->Strings[1];
// */
}
OldToolWndProc=lv1->WindowProc; //重载Iistview消息
lv1->WindowProc=NewToolWndProc; //重载Iistview消息
}
//---------------------------------------------------------------------------
void __fastcall TForm1::NewToolWndProc(Messages::TMessage &Message) //重载Iistview消息
{
if(Message.Msg==WM_VSCROLL||Message.Msg==WM_MOUSEWHEEL)
{
pnlchck1->Visible=false;
pnlchck2->Visible=false;
}
OldToolWndProc(Message);
}
void __fastcall TForm1::lv1SelectItem(TObject *Sender, TListItem *Item,
bool Selected)
{
Selected=true;
}
void __fastcall TForm1::lv1CustomDrawSubItem(TCustomListView *Sender,
TListItem *Item, int SubItem, TCustomDrawState State,
bool &DefaultDraw)
{
TListView* tempListview=(TListView * )Sender;
TRect Userect;
ListView_GetSubItemRect(tempListview->Handle ,Item->Index, SubItem, LVIR_LABEL,&Userect);// Item-> DisplayRect(drBounds);
if (SubItem==2&&Item->Index==10)
{
pnlchck1->Left=Userect.left;
pnlchck1->Top= Userect.Top;
if (Userect.Top<0)
{
pnlchck1->Visible=false;
}
else
{
pnlchck1->Visible=true;
}
pnlchck1->Width=Userect.Width();
pnlchck1->Height =Userect.Height();
}
if (SubItem==2&&Item->Index==11)
{
pnlchck2->Left=Userect.left;
pnlchck2->Top= Userect.Top;
pnlchck2->Width=Userect.Width();
pnlchck2->Height =Userect.Height();
if (Userect.Top<0)
{
pnlchck2->Visible=false;
}
else
{
pnlchck2->Visible=true;
}
}
DefaultDraw=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::lv1AdvancedCustomDrawItem(TCustomListView *Sender,
TListItem *Item, TCustomDrawState State, TCustomDrawStage Stage,
bool &DefaultDraw)
{
//*
TListView* tempListview=(TListView * )Sender;
TRect Userect;
TRect Userect1;
TRect Userect2;
TRect UserectIcon;
TRect Rectone=Item->DisplayRect(drBounds);
if (Item->Selected)
{
tempListview->Canvas->Brush->Color=clSkyBlue; //选择行单独颜色
tempListview->Canvas->Pen->Color=clWhite;
tempListview->Canvas->Pen->Width=1;
tempListview->Canvas->FillRect(Rectone);
}
else
{
tempListview->Canvas->Brush->Color=clWhite; //未选择行,下方增加横线
tempListview->Canvas->Pen->Color=clSkyBlue;
tempListview->Canvas->Pen->Width=1;
tempListview->Canvas->FillRect(Rectone);
TRect line=Item->DisplayRect(drBounds);
line.Bottom-=1;
tempListview->Canvas->MoveTo(line.Left,line.Bottom) ;
tempListview->Canvas->LineTo(line.right,line.Bottom) ;
}
//*
for (int i=0;i<tempListview->Columns->Count;i++)
{
ListView_GetSubItemRect(tempListview->Handle ,Item->Index, i, LVIR_LABEL,&Userect);// Item-> DisplayRect(drBounds);得到item的大小
if (i==0)
{
ListView_GetSubItemRect(tempListview->Handle, Item->Index, i, LVIR_ICON,&UserectIcon); //得到item图标的大小
il1->Draw(tempListview->Canvas,UserectIcon.left,UserectIcon.Top,Item->ImageIndex,true); //自画图标
}
String str=Item->SubItems->Strings[1];
String onerow="支付宝的关联启动程序";
String onerow2="用于支付宝的安全功能";
Userect1=Userect;
Userect2=Userect;
Userect1.Bottom=Userect.Bottom-Userect.Height()/2;
Userect2.Top=Userect.Bottom-Userect.Height()/2;
int tem=Userect.Height();
int temp=Userect1.Height(); //创建两行,
int temp1=Userect2.Height() ;//创建两行,
if (!Item->SubItems->Strings[i].Trim().IsEmpty())
{
if (i==2&&Item->Index==1)
{
}
else
{
HFONT odlfont,Usefont,UseLittle;
Usefont=CreateFont( //为第一行创建一种字体
12, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_BLACK, // nWeight FW_NORMAL
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
PROOF_QUALITY, // nQuality DEFAULT_QUALITY
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"宋体");
UseLittle=CreateFont( //为第二行创建一种字体
12, // nHeight
0, // nWidth
0, // nEscapement
0, // nOrientation
FW_THIN, // nWeight FW_NORMAL
FALSE, // bItalic
FALSE, // bUnderline
0, // cStrikeOut
ANSI_CHARSET, // nCharSet
OUT_DEFAULT_PRECIS, // nOutPrecision
CLIP_DEFAULT_PRECIS, // nClipPrecision
PROOF_QUALITY, // nQuality DEFAULT_QUALITY
DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily
"宋体");
tempListview->Canvas->Font->Color=clBlack;
odlfont=SelectObject(tempListview->Canvas->Handle,Usefont);//改变Canvas的字体,并备份老的字体。
:: DrawText(tempListview->Canvas->Handle,onerow.c_str(),onerow.Length(),(RECT *)&Userect1,DT_VCENTER|DT_END_ELLIPSIS|DT_SINGLELINE);//|DT_END_ELLIPSIS
//重写第一行的的字。
SelectObject(tempListview->Canvas->Handle,UseLittle);
DeleteObject(Usefont);
:: SetTextColor(tempListview->Canvas->Handle,clGray);//RGB(255,255,255));
//改变字体颜色
::DrawText(tempListview->Canvas->Handle, onerow2.c_str(),onerow2.Length(),(RECT *)&Userect2,DT_VCENTER|DT_END_ELLIPSIS|DT_SINGLELINE);
//重写第二行的的字。
DeleteObject(UseLittle);
:: SetTextColor(tempListview->Canvas->Handle,clBlack);//RGB(255,255,255));
}
}
}
}
//---------------------------------------------------------------------------
HPP文件
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "panelcheck.h"
#include <ComCtrls.hpp>
#include <ExtCtrls.hpp>
#include <ImgList.hpp>
#include <Dialogs.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TListView *lv1;
Tpanelcheck *pnlchck1;
Tpanelcheck *pnlchck2;
TButton *btn1;
TImageList *il1;
TButton *btn2;
TListView *lv2;
TImageList *il2;
TListView *lv3;
TTreeView *tv1;
TListBox *lst1;
void __fastcall lv1SelectItem(TObject *Sender, TListItem *Item,
bool Selected);
void __fastcall lv2CustomDrawItem(TCustomListView *Sender,
TListItem *Item, TCustomDrawState State, bool &DefaultDraw);
void __fastcall lv1CustomDrawSubItem(TCustomListView *Sender,
TListItem *Item, int SubItem, TCustomDrawState State,
bool &DefaultDraw);
void __fastcall lv2SelectItem(TObject *Sender, TListItem *Item,
bool Selected);
void __fastcall lv3CustomDrawItem(TCustomListView *Sender,
TListItem *Item, TCustomDrawState State, bool &DefaultDraw);
void __fastcall lv1AdvancedCustomDrawItem(TCustomListView *Sender,
TListItem *Item, TCustomDrawState State, TCustomDrawStage Stage,
bool &DefaultDraw);
void __fastcall tv1AdvancedCustomDraw(TCustomTreeView *Sender,
const TRect &ARect, TCustomDrawStage Stage, bool &DefaultDraw);
void __fastcall lst1DrawItem(TWinControl *Control, int Index,
TRect &Rect, TOwnerDrawState State);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
TWndMethod OldToolWndProc;
void __fastcall NewToolWndProc(Messages::TMessage &Message);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif