C++ Builder

La Isla Bonita



Form Movement Detection
Form Movement Detection


Description

This example shows how detect the movement of a Form (Visible Window) along either coordinates axes (X and/or Y). The method uses a TTimer (Timer1) dynamically created.

FormMovementDetection.cpp

#include <vcl.h>
#pragma hdrstop

#include "FormMovementDetection.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TTimer* Timer1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
    pri_OldTop  = Top;
    pri_OldLeft = Left;
    Color = 0x984387;
    Caption = "Form Movement Detection";

    Timer1 = new TTimer( this );
    Timer1->Enabled  = true;
    Timer1->Name     = "Timer1";
    Timer1->Interval = 100;
    Timer1->OnTimer  = Timer1Timer;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
     bool X_axis = false;
     bool Y_axis = false;
     String SY = "";
     String SX = "";

     if ( Left != pri_OldLeft )
     {
         pri_OldLeft  = Left;
         X_axis = true;
         SX = "Form has moved along the X axis";
     }

     if ( Top != pri_OldTop )
     {
         pri_OldTop   = Top;
         Y_axis = true;
         SY = "Form has moved along the Y axis";
     }

     if (X_axis || Y_axis)
     {
         ShowMessage(SX + "\n" + SY);
     }
}

FormMovementDetection.h

#ifndef FormMovementDetectionH
#define FormMovementDetectionH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        void __fastcall Timer1Timer(TObject *Sender);
private:	// User declarations
    int pri_OldTop;   //Old Top Coordinate
    int pri_OldLeft;  //Old Left Coordinate
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2003 Rodolfo A. Frino. All rights reserved.