WINCE下点阵转化为位图

       在嵌入式开发中,有时候需要将外部设备发过来的点阵数据在ARM显示屏上显示,为了在屏上将点阵显示出来,需要首先想点阵数据转化为位图,然后将位图绘制到DC上,为了方便点阵数据的处理和绘制,封装了一个点阵类,参考代码如下,有问题的地方欢迎大家指点。

头文件:

/********************************************************************
filename: 	CDotMatrix.h
created:	2011-10-26
author:		firehood

purpose:	实现点阵数据转化为位图并显示
*********************************************************************/
#pragma once

class CDotMatrix
{
public:
	CDotMatrix();
	~CDotMatrix();
public:
	/*
	函数名:Create
	功能:  创建点阵位图
	参数:   [in]hdc    DC句柄
	        [in]width  点阵宽度
	        [in]height 点阵高度 
	返回值:TRUE 成功,FALSE 失败 
	*/
	BOOL Create(HDC hdc, int width, int height);
	
	/*
	函数名:FillBit
	功能:  向位图指定位置填充一个像素点的值
	参数:   [in]bit        点阵像素点的值 
	        [in]nPos       要填充的位置
	        [in]foreColor  前景色
	        [in]bgColor    背景色
	返回值:TRUE 成功,FALSE 失败 
	*/
	BOOL FillBit(int bit, int nPos, COLORREF foreColor, COLORREF bgColor);
	
	/*
	函数名:FillData
	功能:  向位图中填充点阵数据
	参数:   [in]pData      要填充的点阵数据
	        [in]size       点阵数据大小
			[in]foreColor  前景色
	        [in]bgColor    背景色
			[int]nStartPos 位图中要填充起始位置
	返回值:TRUE 成功,FALSE 失败 
	*/
	BOOL FillData(const BYTE* pData,int size, COLORREF foreColor, COLORREF bgColor,int nStartPos = 0);
    
	/*
	函数名:Draw
	功能:  绘制位图到目标DC
	参数:   [in]hdc           目标DC
	        [in]dstRect       绘制到目标DC的矩形区域
	        [in]srcRect       源图像上要绘制的矩形区域
			[in]crTransparent 指定透明颜色
	返回值:无
	*/
	void Draw(HDC hdc, const RECT* dstRect, const RECT* srcRect,COLORREF crTransparent);
private:
	UINT     m_width;   // 点阵宽度
    UINT     m_height;  // 点阵高度
	HDC      m_hMemDc;  // 内存DC
	HBITMAP  m_Bitmap;  // 点阵位图
	BYTE*    m_pBmpBit; // 位图数据
};

源文件:

/********************************************************************
filename: 	CDotMatrix.h
created:	2011-10-26
author:		firehood

purpose:	实现点阵数据转化为位图并显示
*********************************************************************/
#include "stdafx.h"
#include"CDotMatrix.h"

CDotMatrix::CDotMatrix()
{
	m_hMemDc = NULL;
	m_Bitmap = NULL;
	m_pBmpBit = NULL;
	m_width = 0;
	m_height = 0;
}

CDotMatrix::~CDotMatrix()
{
	if(m_Bitmap)
		DeleteObject(m_Bitmap);
	if(m_hMemDc)
		DeleteDC(m_hMemDc);
}

/*
函数名:Create
功能:  创建点阵位图
参数:   [in]hdc    DC句柄
        [in]width  点阵宽度
        [in]height 点阵高度 
返回值:TRUE 成功,FALSE 失败 
*/
BOOL CDotMatrix::Create(HDC hdc, int width, int height)
{
	m_hMemDc = CreateCompatibleDC(hdc);   
    m_width = width;
	m_height= height;
	
	BITMAPINFOHEADER bmpHdr = {0};
	bmpHdr.biSize = sizeof (BITMAPINFOHEADER);
	bmpHdr.biWidth = width;
	bmpHdr.biHeight = -height;
	bmpHdr.biPlanes = 1;
	bmpHdr.biBitCount = 32;
	bmpHdr.biCompression = BI_RGB;

	m_Bitmap = CreateDIBSection (m_hMemDc, (BITMAPINFO *)&bmpHdr, DIB_RGB_COLORS, (void**)&m_pBmpBit, NULL, 0);
	if(m_Bitmap == NULL)
		return FALSE;
	return TRUE;
}

