2013-03-26 94 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

相關問題