WINCE下点阵转化为位图
2011-10-26 16:24
645 查看
在嵌入式开发中,有时候需要将外部设备发过来的点阵数据在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); }
相关文章推荐
- WINCE 半透明及透明位图的实现技术
- 同样指令bmp.LockBits不同平台(Windows/WinCE)获取位图的数据不同
- wince 拼音输入法转化成英文输入法 、 获得和设置wince输入法 源码
- WinCE 程序设计 (3rd 版)--2.3 位图
- C语言实现将彩色BMP位图转化为二值图
- WINCE下由HBITMAP获取位图RGB颜色数据
- 位图转化为矢量图
- C语言实现将彩色BMP位图转化为二值图
- wince(2) 位图的使用
- 获得位图之点阵
- wince窗口 屏幕拷贝为位图
- [转] WINCE如何调节位图透明度并显示
- 同样指令bmp.LockBits不同平台(Windows/WinCE)获取位图的数据不同
- WINCE 自绘位图按钮
- Android中常用的位图操作(View与Bitmap转化、圆角、灰化、提取Alpha、旋转、倒影、剪切……)
- WINCE如何调节位图透明度并显示
- 【c++】点阵字库提取汉字转位图
- Android中常用的位图操作(View与Bitmap转化、圆角、灰化、提取Alpha、旋转、倒影、剪切……)
- wince(2.3)获取位图某一点的RGB值
- UIImage与位图(RGBA8 Bitmaps)的相互转化