I stumbled across a really weird behavior recently. When I use a TActionMainMenuBar (or a TActionToolBar) in my program, compile and run, and then start Photoshop CS5 or Internet Explorer 9, the ActionMainMenuBar (and ActionToolBar) loses all its settings. The colors defined in the assigned colormap disappear and the font settings are also lost. Has anyone seen this before and knows a workaround?
D2007 Pro (all updates applied), D2010 Pro (all updates applied), Vista Home Premium 32 bit, NVidia GForce 8600 GT, latest driver installed.
Steps to reproduce:
If you start Photoshop or IE first and then the Delphi program, nothing happens. The bug is also present when in design mode in the IDE. A fellow developer has already confirmed the described behavior for his system with Win7 Pro 32bit and a ATI Radeon 9800 Pro.
Thx for any comments/ solutions
Phil
PS: Using Photoshop CS3 doesn't produce this bug
Here's one other way of reproducing the problem:
Perform(WM_SETTINGCHANGE, 0, 0);
in its click handler.
So now we know that a WM_SETTINGCHANGE
broadcast might be causing the problem, we might wonder if launching IE produces the same:
type
TForm1 = class(TForm)
..
protected
procedure WMSettingChange(var Message: TWMSettingChange);
message WM_SETTINGCHANGE;
..
procedure TForm1.WMSettingChange(var Message: TWMSettingChange);
begin
Memo1.Lines.Add(IntToHex(Message.Flag, 4) + ', ' + Message.Section);
inherited;
end;
After we run our application and then launch IE, a few seconds later the below appears in the memo:
0000, Software\Microsoft\Internet Explorer\SearchScopes
I have no idea what IE has to say to our form (and all other top-level windows) at every launch, and I don't know if it is doing this on every Windows box on earth or just yours and mine, but evidently the ActionMainMenuBar
is no good at handling it.
A win control (the form), receiving a WM_WININICHANGE
, performs a CM_WININICHANGE
and upon receiving it broadcasts the same to all its controls. The below is how it is handled by the menu bar:
procedure TCustomActionMainMenuBar.CMWininichange(var Message: TWMWinIniChange);
begin
inherited;
RequestAlign;
Font.Assign(Screen.MenuFont);
end;
Thinking that the system menu font could have been changed (the code should have looked for a 'WindowsThemeElement' or 'WindowMetrics' section in the message, but anyway..), it is reassigned from the refreshed Screen.MenuFont
. The problem is, we weren't exactly using it.
Additionally the ColorMap responds to a CM_WININICHANGE
by resetting its colors by calling its UpdateColors
method. This is even documented:
UpdateColors is called automatically when an ActionBand component receives a CM_WININICHANGE message.
type
// Derive your own ColorMap that would reset its own colors.
// This is an interposer for simplicity..
TTwilightColorMap = class(actncolormaps.TTwilightColorMap)
public
procedure UpdateColors; override;
published
property Color default clGreen;
property FontColor default clYellow;
property MenuColor default $4488FF;
// reintroduce as many property as necessary, probably all is necessary..
end;
TForm1 = class(TForm)
..
private
FSaveMenuFont: TFont; // will hold initial main menu bar's font settings
protected
procedure WMSettingChange(var Message: TWMSettingChange);
message WM_SETTINGCHANGE;
end;
..
procedure TForm1.FormCreate(Sender: TObject);
begin
FSaveMenuFont := TFont.Create;
FSaveMenuFont.Assign(ActionMainMenuBar1.Font);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FSaveMenuFont.Destroy;
end;
procedure TForm1.WMSettingChange(var Message: TWMSettingChange);
begin
inherited;
// The below are the *section*s that really changing system settings
// would notify that I'm aware of, there may be more...
if (Message.Section <> 'WindowsThemeElement')
or (Message.Section <> 'WindowMetrics') then
ActionMainMenuBar1.Font.Assign(FSaveMenuFont)
else
// Develop your logic here. The system menu font might really have been
// changed. You can get it from Screen.MenuFont. But then if we had been
// using the system font, the control already applies the change by default.
end;
procedure TTwilightColorMap.UpdateColors;
begin
inherited;
// Reset your colors, note that system colors might have been
// changed or not. If changed, they should be reflected in 'cl..' constants.
Color := clGreen;
FontColor := clYellow;
MenuColor := $4488FF;
end;
Found it! The problem was that the Colormap property of the ActionMainMenuBar1 is reset to ActionMainMenuBar1.DefaultColormap every time IE9 or Photoshop CS5 is started/closed. I will accept Sertac's solution as an answer because it pointed me into the right direction how to solve this issue.
Here's the final code. It works flawlessly (as far as I can tell) with both IE9 and Photoshop CS5 now.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ActnMan, ActnColorMaps, ActnList, ToolWin, ActnCtrls, ActnMenus,
StdCtrls;
type
TForm1 = class(TForm)
ActionManager1: TActionManager;
ActionMainMenuBar1: TActionMainMenuBar;
Action1: TAction;
Action2: TAction;
Action3: TAction;
Action4: TAction;
TwilightColorMap1: TTwilightColorMap;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure ActionMainMenuBar1GetControlClass(Sender: TCustomActionBar;
AnItem: TActionClient; var ControlClass: TCustomActionControlClass);
protected
procedure WMSettingChange(var Message: TWMSettingChange); message WM_SETTINGCHANGE;
private
{ Private declarations }
FSaveMenuFont: TFont; // will hold initial main menu bar's font settings
FSaveColormap: TTwilightColormap;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
// Fixing paint issue when IE9 was run and closed again
procedure TForm1.ActionMainMenuBar1GetControlClass(Sender: TCustomActionBar;
AnItem: TActionClient; var ControlClass: TCustomActionControlClass);
begin
ActionMainMenuBar1.ColorMap.Assign(FSaveColormap);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FSaveMenuFont := TFont.Create;
FSaveMenuFont.Assign(ActionMainMenuBar1.Font);
FSaveColormap := TTwilightColormap.Create(Self);
FSaveColormap.Assign(ActionMainMenuBar1.Colormap);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
FSaveMenuFont.Destroy;
FSaveColormap.Destroy;
end;
procedure TForm1.WMSettingChange(var Message: TWMSettingChange);
begin
inherited;
// Memo1.Lines.Add(IntToHex(Message.Flag, 4) + ', ' + Message.Section);
ActionMainMenuBar1.Font.Assign(FSaveMenuFont);
// In case Photoshop CS5 was run and closed before
ActionMainMenuBar1.ColorMap.Assign(FSaveColormap);
end;
end.
Thanks again everyone for your help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With