gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:06:46 25 4
gpt4 key购买 nike

我正在将 ListBox.Style := lbOwnerDrawFixedOnDrawItem 一起使用:

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;

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

如何删除矩形?

最佳答案

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

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 处理消息时,不会为列表框调用默认窗口过程。

关于delphi - 如何从 TListBox 中删除虚线焦点矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28649219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com