原创|其它|编辑:郝浩|2008-01-21 09:51:48.000|阅读 13201 次
概述:VC++ MFC 控件上显示BMP图片
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
共7步
0. 装载CTRL+R导入一张位图
1. 先要建一个类:
CBmpControl 给予 Static
2. 添加三个BMP相关的
class CBmpControl : public CStatic
{
// Construction
public:
BOOL LoadBitmapToControl(UINT uID);
CBitmap m_Bmp; //用这个画
int m_BmpID;
3. 实现m_Bmp的画图
BOOL CBmpControl::LoadBitmapToControl(UINT uID)
{
int i = m_Bmp.LoadBitmap(uID);
m_BmpID = uID;
return TRUE;
}
void CBmpControl::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
m_Bmp.Detach();
m_Bmp.LoadBitmap(m_BmpID);
BITMAP bmInfo;
::GetObject( m_Bmp.m_hObject, sizeof(BITMAP), &bmInfo );
INT nWidth, nHeigh;
nWidth = bmInfo.bmWidth;
nHeigh = bmInfo.bmHeight;
CDC pDC;
pDC.CreateCompatibleDC(&dc);
pDC.SelectObject(&m_Bmp);
dc.BitBlt(0, 0, nWidth, nHeigh, &pDC, 0, 0, SRCCOPY);
// Do not call CStatic::OnPaint() for painting messages
}
4. 在工程中调用
// CBmpshowDlg dialog
#include "BmpControl.h"
class CBmpshowDlg : public CDialog
{
// Construction
public:
CBmpshowDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CBmpshowDlg)
enum { IDD = IDD_BMPSHOW_DIALOG};
CBmpControl m_Bmp;//------------------------------
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
5. 影射
void CBmpshowDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBmpshowDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
DDX_Control(pDX, IDC_BUTTON1, m_Bmp); // 画到IDC_BUTTON1上
//}}AFX_DATA_MAP
}
6. 在需要显示的地方
m_Bmp.LoadBitmapToControl(IDB_BITMAP1);
//--------------------下为源码
//------------------------------------------------------------------------------
//-- CBmpControl.h
//------------------------------------------------------------------------------
#if !defined(AFX_BMPCONTROL_H__C119A2C1_14FD_4538_87FD_313D61D2952F__INCLUDED_)
#define AFX_BMPCONTROL_H__C119A2C1_14FD_4538_87FD_313D61D2952F__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// BmpControl.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CBmpControl window
class CBmpControl : public CStatic
{
// Construction
public:
CBmpControl();
BOOL LoadBitmapToControl(UINT uID);
CBitmap m_Bmp;
int m_BmpID;
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CBmpControl)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CBmpControl();
// Generated message map functions
protected:
//{{AFX_MSG(CBmpControl)
afx_msg void OnPaint();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INS--ERT_LOCATION}}
// Microsoft Visual C++ will ins--ert additional declarations immediately before the previous line.
#endif // !defined(AFX_BMPCONTROL_H__C119A2C1_14FD_4538_87FD_313D61D2952F__INCLUDED_)
//------------------------------------------------------------------------------
//-- BmpControl.cpp : implementation file
//------------------------------------------------------------------------------
// BmpControl.cpp : implementation file
//
#include "stdafx.h"
#include "bmpshow.h"
#include "BmpControl.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CBmpControl
CBmpControl::CBmpControl()
{
}
CBmpControl::~CBmpControl()
{
}
BEGIN_MESSAGE_MAP(CBmpControl, CStatic)
//{{AFX_MSG_MAP(CBmpControl)
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBmpControl message handlers
BOOL CBmpControl::LoadBitmapToControl(UINT uID)
{
int i = m_Bmp.LoadBitmap(uID);
m_BmpID = uID;
return TRUE;
}
void CBmpControl::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
m_Bmp.Detach();
m_Bmp.LoadBitmap(m_BmpID);
BITMAP bmInfo;
::GetObject( m_Bmp.m_hObject, sizeof(BITMAP), &bmInfo );
INT nWidth, nHeigh;
nWidth = bmInfo.bmWidth;
nHeigh = bmInfo.bmHeight;
CDC pDC;
pDC.CreateCompatibleDC(&dc);
pDC.SelectObject(&m_Bmp);
dc.BitBlt(0, 0, nWidth, nHeigh, &pDC, 0, 0, SRCCOPY);
// Do not call CStatic::OnPaint() for painting messages
}
//------------------------------------------------------------------------------
//-- 调用实例
//-- bmpshowDlg.h : header file
//------------------------------------------------------------------------------
// bmpshowDlg.h : header file
//
#if !defined(AFX_BMPSHOWDLG_H__075EE498_1F9A_47BD_B4F4_A4619DE3DC57__INCLUDED_)
#define AFX_BMPSHOWDLG_H__075EE498_1F9A_47BD_B4F4_A4619DE3DC57__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CBmpshowDlg dialog
#include "BmpControl.h"
class CBmpshowDlg : public CDialog
{
// Construction
public:
CBmpshowDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CBmpshowDlg)
enum { IDD = IDD_BMPSHOW_DIALOG};
CBmpControl m_Bmp;
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CBmpshowDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CBmpshowDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INS--ERT_LOCATION}}
// Microsoft Visual C++ will ins--ert additional declarations immediately before the previous line.
#endif // !defined(AFX_BMPSHOWDLG_H__075EE498_1F9A_47BD_B4F4_A4619DE3DC57__INCLUDED_)
//------------------------------------------------------------------------------
//-- 调用实例
//-- bmpshowDlg.c : header file
//------------------------------------------------------------------------------
// bmpshowDlg.cpp : implementation file
//
#include "stdafx.h"
#include "bmpshow.h"
#include "bmpshowDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBmpshowDlg dialog
CBmpshowDlg::CBmpshowDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBmpshowDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBmpshowDlg)
// 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 CBmpshowDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBmpshowDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
DDX_Control(pDX, IDC_BUTTON1, m_Bmp);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBmpshowDlg, CDialog)
//{{AFX_MSG_MAP(CBmpshowDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBmpshowDlg message handlers
BOOL CBmpshowDlg::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
m_Bmp.LoadBitmapToControl(IDB_BITMAP1);
return TRUE; // return TRUE unless you set the focus to a control
}
void CBmpshowDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CBmpshowDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
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;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CBmpshowDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn
文章转载自:电子工程师在线网