ilikenba 发表于 2005-4-29 14:48

你不如把这些变量放到一个类的成员里面!

小菜菜同学 发表于 2005-4-29 15:24

<P>1、1、1、1、
//---------------------------------------------------------------------------</P><P>#include &lt;vcl.h&gt;
#pragma hdrstop</P><P>#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
class cla{
public:
int ox,oy;
};
cla *a=new cla;
int x,y;
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
if(Button=mbLeft){
a-&gt;ox=Image1-&gt;Left;
a-&gt;oy=Image1-&gt;Top;
x=X;
y=Y;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
if(Shift.Contains(ssLeft)){
Image1-&gt;Left+=X-x;
Image1-&gt;Top+=Y-y;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
if(Image1-&gt;Left&gt;Shape1-&gt;Left-20 &amp;&amp;
Image1-&gt;Left&lt;Shape1-&gt;Left+Shape1-&gt;Width+
20&amp;&amp;Image1-&gt;Top&gt;Shape1-&gt;Top-20&amp;&amp;
Image1-&gt;Top&lt;Shape1-&gt;Top+20+Shape1-&gt;Top){
Image1-&gt;Top=Shape1-&gt;Top;
Shape1-&gt;Visible=false;
Image1-&gt;Left=Shape1-&gt;Left;
}else{
Image1-&gt;Left=a-&gt;ox;
Image1-&gt;Top=a-&gt;oy;
}
}</P><P>2、2、2、2、2、2、2、2、2、、2、2、2、2、2、、2、2、2、2、2、2、2、2、2//---------------------------------------------------------------------------</P><P>#include &lt;vcl.h&gt;
#pragma hdrstop</P><P>#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
class cla{
public:
int ox,oy;
int get_x(){return ox;}
int get_y(){return oy;}
};
cla *a=new cla;
int x,y;
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
if(Button=mbLeft){
a-&gt;ox=Image1-&gt;Left;
a-&gt;oy=Image1-&gt;Top;
x=X;
y=Y;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
if(Shift.Contains(ssLeft)){
Image1-&gt;Left+=X-x;
Image1-&gt;Top+=Y-y;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
int x1,y1;
if(Image1-&gt;Left&gt;Shape1-&gt;Left-20 &amp;&amp;
Image1-&gt;Left&lt;Shape1-&gt;Left+Shape1-&gt;Width+
20&amp;&amp;Image1-&gt;Top&gt;Shape1-&gt;Top-20&amp;&amp;
Image1-&gt;Top&lt;Shape1-&gt;Top+20+Shape1-&gt;Top){
Image1-&gt;Top=Shape1-&gt;Top;
Shape1-&gt;Visible=false;
Image1-&gt;Left=Shape1-&gt;Left;
}else{
x1=a-&gt;get_x();
y1=a-&gt;get_y();
Image1-&gt;Left=x1;
Image1-&gt;Top=y1;
}
}</P>

小菜菜同学 发表于 2005-4-29 15:27

<P>以上两种都不行,</P><P>我在没有mousemove\down\up时</P><P>全局变量好使</P>

小菜菜同学 发表于 2005-4-29 15:28

<P>总提示 Unit1.cpp(28): W8060 Possibly incorrect assignment</P><P>定位在 if(Button=mbLeft){</P>

小菜菜同学 发表于 2005-4-29 15:35

<P>简化成这样:还是都等于零,我要吐了</P><P>int ox,oy;
int x,y;
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
if(Button=mbLeft){
ox=Image1-&gt;Left;
oy=Image1-&gt;Top;
x=X;
y=Y;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
int x1,y1;</P><P>x1=ox;
y1=oy;
ShowMessage("ox="+IntToStr(x1) +"oy="+IntToStr(y1) );
}</P>

小菜菜同学 发表于 2005-4-29 15:42

<P>重大发现:以下不是0。而加上if(Button=mbLeft){就为零了。病根找到了,但怎么回事呢?</P><P>int ox,oy;
void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
ox=Image1-&gt;Left;
oy=Image1-&gt;Top;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
ShowMessage("ox="+IntToStr(ox) +"oy="+IntToStr(oy) );
}</P>

小菜菜同学 发表于 2005-4-29 15:54

<P>对,去掉mousedown中的if(Button=mbLeft){}就实现了拖动图片到图形中的功能了。</P><P>总算喘口气。但if(Button=mbLeft){}没什么毛病呀?</P><P>刚写到这,一看书,我恨不得打我自己一巴掌!</P><P>应该是if(Button==mbLeft){}。</P><P></P><P>这种错误BCB6怎么不拦住我呀!?</P><P>让各位见笑了</P>

ilikenba 发表于 2005-4-29 16:02

<P>晕!判断是否相等要用两个等号!</P><P>呵呵!我回答的晚了!BCB的编译器的错误检查不严,你可以通过设置加强!</P>

小菜菜同学 发表于 2005-4-29 16:09

<P>不过,几乎废了3、4个小时,还算有点收获。写出来大家是新手的共勉。就是知道:</P><P>1、怎么一步一步找错误了</P><P>2、知道了怎么恢复默认设置——谢谢</P><P><TABLE cellSpacing=0 cellPadding=4 width="100%"><TR><TD glow(color=#9898BA,strength=2)" vAlign=center width=*><FONT color=#000066><B>ilikenba</B></FONT> </TD><TD vAlign=center width=25></TD><TD vAlign=center width=16></TD></TR></TABLE></P><P>
少校 的指导</P><P>3、熟悉了全局变量的用法</P><P>4、让我花1个小时想想4是什么,还有5....  (3、4个小时可不能白费呀......4是什么呢?伤脑筋呀)

</P>
页: 1 [2]
查看完整版本: 我的bcb怎么在函数外声明的变量,在函数内无法存储