"Paul Ashton" <
XXXX@XXXXX.COM >wrote:
Quote
Please trim your posts.
Quote
Even this way, with a "manual Drag Threshhold" I don't have
an event to reset the dragging pointer because the MouseUp
event is called immediatly when I call BeginDrag.
Rethink your apprach. The OnMouseDown event it where it all
begins so don't worry about resetting anything when finished.
Simply initialize when it starts.
Besides, if you had developed your test just a bit further,
you'd have (or should have) discovered that OnEndDrag would
permit you to implement your logic.
Have a look at the sample below. It works as you desire and I
added another class that produces a nice looking DragObject.
To add the class to your application, Click File | New | Unit
and save it as DragObject.cpp and then copy and paste and
include the header in the main form's (or any other form's)
unit.
TPoint ClickPoint;
bool FDragging;
TDCObject *FDCObject;
//-------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
// assign these events using the Object Inspector
OnDragOver = ControlDragOver;
OnDragDrop = ControlDragDrop;
Panel1->OnMouseDown = ControlMouseDown;
Panel1->OnMouseMove = ControlMouseMove;
Panel1->OnMouseUp = ControlMouseUp;
Panel1->OnStartDrag = ControlStartDrag;
Panel1->OnDragOver = ControlDragOver;
Panel1->OnDragDrop = ControlDragDrop;
Panel1->OnEndDrag = ControlEndDrag;
Label1->OnMouseDown = ControlMouseDown;
Label1->OnMouseMove = ControlMouseMove;
Label1->OnMouseUp = ControlMouseUp;
Label1->OnStartDrag = ControlStartDrag;
Label1->OnDragOver = ControlDragOver;
Label1->OnDragDrop = ControlDragDrop;
Label1->OnEndDrag = ControlEndDrag;
// this is needed because by default few controls have csDisplayDragImage set
SetControlsControlStyle( this );
}
//-------------------------------------------------------------
void __fastcall TForm1::SetControlsControlStyle( TControl *AControl )
{
AControl->ControlStyle = AControl->ControlStyle << csDisplayDragImage;
TWinControl *wControl = dynamic_cast<TWinControl*>( AControl );
if( wControl )
{
for( int x = 0; x < wControl->ControlCount; ++x )
{
SetControlsControlStyle( wControl->Controls[x] );
}
}
}
//-------------------------------------------------------------
void __fastcall TForm1::ControlMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
ClickPoint = Point( X, Y );
FDragging = false;
}
//-------------------------------------------------------------
void __fastcall TForm1::ControlMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
if( Shift.Contains(ssLeft) )
{
int ThreshHold = 3;
if( (abs(ClickPoint.x - X)>ThreshHold) || (abs(ClickPoint.y - Y)>ThreshHold) )
{
ClickPoint = Point( X, Y );
FDragging = true;
TControl *pControl = static_cast<TControl*>( Sender );
pControl->BeginDrag( true );
}
}
}
//-------------------------------------------------------------
void __fastcall TForm1::ControlMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
if( ! FDragging )
{
ShowMessage("Execute OnClick code here");
}
}
//-------------------------------------------------------------
void __fastcall TForm1::ControlStartDrag(TObject *Sender, TDragObject *&DragObject)
{
FDCObject = new TDCObject( static_cast<TControl*>(Sender), ClickPoint.x, ClickPoint.y );
DragObject = FDCObject;
}
//-------------------------------------------------------------
void __fastcall TForm1::ControlEndDrag(TObject *Sender, TObject *Target, int X, int Y)
{
delete FDCObject;
}
//-------------------------------------------------------------
void __fastcall TForm1::ControlDragOver(TObject *Sender, TObject *Source, int X, int Y, TDragState State, bool &Accept)
{
// set Accept to false if the object is in a bad place else do nothing
}
//-------------------------------------------------------------
void __fastcall TForm1::ControlDragDrop(TObject *Sender, TObject *Source, int X, int Y)
{
// Determine if the control was dropped in a good place and make your move
}
//-------------------------------------------------------------
//-------------------------------------------------------------
#ifndef DragObjectH
#define DragObjectH
//-------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
//-------------------------------------------------------------
class TCrackControl : public TControl
{
public:
__property Color;
};
//-------------------------------------------------------------
class TDCObject : public TDragControlObject
{
protected:
virtual TDragImageList* __fastcall GetDragImages();
private:
int FX, FY;
TDragImageList* FDragImages;
public:
__fastcall TDCObject( TControl *AControl, int X, int Y );
__fastcall ~TDCObject();
};
//-------------------------------------------------------------
#endif
//-------------------------------------------------------------
#pragma hdrstop
#include "DragObject.h"
//-------------------------------------------------------------
#pragma package(smart_init)
//-------------------------------------------------------------
__fastcall TDCObject::TDCObject( TControl *AControl, int X, int Y ) : TDragControlObject( Control )
{
FX = X;
FY = Y;
Control = AControl;
FDragImages = NULL;
}
//-------------------------------------------------------------
__fastcall TDCObject::~TDCObject()
{
delete FDragImages;
}
//-------------------------------------------------------------
TDragImageList* __fastcall TDCObject::GetDragImages()
{
if( !FDragImages )
{
FDragImages = new TDragImageList( NULL );
Graphics::TBitmap* Bitmap = new Graphics::TBitmap;
Bitmap->Width = Control->Width;
Bitmap->Height = Control->Height;
TWinControl* wControl = dynamic_cast<TWinControl*>( Control );
if( wControl )
{
Bitmap->Canvas->Lock();
wControl->PaintTo( Bitmap->Canvas->Handle, 0, 0 );
Bitmap->Canvas->Unlock();
}
else
{
TControlCanvas *pCanvas = new TControlCanvas();
pCanvas->Control = Control;
TRect R = Rect( 0, 0, Control->Width, Control->Height );
Bitmap->Canvas->CopyRect( R, pCanvas, R );
}
FDragImages->Width = Bitmap->Width;
FDragImages->Height = Bitmap->Height;
FDragImages->SetDragImage( FDragImages->AddMasked(Bitmap, (static_cast<TCrackControl*>(Control))->Color), FX, FY );
delete Bitmap;
}
return FDragImages;
}
//-------------------------------------------------------------
~ JD