Ho bisogno di cambiare il colore di un TPanel quando gli stili VCL sono abilitati. Ho provato a utilizzare e modificare il codice elencato nell'articolo Changing the color of Edit Controls with VCL Styles Enabled, ma non funziona per un TPanel. Come posso cambiare il colore di un TPanel con gli stili VCL abilitati?Come posso cambiare il colore di un TPanel con gli stili VCL abilitati?
risposta
Il TPanel
non utilizza un gancio di stile per disegnare il controllo, quindi non è possibile utilizzare la tecnica descritta nell'articolo. devi invece sostituire il metodo paint
.
Verificare questo esempio utilizzando una classe interposer.
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;
In XE5, se si spegne la bandiera seClient nella proprietà StyleElements, quindi la proprietà Colore funziona di nuovo come previsto.
Grazie per la punta , aggiornavo il mio vecchio codice Delphi con Berlin 10.1 Update 2 e non dipingevo lo sfondo di un discendente di controllo TPanel che stavo usando –
@GeorgeBirbilis: sono contento che ti abbia aiutato. La risposta accettata sembrava eccessiva. – costa
Sulla base di @ di Costa risposta, l'uso:
StyleElements := StyleElements - [seClient];
nel costruttore della classe discendente TPanel
o se semplicemente avete qualche TPanel (o classe discendente) istanza si può fare:
with myPanel do StyleElements := StyleElements - [seClient];
L'- [...] sintassi viene utilizzato in quanto i StyleElements è un insieme
Per ulteriori informazioni su StyleElements leggere questo articolo:
Stili di sintonia VCL per Form e controlli - http://edn.embarcadero.com/article/42812
Grazie molto ¡¡¡ – Salvador
sembra che ci sia un modo molto più semplice, vedere l'altra risposta –