Responding to Mouse-down Messages

From RAD Studio
Jump to: navigation, search

Go Up to Handling Mouse-down and Key-down Messages


A MouseDown method is a protected method for a control's OnMouseDown event. The control itself calls MouseDown in response to a Windows mouse-down message. When you override the inherited MouseDown method, you can include code that provides other responses in addition to calling the OnMouseDown event.

To override MouseDown, add the MouseDown method to the TDBCalendar class:

type
  TDBCalendar = class(TSampleCalendar);
   .
   .
   .
  protected
    procedure MouseDown(Button: TButton, Shift: TShiftState, X: Integer, Y: Integer);
      override;
   .
   .
   .
  end;
procedure TDBCalendar.MouseDown(Button: TButton; Shift: TShiftState; X, Y: Integer);
var
  MyMouseDown: TMouseEvent;
begin
  if not ReadOnly and FDataLink.Edit then
    inherited MouseDown(Button, Shift, X, Y)
  else
  begin
    MyMouseDown := OnMouseDown;
    if Assigned(MyMouseDown then MyMouseDown(Self, Button, Shift, X, Y);
  end;
end;


//header file
class PACKAGE TDBCalendar : public TSampleCalendar
{
.
.
.
protected:
    virtual void __fastcall MouseDown(TMouseButton Button,  TShiftState Shift, int X,       int Y);
    .
    .
    .
};


//implmentation file
void __fastcall TDBCalendar::MouseDown(TMouseButton Button,  TShiftState Shift, int  X,   int Y)
{
  TMouseEvent MyMouseDown;                               // declare event type
  if (!FReadOnly && FDataLink->Edit())                   // if the field can be edited
    TSampleCalendar::MouseDown(Button, Shift, X, Y);   // call the inherited MouseDown
  else
  {
    MyMouseDown = OnMouseDown;                          // assign OnMouseDown event
    if (MyMouseDown != NULL) MyMouseDown(this, Button,  // execute code in the...
       Shift, X, Y);                                    // ...OnMouseDown event handler
  }
}

When MouseDown responds to a mouse-down message, the inherited MouseDown method is called only if the control's ReadOnly property is False and the data link object is in edit mode, which means the field can be edited. If the field cannot be edited, the code the programmer put in the OnMouseDown event handler, if one exists, is executed.

See Also