高分求问鼠标响应事件 class CChildView : public CWnd然后在CChildView 中添加void CChildView::OnLButtonUp(UINT nFlags, CPoint point)但是一直不响应这个事件,设了断点发现也没有去执行没辙了,求问各位达人送上高分,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 添加消息映射宏BEGIN_MESSAGE_MAP(CChildView, CView) ON_WM_LBUTTONUP() END_MESSAGE_MAP() EGIN_MESSAGE_MAP(CChildView, CView)ON_WM_LBUTTONUP()END_MESSAGE_MAP() 这样编译会报错那个鼠标响应是用MFC添加进去的消息,自动生成如下:BEGIN_MESSAGE_MAP(CChildView, CWnd) ON_WM_ACTIVATE() ON_WM_PAINT() ON_WM_LBUTTONUP()END_MESSAGE_MAP()但是点击就是没反应 EGIN_MESSAGE_MAP(CChildView, CView)ON_WM_LBUTTONUP()END_MESSAGE_MAP() 这样编译会报错那个鼠标响应是用MFC添加进去的消息,自动生成如下:BEGIN_MESSAGE_MAP(CChildView, CWnd) ON_WM_ACTIVATE() ON_WM_PAINT() ON_WM_LBUTTONUP()END_MESSAGE_MAP()但是点击就是没反应 对鼠标的响应 VC 中对鼠标的响应常用的有以下事件: a. OnLButtonDblClk :语法为 void Object1 Wnd :: OnLButtonDblClk( UINT nFlags, CPoint point ){} ,表示控件对双击鼠标左键时的响应。 b. OnLButtonDown :语法为 void Object1 Wnd :: OnLButtonDown( UINT nFlags, CPoint point ){} ,表示控件对按下鼠标左键时的响应。 c. OnLButtonUp :语法为 void Object1 Wnd :: OnLButtonUp( UINT nFlags, CPoint point ){} ,表示控件对按下的鼠标左键放开后的响应。 d. OnRButtonDblClk :语法为 void Object1 Wnd :: OnRButtonDblClk( UINT nFlags, CPoint point ){} ,表示控件对双击鼠标右键时的响应。 e. OnRButtonDown :语法为 void Object1 Wnd :: OnRButtonDown( UINT nFlags, CPoint point ){} ,表示控件对按下鼠标右键时的响应。 f. OnRButtonUp :语法为 void Object1 Wnd :: OnRButtonUp( UINT nFlags, CPoint point ){} ,表示控件对按下的鼠标右键放开后的响应。 其中的参数 nFlags 用于指示按下的键的代码,它可以是下面的几个值或它们的组合: MK_CONTROL 表示按下 CTRL 键 ; MK_LBUTTON 表示按下鼠标左键; MK_MBUTTON 表示按下鼠标中键; MK_RBUTTON 表示按下鼠标右键; MK_SHIFT 表示按下 SHIFT 键; Point 参数表示鼠标的光标位置相对于所在窗口的左上角的水平和垂直坐标值。 在 VC 中用鼠标响应事件来完成人机交互中的控制功能是比较容易的。例如把一个控件 Object1 的位置移动到鼠标光标的位置,下面简单的程序就可完成: void Object1::OnLButtonDown(UINT nFlags, CPoint point) { CPoint Position ; Position = point; Object1->left=Position.x; Object1->top=Position.y; // 按下鼠标左键后控件的水平和垂直方向的值分别与鼠标的光标位置一致 } SDI工程, CMainFrame类下的两个虚函数(其中m_wndView是CTestView声明的对象 CTestView m_wndView,CTestView是从classwizard中从generic CWnd继承过来) BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo) { // let the view have first crack at the command if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo)) return TRUE; // otherwise, do default handling return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo); } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { if( !CFrameWnd::PreCreateWindow(cs) ) return FALSE; // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.dwExStyle &= ~WS_EX_CLIENTEDGE; cs.lpszClass = AfxRegisterWndClass(0); return TRUE; } 两个消息映射 int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; // create a view to occupy the client area of the frame if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL)) { TRACE0("Failed to create view window\n"); return -1; } if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) { TRACE0("Failed to create toolbar\n"); return -1; // fail to create } if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } // TODO: Delete these three lines if you don't want the toolbar to // be dockable m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY); EnableDocking(CBRS_ALIGN_ANY); DockControlBar(&m_wndToolBar); return 0; } void CMainFrame::OnSetFocus(CWnd* pOldWnd) { // forward focus to the view window m_wndView.SetFocus(); } CTestView类 class CTestView : public CWnd { // Construction public: CTestView(); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CTestView) public: virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL); protected: virtual BOOL PreCreateWindow(CREATESTRUCT& cs); //}}AFX_VIRTUAL public: virtual ~CTestView(); // Generated message map functions protected: //{{AFX_MSG(CTestView) afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); afx_msg void OnShowWindow(BOOL bShow, UINT nStatus); //}}AFX_MSG DECLARE_MESSAGE_MAP() private: HDC m_WindowDC; }; .cpp文件中 BEGIN_MESSAGE_MAP(CTestView, CWnd) //{{AFX_MSG_MAP(CTestView) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_SHOWWINDOW() //}}AFX_MSG_MAP END_MESSAGE_MAP() 两个虚函数 BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Add your specialized code here and/or call the base class if (!CWnd::PreCreateWindow(cs)) return FALSE; cs.dwExStyle |= WS_EX_CLIENTEDGE; cs.style &= ~WS_BORDER; cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, ::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW 1), NULL); return TRUE; } BOOL CTestView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) { // TODO: Add your specialized code here and/or call the base class if (!CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext)) return FALSE; return TRUE; } 三个消息映射 void CChildView::OnShowWindow(BOOL bShow, UINT nStatus) { CWnd::OnShowWindow(bShow, nStatus); m_MouseAction = ActionDefault; HCURSOR cursor = AfxGetApp()->LoadCursor(IDC_CURSOR_NORMAL); ::SetClassLong(m_hWnd, GCL_HCURSOR, (long)cursor); ::SetCursor(cursor); m_WindowDC = ::GetDC(m_hWnd); } void CTestView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CWnd::OnLButtonDown(nFlags, point); } void CTestView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CWnd::OnLButtonUp(nFlags, point); } OnShowWindow可以响应,但是响应不了鼠标点击消息,不解,不知道是不是被拦截了。 高手指教,要装墙了,用spy 观看,能够响应鼠标点击的两个消息,但是程序中就是没有执行。:在CMainFrame::OnCmdMsg中不需要做什么处理吧,如果在CTestView类中实现了OnLButtonDown并且你在此视图区域点击了,那么自然就应该被触发。这样试一下 BOOL CTestView::PreTranslateMessage(MSG* pMsg) { // TODO: Add your specialized code here and/or call the base class if(pMsg->message == WM_LBUTTONDOWN) { AfxMessageBox("WM_LBUTTONDOWN"); } return CView::PreTranslateMessage(pMsg); } 另外,不要保存DC,去掉m_WindowDC = ::GetDC(m_hWnd); 这句,用到的时候获取DC,用完释放。你的OnLButtonDown和OnLButtonUp里根据没有任何的代码,你怎么判断没有响应呢? 你加一个AfxMessageBox("OnLButtonDown");和AfxMessageBox("OnLButtonUp");测试一下,肯定会响应的。加了几个简单的测试,在消息函数中添加了一些测试代码,也没有进入到两个消息中去你的void CTestView::OnLButtonDown(UINT nFlags, CPoint point) 中根本没有响应代码啊??当然没有反映了查到一些问题了,我在childview中会显示地图,显示的地图只有一部分能够响应鼠标消息,继续调试中1.消息发出的指定窗口不对,即PostMessage或者SendMessage的Handle没有设对; 2.该消息已经被其他的拦截了——如果是这样的话,可以PreTranslateMessage一下。 广州的失业的人可能会再多 《〈〉高分》》》如何区分TREEVIEW中的根结点《〈〉高分》》》 TabCtrl窗口大小设置问题,谢谢了! 求助:各位大侠,一个ActiveX控件的问题,在线等,急..... 用dirextx开发的问题 用MFC写了一个程序作为Server,但运行一段时间,机器就基本动不了了,请帮忙分析一下可能的原因。 关于GlobalAlloc的问题 拿位大虾进来!500分!! 分值不多,进来看看!!!!!!!!!!!!!!! 在窗体创建的时候,如何将窗体最大化??? 一个程序调试的问题 字符串转成浮点型
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
这样编译会报错那个鼠标响应是用MFC添加进去的消息,自动生成如下:
BEGIN_MESSAGE_MAP(CChildView, CWnd)
ON_WM_ACTIVATE()
ON_WM_PAINT()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()但是点击就是没反应
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
这样编译会报错那个鼠标响应是用MFC添加进去的消息,自动生成如下:
BEGIN_MESSAGE_MAP(CChildView, CWnd)
ON_WM_ACTIVATE()
ON_WM_PAINT()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()但是点击就是没反应
SDI工程,
CMainFrame类下的两个虚函数(其中m_wndView是CTestView声明的对象 CTestView m_wndView,CTestView是从classwizard中从generic CWnd继承过来)
BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
// let the view have first crack at the command
if (m_wndView.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
return TRUE; // otherwise, do default handling
return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
} BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if( !CFrameWnd::PreCreateWindow(cs) )
return FALSE;
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs cs.dwExStyle &= ~WS_EX_CLIENTEDGE;
cs.lpszClass = AfxRegisterWndClass(0);
return TRUE;
} 两个消息映射
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// create a view to occupy the client area of the frame
if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
{
TRACE0("Failed to create view window\n");
return -1;
} if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
} if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
} // TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar); return 0;
} void CMainFrame::OnSetFocus(CWnd* pOldWnd)
{
// forward focus to the view window
m_wndView.SetFocus();
} CTestView类
class CTestView : public CWnd
{
// Construction
public:
CTestView();
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CTestView)
public:
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL public:
virtual ~CTestView(); // Generated message map functions
protected:
//{{AFX_MSG(CTestView)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnShowWindow(BOOL bShow, UINT nStatus);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
HDC m_WindowDC;
}; .cpp文件中
BEGIN_MESSAGE_MAP(CTestView, CWnd)
//{{AFX_MSG_MAP(CTestView)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_SHOWWINDOW()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
两个虚函数
BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized code here and/or call the base class if (!CWnd::PreCreateWindow(cs))
return FALSE; cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~WS_BORDER;
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS,
::LoadCursor(NULL, IDC_ARROW), HBRUSH(COLOR_WINDOW 1), NULL); return TRUE;
} BOOL CTestView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class if (!CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext))
return FALSE; return TRUE;
} 三个消息映射
void CChildView::OnShowWindow(BOOL bShow, UINT nStatus)
{
CWnd::OnShowWindow(bShow, nStatus); m_MouseAction = ActionDefault;
HCURSOR cursor = AfxGetApp()->LoadCursor(IDC_CURSOR_NORMAL);
::SetClassLong(m_hWnd, GCL_HCURSOR, (long)cursor);
::SetCursor(cursor);
m_WindowDC = ::GetDC(m_hWnd);
} void CTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default CWnd::OnLButtonDown(nFlags, point);
} void CTestView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default CWnd::OnLButtonUp(nFlags, point);
} OnShowWindow可以响应,但是响应不了鼠标点击消息,不解,不知道是不是被拦截了。
高手指教,要装墙了,用spy 观看,能够响应鼠标点击的两个消息,但是程序中就是没有执行。
:在CMainFrame::OnCmdMsg中不需要做什么处理吧,如果在CTestView类中实现了OnLButtonDown并且你在此视图区域点击了,那么自然就应该被触发。
这样试一下
BOOL CTestView::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_LBUTTONDOWN)
{
AfxMessageBox("WM_LBUTTONDOWN");
}
return CView::PreTranslateMessage(pMsg);
}
另外,不要保存DC,去掉m_WindowDC = ::GetDC(m_hWnd); 这句,用到的时候获取DC,用完释放。
你的OnLButtonDown和OnLButtonUp里根据没有任何的代码,你怎么判断没有响应呢?
你加一个AfxMessageBox("OnLButtonDown");和AfxMessageBox("OnLButtonUp");测试一下,肯定会响应的。加了几个简单的测试,在消息函数中添加了一些测试代码,也没有进入到两个消息中去
你的void CTestView::OnLButtonDown(UINT nFlags, CPoint point) 中根本没有响应代码啊??当然没有反映了
查到一些问题了,我在childview中会显示地图,显示的地图只有一部分能够响应鼠标消息,继续调试中1.消息发出的指定窗口不对,即PostMessage或者SendMessage的Handle没有设对;
2.该消息已经被其他的拦截了——如果是这样的话,可以PreTranslateMessage一下。