在对话框中应用CScrollView显示图像

1、用vs2008创建一个基于对话框的工程DialogView;
2、添加一个新类CMyDocument,基类为CDocument;
3、添加一个新类CMyView,基类为CScrollView;
4、修改CMyDocument的头文件:
#pragma once

// CMyDocument document
class CDialogView;

class CMyDocument : public CDocument
{
//DECLARE_DYNCREATE(CMyDocument)
friend class CDialogView;

public:
CMyDocument();
DECLARE_DYNCREATE(CMyDocument)
public:
virtual ~CMyDocument();
#ifndef _WIN32_WCE
virtual void Serialize(CArchive& ar); // overridden for document i/o
#endif
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif

protected:
virtual BOOL OnNewDocument();

DECLARE_MESSAGE_MAP()
};

5、修改CMyView的头文件:
#pragma once

// CMyView view
class CDialogView;

class CMyView : public CScrollView
{
//DECLARE_DYNCREATE(CMyView)
friend class CDialogView;
protected:
CMyView(); // protected constructor used by dynamic creation
DECLARE_DYNCREATE(CMyView)
virtual ~CMyView();

public:
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif

protected:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual void OnInitialUpdate(); // first time after construct

DECLARE_MESSAGE_MAP()
};
6、修改DialogView执行文件:
在文件中加入 #include "MyScroll.h"
#include "MyDocument.h"

BOOL CDialogView::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);// Set big icon
SetIcon(m_hIcon, FALSE);// Set small icon

// TODO: Add extra initialization here
CCreateContext pContext;
CWnd* pFrameWnd = this;
pContext.m_pCurrentDoc = new CMyDocument;
pContext.m_pNewViewClass = RUNTIME_CLASS(CMyView);
CMyView* pView = (CMyView *)((CFrameWnd*)pFrameWnd)->CreateView(&pContext);
ASSERT(pView);
pView->m_nMapMode = MM_TEXT;
pView->ShowWindow(SW_NORMAL);
CRect rectWindow;
GetWindowRect(rectWindow);
rectWindow.right -= 30;
rectWindow.bottom -= 100;
pView->MoveWindow(rectWindow);

return TRUE; // return TRUE unless you set the focus to a control
}

7、在CMyView的OnDraw函数中添加代码:
void CMyView::OnDraw(CDC* pDC)
{
//CDocument* pDoc = GetDocument();
// TODO: add draw code here
CBitmap BK;
BK.LoadBitmap(IDB_BITMAP1);//需要添加一位图用于显示
CDC MemDC;
MemDC.CreateCompatibleDC(pDC);
MemDC.SelectObject(&BK);
BITMAP bm;
BK.GetBitmap(&bm);

pDC->BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &MemDC, 0, 0, SRCCOPY);

CRect m_Rect;
GetClientRect(&m_Rect);
m_Rect.bottom += 100;
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = bm.bmWidth;
sizeTotal.cy = bm.bmHeight;
SetScrollSizes(MM_TEXT, sizeTotal);
}

参考:http://download.csdn.net/down/610747/jia_xiaoxin
http://www.codeguru.com/Cpp/W-D/dislog/article.php/c5009/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值