📄 bitmap1.cpp
字号:
///////////////////////////////////////////////////////////////////
// Module : BITMAP1.CPP
//
// Purpose : Shows how to use bitmap resources in an MFC program.
//
// Author : Rob McGregor, rob_mcgregor@compuserve.com
///////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h" // Resource header - contains resource IDs
///////////////////////////////////////////////////////////////////
// Class CMainWnd - derived from MFC's CFrameWnd
class CMainWnd : public CFrameWnd
{
protected:
CBitmap m_bmpBack; // resource IDR_BMPBACKGROUND
CBitmap m_bmpBtn1; // resource IDR_BMPBTN1
CBitmap m_bmpBtn1Lit; // resource IDR_BMPBTN1LIT
CBitmap m_bmpBtn2; // resource IDR_BMPBTN2
CBitmap m_bmpBtn2Lit; // resource IDR_BMPBTN2LIT
CRect m_rcTop; // rect for blitting
CRect m_rcBtm; // rect for blitting
CRect m_rcClient; // rect for blitting
void BlitBitmap(CRect& rc, CBitmap& bm);
void ShowBitmaps();
public:
// Helper method called by MFC
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
void PositionRects();
// Message handler
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP();
};
///////////////////////////////////////////////////////////////////
// Class CBitmapApp - Application class derived from CWinApp
class CBitmapApp : public CWinApp
{
protected:
virtual BOOL InitInstance();
};
///////////////////////////////////////////////////////////////////
// Implementation
///////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CMainWnd, CFrameWnd)
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////
// CMainWnd::PreCreateWindow()
BOOL CMainWnd::PreCreateWindow(CREATESTRUCT& cs)
{
// Get the bitmap handles from the resource data
m_bmpBack.LoadBitmap(IDR_BMPBACKGROUND);
m_bmpBtn1.LoadBitmap(IDR_BMPBTN1);
m_bmpBtn1Lit.LoadBitmap(IDR_BMPBTN1LIT);
m_bmpBtn2.LoadBitmap(IDR_BMPBTN2);
m_bmpBtn2Lit.LoadBitmap(IDR_BMPBTN2LIT);
// Call inherited method
return CFrameWnd::PreCreateWindow(cs);
}
///////////////////////////////////////////////////////////////////
// CMainWnd::OnEraseBkgnd()
BOOL CMainWnd::OnEraseBkgnd(CDC* pDC)
{
CFrameWnd::OnEraseBkgnd(pDC); // call inherited method
PositionRects();
BlitBitmap(m_rcClient, m_bmpBack);
// "Blit" the unlit versions to the screen
BlitBitmap(m_rcTop, m_bmpBtn1);
BlitBitmap(m_rcBtm, m_bmpBtn2);
return TRUE;
}
///////////////////////////////////////////////////////////////////
// CMainWnd::PositionRects()
void CMainWnd::PositionRects()
{
// Set the rect positions
CRect rc;
GetClientRect(&rc);
m_rcClient = rc;
m_rcTop.left = 523; m_rcTop.top = 70;
m_rcTop.right = 598; m_rcTop.bottom = 145;
m_rcBtm.left = 523; m_rcBtm.top = 168;
m_rcBtm.right = 598; m_rcBtm.bottom = 243;
}
///////////////////////////////////////////////////////////////////
// CMainWnd::BlitBitmap()
void CMainWnd::BlitBitmap(CRect& rc, CBitmap& bm)
{
CClientDC dc(this);
CDC dcMem;
HBITMAP hbmpOld;
// Get a compatible memory DC
dcMem.CreateCompatibleDC(&dc);
// Select the bitmap into the DC
hbmpOld = (HBITMAP)dcMem.SelectObject(bm);
// "Blit" the bitmap to the screen
//
GetDC()->BitBlt(rc.left, rc.top, rc.right, rc.bottom,
&dcMem, 0, 0, SRCCOPY);
dcMem.SelectObject(hbmpOld);
}
///////////////////////////////////////////////////////////////////
// CMainWnd::OnMouseMove()
void CMainWnd::OnMouseMove(UINT nFlags, CPoint point)
{
// check to see if the pointer is over a bitmap area
//
if (m_rcTop.PtInRect(point))
BlitBitmap(m_rcTop, m_bmpBtn1Lit); // "light" the top image
else if (m_rcBtm.PtInRect(point))
BlitBitmap(m_rcBtm, m_bmpBtn2Lit); // "light" the bottom image
else // turn the lights off
{
// "Blit" the unlit versions to the screen
BlitBitmap(m_rcTop, m_bmpBtn1);
BlitBitmap(m_rcBtm, m_bmpBtn2);
}
// Call the inherited method
CFrameWnd::OnMouseMove(nFlags, point);
}
///////////////////////////////////////////////////////////////////
// CBitmapApp::InitInstance - overrides CWinApp::InitInstance
BOOL CBitmapApp::InitInstance()
{
// Allocate a new frame window object
CMainWnd* pFrame = new CMainWnd;
// Initialize the frame window
pFrame->Create(0, _T("Bitmap Resource Demo Program"),
WS_SYSMENU | WS_BORDER | WS_VISIBLE | WS_CAPTION,
CRect(0, 0, 640, 480));
// Center the frame window
pFrame->CenterWindow();
// Show the frame window
pFrame->ShowWindow(m_nCmdShow);
pFrame->UpdateWindow();
// Assign the frame window as the app's main frame window
this->m_pMainWnd = pFrame;
return TRUE;
}
// Declare, create, and run the application
CBitmapApp MyBitmapApp;
///////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -