Possible Duplicate:
How to select a Menu Item without closing the Menu?
When the popup menu is showing I need to check/uncheck items but I dont want the popup menu to close and reopen. It causes an annoying flicker.
Here is my code so far:
type
TForm1 = class(TForm)
PopupMenu1: TPopupMenu;
N11: TMenuItem;
N21: TMenuItem;
N31: TMenuItem;
procedure PopupMenu1Popup(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
PopupPt: TPoint;
procedure OptionClick(Sender: TObject);
public
end;
implementation
procedure TForm1.FormCreate(Sender: TObject);
begin
Self.PopupMenu := PopupMenu1;
PopupMenu1.OnPopup := PopupMenu1Popup;
N11.OnClick := OptionClick;
N21.OnClick := OptionClick;
N31.OnClick := OptionClick;
end;
procedure TForm1.OptionClick(Sender: TObject);
begin
PopupMenu1.Tag := 1;
try
(Sender as TMenuItem).Checked := not (Sender as TMenuItem).Checked;
PopupMenu1.Popup(PopupPt.x, PopupPt.y);
finally
PopupMenu1.Tag := 0;
end;
end;
procedure TForm1.PopupMenu1Popup(Sender: TObject);
begin
if PopupMenu1.Tag = 0 then
PopupPt := Mouse.CursorPos;
end;
My question, can this be done better?
EDIT 1: There is a small problem with the duplicate accepted answer How to select a Menu Item without closing the Menu:
If I set Item.Checked := not Item.Checked
right after Item.Click;
(MenuSelectID
method) - The Item is not invalidated, and the check is not drawn until I leave the Item area and Enter back to it...
I have also tried: CheckMenuItem(Menu.Handle, ItemID, MF_BYCOMMAND or MF_CHECKED);
- Same effect.
So, I'm back to ground zero, becouse my main goal is to Check/Uncheck menu items. same as with status bar context menu in Microsoft Office (2010 at least):
EDIT 2: Adding InvalidateRect(FPopupWindowHandle, nil, False);
right after setting Checked
fixed the invalidation problem.