- Код: Выделить всё
procedure TForm1.OnDrawItem(Control: TWinControl; Index: Integer; ARect: TRect;
State: TOwnerDrawState);
var
CBText : String;
CBItem : TColorCheckListItem;
TextSize : TSize;
TextPosY : Integer;
PPoints : array[1..3] of TPoint;
FCheckRectSize : Integer = 20; { by default }
HRectIndent : Integer = 5;
VRectIndent : Integer = 2;
begin
with (Control as TCheckListBox).Canvas do begin
//Pen.Color := clBlack;
//FCheckRectSize := Round((ARect.Bottom - ARect.Top) - 2*VRectIndent);
if (Index <= CheckListBox.Items.Count-1) then begin
CBText := CheckListBox.Items.Strings[Index];
CBItem := TColorCheckListItem(CheckListBox.Items.Objects[Index]);
if Assigned(CBItem) then begin
Rectangle(ARect.Left + HRectIndent, ARect.Top + VRectIndent,
ARect.Left + FCheckRectSize + HRectIndent, ARect.Top + FCheckRectSize + VRectIndent);
Brush.Color := CBItem.Color;
if(CBItem.IsChecked) then begin
{ draw empty rectangle with small clip at left top corner }
PPoints[0].x := Round(ARect.Left + HRectIndent + 1);
PPoints[0].y := Round(ARect.Top + VRectIndent + FCheckRectSize/2);
PPoints[1].x := Round(ARect.Left + HRectIndent + 1);
PPoints[1].y := Round(ARect.Top + VRectIndent + 1);
PPoints[2].x := Round(ARect.Left + HRectIndent + FCheckRectSize/2);
PPoints[2].y := Round(ARect.Top + VRectIndent + 1);
Polygon(PPoints);
end
else
{ fill rect }
FillRect(ARect.Left + HRectIndent + 1, ARect.Top + VRectIndent + 1,
ARect.Left + FCheckRectSize + HRectIndent - 1, ARect.Top + FCheckRectSize + VRectIndent - 1);
end;
TextSize := Canvas.TextExtent(CBText);
TextPosY := ARect.Top + Round(((ARect.Bottom - ARect.Top)/2 - TextSize.cy/2));
if(CBItem.BoldText = True) then
Font.Bold := True;
Brush.Style := bsClear;
TextOut(ARect.Left + (HRectIndent * 2) + FCheckRectSize, TextPosY, CBText);
if (odSelected in State) then begin
Brush.Color := clSkyBlue;
Font.Color := clHighlightText;
FillRect(ARect);
DrawFocusRect(aRect);
end
else begin
Brush.Color := clWindow;
Font.Color := clWindowText;
end;
end;
end;
end;
Имеется несколько вопросов:
1. Почему-то, когда срабатывается условие "if(CBItem.IsChecked)" то переменная FCheckRectSize меняется своё значение на 6 (О_о), хотя должна быть 20.
2. Почему-то, когда добавляешь элементы в список, текст на первых элементах списка начинает отрисовываться жирным (О_о), выглядит это так:
3. Когда добавишь много записей в список, появляется скроллбар, начинаешь его двигать, список заливается черным:
Во вложение добавил архив с проектом, можете проверить, у вас такая же беда или это у меня косяки? Что я делаю не правильно? Да и как вообще правильно отрисовывать элементы?