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

来源:互联网 发布:java语法 编辑:程序博客网 时间:2024/05/19 20:46

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/

原创粉丝点击