2010. november 11., csütörtök

How to prevent a TDBEdit from taking over its parent's popup menu


Problem/Question/Abstract:

I have several TDBEdits on a groupbox which is on a TabControl. The TabControl has its own PopupMenu and the DBEdits are assuming this menu, even though I only want the DBEdits to have the standard Windows Edit/ Cut/ Paste. How do I stop the DBEdit's from assuming the TabControls PopUp? Their PopUpMenu property is not set to anything.

Answer:

That turned out to be a pretty hairy problem. I was able to solve it but the solution ain't pretty. Attach a common handler to all the edits OnContextMenu event. Modify the form as follows:

{ ... }
private
{ Private declarations }

procedure wmUser(var msg: TMessage); message wm_user;
public
  { Public declarations }
end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

type
  twc = class(twincontrol)
  public
    property DefWndProc;
  end;

procedure TForm1.wmUser(var msg: TMessage);
begin
  with twc(msg.wparam) do
    callwindowproc(defwndproc, handle, wm_contextmenu, handle, msg.lparam);
end;

procedure TForm1.Edit1ContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean);
begin
  handled := true;
  postmessage(handle, wm_user, wparam(Sender),
    lparam(PointToSmallpoint((Sender as Tedit).ClientToScreen(mousepos))));
end;

What this does is jumping through hoops to get the WM_CONTEXTMENU message that triggers the menu past the default message handling code in the VCL. Tested with normal TEdits, D5.01, on Win95B.

Nincsenek megjegyzések:

Megjegyzés küldése