CRotateBmpDlg::CRotateBmpDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_ROTATEBMP_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
strFilePath = "";
m_hBitmap = NULL;
}
CRotateBmpDlg::~CRotateBmpDlg()
{
/*if (lpSrcBits)
{
delete lpSrcBits;
}*/
if (m_bitmap.m_hObject)
{
m_bitmap.Detach();
}
DeleteObject(&m_hBitmap);
DeleteObject(&m_bitmap);
}
void CRotateBmpDlg::OnBnClickedButton1()
{
CFileDialog dlgFileOpen(TRUE, NULL, _T("*.bmp"), OFN_HIDEREADONLY,
_T("打印文件(*.bmp)|*.bmp|所有文件(*.*)|*.*|"), this);
if (dlgFileOpen.DoModal() == IDCANCEL)
{
return;
}
strFilePath = dlgFileOpen.GetPathName();
CRect rect;
this->GetClientRect(rect);
m_hBitmap = (HBITMAP)LoadImage(NULL, strFilePath, IMAGE_BITMAP, rect.Width(), rect.Height() - 60, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
Invalidate();//使窗口无效,产生WM_PAINT消息,WM_PAINT消息在消息队列的优先级低
//想让窗口立即重绘,可以sendmessage(WM_PAINT)或者
/*InvalidateRect(NULL, TRUE);*/
/*UpdateWindow();*/
}
void CRotateBmpDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
if (strFilePath.GetLength() > 4 && m_hBitmap)
{
if (m_bitmap.m_hObject)
{
m_bitmap.Detach();
}
m_bitmap.Attach(m_hBitmap);
CPaintDC dc(this);//构造会调用BeginPaint()和析构EndPaint(),并从消息队列中清除WM_PAINT消息。
CRect rect;
GetClientRect(rect);
CDC memDC;
memDC.CreateCompatibleDC(&dc);
BITMAP bmp;
m_bitmap.GetBitmap(&bmp);
CBitmap *pBmpOld = memDC.SelectObject(&m_bitmap);
/* int panelsize = 0;
if (bitmapinfo.bmBitsPixel < 16)
{
panelsize = pow(2, bitmapinfo.bmBitsPixel) * sizeof(RGBQUAD);
}*/
dc.StretchBlt(0, 0, bmp.bmWidth, bmp.bmHeight, &memDC, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
memDC.SelectObject(pBmpOld);
DeleteDC(memDC);
}
else
{
CDialogEx::OnPaint();//进去OnPaint()会调用BeginPaint()和EndPaint(),并从消息队列中清除WM_PAINT消息。
}
}
}
OnPaint()进去了后一定要清除队列的WM_PAINT这样才不会重复响应,所以每个分支必须调用且只能调用一次CPaintDC()或者CDialogEx::OnPaint();