Skip to content

Commit 5f49a72

Browse files
Improved fix for issue #95:
- Separated creation and initialization of the ribbon framework. Constructor will now only perform non UI/handle related initialization of the framework - Load() will now call HandleNeeded before passing the parent form's handle to FFramework.Initialize. That way, recreating the window handle of the form no longer causes issues - ApplicationModes are now restored when recreating the window handle
1 parent f22d842 commit 5f49a72

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

Lib/UIRibbon.pas

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -656,10 +656,6 @@ constructor TUIRibbon.Create(AOwner: TComponent);
656656
end;
657657

658658
Parent := ParentForm; // Make the form the parent of the ribbon control.
659-
660-
if csDesigning in ComponentState then
661-
exit; // Initializing the ribbon doesn't work in design time, so exit here.
662-
663659
LoadFramework;
664660
end;
665661

@@ -1024,8 +1020,19 @@ procedure TUIRibbon.Load();
10241020
Inst: THandle;
10251021
lForm: TCustomForm;
10261022
begin
1023+
// Don't try to load the framework at design time
1024+
if csDesigning in ComponentState then
1025+
exit;
1026+
1027+
if (not Available) then
1028+
Height := 0;
1029+
10271030
if (Available) and (inherited Visible) and not (FLoaded) then
10281031
begin
1032+
//Load the framework, using the parent form's handle
1033+
FLoaded := True;
1034+
HandleNeeded;
1035+
FFramework.Initialize(GetParentForm(Self.Owner as TControl).Handle, Self);
10291036
// Load mapper for mapping between commands and VCL actions
10301037
fRibbonMapper := TRibbonMarkupElementList.LookupListByResourceName(FResourceName);
10311038
if Assigned(fRibbonMapper) then
@@ -1037,9 +1044,9 @@ procedure TUIRibbon.Load();
10371044
Inst := FResourceInstance;
10381045
try
10391046
FFramework.LoadUI(Inst, PChar(FResourceName + '_RIBBON'));
1040-
FLoaded := True;
10411047
except
10421048
on E: EOleException do begin
1049+
FLoaded := False;
10431050
E.Message := Format(sErrorLoadingRibbonRessource, [Self.ResourceName, e.Message, e.ErrorCode]);
10441051
raise;
10451052
end;
@@ -1049,6 +1056,10 @@ procedure TUIRibbon.Load();
10491056
if roAutoPreserveState in Options then
10501057
LoadRibbonSettings();
10511058

1059+
// Restore potential old application mode settings. Certain situations (e.g. RecreateWnd) will recreate the ribbon with the default modes, so we need to reapply them here.
1060+
if fApplicationModes <> [] then
1061+
Set_ApplicationModes(fApplicationModes);
1062+
10521063
lForm := GetParentForm(Self);
10531064
// Set the background color for the form if not yet defined. Use the same
10541065
// color as the ribbon bar, just a bit more brightened, if themes are enabled.
@@ -1070,31 +1081,27 @@ procedure TUIRibbon.LoadFramework;
10701081
var
10711082
Intf: IUnknown;
10721083
begin
1084+
// Don't try to load the framework at design time
10731085
if (csDesigning in ComponentState) then
10741086
exit;
1075-
FAvailable := Succeeded(CoCreateInstance(CLSID_UIRibbonFramework, nil,
1076-
CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IUnknown, Intf));
1077-
if (not FAvailable) then
1078-
Height := 0
1079-
else
1080-
begin
1081-
FFramework := Intf as IUIFramework;
1082-
FFramework.Initialize(GetParentForm(Self.Owner as TControl).Handle, Self);
1083-
end;
1087+
1088+
FAvailable := Succeeded(CoCreateInstance(CLSID_UIRibbonFramework, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IUnknown, Intf));
1089+
FFramework := Intf as IUIFramework;
10841090
end;
10851091

10861092
procedure TUIRibbon.CreateWnd;
10871093
begin
10881094
inherited;
1089-
LoadFramework(); // Always force reload of framework, see issue #95
1090-
Load();
1095+
Load;
10911096
end;
10921097

10931098
procedure TUIRibbon.DestroyWnd;
10941099
begin
10951100
inherited;
10961101
if Assigned(FFramework) then
10971102
FFramework := nil;
1103+
if (csRecreating in ControlState) then
1104+
LoadFramework;
10981105
FCommands.Clear;
10991106
fLoaded := False;
11001107
end;

0 commit comments

Comments
 (0)