MFC自定义控件编写过程(精)

更新时间:2023-12-14 01:00:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

???自定义控件编写过程

功能:点击客户区,新建一个灰色的小窗体。点击这个小窗体,弹出一个对话框,其信息为“你点击了自封装的控件”

????自定义类公有继承CWnd类

????在自定义类的头文件中添加DECLARE_DYNCREATE(MySelfWnd 在其源文件中添加IMPLEMENT_DYNCREATE(MySelfWnd, CWnd 这么做的原因请看候???《深入浅出???》? ????覆盖????中的虚函数Create??????? ????WM_LBUTTONDOWN添加其消息映射函数 [cpp] view plaincopy 1 // MySelfWnd1.h

2 class MySelfWnd : public CWnd 3 { 4 public: 5 MySelfWnd(;

6 DECLARE_DYNCREATE(MySelfWnd

7 virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL; 8 virtual ~MySelfWnd(; 9 protected:

10 afx_msg void OnLButtonDown(UINT nFlags, CPoint point; 11 DECLARE_MESSAGE_MAP( 12 };

13 //MySelfWnd1.cpp 14 #include \ 15 #include \ 16 #include \ 17 MySelfWnd::MySelfWnd( 18 { ??? ???}

???MySelfWnd::~MySelfWnd( ???{

???}

???IMPLEMENT_DYNCREATE(MySelfWnd, CWnd ???BEGIN_MESSAGE_MAP(MySelfWnd, CWnd ???//{{AFX_MSG_MAP(MySelfWnd ???ON_WM_LBUTTONDOWN( ???//}}AFX_MSG_MAP ???END_MESSAGE_MAP(

???void MySelfWnd::OnLButtonDown(UINT nFlags, CPoint point ???{

???// TODO: Add your message handler code here and/or call default ???MessageBox(\你点击了自封装的控件\; ???CWnd::OnLButtonDown(nFlags, point; ???}

???BOOL MySelfWnd::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 ???// 重新注册窗口类,

???lpszClassName=AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW , AfxGetApp(->LoadStandardCursor(IDC_ARROW,

???(HBRUSHGetStockObject(LTGRAY_BRUSH, NULL ;

???return CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext; ???}

???////view类

???void CMySelfWndView::OnLButtonDown(UINT nFlags, CPoint point ???{

???// TODO: Add your message handler code here and/or call default ???static CRect rect;

???rect.SetRect(10,10,100,100; ???myselfwnd=new MySelfWnd ;

???myselfwnd->Create(\,NULL,WS_CHILDWINDOW | WS_VISIBLE, rect,this,456; ???CView::OnLButtonDown(nFlags, point;

???}

本文来源:https://www.bwwdw.com/article/je45.html

Top