Archive for the ‘popup’ category

Popup menu with MDI

January 11, 2007

Situation: we have a MDI application whose main form (FormStyle = fsMDIForm) has a TPopupMenu (MainForm.PopupMenu = PopupMenu1) for some shortcut functions when user clicks mouse on the main form.

Problem: this PopupMenu1 is popping up everywhere in all the MDI children windows, but we want to use for example the Windows default popup menu in edit boxes:


Solution: Remove the PopupMenu1 from main form’s properties and create an OnContextPopup event handler:

procedure TMainForm.FormContextPopup(Sender: TObject;
  MousePos: TPoint; var Handled: Boolean);
  ScreenPoint: TPoint;
  // convert client area coordinates
  // to global screen coordinates
  ScreenPoint := ClientToScreen(MousePos);
  // see if the control under cursor in our main form
  if (FindDragTarget(ScreenPoint, True) is TMainForm)
  then begin
    // show our shortcut menu
    PopupMenu1.Popup(ScreenPoint.X, ScreenPoint.Y);
    // we handled the request for context menu
    Handled := True;