应该不难:在OnDrawItem中如何使得该控件缺省的OnDrawItem也执行?

rick29 2003-10-22 09:46:51
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中什么也显示不出来了……
...全文
31 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
rick29 2003-10-22
  • 打赏
  • 举报
回复
难道响应了OnDrawItem事件后就必须写完所有事件代码吗?郁闷……
rick29 2003-10-22
  • 打赏
  • 举报
回复
注:lstResult的Style已经设为lbOwnerDrawFixed。
superyys 2003-10-22
  • 打赏
  • 举报
回复
?
注:lstResult的Style已经设为lbOwnerDrawFixed。
delphi_xizhousheng 2003-10-22
  • 打赏
  • 举报
回复
//这里就响应了你的代码
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,就必须处理所有的情况
delphi_xizhousheng 2003-10-22
  • 打赏
  • 举报
回复
因为在lbOwnerDrawFixed的作用下,Owner的默认行为是不在Canvas上画任何东西的。故inherited指示字被编译器忽略。你在begin处设断点跟踪VCL看看
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
//
end;
zhoutian618 2003-10-22
  • 打赏
  • 举报
回复
把该控件对应的缺省的OnDrawItem代码拷贝出来加在你的代码之前吧。
delphi_xizhousheng 2003-10-22
  • 打赏
  • 举报
回复
所有设置不变,在设计期时为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;

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