How to shield the default right-click menu for the control

zhaozj2021-02-16  54

In many cases we might want to shield the default right-click menu of some controls.

Shield control Right-click menu has many ways, and now I know four of them.

Before introducing, let's take a look at the following code, this code is a code of the TCONTROL object in the VCL library.

From this code we can see that the pop-up events of the right-click menu are written in the pop-up menu message.

Private

Procedure WMCONTextMenu (var message: twmcontextmenu); Message WM_CONTextMenu;

...

Procedure Tcontrol.WmContextMenu (Var Message: TwmContextMenu);

VAR

PT, Temp: TPOINT;

Handled: boolean;

PopupMenu: TPopupmenu;

Begin

IF message.result <> 0.

IF csdesigning.

PT: = SmallPointTopoint (Message.pos);

IF pt.x <0 THEN

Temp: = Pt

Else

Begin

Temp: = ScreenToClient (PT);

IF not PtinRect (ClientRect, Temp) THEN

Begin

inherited;

EXIT;

END;

END;

Handled: = false;

{* Call onContextPopup}

DOCONTEXTPOPUP (TEMP, HANDLED);

Message.Result: = ORD (Handled);

IF Handled.

Popupmenu: = GetPopupMenu;

{* Pop-up menu}

IF (PopupMenu <> nil) and popupmenu.autopup1

Begin

SendcancelMode (nil);

PopupMenu.PopuppComponent: = Self;

IF pt.x <0 THEN

PT: = ClientToscreen (Point (0,0));

PopupMenu.Popup (pt.x, pt.y);

Message.Result: = 1;

END;

IF message.result = 0 THEN

{* Inherited Windows Right-click Menu}

inherited;

END;

Here is a few common methods for masking the right-click menu

The first one: binding menu method:

This method is the easiest and does not need to write. The PopupMenu property of a visual control is set to a PopupMenu without a menu item.

Second: Writing an event method

This method is also very simple, such as: We want to block the right-click menu of Edit1 control, we can be on onContextPopup

Implementation in the event.

code show as below:

Procedure tform1.edit1contextpopup (sender: Tobject; mousepos: tpoint;

Var handled: boolean;

Begin

Handled: = True;

END;

Why is it set to true? As can be seen from the source code in the VCL library, when you are True, you will be jumped out of the following custom bounce.

The menu and the default Windows Right-click code.

But sometimes the control does not provide an onContextPopup event, such as the Tmaskedit control, what should we do, we know that the Tmaskedit control and TEDIT control are derived classes for the TControls control, so we can implement as follows. code show as below:

Private

Procedure NoneContextPopup (Sender: Tobject; Mousepos: Tpoint; Var Handled:

Boolean;

...

Procedure TFORM1.NONECONTEXTPOPUP (Sender: Tobject; mousepos: tpoint; var

Handled: boolean;

Begin

Handled: = True;

END;

Procedure TFORM1.FormCreate (Sender: TOBJECT);

Begin

Tedit (maskedit1) .onTextPopup: = NoneContextPopup;

END;

The third type: intercepting system message method:

Sometimes we use the two methods described above to not help, at this time, you can cross the system message method, we can achieve the right-click menu of the shielding control by judging the handle of the control.

code show as below:

Private

Procedure MouserightMessage (Var Msg: TMSG; VAR HANDED: BOOLEAN);

...

Procedure TFORM1.MOUSERIGHTMESSAGE (VAR MSG: TMSG; VAR HANDLED: BOOLEAN);

Begin

IF (msg.Message = WM_RBUTTONDOWN) and (msg.hwnd = Edit1.handle) THEN

Begin

Handled: = True;

END;

END;

Procedure TFORM1.FormCreate (Sender: TOBJECT);

Begin

Application.onMessage: = mouserightmessage;

END;

The fourth type: the right-click message method for intercepting the control:

But sometimes we want to hide the right-click menu of a control forever, then we can implement it by the following methods.

code show as below:

Type

Tospedit = Class (TEDIT)

public

Procedure WmrButtondown (VAR MSG: TMSG); Message WM_RBUTTONDNDOWN;

END;

Procedure Tospedit.wmrButtondown (VAR MSG: TMSG);

Begin

//

END;

Wen Jianhua

2004-2-9

Fujian

Related resources: with sound access new year fireworks animation effects - html-code

转载请注明原文地址:https://www.9cbs.com/read-19674.html

New Post(0)