给ListView的条目自绘边框

procedure TGroupItem.ListView1AdvancedCustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage; var DefaultDraw: Boolean); var NodeRect: TRect; NodeLabelRect:TRect; imageWidth,ImageHeight, RectWidth,RectHeight: Integer; begin if Sender <> FListView then begin Exit; end; with Sender.Canvas do begin DefaultDraw := False;//禁止ListView的默认绘制 if Item.Selected then begin Font.Color:=clwhite; Brush.Color :=clHighlight; end else begin Font.Color:=clblack; Brush.Color :=clwhite; end; //自绘标题 NodeLabelRect := Item.DisplayRect(drLabel); TextRect(NodeLabelRect,NodeLabelRect.Left,NodeLabelRect.Top,Item.Caption); NodeRect := Item.DisplayRect(drIcon); imageWidth := (Sender as TRzListView).LargeImages.Width; ImageHeight := (Sender as TRzListView).LargeImages.Height; RectWidth := NodeRect.Right - NodeRect.Left; RectHeight := NodeRect.Bottom - NodeRect.Top; //计算一个使图像居中的rect坐标 if imageWidth < RectWidth then begin //MX中特意在X方向不居中,为使右侧在有控制条的情况下也为两个按钮留出足够大的空间 //NodeRect.Left := NodeRect.Left + (RectWidth - imageWidth) div 2; NodeRect.Right := NodeRect.Left + imageWidth; end; if ImageHeight < RectHeight then begin NodeRect.Top := NodeRect.Top + (RectHeight - ImageHeight) div 2; NodeRect.Bottom := NodeRect.Top + ImageHeight; end; //绘制图片 (Sender as TRzListView).LargeImages.Draw(Sender.Canvas, NodeRect.Left, NodeRect.top, Item.ImageIndex, True); //在选中的条目的图片周围绘制上一个矩形 if Item.Selected then begin Brush.Color := clBlack; Sender.Canvas.FrameRect(NodeRect); end; end; end;

你可能感兴趣的:(ListView,div)