4

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.

Community
  • 1
  • 1
ZigiZ
  • 2,480
  • 4
  • 25
  • 41
  • 3
    See [How to select a Menu Item without closing the Menu?](http://stackoverflow.com/q/5983217/576719). The example shows how to do this with a `TPopupMenu`. – LU RD Jan 06 '13 at 13:09
  • @LURD, Thanks. my question is in fact exact duplicate. – ZigiZ Jan 06 '13 at 14:46
  • "InvalidateRect(FPopupWindowHandle, nil, False);" do you know how to make that work with VCL styles on? – hikari Aug 15 '20 at 15:39

0 Answers0