GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Jul-2005, 14:12
shakoush2001 shakoush2001 is offline
New Member
 
Join Date: Jul 2005
Posts: 10
shakoush2001 is on a distinguished road
Angry

Drag and Drop of images ??


hi there this is my first time I post here, Ive taken some courses in C++ and Iam on my own now, but there are still alot of basic issues I need to work on so please do not mind the following question:

Iam working on a chess interface and I have an TBitmap for every stone, when a stone is to be moved it will be dragged onMouseUP I will check if hte new position is valid, if it is the stone will take its new position else it will return to its former position, the problem is that Iam not able to drag the image what I tried is the following :

1- on mouse down record initialX and initialY
2-While dragging change Top and Left of the TBitmap according to the X an Y coordinates of the event handler

3-OnMouseUp check for the validity of hte position of the TBitmap.

Any help please??
  #2  
Old 02-Jul-2005, 03:02
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
Hi, shakoush2001
For new users this is important to read. Read it so that you get max. advantage from this forum.

From what you write it seems you are using Borland compiler? Is that right? Things like this are important to know by people that try to help you.

The logic you described in points 1, 2 & 3 is correct. Did you forget to call repaint() in your code or do you have any code? Please send your current code.
  #3  
Old 02-Jul-2005, 04:58
shakoush2001 shakoush2001 is offline
New Member
 
Join Date: Jul 2005
Posts: 10
shakoush2001 is on a distinguished road
CPP / C++ / C Code:
//---------------------------------------------------------------------------

void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
 X1 = X; // X1 & Y1 declared in header file
 Y1 = Y;
 dragging = true;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
 if(dragging)
 {

 Image1->Left = X1;
 Image1->Top = Y1;
 X1 = X;
 Y1 = Y;
 Repaint();

 }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
 dragging = false;
   
}
//---------------------------------------------------------------------------




Iam using Borland C++Builder 5
  #4  
Old 02-Jul-2005, 05:55
maprich maprich is offline
Member
 
Join Date: May 2005
Posts: 163
maprich has a spectacular aura aboutmaprich has a spectacular aura about
It is a long time since I have used Borland Builder but...

It seems that Image1 is one of your chess pieces.

In FormMouseDown try "X1 = X; X2 = Image1->Left;" Note that X2 here is new variable to store the coordinate more permanently so that you can put the Image1 back to its original place if the ultimate destination is not valid.

In Image1MouseMove I think the mouse coordinates (X,Y) are relative to the up-left corner of the Image1 itself. In that case you should write something like:
CPP / C++ / C Code:
 Image1->Left += X - X1;
 Image1->Top += Y - Y1;
 Repaint();

finally you still need logic in the mouse up to check if the new location is valid... and to "snap" the piece into its place.

Although it would be much better if you made your ChessBoard image (or Canvas control) to handle the mouse events. It gets messy if you are going to write message handlers for every chess piece in the board.. not effective.
  #5  
Old 02-Jul-2005, 08:52
shakoush2001 shakoush2001 is offline
New Member
 
Join Date: Jul 2005
Posts: 10
shakoush2001 is on a distinguished road
Unhappy

Thanks for helping me, but I wana ask you something. If I use the event handlers of every image by itself then I could not drag adn drop them using mouseUp and mouseMove, because the event handlers of the image are only active within image ( at least thats what i figured out :|).

Anyhow, I made to code work but I dont think that it is effective and it flickers alot, I painted the TBitmap Image on the canvas and moved it accordingly

CPP / C++ / C Code:
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
  dragging = true;
 for(int i = 0; i < Image1->Width; i++)
   for(int j = 0; j < Image1->Height; j++)
      Canvas->Pixels[i + X][j + Y] = Image1->Canvas->Pixels[i][j];
    
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
 if(dragging)
 {
 Repaint();
  for(int i = 0; i < Image1->Width; i++)
    for(int j = 0; j < Image1->Height; j++)
       Canvas->Pixels[i + X][j + Y] = Image1->Canvas->Pixels[i][j];
   

 }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
 dragging = false;
}
//---------------------------------------------------------------------------



Now, the method you gave me looked like this in the end

CPP / C++ / C Code:
//---------------------------------------------------------------------------

void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
 X1 = X;
 X2 = Image1->Top;
 Y1 = Y;
 Y2 = Image1->Left;
 dragging = true;

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
if(dragging)
 {

 Image1->Left += X - X1;
 Image1->Top += Y - Y1;
 Repaint();


 }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
 dragging = false;
}
//---------------------------------------------------------------------------


But this did not work, actually I want the image to be dragged everything after that is in control, any suggestions ??
 
 

Recent GIDBlogLast Week of IA Training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.