标签:
新建 Dialog based MFC项目DrawImage
在资源中新添加一个Dialog,IDD_PICBOXDLG
在此dialog上添加Pictre Contrl
shift+ctrl+x 添加对应的类:CPicBoxDlg
DrawImage.cpp中加入:
CPicBoxDlg* g_Dlg=NULL;
DrawImageDlg.cpp中:
extern CPicBoxDlg* g_Dlg;
在资源面板找到DrawImage窗口添加按钮,双击添加事件
void CDrawImgDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
if(g_Dlg == NULL)
{
g_Dlg = new CPicBoxDlg;
if(g_Dlg->Create(IDD_PICBOXDLG, this))
{
g_Dlg->ShowPicBox(g_Dlg->GetSafeHwnd());
g_Dlg->ShowWindow(SW_SHOWNORMAL);
::AnimateWindow(g_Dlg->GetSafeHwnd(), 300, AW_BLEND);
::SetForegroundWindow(g_Dlg->GetSafeHwnd());
}
//CPicBoxDlg dlg;
//dlg.DoModal();
}
}
项目添加BmpFram.h
#pragma once
#include <xstring>
#include <GdiPlus.h>
using namespace std;
using namespace Gdiplus;
// CBmpFrame
class CBmpFrame : public CStatic {
DECLARE_DYNAMIC(CBmpFrame)
public:
CBmpFrame(CWnd* m_pWnd, wstring wsImgPath, BOOL fIsTransparentBg = FALSE, COLORREF rgbColor = NULL);
CBmpFrame(CWnd* m_pWnd, UINT uiBmpID, BOOL fIsTransparentBg = TRUE, COLORREF rgbColor = #ffffff);
virtual ~CBmpFrame();
private:
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_pGdiToken;
CBitmap m_bmpSource;
BITMAP m_bmSource;
CWnd* m_pWnd;
UINT m_uiBmpID;
wstring m_wsImgPath;
BOOL m_fIsTransparentBg;
COLORREF m_rgbColor;
BOOL m_fIsGdiplus;
public:
void Draw(CDC *pDC);
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnPaint();
};
BmpFrame.cpp
// BmpBox.cpp : implementation file
//
#include "stdafx.h"
#include "BmpFrame.h"
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#include <GdiPlus.h>
using namespace Gdiplus;
#endif
#pragma comment(lib,"gdiplus.lib")
// CBmpFrame
IMPLEMENT_DYNAMIC(CBmpFrame, CStatic)
CBmpFrame::CBmpFrame(CWnd* pWnd, wstring wsImgPath, BOOL fIsTransparentBg, COLORREF rgbColor)
{
GdiplusStartup(&m_pGdiToken,&m_gdiplusStartupInput,NULL);
m_pWnd = pWnd;
m_wsImgPath = wsImgPath;
m_fIsTransparentBg = fIsTransparentBg;
m_fIsGdiplus = TRUE;
}
CBmpFrame::CBmpFrame(CWnd* pWnd, UINT uiBmpID, BOOL fIsTransparentBg, COLORREF rgbColor)
{
GdiplusStartup(&m_pGdiToken,&m_gdiplusStartupInput,NULL);
m_pWnd = pWnd;
m_uiBmpID = uiBmpID;
m_fIsTransparentBg = fIsTransparentBg;
m_rgbColor = rgbColor;
m_fIsGdiplus = FALSE;
}
CBmpFrame::~CBmpFrame()
{
GdiplusShutdown(m_pGdiToken);
}
BEGIN_MESSAGE_MAP(CBmpFrame, CStatic)
ON_WM_PAINT()
END_MESSAGE_MAP()
void CBmpFrame::Draw(CDC *pDC)
{
CRect rectWnd;
GetClientRect(&rectWnd);
CDC* pCDC = new CDC;
//CPaintDC paintDC(m_pWnd);
pCDC->CreateCompatibleDC(pDC);
if(m_fIsGdiplus)
{
//使用gdi+绘图,并做平滑处理
Graphics graphics(pDC->GetSafeHdc());
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
graphics.SetInterpolationMode(InterpolationModeHighQuality);
//InterpolationModeHighQualityBilinear
//InterpolationModeHighQualityBicubic
Gdiplus::Image* pImage = new Gdiplus::Image(m_wsImgPath.data(), FALSE);
if(pImage == NULL)
{
MessageBox(L"Gdiplus::Image is NULL.");
return;
}
if(m_fIsTransparentBg)
{
ImageAttributes imAtt;
imAtt.SetColorKey(m_rgbColor, m_rgbColor, ColorAdjustTypeDefault);
graphics.DrawImage(pImage, Rect(0, 0, rectWnd.Width(), rectWnd.Height()), 0, 0, pImage->GetWidth(), pImage->GetHeight(), UnitPixel, &imAtt);
}
else
{
graphics.DrawImage(pImage, Rect(0, 0, rectWnd.Width(), rectWnd.Height()), 0, 0, pImage->GetWidth(), pImage->GetHeight(), UnitPixel, NULL);
}
delete pImage;
pImage = NULL;
}
else
{
//一般绘图
m_bmpSource.DeleteObject();
m_bmpSource.LoadBitmap(m_uiBmpID);
m_bmpSource.GetObject(sizeof(BITMAP), &m_bmSource);
if(m_fIsTransparentBg)
{
pDC->TransparentBlt(0,0,rectWnd.Width(), rectWnd.Height(), pCDC,0,0, m_bmSource.bmWidth, m_bmSource.bmWidth, m_rgbColor);
}
else
{
pDC->StretchBlt(0,0,rectWnd.Width(), rectWnd.Height(), pCDC,0,0, m_bmSource.bmWidth, m_bmSource.bmWidth, SRCCOPY);
}
}
pCDC->DeleteDC();
}
void CBmpFrame::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CStatic::OnPaint() for painting messages
Draw(&dc);
}
PicBoxDlg.h
#pragma once
#include "BmpFrame.h"
// CPicBoxDlg dialog
class CPicBoxDlg : public CDialog
{
DECLARE_DYNAMIC(CPicBoxDlg)
CBmpFrame m_bmpFrame;
public:
CPicBoxDlg(CWnd* pParent = NULL); // standard constructor
virtual ~CPicBoxDlg();
// Dialog Data
enum { IDD = IDD_PICBOXDLG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
DECLARE_MESSAGE_MAP()
};
PicBoxDlg.cpp
// CPicBoxDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DrawImg.h"
#include "PicBoxDlg.h"
// CPicBoxDlg dialog
IMPLEMENT_DYNAMIC(CPicBoxDlg, CDialog)
CPicBoxDlg::CPicBoxDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPicBoxDlg::IDD, pParent)
, m_bmpFrame(NULL, L"./res/cross.png")
{
}
CPicBoxDlg::~CPicBoxDlg()
{
}
void CPicBoxDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_PICBOX, m_bmpFrame);
}
BEGIN_MESSAGE_MAP(CPicBoxDlg, CDialog)
END_MESSAGE_MAP()
MFC非模态窗口gdi+自绘图片
标签:
原文地址:http://www.cnblogs.com/freemindblog/p/5783261.html