-1

我正在使用或“学习”C++/CLI,因为我喜欢 GUI 的外观,并且我试图在鼠标悬停在图片上和不在图片上时触发一些事件,但它不起作用,唯一有效的事件是鼠标点击图片时。

我的代码在下面

void pictureBox1_MouseEnter(Object^ sender, System::Windows::Forms::MouseEventArgs^ ) {
    label1->Text = String::Concat( sender->GetType(), ": Enter" );
}

void pictureBox1_MouseHover(Object^ sender,  System::Windows::Forms::MouseEventArgs^ ) {
    label1->Text = String::Concat( sender->GetType(), ": MouseHover" );
}

void pictureBox1_MouseLeave(Object^ sender,  System::Windows::Forms::MouseEventArgs^ ) {
    label1->Text = String::Concat( sender->GetType(), ": MouseLeave" );
}

private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {
    label1->Text = String::Concat( sender->GetType(), ": Click" );
}
4

2 回答 2

0

如果这是您的全部代码,那么您所做的就是定义一些方法。UI 不知道当这些事件发生时它应该调用它们。

您需要做的是在各种对象上添加事件处理程序。从您的本地方法创建一个委托,并将其(使用+=运算符)添加到事件中。

MouseEnterMouseHoverMouseLeave都被定义为EventHandler,而不是MouseEventHandler。这意味着该方法应该采用EventArgs,而不是MouseEventArgs,因此请切换您的方法声明。

// Do this in the constructor.
this->pictureBox1->MouseEnter += gcnew EventHandler(this, &Form1::pictureBox1_MouseEnter);
this->pictureBox1->MouseHover += gcnew EventHandler(this, &Form1::pictureBox1_MouseHover);
this->pictureBox1->MouseLeave += gcnew EventHandler(this, &Form1::pictureBox1_MouseLeave);

this->pictureBox1->Click += gcnew EventHandler(this, &Form1::pictureBox1_Click);
于 2013-03-26T16:54:17.133 回答
0

也许您忘记将这些方法添加为回调

于 2013-03-26T16:45:54.837 回答