hjlin您好: 1.請您修正一下不良標題!!! 2.請參考包子兄的作品
利用滑鼠截取 Image 的部分區域
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=34115
Unit1.cpp //--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit1.h" bool gbMDown=false;
TRect grtLTRB; #define IMAGE_WIDTH 300
#define IMAGE_HEIGHT 300 //---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender)
{
Panel1->DoubleBuffered=true; Image1->Width=IMAGE_WIDTH;
Image1->Height=IMAGE_HEIGHT;
Image1->Left=(Panel1->Width-Image1->Width)/2;
Image1->Top=(Panel1->Height-Image1->Height)/2;
Image1->Stretch=true;
Image1->Picture->Bitmap->LoadFromFile("ImageRect1.bmp"); Image2->Width=IMAGE_WIDTH;
Image2->Height=IMAGE_HEIGHT;
Image2->Left=(Panel1->Width-Image2->Width)/2;
Image2->Top=(Panel1->Height-Image2->Height)/2;
Image2->Stretch=true;
Image2->Canvas->Brush->Color=clWhite;
Image2->Canvas->FillRect(Rect(0, 0, Image2->Width, Image2->Height));
Image2->Picture->Bitmap->TransparentColor=clWhite;
Image2->Transparent=true; Image3->Width=IMAGE_WIDTH;
Image3->Height=IMAGE_HEIGHT;
Image3->Left=(Panel2->Width-Image3->Width)/2;
Image3->Top=(Panel2->Height-Image3->Height)/2;
Image3->Stretch=true;
Image3->Canvas->Brush->Color=clWhite;
Image3->Canvas->FillRect(Rect(0, 0, Image3->Width, Image3->Height));
}
//---------------------------------------------------------------------------
//取得選取的起始點 void __fastcall TForm1::Image2MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
gbMDown=true;
grtLTRB.Left=X;
grtLTRB.Top=Y;
grtLTRB.Right=X;
grtLTRB.Bottom=Y;
}
//--------------------------------------------------------------------------- //取得Mouse移動座標
void __fastcall TForm1::Image2MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(gbMDown)
{
if(X<0) X=0;
if(X>Image2->Width) X=Image2->Width;
if(Y<0) Y=0;
if(Y>Image2->Height) Y=Image2->Height; grtLTRB.Right=X;
grtLTRB.Bottom=Y; Image2->Canvas->FillRect(Rect(0, 0, Image2->Width, Image2->Height));
Image2->Canvas->Rectangle(grtLTRB);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image2MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
gbMDown=false;
Image2->Canvas->FillRect(Rect(0, 0, Image2->Width, Image2->Height));
Image3->Canvas->FillRect(Rect(0, 0, Image3->Width, Image3->Height));
Image3->Canvas->CopyRect(grtLTRB, Image1->Canvas, grtLTRB);
Label2->Caption="被截取的區域: Rect("+IntToStr(grtLTRB.Left)+", "+
IntToStr(grtLTRB.Top)+", "+IntToStr(grtLTRB.Right)+", "+
IntToStr(grtLTRB.Bottom)+")";
}
//--------------------------------------------------------------------------- Unit1.h //--------------------------------------------------------------------------- #ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp>
#include
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TPanel *Panel1;
TPanel *Panel2;
TLabel *Label2;
TImage *Image1;
TImage *Image2;
TImage *Image3;
void __fastcall FormCreate(TObject *Sender);
void __fastcall Image2MouseDown(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall Image2MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
void __fastcall Image2MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
HAVE A NICE DAY FOR YOU 發表人 - axsoft 於 2003/07/28 17:35:08