您好!
我剛為您寫了一段,您看看是不是您要的!
//.h
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp>
#include
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TImage *Image1;
void __fastcall Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y);
void __fastcall Image1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
void __fastcall FormCreate(TObject *Sender);
private: // User declarations
Graphics::TBitmap *ComputerBmp0;
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//.cpp
#include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int px,py,ox,oy;
int st1=0;
TRect SRect,DRect;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
if(Button == mbLeft && st1==0) //判斷是否按下左鍵且尚未移到目的地
{
ox = Image1->Left; //保存原始水平座標位置
oy = Image1->Top; //保存原始垂直座標位置
px=X;
py=Y;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(Shift.Contains(ssLeft) && st1 ==0)
{
Image1->Left = Image1->Left (X-px); //移到新的位置
Image1->Top = Image1->Top (Y-py); //移到新的位置
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
SRect = Rect(0,0,32,32);
ComputerBmp0=new Graphics::TBitmap;
ComputerBmp0->LoadFromFile("Computer0.bmp");
//ComputerBmp0->Transparent = true;
DRect.top = 0;
DRect.left = 8;
DRect.Bottom = DRect.top 32;
DRect.Right = DRect.left 32;
Image1->Canvas->TextOutA(DRect.left-8,DRect.Bottom 2,"我的電腦");
Image1->Canvas->CopyRect(DRect,ComputerBmp0->Canvas,SRect);
}