Einzelnen Beitrag anzeigen

Amateurprofi

Registriert seit: 17. Nov 2005
Ort: Hamburg
1.041 Beiträge
 
Delphi XE2 Professional
 
#12

AW: Farbänderung in Delphi

  Alt 17. Mai 2015, 02:27
Ich habe das in einem Spiel einmal so gelöst.
Falls ich beim Kopieren irgendein Detail vergessen habe: Sorry.

Delphi-Quellcode:
type
   TCaptionState=(csNothing,csWon,csLost,csEnd,csWarn,csInfo);

   // Farben der Window-Caption
   TCaptionColor=Record
      BK1:TColor;
      BK2:TColor;
      FC:TColor;
   End;
   TCaptionColors=Array[TCaptionState] of TCaptionColor;

const
   CaptionColors:TCaptionColors=
      ((BK1:$6A240A; BK2:$F0CAA6; FC:clWHite) // csNothing
      ,(BK1:clGreen; BK2:clYellow; FC:clWhite) // csWon
      ,(BK1:clRed; BK2:clYellow; FC:clWhite) // csLost
      ,(BK1:$FF8700; BK2:clYellow; FC:clWhite) // csEnd
      ,(BK1:clYellow; BK2:clRed; FC:clRed) // csWarn
      ,(BK1:clYellow; BK2:$00E948; FC:clBlack)); // csInfo
In Deklaration der Form:
Delphi-Quellcode:
    
    CaptionState:TCaptionState;
    PROCEDURE WMNCPAINT(var Msg:TMessage); message WM_NCPAINT;
  
    PROCEDURE WMNCACTIVATE(var Msg: TWMNCActivate); message WM_NCACTIVATE;
    PROCEDURE CMTEXTCHANGED(var Msg:TMessage); message CM_TEXTCHANGED;
    PROCEDURE DrawCaption;
Im implementation Teil

Delphi-Quellcode:
PROCEDURE TMain.WMNCPAINT(var Msg:TMessage);
begin
   inherited;
   DrawCaption;
end;
Delphi-Quellcode:
PROCEDURE TMain.WMNCACTIVATE(var Msg:TWMNCActivate) ;
begin
   inherited;
   if Msg.Active then DrawCaption;
end;
Delphi-Quellcode:
PROCEDURE TMain.CMTEXTCHANGED(var Msg:TMessage);
begin
   inherited;
   DrawCaption;
end;
Delphi-Quellcode:
PROCEDURE TMain.DrawCaption;
var FH,FW,CH,IH,IW,BW,X,Y:Integer; Bmp:TBitMap; Canvas:TCanvas; R:TRect;
    Metrics:TNONCLIENTMETRICS;
begin
   if WindowState=wsMinimized then Exit;
   if CaptionState=csNothing then Exit;
   Canvas:=TCanvas.Create;
   Canvas.Handle:=GetWindowDC(Self.Handle);
   CH:=GetSystemMetrics(SM_CYCAPTION)-1; // Unten scheint ein Pixel zu fehlen
   BW:=3*GetSystemMetrics(SM_CXSize); // W Buttons
   // Rect der Caption, abzüglich der Minimize,Maximize,Close Buttons definieren
   if WindowState=wsMaximized then begin
      SetRect(R,0,0,Width-BW,CH);
   end else begin
      FH:=GetSystemMetrics(SM_CYFRAME);
      FW:=GetSystemMetrics(SM_CXFRAME);
      SetRect(R,FW,FH,Width-FW-BW,FH+CH);
   end;
   // Icon in Bitmap kopieren
   IH:=GetSystemMetrics(SM_CYSMICON); // H Icon
   IW:=GetSystemMetrics(SM_CXSMICON); // W Icon
   Bmp:=TBitMap.Create;
   Bmp.Width:=IW;
   Bmp.Height:=IH;
   X:=R.Left+2;
   Y:=(R.Top+R.Bottom-IH) div 2;
   BitBlt(Bmp.Canvas.Handle,0,0,IW,IH,Canvas.Handle,X,Y,srccopy);
   // Canvas Hintergrund zeichen
   with CaptionColors[CaptionState] do
      GradientFillCanvas(Canvas,BK1,BK2,R,gdHorizontal);
   // Icon zeichnen
   Canvas.Draw(X,Y,Bmp);
   // Caption Text zeichnen
   Inc(R.Left,2+IW+4);
   Dec(R.Right,4);
   Metrics.cbSize:=SizeOf(TNONCLIENTMETRICS);
   SystemParametersInfo(SPI_GETNONCLIENTMETRICS,Metrics.cbSize,@Metrics,0);
   Canvas.Font.Height:=Metrics.lfCaptionFont.lfHeight;
   Canvas.Font.Style:=[fsBold];
   Canvas.Font.Color:=CaptionColors[CaptionState].FC;
   Canvas.Brush.Style:=bsClear;
   Y:=(R.Top+R.Bottom-Canvas.TextHeight(' ')) div 2;
   Canvas.TextRect(R,R.Left,Y,Caption);
   // Aufräumen
   ReleaseDC(Self.Handle,Canvas.Handle);
   Canvas.Free;
   Bmp.Free;
end;
Eine Farbänderung wird erreicht, indem CaptionState auf den gewünschten Wert gesetzt und Caption ein anderer Text zugeordnet wird.
Das zuweisen eines neuen Textes löst dann ein CM_TEXTCHANGED aus.
Gruß, Klaus
Die Titanic wurde von Profis gebaut,
die Arche Noah von einem Amateur.
... Und dieser Beitrag vom Amateurprofi....

Geändert von Amateurprofi (17. Mai 2015 um 02:38 Uhr)
  Mit Zitat antworten Zitat