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

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

发布时间:2020-12-15 09:46:37 所属栏目:大数据 来源:网络整理
导读:使用Delphi Tokyo 10.2,使用程式化主题.我试图突出显示表单上的组件,例如,ComboBoxes,EditTexts等.例如,如果用户输入了无效数据,我想突出显示该组件. 在过去,我们只是将组件着色为红色,并且通常通过调整大小/移动/重绘来保持颜色.现在有了主题,我们需要做更
使用Delphi Tokyo 10.2,使用程式化主题.我试图突出显示表单上的组件,例如,ComboBoxes,EditTexts等.例如,如果用户输入了无效数据,我想突出显示该组件.

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

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

我还尝试使用WinAPI SetRop2(..)在组件周围绘制一个红色矩形.例如,here是一些聪明的代码,我调整了一个TWinControl并在它周围画一个红色框;我也可以使用类似的调用删除redbox.这有效:

enter image description here

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

我考虑过的其他事情:

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

我也在考虑根据需要动态绘制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;

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

begin
  HiLiteMe(checkListBox1,TRUE,clRed);   // Draw a RedBox around the CheckListBox,Invalid.
  HiLiteMe(bitBtn3,clBlue);        // Draw a Blue Box around the Button,Required.
end;

blue box indicates required,red box indicates invalid

这样称为删除HiLites ……

begin
  HiLiteMe(checkListBox1,FALSE);   // Draw a RedBox around the CheckListBox,FALSE);        // Draw a Blue Box around the Button,Required.
end;

解决方法

我建议只在控件的一侧(例如左侧或底部)显示或隐藏一个红色TShape.

(编辑:李大同)

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

    推荐文章
      热点阅读