2011-02-06 11 views
0

그래서 내 프로그램에서 정말 비슷한 단추가로드되어 모두 동일한 변수를 갖고 동일한 기능을 수행합니다 ... 그래서 "CustomButton"클래스를 만들 것이라고 생각했습니다. 자식 C++의 버튼은,하지만 내 기능과 모든 이미 그것. I 클래스를 위처럼 설정사용자 지정 컨트롤 사용자 지정 함수

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); 
    } 
}; 

모든이있을 때 문제는 내가 잘 변수를 변경할 수 있습니다,하지만, 내가 시도하고 그 기능을 변경할 때 ... 그냥 전혀 다른 기능을 수행 중지합니다. 나는 전혀 encButton_Click 함수를 호출하지 않습니다

CustomButton ^encButton; 
this->encButton->Click += gcnew System::EventHandler(this, &Form1::encButton_Click); 

은 그냥 완전히 무시 ... 나중에 내가이 작업을 수행 할 때, 의미한다. 내가하려고하면 똑같이해라.

나는 내가하고있는 것을 좋아하지 않는 것을 덮어 쓰고 있다고 생각한다. 그러나 나는 내가하려고하는 일을하는 다른 방법을 생각할 수 없다.

답변

1

원본 프레임 워크 코드가 작동하도록 기본 클래스 메서드를 호출해야합니다. 수정 :

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); 
    } 
} 

기본 클래스 메소드를 처음 또는 마지막으로 호출 할 수 있습니다. 마지막으로 올바른 방법을 사용하는 동안 기본 클래스 메서드를 먼저 호출하도록 단추의 상태를 변경해야합니다. 그것은 달려있다.

+0

감사합니다. 호기심에서 - 만약 당신이 알고 있다면 ... 원래의 프레임 워크 코드는 더 이상 함수가 실행되지 않는다는 것을 의미합니까? –

+0

잘 모르겠습니다. 프레임 워크의 OnMouseUp/Down 기본 메서드는 Click 이벤트를 처리합니다. –

+0

죄송합니다. 나는이 말을 몹시 말했습니다 ... 나는 왜 this-> encButton-> MouseUp + = gcnew System :: Windows :: Forms :: MouseEventHandler (this, & Form1 :: encButton_Click); 작동하지 않을까요? –