学习编制一个取色软件,如何在ListBox中加入色块和颜色值。我的代码如下,不知为什么不行?求助。
procedure TfrmCPicker.LBCjDrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  tmpOldColor: TColor;
begin
  with (Control as TListBox).Canvas do
    begin
       tmpOldColor:=Canvas.Brush.Color;
       Canvas.FillRect(Rect);
       Canvas.Brush.Color:=RGBToColor(strtoint(EdRed.Text),strtoint(EdGreen.Text),strtoint(EdBlue.Text));
       Canvas.Rectangle(Rect.Left+4,Rect.Top+1,(Rect.Right+Rect.Left)div 3,Rect.Bottom-1);
       Canvas.Brush.Color:=tmpOldColor;
       Canvas.TextOut((Rect.Left+Rect.Right)div 2,Rect.Top+1,(Control as TListBox).Items[Index]);
    end;
end;procedure TfrmCPicker.btnPickClick(Sender: TObject);
begin
  LBCj.Items.Add(EdColor.Text);
end;

解决方案 »

  1.   

    使用OnDrawItem的前提是TListBox.Style属性值为lbOwnerDrawFixed, lbOwnerDrawVariable, lbVirtualOwnerDraw之一,而默认是lbStandard。—————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    —————————————————————————————————
      

  2.   

    说的有道理
    Style属性问题,呵呵
      

  3.   

    OnDrawItem 和 OnMeasureItem 事件结合起来用就能达到自己画的目的.
      

  4.   

    我已经将TListBox.Style属性值为lbOwnerDrawFixed,也能加入色块,只是在Form上,而不是在Listbox里,大家帮忙啊?
      

  5.   

    因为你的程序将土块画到Form上去了。将with (Control as TListBox).Canvas do改为:
    with (Control as TListBox) do—————————————————————————————————
    宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
    —————————————————————————————————