那位调试一下我的程序,这个问题很奇怪.
我的程序是要实现标注型水印,在打开的原始图片上动态叠加多个标签和图片,并且能够拖动,我单独把标签和图片分别在两个程序里实验是成功的,但是现在把它们合在一起就出问题,说我没有定义Image.
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <Dialogs.hpp>
#include <ExtCtrls.hpp>
#include <ExtDlgs.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TOpenPictureDialog *OpenPictureDialog1;
TMemo *Memo1;
TButton *Button2;
TOpenPictureDialog *OpenPictureDialog2;
TPageControl *PageControl1;
TTabSheet *TabSheet1;
TScrollBox *ScrollBox1;
TImage *Image;
TScrollBox *ScrollBox2;
TButton *Button3;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Memo1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift);
void __fastcall Memo1KeyPress(TObject *Sender, char &Key);
void __fastcall Memo1KeyUp(TObject *Sender, WORD &Key,
TShiftState Shift);
void __fastcall FormCreate(TObject *Sender);
void __fastcall LabelMouseDown(TObject *Sender,TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall LabelMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall LabelMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
void __fastcall ImageMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y);
void __fastcall ImageMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y);
void __fastcall ImageMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
AnsiString FileName1,FileName2;
bool lbDown;
int x;
int y;
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TLabel *Label[10];
TImage *Image[10];
int i=0;
int j=0;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Memo1->Visible=true;
AnsiString str_obj_text="ÇëÊäÈëÎı¾";
Memo1->Text=str_obj_text;
Label[i]=new TLabel(this);
Label[i]->Caption="ÇëÊäÈëÎı¾";
Label[i]->Left=50*i;
Label[i]->Top=50*i;
Label[i]->Width=100;
Label[i]->Height=30;
Label[i]->Parent=ScrollBox1;
Label[i]->OnMouseDown=LabelMouseDown;
Label[i]->OnMouseMove=LabelMouseMove;
Label[i]->OnMouseUp=LabelMouseUp;
Label[i]->Transparent=true;
Label[i]->Show();
i=i+1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1KeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
TLabel *Label;
Label= dynamic_cast<TLabel *>(Sender);
if (Label==NULL)
return;
Label->Caption=Memo1->Text;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1KeyPress(TObject *Sender, char &Key)
{
TLabel *Label;
Label= dynamic_cast<TLabel *>(Sender);
if (Label==NULL)
return;
Label->Caption=Memo1->Text;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo1KeyUp(TObject *Sender, WORD &Key,
TShiftState Shift)
{
TLabel *Label;
Label= dynamic_cast<TLabel *>(Sender);
if (Label==NULL)
return;
Label->Caption=Memo1->Text;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabelMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
lbDown=true;
x=X;
y=Y;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabelMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
lbDown=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::LabelMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(!lbDown)
return;
TLabel *Label;
Label= dynamic_cast<TLabel *>(Sender);
if (Label==NULL)
return;
Label->Left+=(X-x);
Label->Top+=(Y-y);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
lbDown=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ImageMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
lbDown=true;
x=X;
y=Y;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ImageMouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
if(lbDown)
{
TImage *Image;
Image = dynamic_cast<TImage *>(Sender);
if (Image==NULL)
return;
Image->Left+=(X-x);
Image->Top+=(Y-y);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ImageMouseUp(TObject *Sender, TMouseButton Button,
TShiftState Shift, int X, int Y)
{
lbDown=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if (!OpenPictureDialog2->Execute()) return;
FileName2 = OpenPictureDialog2->FileName;
Image->Picture->LoadFromFile(FileName2);
Image->Height=Image->Picture->Height;
Image->Width=Image->Picture->Width;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Image[j]=new TImage(this);
Image[j]->Parent=ScrollBox1;
if (!OpenPictureDialog1->Execute()) return;
FileName1 = OpenPictureDialog1->FileName;
Image[j]->Picture->LoadFromFile(FileName1);
Image[j]->Left=TabSheet1->ClientWidth/2;
Image[j]->Top=TabSheet1->ClientHeight/2;
Image[j]->Height=1000;//Image1->Picture->Height;
Image[j]->Width=1000;//Image1->Picture->Width;
Image[j]->Transparent=true;
Image[j]->OnMouseDown=ImageMouseDown;
Image[j]->OnMouseMove=ImageMouseMove;
Image[j]->OnMouseUp=ImageMouseUp;
ScrollBox1->DoubleBuffered=true;
j=j+1;
}
//---------------------------------------------------------------------------