C ++ Builder,TShapes,如何更改颜色OnMouseEnter

时间:2015-03-31 12:08:32

标签: c++ c++builder onmouseover c++builder-xe7

HeJ小鼠!我正在尝试以编程方式创建TShape。当我运行程序并单击按钮 - 一切正常。但是当我再次单击该按钮时,事件OnMouseEnter(OnMouseLeave)仅适用于LAST Shape。不适用于以前的任何一个。

    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;
}

1 个答案:

答案 0 :(得分:1)

您的OnMouse...事件处理程序正在使用i索引Shape[]数组,但i包含 last {{的索引1}}您创建的(并且顺便说一下,在创建第一个TShape之前,当您递增Shape[0]时,不会填充i。)

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

TShape