⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dibsectiontestview.cpp

📁 DIBSection在CE下使用的例子源代码
💻 CPP
字号:
// DIBSectionTestView.cpp : implementation of the CDIBSectionTestView class
//

#include "stdafx.h"
#include "DIBSectionTest.h"

#include "DIBSectionTestDoc.h"
#include "DIBSectionTestView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDIBSectionTestView

IMPLEMENT_DYNCREATE(CDIBSectionTestView, CView)

BEGIN_MESSAGE_MAP(CDIBSectionTestView, CView)
	//{{AFX_MSG_MAP(CDIBSectionTestView)
	ON_COMMAND(ID_VIEW_STRETCH, OnViewStretch)
	ON_UPDATE_COMMAND_UI(ID_VIEW_STRETCH, OnUpdateViewStretch)
	ON_WM_ERASEBKGND()
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND_RANGE(ID_OPTIONS_MONOCHROMEBITMAP, ID_OPTIONS_TRUECOLORBITMAP, OnOptions)
	ON_UPDATE_COMMAND_UI_RANGE(ID_OPTIONS_MONOCHROMEBITMAP, ID_OPTIONS_TRUECOLORBITMAP, OnUpdateOptions)
	ON_COMMAND(ID_VIEW_PROPS, OnViewProperties)
	ON_UPDATE_COMMAND_UI(ID_VIEW_PROPS, OnUpdateViewProperties)
	//}}AFX_MSG_MAP
	// Standard printing commands
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDIBSectionTestView construction/destruction

CDIBSectionTestView::CDIBSectionTestView()
{
    m_bStretch = FALSE;
    m_nBitmapID = -1;
}

CDIBSectionTestView::~CDIBSectionTestView()
{
}

BOOL CDIBSectionTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDIBSectionTestView drawing

void CDIBSectionTestView::OnDraw(CDC* pDC)
{
	CDIBSectionTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

    if (pDoc->m_DIBSection.GetSafeHandle())
    {
        if (m_bStretch)
        {
            CRect rect;
            GetClientRect(rect);
            pDoc->m_DIBSection.Stretch(pDC, CPoint(0,0), rect.Size());
        }
        else
            pDoc->m_DIBSection.Draw(pDC, CPoint(0,0));
    }
}

/////////////////////////////////////////////////////////////////////////////
// CDIBSectionTestView diagnostics

#ifdef _DEBUG
void CDIBSectionTestView::AssertValid() const
{
	CView::AssertValid();
}

void CDIBSectionTestView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CDIBSectionTestDoc* CDIBSectionTestView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDIBSectionTestDoc)));
	return (CDIBSectionTestDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDIBSectionTestView message handlers

void CDIBSectionTestView::OnViewStretch() 
{
    m_bStretch = !m_bStretch;
    Invalidate();
}

void CDIBSectionTestView::OnUpdateViewStretch(CCmdUI* pCmdUI) 
{
    pCmdUI->SetCheck(m_bStretch);
}

BOOL CDIBSectionTestView::OnEraseBkgnd(CDC* pDC) 
{
	CDIBSectionTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

    if (!pDoc->m_DIBSection.GetSafeHandle())
	    return CView::OnEraseBkgnd(pDC);

    if (m_bStretch)
        return TRUE;

    CRect ClientRect, ImageRect;
    GetClientRect(ClientRect);
    ImageRect.SetRect(0,0, pDoc->m_DIBSection.GetWidth(), pDoc->m_DIBSection.GetHeight());

#ifdef _WIN32_WCE
    HBRUSH hBrush = ::CreateSolidBrush(RGB(255,255,255));
#else
    HBRUSH hBrush =  (HBRUSH) GetClassLong(m_hWnd, GCL_HBRBACKGROUND);
#endif

    ::FillRect(pDC->GetSafeHdc(), 
               CRect(ImageRect.right,0, ClientRect.right, ImageRect.bottom), hBrush);
    ::FillRect(pDC->GetSafeHdc(), 
               CRect(0, ImageRect.bottom, ClientRect.right, ClientRect.bottom), hBrush);

#ifdef _WIN32_WCE
    ::DeleteObject(hBrush);
#endif

    return TRUE;
}

void CDIBSectionTestView::OnFileOpen() 
{
	CDIBSectionTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	CString strFilter = "Bitmap Files (*.bmp)|*.bmp|All Files (*.*) |*.*||";

	CFileDialog	dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY|OFN_EXPLORER, strFilter, NULL); 
	if (dlg.DoModal() == IDOK) 
    {
        if (pDoc->m_DIBSection.Load(dlg.GetPathName()))
            pDoc->SetTitle(dlg.GetFileTitle());
        else
            AfxMessageBox(_T("Unable to open bitmap file"));

        m_nBitmapID = -1;
        Invalidate();
    }
}

