使用Delphi Tokyo突出显示主题Delphi应用程序中的控件
使用Delphi Tokyo 10.2,使用程式化主题.我试图突出显示表单上的组件,例如,ComboBoxes,EditTexts等.例如,如果用户输入了无效数据,我想突出显示该组件.
在过去,我们只是将组件着色为红色,并且通常通过调整大小/移动/重绘来保持颜色.现在有了主题,我们需要做更多的事情才能让颜色显示并持续存在. 我试过禁用每个组件的StyleElements [seFont,seClient,seBorder]属性来强制显示颜色.这有效但似乎很糟糕,特别是当有许多组件被验证时.此外,简单地将组件着色为红色可能看起来不适合某些主题. 我还尝试使用WinAPI SetRop2(..)在组件周围绘制一个红色矩形.例如,here是一些聪明的代码,我调整了一个TWinControl并在它周围画一个红色框;我也可以使用类似的调用删除redbox.这有效: ……但显然不会通过重绘而坚持下去.似乎添加自定义绘画方法可能在这里有点过分.除非,有更好的方法吗? 我考虑过的其他事情: 所有的组件都放在面板上,我考虑使用受保护的黑客在面板的画布上围绕组件绘制红色rects,但同样,更多的自定义绘图例程…… 我也在考虑根据需要动态绘制TShapes,但这让我感到愚蠢. 在相同的情况下必须有其他人,数据输入验证在旧版本的Delphi中工作得很好,但在主题时看起来并不那么好.使用主题时最好的方法是什么? SetRop2(..)方法似乎是最干净的,但有人可以建议一种简单的方法来保持颜色的持久性吗?我也欢迎其他想法.谢谢. 编辑 这对我很有用,我希望它对其他人有帮助. // 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; 这样称为删除HiLites …… begin HiLiteMe(checkListBox1,FALSE); // Draw a RedBox around the CheckListBox,FALSE); // Draw a Blue Box around the Button,Required. end; 解决方法
我建议只在控件的一侧(例如左侧或底部)显示或隐藏一个红色TShape.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |