title: drawing a checkbox in a TListView
body: <p>I need to draw a checkbox in a particular column in a<code>TListView</code>, so i check this question <a href="https://stackoverflow.com/questions/5478972/how-can-i-setup-tlistview-with-checkboxes-in-only-certain-columns"><code>How can I setup TListView with CheckBoxes in only certain columns?</code></a> and in the accepted answer suggest use the method described in this another question <a href="https://stackoverflow.com/questions/5306037/how-to-set-a-checkbox-tstringgrid-in-delphi"><code>How to set a Checkbox TStringGrid in Delphi?</code></a>, now porting that code to work with a ListView i come with this :</p>
  
  <pre><code>procedure TForm15.ListView1CustomDrawSubItem(Sender: TCustomListView; Item: TListItem; SubItem: Integer; State: TCustomDrawState; var DefaultDraw: Boolean);
  const
    PADDING = 4;
  var
    h    : HTHEME;
    s    : TSize;
    r    : TRect;
    Rect : TRect;
    i    : Integer;
    Dx   : Integer;
  begin
    if (SubItem=1) then
    begin
      DefaultDraw:=True;
      Rect  :=Item.DisplayRect(drBounds);
      Dx:=0;
  
      for i := 0 to SubItem do
      Inc(Dx,Sender.Column[i].Width);
      Rect.Left  :=Rect.Left+Dx;
  
      Rect.Right :=Rect.Left+Sender.Column[SubItem+1].Width;
  
      FillRect(Sender.Canvas.Handle, Rect, GetStockObject(WHITE_BRUSH));
      s.cx := GetSystemMetrics(SM_CXMENUCHECK);
      s.cy := GetSystemMetrics(SM_CYMENUCHECK);
      if UseThemes then
      begin
        h := OpenThemeData(Sender.Handle, 'BUTTON');
        if h &lt;&gt; 0 then
          try
            GetThemePartSize(h, Sender.Canvas.Handle, BP_CHECKBOX, CBS_CHECKEDNORMAL, nil, TS_DRAW, s);
            r.Top    := Rect.Top + (Rect.Bottom - Rect.Top - s.cy) div 2;
            r.Bottom := r.Top + s.cy;
            r.Left   := Rect.Left + PADDING;
            r.Right  := r.Left + s.cx;
            DrawThemeBackground(h, Sender.Canvas.Handle, BP_CHECKBOX, IfThen(CompareText(Item.SubItems[1],'True')=0, CBS_CHECKEDNORMAL, CBS_UNCHECKEDNORMAL), r, nil);
          finally
            CloseThemeData(h);
          end;
      end
      else
      begin
        r.Top    := Rect.Top + (Rect.Bottom - Rect.Top - s.cy) div 2;
        r.Bottom := r.Top + s.cy;
        r.Left   := Rect.Left + PADDING;
        r.Right  := r.Left + s.cx;
        DrawFrameControl(Sender.Canvas.Handle, r, DFC_BUTTON, IfThen(CompareText(Item.SubItems[1],'True')=0, DFCS_CHECKED, DFCS_BUTTONCHECK));
      end;
     //r := Classes.Rect(r.Right + PADDING, Rect.Top, Rect.Right, Rect.Bottom);
     // DrawText(Sender.Canvas.Handle,   StringGrid1.Cells[ACol, ARow], length(StringGrid1.Cells[ACol, ARow]),  r, DT_SINGLELINE or DT_VCENTER or DT_LEFT or DT_END_ELLIPSIS);
    end
    else
    DefaultDraw:=False;
  end;
  </code></pre>
  
  <p>but i fail miserably in my attempt to draw a checkbox  :(, can someone point me in the right direction to draw the checkbox in the listview, (the code does not draw any checkbox in the listview).</p>
  
  <p>The listview is in vsReport mode and had 3 columns, i want put the checkbox in the third column. please don't suggest  which use  a thrid party component, i want use the TlistView control.</p>
  
  <p><strong>UPDATE 1</strong> : thanks  to the sertac recomendattion setting the <code>DefaultDraw</code> value now the checkboxes are shown, but the another columns looks awfull.</p>
  
  <p><img src="https://i.stack.imgur.com/ltG5G.png" alt="enter image description here"></p>
  
  <p><strong>UPDATE 2</strong> ,  following the Andreas suggestions the listview now look better, but still shown the black box;</p>
  
  <p><img src="https://i.stack.imgur.com/cA3qw.png" alt="enter image description here"></p>
  
  <pre><code>procedure TForm15.ListView1CustomDrawSubItem(Sender: TCustomListView; Item: TListItem; SubItem: Integer; State: TCustomDrawState; var DefaultDraw: Boolean);
  var
    h    : HTHEME;
    s    : TSize;
    r    : TRect;
    Rect : TRect;
    i    : Integer;
    Dx   : Integer;
  begin
    if (SubItem=2) then
    begin
      DefaultDraw:=False;
      Rect  :=Item.DisplayRect(drBounds);
  
      Dx:=0;
      for i := 0 to SubItem-1 do
        Inc(Dx,Sender.Column[i].Width);
  
      Rect.Left  :=Rect.Left+Dx;
      Rect.Right :=Rect.Left+Sender.Column[SubItem].Width;
      FillRect(Sender.Canvas.Handle, Rect, GetStockObject(WHITE_BRUSH));
      s.cx := GetSystemMetrics(SM_CXMENUCHECK);
      s.cy := GetSystemMetrics(SM_CYMENUCHECK);
      Dx   := (Sender.Column[SubItem].Width-GetSystemMetrics(SM_CXMENUCHECK)) div 2;
      if UseThemes then
      begin
        h := OpenThemeData(Sender.Handle, 'BUTTON');
        if h &lt;&gt; 0 then
          try
            GetThemePartSize(h, Sender.Canvas.Handle, BP_CHECKBOX, CBS_CHECKEDNORMAL, nil, TS_DRAW, s);
            r.Top    := Rect.Top + (Rect.Bottom - Rect.Top - s.cy) div 2;
            r.Bottom := r.Top + s.cy;
            r.Left   := Rect.Left + Dx;
            r.Right  := r.Left + s.cx;
            DrawThemeBackground(h, Sender.Canvas.Handle, BP_CHECKBOX, IfThen(CompareText(Item.SubItems[SubItem-1],'True')=0, CBS_CHECKEDNORMAL, CBS_UNCHECKEDNORMAL), r, nil);
          finally
            CloseThemeData(h);
          end;
      end
      else
      begin
        r.Top    := Rect.Top + (Rect.Bottom - Rect.Top - s.cy) div 2;
        r.Bottom := r.Top + s.cy;
        r.Left   := Rect.Left + Dx;
        r.Right  := r.Left + s.cx;
        DrawFrameControl(Sender.Canvas.Handle, r, DFC_BUTTON, IfThen(CompareText(Item.SubItems[SubItem-1],'True')=0, DFCS_CHECKED, DFCS_BUTTONCHECK));
      end;
    end;
  end;
  </code></pre>