RT。俺用了一句Inherited;好象不解决问题……在TListBox中。比如:
procedure TfrmHiQuery.lstResultDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  sExecStat: AnsiString;
begin
  with lstResult do
  begin
    sExecStat := ......;
    if sExecStat = 'done' then
      Canvas.Font.Color := clGreen;
  end;  inherited;
end;则运行后lstResult中什么也显示不出来了……

解决方案 »

  1.   

    注:lstResult的Style已经设为lbOwnerDrawFixed。
      

  2.   

    难道响应了OnDrawItem事件后就必须写完所有事件代码吗?郁闷……
      

  3.   

    所有设置不变,在设计期时为ListBox1.items添加值 包括'done' procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    begin
        if TListBox(Control).Items[index]='done' then
        begin
          TListBox(Control).Canvas.Font.Color:=clblack;
          TListBox(Control).Canvas.TextRect(rect,rect.Left,rect.Top,TListBox(Control).Items[index]);
        end
       else
       begin
         TListBox(Control).Canvas.Font.Color:=clred;
         TListBox(Control).Canvas.TextRect(rect,rect.Left,rect.Top,TListBox(Control).Items[index]);
       end;
    end;
      

  4.   

    把该控件对应的缺省的OnDrawItem代码拷贝出来加在你的代码之前吧。
      

  5.   

    因为在lbOwnerDrawFixed的作用下,Owner的默认行为是不在Canvas上画任何东西的。故inherited指示字被编译器忽略。你在begin处设断点跟踪VCL看看
    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    begin
    //
    end;
      

  6.   

    //这里就响应了你的代码
     if Assigned(FOnDrawItem) then FOnDrawItem(Self, Index, Rect, State) else
      begin
       //下面是inherited部分
        FCanvas.FillRect(Rect);
        if Index < Count then
        begin
          Flags := DrawTextBiDiModeFlags(DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX);
          if not UseRightToLeftAlignment then
            Inc(Rect.Left, 2)
          else
            Dec(Rect.Right, 2);
          Data := '';
          if (Style in [lbVirtual, lbVirtualOwnerDraw]) then
            Data := DoGetData(Index)
          else
            Data := Items[Index];
          DrawText(FCanvas.Handle, PChar(Data), Length(Data), Rect, Flags);
        end;//从上面的代码看出 你要是重写了这个事件的话,你的代码就完全替代了inherited,就必须处理所有的情况
      

  7.   


    注:lstResult的Style已经设为lbOwnerDrawFixed。