TsCheckListBox and DrawItem-method

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #34949
    ralfiii
    Participant

    Hello!

    I've added enhanced a TChckListBox by adding just little color blocks to the entries.

    (See the attached image)

    Here's how (just a sample):

    Code:
    procedure TForm1.CheckListBox1DrawItem(Control: TWinControl; Index: Integer;
    Rect: TRect; State: TOwnerDrawState);
    const ColorFieldWidth = 10;
    var ItemColor : TColor;
    IsSelected : boolean;
    const ColArr : array[0..3] of TColor = (clBlack, clRed, clBlue, clYellow);
    begin
    IsSelected:=odSelected in State;

    with (Control as TCheckListBox) do
    begin
    ItemColor:=ColArr[Index mod 4];

    // Clear Cell
    if IsSelected then
    Canvas.Brush.Color:=$00AED7FF else
    Canvas.Brush.Color:=Color;

    Canvas.FillRect(Rect);

    // Draw Colorfield
    Canvas.Brush.Color:=ItemColor;
    with Rect do
    Canvas.FillRect(Classes.Rect(Left+2,Top+2,Left+2+ColorFieldWidth,Bottom-3));

    // Output Text
    Canvas.Brush.Style:=bsClear;
    Canvas.Font.Color:=clBlack;
    with Rect do
    Canvas.TextOut(Left+ColorFieldWidth+7, Top+1, Items[Index]);
    end;
    end;

    Now I'd need the ckecklistbox to be transparent so that the gradient background of the form comes through.

    I know I have no chance with a standard VCL-control I changed to the TsCheckListBox. Setting the SkinSection to “CheckBox” there. Does it.

    However, I need to do some manual painting too. I need to draw the graphic and I guess I also need to draw the text as it is shifted a bit to the right to give space for the graphic. So far it's easy.

    BUT: How do I draw the selection-marker?!?

    In the old VCL that was easy as it was one-color only. Now the sCheckListBox has a very nice multicolor-selection-bar. Any chance I can get the component to draw it?

    #47378
    ralfiii
    Participant

    I have changed the code for the TsCheckListBox and attached a pic of my current (left) and the normal (right) focus rect and my current focus rect, just to make it easier to understand what my problem is.

    Here's the code:

    Code:
    procedure TForm1.sCheckListBox1DrawItem(Control: TWinControl; Index: Integer;
    Rect: TRect; State: TOwnerDrawState);
    var IsSelected : boolean;
    const ColorFieldWidth = 10;
    const ColArr : array[0..3] of TColor = (clBlack, clRed, clBlue, clYellow);
    begin
    IsSelected:=odSelected in State;

    with (Control as TsCheckListBox) do
    begin
    // Clear Cell
    if IsSelected then
    begin
    Canvas.Brush.Color:=$00AED7FF;
    Canvas.FillRect(Rect);
    end;

    // Draw Colorfield
    Canvas.Brush.Color:=ColArr[Index mod 4];
    with Rect do
    Canvas.FillRect(Classes.Rect(Left+2,Top+2,Left+2+ColorFieldWidth,Bottom-3));

    // Output Text
    Canvas.Brush.Style:=bsClear;
    Canvas.Font.Color:=clBlack;
    with Rect do
    Canvas.TextOut(Left+ColorFieldWidth+7, Top+1, Items[Index]);
    end;
    end;

    #47410
    Support
    Keymaster

    Thanks for message, this topic was moved to 'Tips and tricks'.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.