2005. május 17., kedd

How to change the color of a menu


Problem/Question/Abstract:

How to change the color of a menu

Answer:

You can set the menu to owner-drawn and draw the items yourself in the OnDrawItem handlers. This does not work for the menubar, however.

The menu colors are a system setting which the user can specify in the display properties applet. You should not mess with that. One can do so when the app is activated and restore the old setting when it is deactivated or closed, but that is also not very satisfactory since the change still hits all other running apps as well and in the case of changes to the color scheme it also may take a noticable delay to take effect.

If you want to play with it: drop a TApplicationEvents component on the form, connect the OnActivate and OnDeactivate events of it to handlers, add one for the forms OnClose event, modify as below.

{ ... }
private
{ Private declarations }
FOldMenuColor: TColorRef;
FOldMenuTextColor: TColorRef;
public
{ Public declarations }
end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ApplicationEvents1Activate(Sender: TObject);
var
  newcolors: array[0..1] of TColorRef;
  indices: array[0..1] of Integer;
begin
  FOldMenuColor := ColorToRGB(clMenu);
  FOldMenuTextColor := ColorToRGB(clMenuText);
  newcolors[0] := ColorToRGB(clAqua);
  newcolors[1] := ColorToRGB(clNavy);
  indices[0] := COLOR_MENU;
  indices[1] := COLOR_MENUTEXT;
  SetSysColors(2, indices, newcolors);
end;

procedure TForm1.ApplicationEvents1Deactivate(Sender: TObject);
var
  newcolors: array[0..1] of TColorRef;
  indices: array[0..1] of Integer;
begin
  newcolors[0] := FOldMenuColor;
  newcolors[1] := FOldMenuTextColor;
  indices[0] := COLOR_MENU;
  indices[1] := COLOR_MENUTEXT;
  SetSysColors(2, indices, newcolors);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  ApplicationEvents1Deactivate(self);
end;

Nincsenek megjegyzések:

Megjegyzés küldése