void CDIBSectionTestView::OnOptions(UINT nID) 
{
    switch (nID)
    {
        case ID_OPTIONS_MONOCHROMEBITMAP: m_nBitmapID = IDB_GRADIENT2;     break;
        case ID_OPTIONS_4COLORBITMAP:     m_nBitmapID = IDB_GRADIENT4;     break;
        case ID_OPTIONS_16COLORBITMAP:    m_nBitmapID = IDB_GRADIENT16;    break;
        case ID_OPTIONS_256COLORBITMAP:   m_nBitmapID = IDB_GRADIENT256;   break;
        case ID_OPTIONS_16BITBITMAP:      m_nBitmapID = IDB_GRADIENT65K;   break;
        case ID_OPTIONS_TRUECOLORBITMAP:  m_nBitmapID = IDB_GRADIENT_TRUE; break;
    }

	CDIBSectionTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

    if (!pDoc->m_DIBSection.SetBitmap(m_nBitmapID))
        AfxMessageBox(_T("Unable to load bitmap file"));

    Invalidate();
}

void CDIBSectionTestView::OnUpdateOptions(CCmdUI* pCmdUI) 
{
    switch (pCmdUI->m_nID)
    {
        case ID_OPTIONS_MONOCHROMEBITMAP: 
            pCmdUI->SetCheck(m_nBitmapID == IDB_GRADIENT2);      
            break;

        case ID_OPTIONS_4COLORBITMAP:
#if defined(_WIN32_WCE) && !defined(_WIN32_WCE_EMULATION)
            pCmdUI->SetCheck(m_nBitmapID == IDB_GRADIENT4);      
#else
            pCmdUI->SetCheck(FALSE);
            pCmdUI->Enable(FALSE);
#endif
            break;

        case ID_OPTIONS_16COLORBITMAP:
            pCmdUI->SetCheck(m_nBitmapID == IDB_GRADIENT16);      
            break;

        case ID_OPTIONS_256COLORBITMAP:
            pCmdUI->SetCheck(m_nBitmapID == IDB_GRADIENT256);      
            break;

        case ID_OPTIONS_16BITBITMAP:
            pCmdUI->SetCheck(m_nBitmapID == IDB_GRADIENT65K);      
            break;

        case ID_OPTIONS_TRUECOLORBITMAP:
            pCmdUI->SetCheck(m_nBitmapID == IDB_GRADIENT_TRUE);      
            break;
    }
}

void CDIBSectionTestView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();

    OnOptions(ID_OPTIONS_TRUECOLORBITMAP);
}

void CDIBSectionTestView::OnViewProperties() 
{
	CDIBSectionTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

    CString str;
    str.Format(_T("Display Image Properties:\n")
               _T("Dim:\t%d x %d\n")
               _T("Depth:\t%d bits\n")
               _T("Size:\t%d Kb"),
               pDoc->m_DIBSection.GetWidth(), pDoc->m_DIBSection.GetHeight(),
               pDoc->m_DIBSection.GetBitCount(), pDoc->m_DIBSection.GetImageSize() / 1024);

    MessageBox(str, _T("Properties"), MB_ICONINFORMATION);
}

void CDIBSectionTestView::OnUpdateViewProperties(CCmdUI* pCmdUI) 
{
	CDIBSectionTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

    pCmdUI->Enable(pDoc->m_DIBSection.GetSafeHandle() != NULL);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -