所以在我的程序中,我有很多类似的按钮,它们都有相同的变量,执行相同的功能……所以我想我应该创建一个"CustomButton“类,它是C++按钮的子类,但是包含了我的函数和所有已有的函数。
public ref class CustomButton : public System::Windows::Forms::Button{
protected:
virtual void OnMouseDown(System::Windows::Forms::MouseEventArgs ^e) override{
if(e->Button == System::Windows::Forms::MouseButtons::Left) this->Location = System::Drawing::Point(this->Location.X+1, this->Location.Y+1);
}
virtual void OnMouseUp(System::Windows::Forms::MouseEventArgs ^e) override{
if(e->Button == System::Windows::Forms::MouseButtons::Left) this->Location = System::Drawing::Point(this->Location.X-1, this->Location.Y-1);
}
};如上所示,我可以很好地更改变量,但是当我尝试更改它的函数时…它只是完全停止执行其他功能。我是说,后来当我这么做的时候...
CustomButton ^encButton;
this->encButton->Click += gcnew System::EventHandler(this, &Form1::encButton_Click);它完全忽略了它,根本不会调用encButton_Click函数。如果我试着让它鼠标按下,也是一样的。
我想我改写了一些它不喜欢我做的事情...但是我想不出另一种方法来做我想要做的事情?
发布于 2011-02-07 03:00:44
您必须调用基类方法来保持原始框架代码的工作。修复:
virtual void OnMouseDown(System::Windows::Forms::MouseEventArgs ^e) override {
__super::OnMouseDown(e);
if (e->Button == System::Windows::Forms::MouseButtons::Left) {
this->Location = System::Drawing::Point(this->Location.X+1, this->Location.Y+1);
}
}您可以选择是先调用基类方法还是最后调用基类方法。虽然last通常是正确的方法,但您更改的按钮状态足以保证首先调用基类方法。那得看情况。
https://stackoverflow.com/questions/4915332
复制相似问题