如題。
我自定義了一個類,繼承自CWnd,想讓它回應MouseMove訊息,在Class Wizard里可以添加訊息回應函式,但是移動滑鼠,并不會進入函式。
到底是什么原因呢?
uj5u.com熱心網友回復:
1 你的類必須要有個視窗,2 Mouse Move 一定在你的視窗內
uj5u.com熱心網友回復:
多載一下訊息方法試試呢virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
uj5u.com熱心網友回復:
具體該怎么操作呢,我對這塊比較陌生,還麻煩您指點一下
uj5u.com熱心網友回復:
為什么呢
uj5u.com熱心網友回復:
一兩句話很難說清楚建議看侯杰的《深入淺出MFC》
uj5u.com熱心網友回復:
創建CWnd之后并沒有系結到HWND內核物件,需要手動呼叫CWnd::Create函式創建HWND已完成系結,或者通過CWnd::SubclassWindow子類化到一個已經存在的HWND視窗,其實你只要明白一點就好了,MFC里的視窗類CWnd就干一件事情,保存一個Win32視窗的句柄HWND到其成員變數m_hWnd,然后通過SetWindowLong去替換Win32視窗的視窗程序到其類靜態函式WindowProc以實作訊息攔截,攔截后的訊息就用其成員函式進行處理了,比如CWnd::OnMouseMove,就是攔截的WM_MOUSEMOVEuj5u.com熱心網友回復:
要完全掌握MFC機制,你需要看《深入淺出MFC》然后加上自己看MFC源代碼。uj5u.com熱心網友回復:
多載WindowProc 或者完善 訊息映射部分
//.h
DECLARE_MESSAGE_MAP()
//.cpp
BEGIN_MESSAGE_MAP( CMyWindow, CWnd )
//{{AFX_MSG_MAP( CMyWindow )
ON_WM_PAINT()
ON_COMMAND( IDM_ABOUT, OnAbout )
ON_WM_MOUSEMOVE( )
//}}AFX_MSG_MAP
END_MESSAGE_MAP( )
uj5u.com熱心網友回復:
你這個類需要跟Dialog啥的界面關聯上。然后再這個界面上移動滑鼠。
uj5u.com熱心網友回復:
MFC類,需要訊息映射,訊息才會進入類BEGIN_MESSAGE_MAP(CHelloWorldView, CView)
// 標準列印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CHelloWorldView::OnFilePrintPreview)
ON_WM_ERASEBKGND()
ON_COMMAND(ID_FLIP_IMAGE, &CHelloWorldView::OnFlipImage)
.........................
END_MESSAGE_MAP()
uj5u.com熱心網友回復:
嗯,以前囫圇吞棗的看過。
uj5u.com熱心網友回復:
我用類向導生成訊息回應函式,上面這些message map里的內容都有
uj5u.com熱心網友回復:
我的工程是單檔案,不是Dialog based,不知道該怎么關聯
uj5u.com熱心網友回復:
這個自定義類是怎么創建啟動的? Create ? SubclassWindow?uj5u.com熱心網友回復:
這個類本身使用Class wizard的創建的。
我好像并沒有創建這個類的物件。在View類里定義這個類的物件行嗎
uj5u.com熱心網友回復:
你得給他一個可以展現的機會
uj5u.com熱心網友回復:
如樓上所說,需要訊息映射。uj5u.com熱心網友回復:
這個自定義類是怎么創建啟動的? Create ? SubclassWindow?
這個類本身使用Class wizard的創建的。
我好像并沒有創建這個類的物件。在View類里定義這個類的物件行嗎
你得給他一個可以展現的機會
我當然知道要“給他一個可以展現的機會”,就是不知道該怎么給啊
uj5u.com熱心網友回復:
如樓上所說,需要訊息映射。
不是訊息映射的問題
uj5u.com熱心網友回復:
#if !defined(AFX_MYWND_H__B293370D_6107_4EE1_88EB_E8D48C70CB76__INCLUDED_)
#define AFX_MYWND_H__B293370D_6107_4EE1_88EB_E8D48C70CB76__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyWnd.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CMyWnd window
class CMyWnd : public CWnd
{
DECLARE_DYNAMIC(CMyWnd)
// Construction
public:
CMyWnd();
// Attributes
public:
CPoint m_MousePos;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyWnd)
public:
virtual BOOL Create(LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
protected:
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CMyWnd();
// Generated message map functions
protected:
//{{AFX_MSG(CMyWnd)
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MYWND_H__B293370D_6107_4EE1_88EB_E8D48C70CB76__INCLUDED_)
// MyWnd.cpp : implementation file
//
#include "stdafx.h"
#include "MyWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMyWnd
IMPLEMENT_DYNAMIC(CMyWnd, CWnd)
CMyWnd::CMyWnd()
{
m_MousePos.x = m_MousePos.y = 0;
}
CMyWnd::~CMyWnd()
{
}
//訊息映射模式
BEGIN_MESSAGE_MAP(CMyWnd, CWnd)
//{{AFX_MSG_MAP(CMyWnd)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyWnd message handlers
BOOL CMyWnd::Create(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
LPCTSTR lpszClassName = AfxRegisterWndClass(CS_OWNDC|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS);
return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
}
void CMyWnd::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_MousePos = point;
Invalidate(FALSE);
CWnd::OnMouseMove(nFlags, point);
}
//多載WindowProc
LRESULT CMyWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch(message)
{
case(WM_ERASEBKGND):
{
return TRUE;
}
case(WM_PAINT):
{
PAINTSTRUCT ps;
CDC *pDC = BeginPaint(&ps);
int nSaveDc = pDC->SaveDC();
CRect rcRect;
GetClientRect(&rcRect);
pDC->FillSolidRect(&rcRect, RGB(255, 0, 0));
CString szStr;
szStr.Format(_T("x=%d y=%d\n"), m_MousePos.x, m_MousePos.y);
pDC->SetTextColor(RGB(0, 255, 0));
pDC->SetBkMode(TRANSPARENT);
CFont m_Font;
m_Font.CreatePointFont(120, _T("Arial"));
CFont *pOldFont = pDC->SelectObject(&m_Font);
pDC->DrawText(szStr, rcRect, DT_VCENTER|DT_CENTER|DT_SINGLELINE);
pDC->SelectObject(pOldFont);
pDC->RestoreDC(nSaveDc);
EndPaint(&ps);
return 0;
}
}
return CWnd::WindowProc(message, wParam, lParam);
}
//測驗類
#include "MyWnd.h"
class CSDI3View : public CView
{
protected:
CMyWnd m_MyWnd;
public:
virtual void OnInitialUpdate();
virtual BOOL DestroyWindow();
……
void CSDI3View::OnInitialUpdate()
{
CView::OnInitialUpdate();
m_MyWnd.Create(_T("Test my Window"), WS_CHILD|WS_VISIBLE|WS_BORDER,
CRect(0,0, 200, 200), this, 1024);
}
BOOL CSDI3View::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
if(m_MyWnd.m_hWnd)
m_MyWnd.DestroyWindow();
return CView::DestroyWindow();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/95376.html
標籤:界面
