C++ Builder、TShapes、如何更改颜色 OnMouseEnter
C++ Builder, TShapes, How to change color OnMouseEnter
嘿!我正在尝试以编程方式创建 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
包含 last TShape
你创建了(顺便说一句,你在创建第一个 TShape
之前递增 i
时没有填充 Shape[0]
m)。
要执行您正在尝试的操作,事件处理程序需要使用其 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;
}
嘿!我正在尝试以编程方式创建 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
包含 last TShape
你创建了(顺便说一句,你在创建第一个 TShape
之前递增 i
时没有填充 Shape[0]
m)。
要执行您正在尝试的操作,事件处理程序需要使用其 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;
}