15,979
社区成员
发帖
与我相关
我的任务
分享
#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();
}