Unit1.pas
上传用户:lele20108
上传日期:2022-07-05
资源大小:15k
文件大小:6k
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, ExtCtrls, ImgList, ShellAPI;
- //定义一个记录用来存放listview的内容
- const
- BigItemHeight = 100;
- smallItemheight = 50;
- type
- Plistdata = ^Tlistdata;
- Tlistdata = record
- Caption: string;
- second: string;
- three: string;
- bigstr1: string;
- bigstr2: string;
- picon: TIcon; //图标
- SelectedFlag: Boolean; //选中标志
- end;
- type
- TForm1 = class(TForm)
- listbox1: TListBox;
- Panel1: TPanel;
- Label1: TLabel;
- ImageList1: TImageList;
- procedure FormCreate(Sender: TObject);
- procedure listbox1DrawItem(Control: TWinControl; Index: Integer;
- Rect: TRect; State: TOwnerDrawState);
- procedure listbox1MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure Label1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- listboxdata: TList;
- implementation
- {$R *.dfm}
- procedure TForm1.FormCreate(Sender: TObject);
- var
- listdata: Plistdata;
- i: Integer;
- begin
- listbox1.Style := lbOwnerDrawVariable;
- listbox1.ItemHeight := 50; //设定item高度
- //初使化listbox的数据到tlist中
- listboxdata := tlist.Create;
- for i := 0 to 5 do
- begin
- New(listdata);
- listdata^.Caption := '第' + inttostr(i) + '行Caption内容';
- listdata^.second := '第' + inttostr(i) + '行第二列数据';
- listdata^.three := '第' + inttostr(i) + '行第三列数据';
- listdata^.bigstr1 := '选中显示内容第' + inttostr(i) + '行第一列数据';
- listdata^.bigstr2 := '选中显示内容第' + inttostr(i) + '行第二列数据';
- listdata^.SelectedFlag := false;
- listdata^.picon := TIcon.Create;
- ImageList1.GetIcon(i, listdata^.picon);
- listboxdata.Add(listdata);
- end;
- //插入空内容到listview
- for i := 0 to 5 do
- listbox1.Items.Add('');
- end;
- procedure TForm1.listbox1DrawItem(Control: TWinControl; Index: Integer;
- Rect: TRect; State: TOwnerDrawState);
- var
- listdata: Plistdata;
- begin
- //取每行的数据
- listdata := listboxdata.Items[index];
- if listdata.SelectedFlag then //选中画大item
- begin
- // 画选中大框
- listbox1.Canvas.Brush.Color := clWhite;
- listbox1.Canvas.Pen.Color := $001C8AEA;
- listbox1.Canvas.RoundRect(Rect.Left, Rect.Top, Rect.Right - 1, Rect.Bottom, 0, 0);
- // 画选中框上部标题框
- listbox1.Canvas.Brush.Color := $00D7D7D7;
- listbox1.Canvas.Pen.Color := $001C8AEA;
- listbox1.Canvas.RoundRect(Rect.Left + 2, Rect.Top + 2, Rect.Right - 3, Rect.Bottom - 75, 0, 0);
- //写caption
- listbox1.Canvas.Brush.Color := $00D7D7D7;
- listbox1.Canvas.Font.Color := $001C8AEA;
- listbox1.Canvas.Font.Style := [fsBold];
- listbox1.Canvas.TextOut(Rect.Left + 15, Rect.Top + 6, listdata.Caption);
- // 画 图标
- listbox1.Canvas.Draw(Rect.Left + 7, Rect.top + (listbox1.ItemHeight - 32) div 2 + 35, listdata.Picon);
- listbox1.Canvas.Brush.Color := clWhite;
- listbox1.Canvas.TextOut(Rect.Left + 45, Rect.Top + 45, listdata.second);
- listbox1.Canvas.TextOut(Rect.Left + 245, Rect.Top + 45, listdata.three);
- listbox1.Canvas.TextOut(Rect.Left + 45, Rect.Top + 75, listdata.bigstr1);
- listbox1.Canvas.TextOut(Rect.Left + 245, Rect.Top + 75, listdata.bigstr2);
- end
- else
- begin
- if (odSelected in State) then
- listbox1.Canvas.Brush.Color := TColor($00FFF8EA)
- else
- listbox1.Canvas.Brush.Color := clWhite;
- listbox1.Canvas.FillRect(Rect); //填充颜色
- // 画出图标
- listbox1.Canvas.Draw(Rect.Left + 7, Rect.top + (listbox1.ItemHeight - 32) div 2, listdata.Picon);
- // Item的第一行文字 // 选中项的文字颜色
- listbox1.Canvas.Font.Color := clBlack;
- listbox1.Canvas.Font.Style := [fsBold];
- listbox1.Canvas.Font.size := 9;
- listbox1.Canvas.TextOut(Rect.Left + 45, Rect.Top + 20, listdata.Caption);
- listbox1.Canvas.Font.Color := clBlack;
- listbox1.Canvas.Font.Style := [];
- listbox1.Canvas.Font.size := 8;
- listbox1.Canvas.TextOut(Rect.Left + 200, Rect.Top + 20, listdata.second);
- listbox1.Canvas.TextOut(Rect.Left + 345, Rect.Top + 20, listdata.three);
- //画底边
- listbox1.Canvas.Pen.Color := $0099A8AC;
- listbox1.Canvas.MoveTo(Rect.Left, Rect.Bottom - 1);
- listbox1.Canvas.LineTo(Rect.right, Rect.Bottom - 1);
- end;
- if (odFocused in State) then //去 焦点虚框
- begin
- DrawFocusRect(listbox1.Canvas.Handle, rect);
- end;
- end;
- procedure TForm1.listbox1MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- var
- index, i: Integer;
- p: TPoint;
- listdata: Plistdata;
- begin
- p.X := x;
- p.Y := y;
- index := listbox1.ItemAtPos(p, True);
- if index = -1 then Exit;
- if listbox1.Items.Count = 0 then exit;
- listdata := listboxdata.Items[index];
- if Button = mbLeft then
- begin
- if not listdata^.SelectedFlag then
- begin
- listdata^.SelectedFlag := True;
- listbox1.Perform(LB_SETITEMHEIGHT, index, BigItemHeight);
- for i := 0 to listboxdata.Count - 1 do
- begin
- if index <> i then
- begin
- listbox1.Perform(LB_SETITEMHEIGHT, i, SmallItemHeight);
- listdata := listboxdata.Items[i];
- listdata^.SelectedFlag := false;
- end;
- end;
- listbox1.Invalidate;
- end;
- end;
- end;
- procedure TForm1.Label1Click(Sender: TObject);
- begin
- ShellExecute(Application.Handle, 'open', PChar('http://www.FreeDelphiTips.com'), nil, nil, SW_ShowNormal);
- end;
- end.