2008. december 14., vasárnap

How to preserve the default popup menu for a component


Problem/Question/Abstract:

Is there a way to keep the default popup menus for e.g. TMemo, TEdit if there is an explicit popup menu for the parent control?

Answer:

You have to trap the WM_CONTEXTMENU message on the level of the parent. Assuming the parent is the form you would add a message handler to the form:

{ ... }
private

procedure WMContextMenu(var Message: TWMContextMenu); message WM_CONTEXTMENU;
{ ... }

and implement it like this:

procedure TForm1.WMContextMenu(var Message: TWMContextMenu);
var
  wnd: HWND;
  ctrl: TWinControl;
begin
  if message.XPos > 0 then
  begin
    wnd := WindowFromPoint(Mouse.CursorPos);
    if wnd <> handle then
    begin
      ctrl := FindControl(wnd);
      if Assigned(ctrl) and (ctrl is TCustomEdit) then
        Exit;
    end;
  end
  else if ActiveControl is TCustomEdit then
    Exit;
  inherited;
end;

Doing the same if the parent with the menu is a panel or tabsheet or such is a bit more difficult, you have to subclass it via WindowProc, or make a descendent class to be able to trap the message.

Nincsenek megjegyzések:

Megjegyzés küldése