我就是用你上次发给我的代码试验成功了再写的:
下面是我改过后的文件:
Child0form.h
//---------------------------------------------------------------------------
#ifndef Child0formH
#define Child0formH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TfrmChild0 : public TForm
{
__published: // IDE-managed Components
TImage *Image1;
TImage *Image2;
void __fastcall FormCreate(TObject *Sender);
void __fastcall Image1MouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
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 Image2MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
void __fastcall Image2MouseDown(TObject *Sender, TMouseButton Button,
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 TfrmChild0(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmChild0 *frmChild0;
//---------------------------------------------------------------------------
#endif
Child0form.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#pragma hdrstop
#include "Child0form.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmChild0 *frmChild0;
TPoint StartPt,EndPt;
bool bDrawing = False, bDraging = False;
//---------------------------------------------------------------------
__fastcall TfrmChild0::TfrmChild0(TComponent *Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------
void __fastcall TfrmChild0::Image1MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
int i,j;
if( bDrawing )
{
bDrawing = False;
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, EndPt.x,EndPt.y);
Image1->Canvas->Pen->Mode = pmCopy;
Image1->Canvas->Pen->Color = clBlack;
Image1->Canvas->Brush->Style = bsClear;
int itop = Image1->Top;
int ileft = Image1->Left;
Image2->Height = abs(EndPt.y - StartPt.y);
Image2->Width = abs(EndPt.x - StartPt.x);
Image2->Picture->Bitmap->Height = abs(EndPt.y - StartPt.y);
Image2->Picture->Bitmap->Width = abs(EndPt.x - StartPt.x);
Image2->Left = (StartPt.x>EndPt.x ? EndPt.x :StartPt.x) +ileft;
Image2->Top = (StartPt.y>EndPt.y ? EndPt.y :StartPt.y) +itop;
for(i=0;i<Image2->Width;i++)
for(j=0;j<Image2->Height;j++)
{
if( Image1->Canvas->Pixels[i+ileft][j+itop]==clRed )
Image2->Canvas->Pixels[j]=clGreen;
else
Image2->Canvas->Pixels[j]=clRed;
}
Image2->Visible = True;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmChild0::Image1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
Image2->Visible = False;
Image1->Canvas->Pen->Color = clWhite;
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, EndPt.x,EndPt.y);
bDrawing = True;
StartPt = TPoint(X,Y);
EndPt = StartPt;
Image1->Canvas->Pen->Mode = pmNot;
Image1->Canvas->Pen->Color = clRed;
Image1->Canvas->Brush->Style =bsClear;
Image1->Canvas->Pen->Style = psDot;
}
//---------------------------------------------------------------------------
void __fastcall TfrmChild0::Image1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
if( bDrawing && ((EndPt.x != X)||(EndPt.y != Y)) )
{
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, EndPt.x,EndPt.y);
Image1->Canvas->Rectangle(StartPt.x,StartPt.y, X,Y);
EndPt.x = X;
EndPt.y = Y;
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmChild0::Image2MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
StartPt.x = X;
StartPt.y = Y;
bDraging = True;
}
//---------------------------------------------------------------------------
void __fastcall TfrmChild0::Image2MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{ if( bDraging )
{
Image2->Left = Image2->Left +X - StartPt.x;
Image2->Top = Image2->Top + Y - StartPt.y;
Image2->Hint = "X:"+IntToStr(Image2->Left)+" Y:"+IntToStr(Image2->Top);
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmChild0::Image2MouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
bDraging = False;
}
//---------------------------------------------------------------------------
void __fastcall TfrmChild0::FormCreate(TObject *Sender)
{
int i,y;
for (i=1;i<Image1->Height; i++)
{
y=100*sin(i/180.0*3.1415);
Image1->Canvas->Pixels[y+200] = clRed;
}
}
//---------------------------------------------------------------------------