Board index » cppbuilder » Drag Drop - why mouse goes to top left

Drag Drop - why mouse goes to top left


2003-07-26 12:40:59 AM
cppbuilder61
I am doing some drag and drop playing around. I have set to mode to manual
and done some coding behind because I like to see the object moving as I am
moving my mouse. The problem I have is that when I mouse on the object to
move it, the mouse cursor does not stay where I had it but always moves to
the top left. So when I click on the center of an object to drag it, it
moves the objects top left corner to that spot. This makes it difficult to
just move the object a little bit down and to the right. Any help is
appreciated, thanks.
Chad
 
 

Re:Drag Drop - why mouse goes to top left

I can get this to happen by just setting an objects DragMode to automatic
and then doing the following on the forms Drag Drop
//--------------------------------------------------------------------------
-
void __fastcall TfrmMain::FormDragDrop(TObject *Sender, TObject *Source, int
X, int Y)
{
AnsiString Type = Source->ClassName();
if (Type == "TShape")
{
TShape *Shp = dynamic_cast<TShape*>(Source);
if (Shp)
{
Shp->EndDrag(true);
Shp->Parent = this;
Shp->SetBounds(X, Y, Shp->Width, Shp->Height);
}
}
What I would like is similar behavior that is seen when designing a form and
moving an object, where whereever you grab the component by (center or
whatever) the mouse cursor stays at that point on the object. If I can
figure out how to get it to behave in this manor using the automatica drag
mode, I can do the same thing on my manual drag mode code.
Chad
"JD" < XXXX@XXXXX.COM >wrote in message
Quote

"Chad Eddy" < XXXX@XXXXX.COM >wrote:
>I am doing some drag and drop [...] when I mouse on the
>object to move it, [...] but always moves to the top left.

You'll need to post some (minimal,>50 lines) code that
reproduces the problem.

~ JD

 

Re:Drag Drop - why mouse goes to top left

Create the event FormDragOver, if I put just a comment "//" it will then
work. I have been setting Accept = true in here but had it commented out in
my code. Just getting started with drag and drop so not sure why this works
if there is nothing except comments in the body of it....
Chad
"JD" < XXXX@XXXXX.COM >wrote in message
Quote

"Chad Eddy" < XXXX@XXXXX.COM >wrote:
>I can get this to happen by just setting an objects DragMode
>to automatic and then doing the following on the forms DragDrop

No you can't because I can't even get it to start dragging
with just the info that you provided.

~ JD

 

{smallsort}

Re:Drag Drop - why mouse goes to top left

"Chad Eddy" < XXXX@XXXXX.COM >wrote:
Quote
[...] Just getting started with drag and drop so not sure
why this works [...]
If you just want to drag the object around it's Parent,
you don't actually want to Drag-N-Drop. You just want to drag:
TPoint mPoint; // make global
void __fastcall TForm1::ObjectMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
TControl* C = dynamic_cast<TControl *>( Sender );
if( C )
{
mPoint = TPoint(X, Y);
C->Tag = true;
}
}
//-------------------------------------------------------------
void __fastcall TForm1::ObjectMouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
static bool Positioning = false;
TControl* C = dynamic_cast<TControl *>( Sender );
if( C && C->Tag && !Positioning )
{
Positioning = true;
if( X < mPoint.x ) C->Left = C->Left - ( mPoint.x - X );
else C->Left = C->Left + ( X - mPoint.x );
if( Y < mPoint.y ) C->Top = C->Top - ( mPoint.y - Y );
else C->Top = C->Top + ( Y - mPoint.y );
Positioning = false;
}
}
//-------------------------------------------------------------
void __fastcall TForm1::ObjectMouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
TControl* C = dynamic_cast<TControl *>( Sender );
if( C && C->Tag )
{
//If the Parent is not the form, you can cast the
//parent to get a different height and width.
//Note that if the form is the parent, the 'else'
//checks have no effect because the form will resize
//unless it's Style is bsSingle
if( C->Left < 0 ) C->Left = 0;
else if( (C->Left + C->Width)>Width ) C->Left = Width - C->Width;
if( C->Top < 0 ) C->Top = 0;
else if( (C->Top + C->Height)>Height ) C->Top = Height - C->Height;
C->Tag = false;
}
}
//-------------------------------------------------------------
~ JD
 

Re:Drag Drop - why mouse goes to top left

