11.3. Derivados de TControl
OnClick
Se produce el evento cuando se realiza un click sobre un control.
__property Classes::TNotifyEvent OnClick = {read=FOnClick, write=FOnClick};
void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
PageControl1->ActivePage->Visible = CheckBox1->Checked;
}
OnDblClick
Se produce el evento cuando se realiza un doble click sobre un control.
__property Classes::TNotifyEvent OnDblClick = {read=FOnDblClick, write=FOnDblClick};
This example notifies the user that the form was double-clicked.
void __fastcall TForm1::FormDblClick(TObject *Sender)
{
Application->MessageBox("You double-clicked the form", "Double-Click Message", MB_OK);
}
OnDragDrop
Sucede cuando el usuario completa la operación de arrastrar y soltar para dibujar un objeto sobre el formulario (se deja caer el objeto en el control soltando el botón del ratón). Se puede responder a este evento cuando el formulario está preparado para soportar el método de arrastrar y soltar.
typedef void __fastcall (__closure *TDragDropEvent)(System::TObject* Sender, System::TObject* Source, int X, int Y);
__property TDragDropEvent OnDragDrop = {read=FOnDragDrop, write=FOnDragDrop};
Los parámetros X e Y son las coordenadas de la posición del ratón sobre el control.
OnDragOver
Sucede cuando se mueve el puntero del ratón sobre el control mientras dibuja el objeto con el ratón. Se puede responder a este evento cuando el formulario está preparado para soportar el método de arrastrar y soltar.
enum TDragState { dsDragEnter, dsDragLeave, dsDragMove }
typedef void __fastcall (__closure *TDragOverEvent)(System::TObject* Sender, System::TObject* Source, int X, int Y, TDragState State, bool &Accept);
__property TDragOverEvent OnDragOver = {read=FOnDragOver, write=FOnDragOver};
TdragState indica el estado del control con relación a otro control. Los posibles casos son los siguientes:
Value | Meaning |
dsDragEnter
| El ratón entra en el control. |
dsDragMove
| Mientras el ratón se mueve sobre el control. |
dsDragLeave
| El ratón deja el control. |
OnMouseDown
Ocurre cuando el usuario pulsa un botón del ratón cuando el puntero del ratón esta sobre el control.
enum TMouseButton { mbLeft, mbRight, mbMiddle };
enum Classes__1 { ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble };
typedef Set TShiftState;
typedef void __fastcall (__closure *TMouseEvent)(System::TObject* Sender, TMouseButton Button, Classes::TShiftState Shift, int X, int Y);
__property TMouseEvent OnMouseDown = {read=FOnMouseDown, write=FOnMouseDown};
Los parámetros de Shift responden a las teclas de shift y a los botones del ratón. Las teclas Shift son la Shift, Ctrl y Alt. X e Y son las coordenadas en pixel del puntero del ratón en el área de cliente del Sender.
The following example requires a form with a four-paneled status bar. (Set the Width of the status panels to 150 before running this example). When the user presses a mouse button, moves the mouse, and releases the mouse button, a rectangle is drawn on the form. When the mouse button is released, the rectangle appears on the form's canvas. Its top-left and bottom-right corners are defined by the location of the mouse pointer when the user pressed and released the mouse button. While the user drags the mouse, the location of the top, left, bottom, and right sides of the rectangle are displayed in the status bar.
int StartX, StartY; // Declare at the top of the form's unit
// Use this code as the OnMouseDown event handler of the form:
void __fastcall TForm1::Button1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y){
StartX = X;
StartY = Y;
}
// Use this code as the OnMouseUp event handler of the form:
void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y){
Form1->Canvas->Rectangle(StartX, StartY, X, Y);
StatusBar1->Panels[0]->Text = "";
StatusBar1->Panels[1]->Text = "";
StatusBar1->Panels[2]->Text = "";
StatusBar1->Panels[3]->Text = "";
}
// Use this code as the OnMouseMove event handler of the form:
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
if (Shift.Contains(ssLeft)) // make sure button is down
{
if (Y > StartY){
StatusBar1->Panels[0]->Text = "Top: " + IntToStr(StartY);
StatusBar1->Panels[2]->Text = "Bottom: " + IntToStr(Y);
}
else{
StatusBar1->Panels[0]->Text = "Top: " + IntToStr(Y);
StatusBar1.Panels[2].Text = "Bottom: " + IntToStr(StartY);
}
if (X > StartX){
StatusBar1->Panels[1]->Text = "Left: " + IntToStr(StartX);
StatusBar1->Panels[3]->Text = "Right: " + IntToStr(X);
}
else{
StatusBar1->Panels[1]->Text = "Left: " + IntToStr(X);
StatusBar1->Panels[3]->Text := "Right: " + IntToStr(StartX);
}
}
}
OnMouseMove
Ocurre cuando el usuario mueve el puntero del ratón mientras el puntero del ratón esta sobre el control.
enum Classes__1 { ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble };
typedef Set TShiftState;
typedef void __fastcall (__closure *TMouseMoveEvent)(System::TObject* Sender, Classes::TShiftState Shift, int X, int Y);
__property TMouseMoveEvent OnMouseMove = {read=FOnMouseMove, write
=FOnMouseMove};
Los parámetros de Shift responden a las teclas de shift y a los botones del ratón. Las teclas Shift son la Shift, Ctrl y Alt. X e Y son las coordenadas en pixel del puntero del ratón en el área de cliente del Sender.
The following code updates two labels when the mouse pointer is moved. The code assumes you have two labels on the form, lbHorz and lbVert. If you attach this code to the OnMouseMove event of a form, lbHorz continually displays the horizontal position of the mouse pointer, and lbVert continually displays the vertical position of the mouse pointer while the pointer is over the form.
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
char xPos[10];
char yPos[10];
itoa(X, xPos, 10);
itoa(Y, yPos, 10);
lbHorz->Caption = xPos;
lbVert->Caption = yPos;
}
OnMouseUp
Ocurre cuando el usuario suelta el botón del ratón que está pulsado cusando el puntero del ratón esta sobre el control.
enum TMouseButton { mbLeft, mbRight, mbMiddle };
enum Classes__1 { ssShift, ssAlt, ssCtrl, ssLeft, ssRight, ssMiddle, ssDouble };
typedef Set TShiftState;
typedef void __fastcall (__closure *TMouseEvent)(System::TObject* Sender, TMouseButton Button, Classes::TShiftState Shift, int X, int Y);
__property TMouseEvent OnMouseUp = {read=FOnMouseUp, write=FOnMouseUp};
Los parámetros de Shift responden a las teclas de shift y a los botones del ratón. Las teclas Shift son la Shift, Ctrl y Alt. X e Y son las coordenadas en pixel del puntero del ratón en el área de cliente del Sender.
The following code draws a rectangle when the user presses a mouse button, moves the mouse, and releases the mouse button. When the mouse button is released, the rectangle appears on the form's canvas. Its top-left and bottom-right corners are defined by the location of the mouse pointer when the user pressed and released the mouse button.
int StartX, StartY; //Declare in interface section of form's unit
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
StartX = X;
StartY = Y;
}
void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
Form1->Canvas->Rectangle(StartX, StartY, X, Y);
}