在对话框中应用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/
總結
以上是生活随笔為你收集整理的在对话框中应用CScrollView显示图像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像处理和图像识别中常用的CxImage
- 下一篇: vs2008中,在OCX控件中应用doc