使用Delphi Tokyo突出显示主题Delphi应用程序中的控件

sse*_*sse 5 delphi winapi themes

使用Delphi Tokyo 10.2,使用程式化主题.我想强调的形式,例如在组件ComboBoxes,EditTexts等.例如,如果用户键入的数据,我想强调的组件.

在过去,我们只是对组件进行着色Red,并且通常通过调整大小/移动/重新绘制来保持颜色.现在有了主题,我们需要做更多的事情才能让颜色显示并持续存在.

我试过禁用每个组件的StyleElements [seFont, seClient, seBorder]属性来强制显示颜色.这有效但似乎很糟糕,特别是当有许多组件被验证时.此外,简单地将组件着色为红色可能看起来不适合某些主题.

我也尝试过简单地在组件周围绘制一个红色矩形WinAPI SetRop2(..).例如,这里有一些聪明的代码,我调整了一个TWinControlDraw一个redbox围绕它; 我也可以删除redbox使用类似的电话.这有效:

在此输入图像描述

......但显然不会通过重绘而坚持下去.似乎添加自定义绘画方法可能在这里有点过分.除非,有更好的方法吗?

我考虑过的其他事情:

所有的组件都放在面板上,我考虑使用受保护的黑客rect在面板的画布周围绘制红色的组件,但同样,更多的自定义绘图例程......

我也在考虑TShapes根据需要动态绘制,但这让我觉得很傻.

在相同的情况下必须有其他人,例如,数据输入验证在旧版本的Delphi中工作得很好,但在主题时看起来并不那么好.使用主题时最好的方法是什么?这种SetRop2(..)方法似乎是最干净的,但有人可以建议一种简单的方法来保持颜色的持久性吗?我也欢迎其他想法.谢谢.

编辑 所以也许,只是动态地绘制TShapes无效的响应并不是那么糟糕.它们通过重新绘制持续存在并且不会从中重复TWinControl,这意味着它们会自动显示在它们突出显示的控件之后.

这对我很有用,我希望它对其他人有帮助.

// assuming owning control will be free'd properly and 
// will in turn free HI_LITE Box.
// 
// tantamount to adding an instance variable, TShape, to existing Control,
// since class helpers don't allow. And I don't want to descend 
// new controls just to have a hiLiteBox Instance Variable.

procedure HiLiteMe(aControl : TWinControl; HILITE_FLAG : Boolean = TRUE; aColor : TColor = clRed);
const OFFSET = 4;                         // specify the offset of the border size of the box.
const BOX_NAME_PREFIX = 'HI_LITE_BOX_';

var
   hiLiteBox : TShape;      // reference created on stack, but object created on the heap,
   uniqueBoxName : String;    // so use the persistent aControl's owned component list to maintain the reference.
begin
  uniqueBoxName := BOX_NAME_PREFIX + aControl.Name;              // uniquename for each associated HiLiteBox.
  HiLiteBox := aControl.FindComponent(uniqueBoxName) as TShape;  // phishing for the HiLiteBox if it was previously created.

  if NOT Assigned(hiLiteBox) then         // create HiLiteBox and make persist outside this proc.
  begin
    if NOT HILITE_FLAG then exit;         // don't create a box if we're just going to hide it anyway.
    hiLiteBox := TShape.Create(aControl); // Create HiLiteBox, setting aControl as owner, quicker retrieval using aControl.findComponent
    hiLiteBox.Parent := aControl.Parent;  // Render the box on the control's parent, e.g., panel, form, etc.
    hiLiteBox.Name :=  uniqueBoxName;
    hiLiteBox.Pen.Color := aColor;        // Color the Pen
    hiLiteBox.Pen.Width := offset-1;      // Make the Pen just slightly smaller than the offset.
    hiLiteBox.Brush.Color := clWindow;    // Choose a brush color, to fill the space between the pen and the Control
    hiLiteBox.Left := aControl.Left - offset;
    hiLiteBox.Width := aControl.Width + offset*2;
    hiLiteBox.Top := aControl.Top - offset;
    hiLiteBox.Height := aControl.Height + offset*2;
  end;

  hiLiteBox.Visible := HILITE_FLAG; // Show/Hide HiLite as appropriate.
end;
Run Code Online (Sandbox Code Playgroud)

用红色和蓝色的盒子打电话给HiLite吧......

begin
  HiLiteMe(checkListBox1, TRUE, clRed);   // Draw a RedBox around the CheckListBox, e.g., Invalid.
  HiLiteMe(bitBtn3, TRUE, clBlue);        // Draw a Blue Box around the Button, e.g., Required.
end;
Run Code Online (Sandbox Code Playgroud)

蓝色框表示必填,红色框表示无效

这样称为删除HiLites...

begin
  HiLiteMe(checkListBox1, FALSE);   // Draw a RedBox around the CheckListBox, e.g., Invalid.
  HiLiteMe(bitBtn3, FALSE);        // Draw a Blue Box around the Button, e.g., Required.
end;
Run Code Online (Sandbox Code Playgroud)

Rae*_*elB 1

我建议仅在显示或隐藏的控件的一侧(例如左侧或底部)使用红色 TShape。