⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bmp256view.cpp

📁 vc++程序设计与技巧中第8章“图形图像编程”部分的VC++原代码
💻 CPP
字号:
// BMP256View.cpp : implementation of the CBMP256View class
//

#include "stdafx.h"
#include "BMP256.h"

#include "BMP256Doc.h"
#include "BMP256View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBMP256View

IMPLEMENT_DYNCREATE(CBMP256View, CView)

BEGIN_MESSAGE_MAP(CBMP256View, CView)
	//{{AFX_MSG_MAP(CBMP256View)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBMP256View construction/destruction

CBMP256View::CBMP256View()
{
	// TODO: add construction code here

}

CBMP256View::~CBMP256View()
{
}

BOOL CBMP256View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CBMP256View drawing

void CBMP256View::OnDraw(CDC* pDC)
{
	CBMP256Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CBMP256View printing

BOOL CBMP256View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CBMP256View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CBMP256View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CBMP256View diagnostics

#ifdef _DEBUG
void CBMP256View::AssertValid() const
{
	CView::AssertValid();
}

void CBMP256View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CBMP256Doc* CBMP256View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBMP256Doc)));
	return (CBMP256Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CBMP256View message handlers

BOOL CBMP256View::GetBitmapAndPalette(LPCTSTR lpszresource, BITMAP bm, CBitmap &bitmap, CPalette &pal, long *w, long *h)
{
	LPCTSTR lpszResourceName = (LPCTSTR) lpszresource;
	
	//装入位图
	HBITMAP hBmp = (HBITMAP)::LoadImage( AfxGetInstanceHandle(), 
		lpszResourceName, IMAGE_BITMAP, 0,0, LR_LOADFROMFILE );
	
	if( hBmp == NULL ) 
		return FALSE;
	
	//将该位图与一个CBitmap对象联系起来
	bitmap.Attach( hBmp );
	
	DIBSECTION ds;
	
	BITMAPINFOHEADER &bmInfo = ds.dsBmih;
	
	
	bitmap.GetObject( sizeof(ds), &ds );
	
	//获取位图的尺寸
	::GetObject(hBmp, sizeof(bm), &bm);
	*w = bm.bmWidth;
	*h = bm.bmHeight;
	CClientDC dc(NULL);

	//创建逻辑调色板
	RGBQUAD *pRGB = new RGBQUAD;
	CDC memDC;
	memDC.CreateCompatibleDC(&dc);
	
	memDC.SelectObject( &bitmap );
	::GetDIBColorTable( memDC, 0, 1, pRGB );
	
	UINT nSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) );
	LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nSize];
	
	pLP->palVersion = 0x300;
	pLP->palNumEntries = 1;
	
	
	pLP->palPalEntry[0].peRed = pRGB[0].rgbRed;
	pLP->palPalEntry[0].peGreen = pRGB[0].rgbGreen;
	pLP->palPalEntry[0].peBlue = pRGB[0].rgbBlue;
	pLP->palPalEntry[0].peFlags = 0;
	
	
	pal.CreatePalette( pLP );
	
	delete[] pLP;
	delete[] pRGB;
	//}
	
	return TRUE;

}

void CBMP256View::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CDC memDC;
	memDC.CreateCompatibleDC( &dc );

	CBitmap bitmap;
	BITMAP bm;
	CPalette palette;
	
	long nWidth;
	long nHeight;

	GetBitmapAndPalette("flag.bmp", bm, bitmap, palette,&nWidth,&nHeight);
	memDC.SelectObject( &bitmap );

	if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE && palette.m_hObject != NULL )
	{
		//把逻辑调色板选入设备上下文
		dc.SelectPalette( &palette, FALSE );
		//把设备上下文中的逻辑调色板实现到系统调色板
		dc.RealizePalette();
	}
	//显示位图
	dc.BitBlt(50, 20, nWidth, nHeight, &memDC, 0, 0,SRCCOPY);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -