C++ C++;生成器,T形状,如何在MouseCenter上更改颜色

C++ C++;生成器,T形状,如何在MouseCenter上更改颜色,c++,c++builder,onmouseover,c++builder-xe7,C++,C++builder,Onmouseover,C++builder Xe7,嘿!我正在尝试以编程方式创建一个TShape。当我运行程序并点击按钮时,一切正常。但是,当我再次单击按钮时,OnMouseEnter(OnMouseLeave)事件仅适用于最后一个形状。不适用于前面的任何选项 int i=0; TShape* Shape[50]; void __fastcall TForm1::Button1Click(TObject *Sender) { int aHeight = rand() % 101 + 90; int bWid

嘿!我正在尝试以编程方式创建一个TShape。当我运行程序并点击按钮时,一切正常。但是,当我再次单击按钮时,OnMouseEnter(OnMouseLeave)事件仅适用于最后一个形状。不适用于前面的任何选项

    int i=0;
    TShape* Shape[50];
    void __fastcall TForm1::Button1Click(TObject *Sender)
{
    int aHeight = rand() % 101 + 90;
    int bWidth = rand() % 101 + 50;
    i++;
    Shape[i] = new TShape(Form1);
    Shape[i]->Parent = this;
    Shape[i]->Visible = true;
    Shape[i]->Brush->Style=stCircle;
    Shape[i]->Brush->Color=clBlack;

    Shape[i]->Top =    aHeight;
    Shape[i]->Left = bWidth;
    Shape[i]->Height=aHeight;
    Shape[i]->Width=bWidth;

    Shape[i]->OnMouseEnter = MouseEnter;
    Shape[i]->OnMouseLeave = MouseLeave;

    Label2->Caption=i;


    void __fastcall TForm1::MouseEnter(TObject *Sender)
{
    Shape[i]->Pen->Color = clBlue;
     Shape[i]->Brush->Style=stSquare;
     Shape[i]->Brush->Color=clRed;
}



void __fastcall TForm1::MouseLeave(TObject *Sender)
{
    Shape[i]->Pen->Color = clBlack;
    Shape[i]->Brush->Style=stCircle;
    Shape[i]->Brush->Color=clBlack;
}

您的
OnMouse…
事件处理程序正在使用
i
索引到
Shape[]
数组中,但是
i
包含您创建的最后一个
TShape
的索引(顺便说一句,在创建第一个
TShape
之前,您没有填充
Shape[0]
m,因为您正在增加
i

要执行您正在尝试的操作,事件处理程序需要使用其
Sender
参数来了解哪个
TShape
当前正在触发每个事件,例如:

TShape* Shape[50];
int i = 0;

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    ...

    Shape[i] = new TShape(this);
    Shape[i]->Parent = this;
    ...
    Shape[i]->OnMouseEnter = MouseEnter;
    Shape[i]->OnMouseLeave = MouseLeave;

    ++i;
    Label2->Caption = i;
}

void __fastcall TForm1::MouseEnter(TObject *Sender)
{
    TShape *pShape = static_cast<TShape*>(Sender);

    pShape->Pen->Color = clBlue;
    pShape->Brush->Style = stSquare;
    pShape->Brush->Color = clRed;
}

void __fastcall TForm1::MouseLeave(TObject *Sender)
{
    TShape *pShape = static_cast<TShape*>(Sender);

    pShape->Pen->Color = clBlack;
    pShape->Brush->Style = stCircle;
    pShape->Brush->Color = clBlack;
}
t形状*形状[50];
int i=0;
void\uu fastcall TForm1::Button1Click(TObject*发送方)
{
...
形状[i]=新的T形状(此);
Shape[i]->Parent=this;
...
形状[i]->onmouseinter=MouseEnter;
Shape[i]->OnMouseLeave=MouseLeave;
++一,;
标签2->Caption=i;
}
void\uu fastcall TForm1::MouseEnter(TObject*发送方)
{
TShape*pShape=静态(发送方);
p形状->笔->颜色=蓝色;
p形状->画笔->样式=stSquare;
p形状->画笔->颜色=clRed;
}
void\uu fastcall TForm1::MouseLeave(TObject*发送方)
{
TShape*pShape=静态(发送方);
p形状->笔->颜色=黑色;
p形状->画笔->样式=stCircle;
p形状->画笔->颜色=黑色;
}