There seems to be a problem using InsertMenu.
Using InsertMenu from within a program does not execute the OnClick action (see example below with button DUMMY).
Using InsertMenu from an external plugin executes incorrect OnClick actions (see example below with button PLUGIN).
Is this a problem with KOL or just my usage? Simplified source code is below:
Main app code:
program mainapp;
uses
Messages, Windows, KOL;
var
PopupMenu: PMenu;
procedure ButtonLoadClick(Dummy: Pointer; Sender: PObj);
var
Module: HModule;
PluginMain: procedure(MainPopupMenu: HMenu); stdcall;
begin
Module := LoadLibrary(PChar(GetStartDir + 'plugin.dll'));
if Module <> 0 then
begin
ShowMessage('Plugin load: OK');
PluginMain := GetProcAddress(Module, 'PluginMain');
if Assigned(PluginMain) then
begin
try
PluginMain(PopupMenu.Handle);
except
ShowMessage('Error loading plugin');
end;
end;
end
else
begin
ShowMessage('Plugin load: Error');
end;
end;
procedure DummyItemClick(Dummy: Pointer; Sender: PMenu; Item: Integer);
begin
ShowMsg('DUMMY', MB_OK);
end;
procedure ButtonDummyClick(Dummy: Pointer; Sender: PObj);
var
MyPopup: PMenu;
begin
MyPopup := NewMenu(nil, 0, ['xxx', 'yyy', 'zzz'], TOnMenuItem(MakeMethod(nil, @DummyItemClick)));
InsertMenu(PopupMenu.Handle, 0, MF_BYPOSITION or MF_POPUP, MyPopup.Handle, 'Dummy');
end;
procedure MenuItemClick(Dummy: Pointer; Sender: PMenu; Item: Integer);
begin
ShowMsg('Count = ' + Int2Str(Sender.Count) + ' : Item = ' + Int2Str(Item) + ' : Text = ' + Sender.ItemText[Item], MB_OK);
end;
begin
Applet := NewForm(nil, 'Test');
NewMenu(Applet, 0, [], nil);
PopupMenu := NewMenu(Applet, 0, ['111', '222', '333', '444', '555'], TOnMenuItem(MakeMethod(nil, @MenuItemClick)));
Applet.SetAutoPopupMenu(PopupMenu);
with NewButton(Applet,'Load')^ do
OnClick := TOnEvent(MakeMethod(nil,@ButtonLoadClick));
with NewButton(Applet,'Dummy').PlaceRight^ do
OnClick := TOnEvent(MakeMethod(nil,@ButtonDummyClick));
Run(Applet);
end.
Plugin code:
library plugin;
uses
Messages, Windows, KOL;
procedure MenuItemClick(Dummy: Pointer; Sender: PMenu; Item: Integer);
begin
ShowMsg('PLUGIN', MB_OK);
end;
procedure PluginMain(MainPopupMenu: HMenu); stdcall;
var
MyPopup: PMenu;
begin
MyPopup := NewMenu(nil, 0, ['xxx', 'yyy', 'zzz'], TOnMenuItem(MakeMethod(nil, @MenuItemClick)));
InsertMenu(MainPopupMenu, 0, MF_BYPOSITION or MF_POPUP, MyPopup.Handle, 'Plugin');
end;
exports PluginMain;
end.