Tôi cần thay đổi màu của TPanel khi bật kiểu VCL. Tôi đã thử sử dụng và sửa đổi mã được liệt kê trong bài viết Changing the color of Edit Controls with VCL Styles Enabled nhưng không hoạt động đối với TPanel. Làm thế nào tôi có thể thay đổi màu sắc của một TPanel với VCL Styles kích hoạt?Làm cách nào để thay đổi màu của TPanel bằng kiểu VCL?
Trả lời
TPanel
không sử dụng móc kiểu để vẽ điều khiển, vì vậy bạn không thể sử dụng kỹ thuật được mô tả trong bài viết. thay vào đó, bạn phải ghi đè phương thức paint
.
Kiểm tra mẫu này bằng cách sử dụng lớp xen kẽ.
type
TPanel=Class(Vcl.ExtCtrls.TPanel)
protected
procedure Paint; override;
End;
Uses
Vcl.Styles,
Vcl.Themes;
{$R *.dfm}
{ TPanel }
procedure TPanel.Paint;
const
Alignments: array[TAlignment] of Longint = (DT_LEFT, DT_RIGHT, DT_CENTER);
VerticalAlignments: array[TVerticalAlignment] of Longint = (DT_TOP, DT_BOTTOM, DT_VCENTER);
var
Rect: TRect;
LColor: TColor;
LStyle: TCustomStyleServices;
LDetails: TThemedElementDetails;
TopColor : TColor;
BottomColor : TColor;
LBaseColor : TColor;
LBaseTopColor : TColor;
LBaseBottomColor: TColor;
Flags: Longint;
procedure AdjustColors(Bevel: TPanelBevel);
begin
TopColor := LBaseTopColor;
if Bevel = bvLowered then
TopColor := LBaseBottomColor;
BottomColor := LBaseBottomColor;
if Bevel = bvLowered then
BottomColor := LBaseTopColor;
end;
begin
Rect := GetClientRect;
LBaseColor := Color;//use the color property value to get the background color.
LBaseTopColor := clBtnHighlight;
LBaseBottomColor := clBtnShadow;
LStyle := StyleServices;
if LStyle.Enabled then
begin
LDetails := LStyle.GetElementDetails(tpPanelBevel);
if LStyle.GetElementColor(LDetails, ecEdgeHighLightColor, LColor) and (LColor <> clNone) then
LBaseTopColor := LColor;
if LStyle.GetElementColor(LDetails, ecEdgeShadowColor, LColor) and (LColor <> clNone) then
LBaseBottomColor := LColor;
end;
if BevelOuter <> bvNone then
begin
AdjustColors(BevelOuter);
Frame3D(Canvas, Rect, TopColor, BottomColor, BevelWidth);
end;
if not (LStyle.Enabled and (csParentBackground in ControlStyle)) then
Frame3D(Canvas, Rect, LBaseColor, LBaseColor, BorderWidth)
else
InflateRect(Rect, -Integer(BorderWidth), -Integer(BorderWidth));
if BevelInner <> bvNone then
begin
AdjustColors(BevelInner);
Frame3D(Canvas, Rect, TopColor, BottomColor, BevelWidth);
end;
with Canvas do
begin
if not LStyle.Enabled or not ParentBackground then
begin
Brush.Color := LBaseColor;
FillRect(Rect);
end;
if ShowCaption and (Caption <> '') then
begin
Brush.Style := bsClear;
Font := Self.Font;
Flags := DT_EXPANDTABS or DT_SINGLELINE or
VerticalAlignments[VerticalAlignment] or Alignments[Alignment];
Flags := DrawTextBiDiModeFlags(Flags);
if LStyle.Enabled then
begin
LDetails := LStyle.GetElementDetails(tpPanelBackground);
if not LStyle.GetElementColor(LDetails, ecTextColor, LColor) or (LColor = clNone) then
LColor := Font.Color;
LStyle.DrawText(Handle, LDetails, Caption, Rect, TTextFormatFlags(Flags), LColor)
end
else
DrawText(Handle, Caption, -1, Rect, Flags);
end;
end;
end;
Trong XE5, nếu bạn tắt cờ seClient trong tài sản StyleElements, sau đó tài sản Màu làm việc một lần nữa như mong đợi.
Thanks for the tip , đã nâng cấp một số mã Delphi cũ của tôi lên Berlin 10.1 Cập nhật 2 và nó không sơn nền của một hậu duệ kiểm soát TPanel tôi đã sử dụng –
@GeorgeBirbilis: Rất vui vì nó đã giúp bạn. Câu trả lời được chấp nhận dường như quá mức cần thiết. – costa
Dựa trên @ costa của câu trả lời, sử dụng:
StyleElements := StyleElements - [seClient];
trong constructor của lớp hậu duệ TPanel bạn
hoặc nếu bạn chỉ có một số TPanel (hoặc lớp hậu duệ) Ví dụ bạn có thể làm:
with myPanel do StyleElements := StyleElements - [seClient];
các - [...] cú pháp được sử dụng kể từ khi StyleElements là một tập hợp
Để biết thêm về StyleElements đọc bài viết này:
chỉnh VCL Styles cho hình thức và điều khiển - http://edn.embarcadero.com/article/42812
Cảm ơn rất nhiều ¡¡¡ – Salvador
có vẻ là một cách dễ dàng hơn nhiều, xem câu trả lời khác –