/*
函数名:FillBit
功能:  向位图指定位置填充一个像素点的值
参数:   [in]bit        点阵像素点的值 
        [in]nPos       要填充的位置
        [in]foreColor  前景色
        [in]bgColor    背景色
返回值:TRUE 成功,FALSE 失败 
*/
BOOL CDotMatrix::FillBit(int bit, int nPos, COLORREF foreColor, COLORREF bgColor)
{
	LPRGBQUAD pRgbTable;
	pRgbTable = (LPRGBQUAD)(m_pBmpBit + nPos * 4);
	if(bit)
	{
		pRgbTable->rgbBlue =  GetBValue(foreColor);
		pRgbTable->rgbGreen = GetGValue(foreColor);
		pRgbTable->rgbRed = GetRValue(foreColor);
	}
	else
	{
		pRgbTable->rgbBlue =  GetBValue(bgColor);
		pRgbTable->rgbGreen = GetGValue(bgColor);
		pRgbTable->rgbRed = GetRValue(bgColor);
	}
	pRgbTable->rgbReserved = 0;
	return TRUE;
}

/*
函数名:FillData
功能:  向位图中填充点阵数据
参数:   [in]pData      要填充的点阵数据
        [in]size       点阵数据大小
        [in]foreColor  前景色
        [in]bgColor    背景色
        [int]nStartPos 要填充起始位置
返回值:TRUE 成功,FALSE 失败 
*/
BOOL CDotMatrix::FillData(const BYTE* pData,int size, COLORREF foreColor, COLORREF bgColor,int nStartPos)
{
	BYTE bit;
	for(int i =0; i<size; i++)
	{
		int nPos = (nStartPos+i)*8;
		for(int j = 7; j >= 0; j--)
		{
			bit = (pData[i]>>j)&0x01;
			// 填充点阵数据
			FillBit(bit,nPos,foreColor,bgColor);
			nPos++;
		}

	}
	return TRUE;
}

/*
函数名:Draw
功能:  绘制位图到目标DC
参数:   [in]hdc           目标DC
        [in]dstRect       绘制到目标DC的矩形区域
        [in]srcRect       源图像上要绘制的矩形区域
        [in]crTransparent 指定透明颜色
返回值:无
*/
void CDotMatrix::Draw(HDC hdc, const RECT* dstRect, const RECT* srcRect,COLORREF crTransparent)
{
	// 将点阵位图选入到内存DC
	HBITMAP hOldBmp= (HBITMAP)SelectObject(m_hMemDc,m_Bitmap);   
	
	// 创建临时DC
	HDC hTempDc = CreateCompatibleDC(hdc);
	// 创建临时位图
	HBITMAP hTempBmp = CreateCompatibleBitmap(
		hdc,
		dstRect->right-dstRect->left, 
		dstRect->bottom-dstRect->top
		);
	// 将临时位图选入到临时DC
	HBITMAP hOldTempBmp = (HBITMAP)SelectObject(hTempDc, hTempBmp);

	// 将点阵位图绘制到临时DC
	StretchBlt(
		hTempDc,
		0, 
		0, 
		dstRect->right-dstRect->left, 
		dstRect->bottom-dstRect->top, 
		m_hMemDc,
		srcRect->left, 
		srcRect->top, 
		srcRect->right-srcRect->left, 
		srcRect->bottom-srcRect->top, 
		SRCCOPY);

	// 将临时DC绘制到目标DC
	TransparentBlt(
		hdc,
		dstRect->left, 
		dstRect->top, 
		dstRect->right-dstRect->left, 
		dstRect->bottom-dstRect->top, 
		hTempDc,
		0, 
		0, 
		dstRect->right-dstRect->left, 
		dstRect->bottom-dstRect->top, 
		crTransparent);
	
	// 恢复并释放环境	
	SelectObject(m_hMemDc,hOldBmp);
	SelectObject(hTempDc,hOldTempBmp);
	DeleteObject(hTempBmp);
	DeleteDC(hTempDc);
}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值