| 发表于:2007-05-24 22:56:307楼 得分:40 |
写得比较粗糙,那个pview也没有释放,仅供参考: // testmfc2dlg.h : 头文件 // #pragma once #include "afxwin.h " class cmyscrollview : public cscrollview { public: void ondraw(cdc *pdc) { pdc-> textout(0, 0, _t( "hello world ")); } declare_dyncreate(cmyscrollview) lpctstr geticonwndclass(dword dwdefaultstyle, uint nidresource); void oninitialupdate(); }; // ctestmfc2dlg 对话框 class ctestmfc2dlg : public cdialog { // 构造 public: ctestmfc2dlg(cwnd* pparent = null);// 标准构造函数 // 对话框数据 enum { idd = idd_testmfc2_dialog }; protected: virtual void dodataexchange(cdataexchange* pdx);// ddx/ddv 支持 // 实现 protected: hicon m_hicon; // 生成的消息映射函数 virtual bool oninitdialog(); afx_msg void onpaint(); afx_msg hcursor onquerydragicon(); declare_message_map() }; // testmfc2dlg.cpp : 实现文件 // #include "stdafx.h " #include "testmfc2.h " #include "testmfc2dlg.h " #ifdef _debug #define new debug_new #endif // ctestmfc2dlg 对话框 ctestmfc2dlg::ctestmfc2dlg(cwnd* pparent /*=null*/) : cdialog(ctestmfc2dlg::idd, pparent) { m_hicon = afxgetapp()-> loadicon(idr_mainframe); } void ctestmfc2dlg::dodataexchange(cdataexchange* pdx) { cdialog::dodataexchange(pdx); } begin_message_map(ctestmfc2dlg, cdialog) on_wm_paint() on_wm_querydragicon() //}}afx_msg_map end_message_map() implement_dyncreate(cmyscrollview, cscrollview) // ctestmfc2dlg 消息处理程序 lpctstr cmyscrollview::geticonwndclass(dword dwdefaultstyle, uint nidresource) { hinstance hinst = afxfindresourcehandle( makeintresource(nidresource), rt_group_icon); hicon hicon = ::loadicon(hinst, makeintresource(nidresource)); if (hicon != null) { createstruct cs; memset(&cs, 0, sizeof(createstruct)); cs.style = dwdefaultstyle; precreatewindow(cs); // will fill lpszclassname with default wndclass name // ignore instance handle from precreatewindow. wndclass wndcls; if (cs.lpszclass != null && getclassinfo(afxgetinstancehandle(), cs.lpszclass, &wndcls) && wndcls.hicon != hicon) { // register a very similar wndclass return afxregisterwndclass(wndcls.style, wndcls.hcursor, wndcls.hbrbackground, hicon); } } return null; // just use the default } void cmyscrollview::oninitialupdate() { setscrollsizes(mm_text, csize(1000, 800)); cscrollview::oninitialupdate(); } bool ctestmfc2dlg::oninitdialog() { cdialog::oninitdialog(); // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动 // 执行此操作 seticon(m_hicon, true);// 设置大图标 seticon(m_hicon, false);// 设置小图标 // todo: 在此添加额外的初始化代码 cmyscrollview *pview = (cmyscrollview *)runtime_class(cmyscrollview)-> createobject(); pview-> create( pview-> geticonwndclass(ws_visible ¦ ws_child, idr_mainframe), _t( "test "), ws_visible ¦ ws_child, crect(0, 0, 300,200), this, 0); pview-> sendmessage(0x0364/*wm_initialupdate*/, 0, 0); return true; // 除非设置了控件的焦点,否则返回 true } // 如果向对话框添加最小化按钮,则需要下面的代码 // 来绘制该图标。对于使用文档/视图模型的 mfc 应用程序, // 这将由框架自动完成。 void ctestmfc2dlg::onpaint() { if (isiconic()) { cpaintdc dc(this); // 用于绘制的设备上下文 sendmessage(wm_iconerasebkgnd, reinterpret_cast <wparam> (dc.getsafehdc()), 0); // 使图标在工作矩形中居中 int cxicon = getsystemmetrics(sm_cxicon); int cyicon = getsystemmetrics(sm_cyicon); crect rect; getclientrect(&rect); int x = (rect.width() - cxicon + 1) / 2; int y = (rect.height() - cyicon + 1) / 2; // 绘制图标 dc.drawicon(x, y, m_hicon); } else { cdialog::onpaint(); } } //当用户拖动最小化窗口时系统调用此函数取得光标显示。 hcursor ctestmfc2dlg::onquerydragicon() { return static_cast <hcursor> (m_hicon); } | | |
|