Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Change windows title color!

I like to know how to change the form's title color (background of caption).

Thanks.
0
jlislo
Asked:
jlislo
  • 2
  • 2
  • 2
  • +1
1 Solution
 
Lee_NoverCommented:
the most simple way is to get some TCaptionBar component :)
check this : http://www.torry.net/captions.htm
0
 
SteveWaiteCommented:
try this from Neil Rubenkings book, Delphi Programming Problem Solver. (ISBN 1-56884-795-5).

unit Mytitleu;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics,
  Controls, Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
    procedure DrawTitle(IsActive: Boolean ;const S : String);
    procedure WMNCPaint(VAR Msg: TWMNCPaint);
      message WM_NCPAINT;
    procedure WMNCActivate(VAR Msg: TWMNCActivate);
      message WM_NCACTIVATE;
    procedure WMSetText(VAR Msg: TWMSetText);
      message WM_SETTEXT;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
{$R *.DFM}

implementation

procedure TForm1.DrawTitle(IsActive: Boolean ;const S : String);
VAR
  TopOfs: Word;
  R: TRect;
  XF,            {frame width}
  YF,            {frame height}
  XS,            {caption bar button width}
  YS: Integer;  {caption bar button height}
begin
  XF := GetSystemMetrics(SM_CXFRAME);
  YF := GetSystemMetrics(SM_CYFRAME);
  XS := GetSystemMetrics(SM_CXSIZE);
  YS := GetSystemMetrics(SM_CYSIZE);
  WITH Canvas DO
    try
      R := Bounds(XF, YF, ClientWidth, YS);
      R.Right := R.Right-XS-1;
      IF biSystemMenu IN BorderIcons THEN R.Left := R.Left+XS+1;
      IF biMinimize IN BorderIcons THEN R.Right := R.Right-XS-1;
      IF biMaximize IN BorderIcons THEN R.Right := R.Right-XS-1;
      Handle := GetWindowDC(Self.Handle);
      Brush.Color := clGreen;
      Font.Color  := clBlack;
      Font.Style  := [];
      Font.Name   := 'Times New Roman';
      TopOfs := TextHeight(S);
      IF TopOfs > YS THEN TopOfs := 0
      ELSE TopOfs := (YS-TopOfs) DIV 2;
      SetTextAlign(Handle, TA_CENTER);
      TextRect(R, (R.Right+R.Left) div 2, R.Top+TopOfs, S);
    finally
      ReleaseDC(Self.Handle, Handle);
      Handle := 0;
    end;
end;

procedure TForm1.WMNCPaint(VAR Msg: TWMNCPaint);
BEGIN
  Inherited;
  DrawTitle(Active, Caption);
END;

procedure TForm1.WMNCActivate(VAR Msg: TWMNCActivate);
BEGIN
  Inherited;
  DrawTitle(Msg.Active, Caption);
END;

procedure TForm1.WMSetText(VAR Msg: TWMSetText);
BEGIN
  Inherited;
  DrawTitle(Active, Caption);
END;

end.

Regards,
Steve
0
 
ginsonicCommented:
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jlisloAuthor Commented:
Thank you all for the help. I find a simple way to do this, please see the followinf code

------------------------------------------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    procedure SetColor ;
    procedure RestoreColor ;
  public
    { Public declarations }
  end;


var
  Form1: TForm1;
  oldColorValue,ColorValue: array [1..5] of longint;
  ColorIndex: array [1..5] of integer;

implementation

{$R *.DFM}

procedure TForm1.SetColor;
begin
  ColorIndex[1] := COLOR_ACTIVECAPTION   ;
  ColorValue[1] := strtoint('$0000FF')   ;

  SetSysColors(1,ColorIndex,ColorValue);
  PostMessage(HWND_BROADCAST,WM_SYSCOLORCHANGE,0,0);
end;

procedure TForm1.RestoreColor;
begin
  ColorIndex[1] := COLOR_ACTIVECAPTION   ;
  ColorValue[1] := oldcolorvalue[1];
  SetSysColors(1,ColorIndex,ColorValue);
  PostMessage(HWND_BROADCAST,WM_SYSCOLORCHANGE,0,0);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   oldColorValue[1] := GetSysColor(COLOR_ACTIVECAPTION );
   Application.OnActivate := FormActivate ;
   Application.OnDeactivate := FormDeactivate ;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
   SetColor ;
end;

procedure TForm1.FormDeactivate(Sender: TObject);
begin
   RestoreColor ;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
   RestoreColor ;
   Application.OnActivate := nil ;
   Application.OnDeactivate := nil ;
end;


end.
 
0
 
jlisloAuthor Commented:
I find the answer myself. Thanks.
0
 
Lee_NoverCommented:
ummmm
well you already lost the 50 points assigned to this q
so you might as well give them away ...
and it's not about the points .. it's the attitude
0
 
SteveWaiteCommented:
Many thanks, jlislo.

Steve
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now