加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 如何从TListBox中删除虚线焦点矩形?

发布时间:2020-12-15 09:35:06 所属栏目:大数据 来源:网络整理
导读:我使用ListBox.Style:= lbOwnerDrawFixed与OnDrawItem: procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; ARect: TRect; State: TOwnerDrawState);begin with ListBox1.Canvas do begin Brush.Color := clWhite; Pen.Color := cl
我使用ListBox.Style:= lbOwnerDrawFixed与OnDrawItem:

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  ARect: TRect; State: TOwnerDrawState);
begin
  with ListBox1.Canvas do begin
    Brush.Color := clWhite;
    Pen.Color := clWhite;
    FillRect(ARect);
  end;
end;

它应该给出一个空的空间来绘制我想要的东西,但事实并非如此.当项目聚焦时,我仍然可以看到它周围的虚线矩形.

如何删除矩形?

解决方法

在OnDrawItem事件处理程序返回后,焦点矩形由TCustomListBox.CNDrawItem中的VCL(*)绘制.

procedure TCustomListBox.CNDrawItem(var Message: TWMDrawItem); 

...

  ..
    if Integer(itemID) >= 0 then
      DrawItem(itemID,rcItem,State)      //-> calls message handler
    else
      FCanvas.FillRect(rcItem);
    if (odFocused in State) and not TStyleManager.IsCustomStyleActive then
      DrawFocusRect(hDC,rcItem);          //-> draws focus rectangle
  ..
..

无论你在事件处理程序中绘制什么,以后都会被VCL集中.

但请注意VCL使用DrawFocusRect绘制焦点矩形:

Because DrawFocusRect is an XOR function,calling it a second time
with the same rectangle removes the rectangle from the screen.

所以只需自己调用DrawFocusRect并让VCL调用擦除它:

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  ARect: TRect; State: TOwnerDrawState);
begin
  with ListBox1.Canvas do begin
    Brush.Color := clWhite;
    Pen.Color := clWhite;
    FillRect(ARect);
    if odFocused in State then  // also check for styles if there's a possibility of using ..
      DrawFocusRect(ARect);
  end;
end;

(*)通常,默认窗口过程为所有者绘制的列表框项目绘制焦点矩形以响应WM_DRAWITEM消息.但是,在VCL处理消息时,不会为列表框调用默认窗口过程.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读