C++ã§ãã¹ã¯ãããã¢ããªã±ã¼ã·ã§ã³ãæ¸ãã¦ããã¨ã¤ãã³ããã³ãã©ï¼ã¡ãã»ã¼ã¸ãã³ãã©ï¼ãæ¸ããã¨ã«ãªãã¨æãã¾ã. ãã¿ã³ãã¯ãªãã¯ãããä½ããå®è¡ãããããã§ããã°åé¡ãªãã§ãããå³ã¯ãªãã¯ä¸ã«ãã¦ã¹ãåãããã¨ãã¯Aã¨ããåãããã¦ã¹ãã¤ã¼ã«ãã¯ãªãã¯ä¸ã«ãã¦ã¹ãåãããã¨ãã¯Bã¨ããå¦çããããã¨ãã«ã©ãããã°ã·ã³ãã«ã«æ¸ããããªã¼ã¨å°ãææ¡ãã¦ãã¾ãã. 以ä¸ãã¬ã¼ã ã¯ã¼ã¯ã¯MFCï¼ã¨ããå¤ã®æè¡ï¼ã使ç¨ãã¦ãã¾ããããã¬ã¼ã ã¯ã¼ã¯ã«ä¾ããªã話ã«ãªãã¾ã.
æåã¯æ¬¡ã®ããã«å®è£
ãã¾ãã.
/********************** Hoge.h **********************/ class Hoge { // Field private: BOOL isRButtonDown=false; BOOL isMButtonDown=false; // Method private: void ShoriA(CPoint pt); void ShoriB(CPoint pt); DECLARE_MESSAGE_MAP() afx_msg void OnMouseMove(UINT nFlags, CPoint pt); afx_msg void OnRButtonDown(UINT nFlags, CPoint point); afx_msg void OnRButtonUp(UINT nFlags, CPoint point); afx_msg void OnMButtonDown( UINT nFlags, CPoint point); afx_msg void OnMButtonUp(UINT nFlags, CPoint point); } /********************** Hoge.cpp **********************/ BEGIN_MESSAGE_MAP(Hoge, CEdit) ON_WM_PAINT() ON_WM_MOUSEMOVE() ON_WM_RBUTTONDOWN() ON_WM_RBUTTONUP() ON_WM_MBUTTONDOWN() ON_WM_MBUTTONUP() END_MESSAGE_MAP() void Hoge::OnPaint() { // æç»å¦ç } void Hoge::OnMouseMove(UINT nFlags, CPoint pt) { if(isRButtonDown){ ShoriA(pt); }else if(isMButtonDown){ ShoriB(pt); } // åæç» OnPaint(); } void Hoge::ShoriA(CPoint pt) { // å³ã¯ãªãã¯ä¸ã®å¦ç } void Hoge::ShoriB(CPoint pt) { // ãã¦ã¹ãã¤ã¼ã«ã¯ãªãã¯ä¸ã®å¦ç } void Hoge::OnRButtonDown(UINT nFlags, CPoint point) { isRButtonDown=TRUE; } void Hoge::OnRButtonUp(UINT nFlags, CPoint point) { isRButtonDown=FALSE; } void Hoge::OnMButtonDown(UINT nFlags, CPoint point) { isMButtonDown=TRUE; } void Hoge::OnMButtonUp(UINT nFlags, CPoint point) { isMButtonDown=FALSE; }
ããã§ãåãã«ã¯åããã§ãããOnMouseMove()å ã«ifæãè¤æ°ããèªã¿ã«ããã§ã. ããã«æ¡ä»¶åå²ãå¢ããã¨ãã³ã¼ããèªã¿ã«ãããªã£ããæ¡ä»¶ã®è¿½å å¿ããçããæããããã¾ã.
å¦çãã·ã³ãã«ã«ããããã«ãé¢æ°ãã¤ã³ã¿ã使ã£ãå®è£
æ¹æ³ã«å¤æ´ãã¾ããã
/********************** Hoge.h **********************/ class Hoge { // Field private: void (Hoge::*pFuncOnMouseMove)(CPoint pt) = NULL; // Method private: void ShoriA(CPoint pt); void ShoriB(CPoint pt); DECLARE_MESSAGE_MAP() afx_msg void OnMouseMove(UINT nFlags, CPoint pt); afx_msg void OnRButtonDown(UINT nFlags, CPoint point); afx_msg void OnRButtonUp(UINT nFlags, CPoint point); afx_msg void OnMButtonDown( UINT nFlags, CPoint point); afx_msg void OnMButtonUp(UINT nFlags, CPoint point); } /********************** Hoge.cpp **********************/ BEGIN_MESSAGE_MAP(Hoge, CEdit) ON_WM_PAINT() ON_WM_MOUSEMOVE() ON_WM_RBUTTONDOWN() ON_WM_RBUTTONUP() ON_WM_MBUTTONDOWN() ON_WM_MBUTTONUP() END_MESSAGE_MAP() void Hoge::OnPaint() { // æç»å¦ç } void Hoge::OnMouseMove(UINT nFlags, CPoint pt) { // NULLãã§ã㯠if (pFuncOnMouseMove) { // ã¡ã³ãã®é¢æ°ãã¤ã³ã¿ã®å¼ã³åºãã«ã¯thisãã¤ã³ã¿ã使ãå¿ è¦ããã (this->*pFuncOnMouseMove)(pt); } } void Hoge::ShoriA(CPoint pt) { // å³ã¯ãªãã¯ä¸ã®å¦ç } void Hoge::ShoriB(CPoint pt) { // ãã¦ã¹ãã¤ã¼ã«ã¯ãªãã¯ä¸ã®å¦ç } void Hoge::OnRButtonDown(UINT nFlags, CPoint point) { pFuncOnMouseMove = &Hoge::ShoriA; } void Hoge::OnRButtonUp(UINT nFlags, CPoint point) { pFuncOnMouseMove = NULL; } void Hoge::OnMButtonDown(UINT nFlags, CPoint point) { pFuncOnMouseMove = &Hoge::ShoriB; } void Hoge::OnMButtonUp(UINT nFlags, CPoint point) { pFuncOnMouseMove = NULL; }
ããªãã·ã³ãã«ãªå®è£ ã«ãªã£ãã¨æãã¾ã. é¢æ°ãã¤ã³ã¿ã®NULLãã§ãã¯ãå¿ è¦ã«ãªããã¨ã¨ã¤ã³ã¿ã¼ãã§ã¼ã¹ãçµ±ä¸ããå¿ è¦ããããã¨ãèæ ®ãã¦ããã¡ãã®å®è£ ã®æ¹ãè¯ãæ°ããã¾ã.