2013-03-26 91 views
-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"); 
} 
+1

“不行”是什么意思?你的意思是它永远不会被调用,或者它不符合你的期望? – 2013-03-26 16:44:41

+0

是的,它从来没有被称为和dosent做任何事情 – 2013-03-26 16:45:17

+0

C#是非常类似的struktur所以也许有人会现在awnser – 2013-03-26 16:48:52

回答

0

如果这是你的整个代码,那么你所做的一切就是定义一些方法。用户界面不知道这些事件发生时应该调用它们。

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

MouseEnterMouseHover,和MouseLeave都被定义为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); 
+0

,但它说错误C2664:'System :: Windows :: Forms :: Control :: MouseEnter :: add':can not将参数1从'System :: Windows :: Forms :: MouseEventHandler ^'转换为'System :: EventHandler ^' – 2013-03-26 16:56:58

+0

啊,我没有检查你使用的事件的定义。所有这三种方法都被定义为EventHandler,而不是MouseEventHandler。您还需要更改您的方法定义。 – 2013-03-26 16:59:39

+0

我改变它,你告诉我,现在它说错误C3352:'void winformsapp :: Form1 :: pictureBox1_MouseEnter(System :: Object ^,System :: EventArgs)':指定的函数不匹配委托类型'无效System :: Object ^,System :: EventArgs ^)' – 2013-03-26 17:03:56

0

也许你忘了这些方法添加为回调

+0

因为我是新进入这一切。你可以请解释更多,并向我展示se方法的例子,回调植入正确+ – 2013-03-26 16:47:05

+0

我真的不明白这个例子 – 2013-03-26 16:49:38

相关问题