Programming C++Builder How to respond to the mouse event when using canvas to draw graphics in CBuilder?
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/ on line 149

How to respond to the mouse event when using canvas to draw graphics in C++Builder?

Your application can respond to the mouse actions: mouse-button down, mouse moved, and mouse-button up. It can also respond to a click (a complete press-and-release, all in one place) that can be generated by some kinds of keystrokes (such as pressing Enter in a modal dialog box).
This section covers:

What's in a mouse event.

C++Builder has three mouse events: OnMouseDown event, OnMouseMove event, and OnMouseUp event.

When an application detects a mouse action, it calls whatever event handler you're defined for the corresponding event, passing five parameters. Use the information in those parameters to customize your responses to the events. The five parameters are as follows:

Parameter Meaning
Sender The object that detected the mouse action
Button Indicates which mouse button was involved: mbLeft, mbMiddle, or mbRight
Shift Indicates the state of the Alt, Ctrl, and Shift keys at the time of the mouse action
X, Y The coordinates where the event occurred

Most of the time, you need the coordinates returned in a mouse-event handler, but sometimes you also need to check Button to determine which mouse button caused the event.

Note: C++Builder uses the same criteria as Microsoft Windows in determining which mouse button has been pressed. Thus, if you have switched the default "primary" and "secondary" mouse buttons (so that the right mouse button is now the primary button), clicking the primary (right) button will record mbLeft as the value of the Button parameter.

Responding to a mouse-down action.

Whenever the user presses a button on the mouse, an OnMouseDown event goes to the object the pointer is over. The object can then respond to the event.
To respond to a mouse-down action, attach an event handler to the OnMouseDown event.

C++Builder generates an empty handler for a mouse-down event on the form:

void __fastcallTForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
  TShiftState Shift, int X, int Y)
    bDrawState = true;

Responding to a mouse-up action.

An OnMouseUp event occurs whenever the user releases a mouse button. The event usually goes to the object the mouse cursor is over when the user presses the button, which is not necessarily the same object the cursor is over when the button is released. This enables you, for example, to draw a line as if it extended beyond the border of the form.

To respond to mouse-up actions, define a handler for the OnMouseUp event.

void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
    bDrawState = false;

Responding to a mouse move.

An OnMouseMove event occurs periodically when the user moves the mouse. The event goes to the object that was under the mouse pointer when the user pressed the button. This allows you to give the user some intermediate feedback by drawing temporary lines while the mouse moves.

To respond to mouse movements, define an event handler for the OnMouseMove event. This example uses mouse-move events to draw intermediate shapes on a form while the user holds down the mouse button, thus providing some feedback to the user. The OnMouseMove event handler draws a line on a form to the location of the OnMouseMove event:

void __fastcall TForm1::FormMouseMove(TObject *Sender, TMouseButton Button,
  TShiftState Shift, int X, int Y)
    if (bDrawState)
        Canvas->LineTo(X, Y); // draw line to current position

With this code, moving the mouse over the form causes drawing to follow the mouse, even before the mouse button is pressed. Mouse-move events occur even when you haven't pressed the mouse button. If you want to track whether there is a mouse button pressed, you need to add an object field to the form object.

Add comment

Security code

Programming - C++Builder