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);
}



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章