class CMemDC : public CDC
{
CSize m_size;
public:
~CMemDC()
{
DeleteDC();
}
CMemDC()
{
m_size.cx = m_size.cy=0;
}
CMemDC(UINT nBitmap,CDC* pDC=NULL)
{
LoadBitmap(nBitmap,pDC);
}
CMemDC(LPCSTR szFile,CDC* pDC=NULL)
{
LoadBitmap(szFile,pDC);
}
CMemDC(int cx,int cy,CDC* pDC=NULL)
{
Create(cx,cy,pDC);
}
BOOL DeleteDC()
{
if(!GetSafeHdc())
return FALSE;
CBitmap* pBitmap = GetCurrentBitmap();
if(pBitmap)
pBitmap ->DeleteObject();
return CDC::DeleteDC();
}
int GetWidth(){return m_size.cx;}
int GetHeight(){return m_size.cy;}
BOOL Create(int cx,int cy,CDC* pDC=NULL)
{//创建空白位图
CBitmap bmp;
if(!bmp.CreateCompatibleBitmap(pDC,cx,cy))
return FALSE;
m_size.cx = cx;
m_size.cy = cy;
CreateCompatibleDC(pDC);
SelectObject(&bmp);
return TRUE;
}
BOOL LoadBitmap(UINT nBitmap,CDC* pDC=NULL)
{//进程内位图资源加载
CBitmap bmp;
if(!bmp.LoadBitmap(nBitmap))
return FALSE;
BITMAP bm;
bmp.GetBitmap(&bm);
m_size.cx = bm.bmWidth;
m_size.cy = bm.bmHeight;
CreateCompatibleDC(pDC);
SelectObject(&bmp);
return TRUE;
}
BOOL LoadBitmap(LPCSTR szFile,CDC* pDC=NULL)
{//进程外位图图片文件加载
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL,szFile,IMAGE_BITMAP,
0,0,LR_LOADFROMFILE);
if(!hBitmap)
return FALSE;
BITMAP bm;
GetObject(hBitmap,sizeof(bm),&bm);
m_size.cx = bm.bmWidth;
m_size.cy = bm.bmHeight;
CreateCompatibleDC(pDC);
SelectObject(hBitmap);
return TRUE;
}
};
// MdcDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Mdc.h"
#include "MdcDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMdcDlg dialog
CMdcDlg::CMdcDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMdcDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMdcDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMdcDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMdcDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMdcDlg, CDialog)
//{{AFX_MSG_MAP(CMdcDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMdcDlg message handlers
BOOL CMdcDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMdcDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
//StretchBlt比BitBlt函数速度慢十倍到几十倍
CRect rect;
GetClientRect(rect);
dc.BitBlt(0,0,rect.Width(),rect.Height(),&m_mdc,0,0,SRCCOPY);
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMdcDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMdcDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
Invalidate(FALSE);
CDialog::OnLButtonDown(nFlags, point);
}
void CMdcDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
//创建空白位图,内部全部是纯黑色
if(!m_dc)
m_dc.LoadBitmap("./1.bmp");
CClientDC dc(this);
m_mdc.DeleteDC();
m_mdc.Create(cx,cy,&dc);
m_mdc.SetStretchBltMode(COLORONCOLOR);
m_mdc.StretchBlt(0,0,cx,cy,&m_dc,0,0,m_dc.GetWidth(),m_dc.GetHeight(),SRCCOPY);
Invalidate(FALSE);
}