www.pudn.com > 旋转文字和移动头像―卡通效果.zip > main.cpp


//--------------------------------------------------------------------------- 
 
#include  
#pragma hdrstop 
 
#include "main.h" 
//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
#pragma resource "*.dfm" 
TForm1 *Form1; 
//--------------------------------------------------------------------------- 
__fastcall TForm1::TForm1(TComponent* Owner) 
        : TForm(Owner) 
{ 
        move = false; 
        Timer1->Enabled = false; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::FormCreate(TObject *Sender) 
{ 
        AnsiString filename; 
        filename = ExtractFileDir(Application->ExeName); //提取可执行文件名 
        filename = filename + "\\face.bmp";             //copy from oicq 
        Image1->Picture->LoadFromFile(filename);        //载入图形 
        //在背景上作图 
        Background->Canvas->Draw(Image1->Left, Image1->Top, Image1->Picture->Graphic); 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button, 
      TShiftState Shift, int X, int Y) 
{ 
        desPt.x = X; 
        desPt.y = Y; 
        move = true; 
        Timer1->Enabled = true;  //启动动画 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::Timer1Timer(TObject *Sender) 
{ 
        if(move) 
        { 
                TRect desrect = this->ClientRect; 
                TRect lastrect; 
                lastrect.Top = Image1->Top; 
                lastrect.Left = Image1->Left; 
                lastrect.Bottom = lastrect.Top + Image1->Height; 
                lastrect.Right = lastrect.Left + Image1->Width; 
                //擦除原来位置的图形 
                Background->Canvas->FillRect(lastrect); 
                int dx, dy; 
                //计算位置差 
                dx = desPt.x - Image1->Left; 
                dy = desPt.y - Image1->Top; 
                if((abs(dx) > 5)||(abs(dy) > 5)) 
                { 
                        Image1->Left += dx/50; 
                        Image1->Top += dy/50; 
                } 
                else 
                { 
                        move = false; 
                } 
                Background->Canvas->Draw(Image1->Left, Image1->Top, Image1->Picture->Graphic); 
                //画到表单上 
                Canvas->CopyRect(desrect, Background->Canvas, desrect); 
        } 
        else 
        { 
                Timer1->Enabled = false;  //关定时器 
        } 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::FormDockDrop(TObject *Sender, 
      TDragDockObject *Source, int X, int Y) 
{ 
        Image1->Left = X; 
        Image1->Top = Y; 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::FormPaint(TObject *Sender) 
{ 
        TRect desrect = this->ClientRect; 
        Canvas->CopyRect(desrect, Background->Canvas, desrect); 
} 
//--------------------------------------------------------------------------- 
void __fastcall TForm1::FormMouseMove(TObject *Sender, TShiftState Shift, 
      int X, int Y) 
{ 
        desPt.x = X; 
        desPt.y = Y; 
        move = true; 
        Timer1->Enabled = true; 
} 
//--------------------------------------------------------------------------- 
int __fastcall TForm1::OnEraseBkgnd(TWMEraseBkgnd &Msg) 
{ 
        return 1; 
}