"Todd Brylski" < XXXX@XXXXX.COM >wrote:
Quote
The code below will drag shapes around, [...]
More good stuff. I had never had a need or an occasion to code
drag (and drop) before. While my code does work, yours is half
of the drag-n-drop and by virute of that alone - the 'correct'
method.
~ JD
 

Re:Drag Drop - why mouse goes to top left

"JD" < XXXX@XXXXX.COM >wrote in message news:3f21ff7e$ XXXX@XXXXX.COM ...
Quote

"Todd Brylski" < XXXX@XXXXX.COM >wrote:
>The code below will drag shapes around, [...]

More good stuff. I had never had a need or an occasion to code
drag (and drop) before. While my code does work, yours is half
of the drag-n-drop and by virute of that alone - the 'correct'
method.
Thanks, although you will notice that if you move the mouse too fast
it stops dragging. That can be fixed by handling dragging in the
form, then you have a different set of relative coordinates to use,
and more code.
If I had to implement this in real life I would just track the
MouseDown, MouseMove and MouseUp messages. That's why I agree with
your opinion, since there is no 'dropping' being implemented.
p.s. How is your string grid with glyph problem going?
 

Re:Drag Drop - why mouse goes to top left

"Todd Brylski" < XXXX@XXXXX.COM >wrote:
Quote
[...] although you will notice that if you move the mouse
too fast it stops dragging. [...]
Oh Really !!?? Mine doesn't ;-)
Quote
p.s. How is your string grid with glyph problem going?
I hacked my way to a solution by replacing the StringGrid
editor. The glyph(s) was to simulate a ComboBox (and an
Up/Down) which I had working but during execution, the borders
of the controls looked sloppy.
Eventually I ended up with a panel sized to 'hide' the
components borders and to line up with the grid lines - all of
which happens in a generic function as long as the panel is
tagged and there is a matching tagged grid on the form.
Then I used the OnSelectCell to position the Editor (panel)
and set focus to the appropriate control.
I know that there are other grids available but none of the
ones I looked at had everything I needed. Most often it was
missing one or more of the ComboBox events found in BCB6.
~ JD
 

Re:Drag Drop - why mouse goes to top left

JD,
Thanks for the help. I got it to work with your code, now just have to
go through it and make sure I understand what is happening. I have been
playing with the mouse events as well as the draganddrop and was liking that
method better anyways. Thanks again
Chad
"JD" < XXXX@XXXXX.COM >wrote in message
Quote

"Chad Eddy" < XXXX@XXXXX.COM >wrote:
>[...] Just getting started with drag and drop so not sure
>why this works [...]

If you just want to drag the object around it's Parent,
you don't actually want to Drag-N-Drop. You just want to drag:

TPoint mPoint; // make global
void __fastcall TForm1::ObjectMouseDown(TObject *Sender, TMouseButton
Button, TShiftState Shift, int X, int Y)
{
TControl* C = dynamic_cast<TControl *>( Sender );
if( C )
{
mPoint = TPoint(X, Y);
C->Tag = true;
}
}
//-------------------------------------------------------------
void __fastcall TForm1::ObjectMouseMove(TObject *Sender, TShiftState
Shift, int X, int Y)
{
static bool Positioning = false;
TControl* C = dynamic_cast<TControl *>( Sender );
if( C && C->Tag && !Positioning )
{
Positioning = true;

if( X < mPoint.x ) C->Left = C->Left - ( mPoint.x - X );
else C->Left = C->Left + ( X - mPoint.x );

if( Y < mPoint.y ) C->Top = C->Top - ( mPoint.y - Y );
else C->Top = C->Top + ( Y - mPoint.y );

Positioning = false;
}
}
//-------------------------------------------------------------
void __fastcall TForm1::ObjectMouseUp(TObject *Sender, TMouseButton
Button, TShiftState Shift, int X, int Y)
{
TControl* C = dynamic_cast<TControl *>( Sender );
if( C && C->Tag )
{
//If the Parent is not the form, you can cast the
//parent to get a different height and width.
//Note that if the form is the parent, the 'else'
//checks have no effect because the form will resize
//unless it's Style is bsSingle
if( C->Left < 0 ) C->Left = 0;
else if( (C->Left + C->Width)>Width ) C->Left = Width - C->Width;

if( C->Top < 0 ) C->Top = 0;
else if( (C->Top + C->Height)>Height ) C->Top = Height -
C->Height;

C->Tag = false;
}
}
//-------------------------------------------------------------

~ JD