In embedded development, the dot matrix data sent from external devices is sometimes displayed on the arm display. to display the dot matrix data on the screen, you must first convert the dot matrix data into a bitmap, the bitmap is then drawn to the DC. In order to facilitate processing and drawing of the dot matrix data, a dot matrix class is encapsulated. Refer to the code below. If you have any questions, please give us some advice.
Header file:
/*************************************** * **************************** Filename: CDotMatrix. hcreated: 2011-10-26author: firehoodpurpose: convert the dot matrix data to a bitmap and display ******************************** * **********************************/# pragma onceclass CDotMatrix {public: CDotMatrix ();~ CDotMatrix (); public:/* function name: Create Function: Create a dot matrix bitmap parameter: [in] hdc DC handle [in] width dot matrix width [in] height dot matrix height return value: TRUE: Success, FALSE: Failure */BOOL Create (HDC hdc, int width, int height);/* function name: FillBit function: Fill in the value parameter of a pixel at the specified position of the bitmap: [in] bit dot matrix pixel value [in] nPos position to be filled [in] foreColor foreground color [in] bgColor background color returned value: TRUE success, FALSE failed */BOOL FillBit (int bit, int nPos, COLORREF foreColor, COLORREF bgColor);/* function name: FillData function: Fill the dot matrix data parameters in the bitmap: [in] the dot matrix data to be filled with pData [in] size the dot matrix data size [in] foreground color [in] bgColor background color [int] nStartPos bitmap needs to be filled with the starting position. Return Value: TRUE success, FALSE Failure */BOOL FillData (const BYTE * pData, int size, COLORREF foreColor, COLORREF bgColor, int nStartPos = 0);/* function name: Draw function: draw bitmap to Target DC parameters: [in] hdc Target DC [in] dstRect draw to the rectangular area of the Target DC [in] srcRect source image to draw the rectangular area [in] crTransparent specify the transparent color return value: no */void Draw (HDC hdc, const RECT * dstRect, const RECT * srcRect, COLORREF crTransparent); private: UINT m_width; // dot matrix width UINT m_height; // lattice height HDC m_hMemDc; // memory DCHBITMAP m_Bitmap; // bitmap BYTE * m_pBmpBit; // bitmap data };
Source file:
/*************************************** * **************************** Filename: cdotmatrix. hcreated: 2011-10-26author: firehoodpurpose: convert the dot matrix data to a bitmap and display ******************************** * ***********************************/# 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);}/* function name: Create Function: Create bitmap parameters: [in] hdc dc handle [in] width lattice width [in] height lattice height returned value: true success, False Failure */bool cdotmatrix: Create (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;}/* function name: fillbit function: Fill in the value parameter of a pixel at the specified position of the bitmap: [in] bit dot matrix pixel value [in] NPOs position to be filled [in] forecolor foreground color [in] bgcolor background color returned value: true success, False Failure */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;}/* function name: filldata function: Fill the bitmap with the dot matrix data parameters: [in] the dot matrix data to be filled with pdata [in] size the dot matrix data size [in] forecolor foreground color [in] bgcolor background color [int] nstartpos to fill the starting position. Return Value: true success, false failed */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; // fill in the fillbit (bit, NPOs, forecolor, bgcolor); NPOs ++ ;}} return true;}/* function name: Draw function: draws a bitmap to the Target DC parameter: [in] HDC Target DC [in] dstrect draw to the rectangular area of the Target DC [in] srcrect source image to draw the rectangular area [in] crtransparent specify the transparent color return value: none */void cdotmatrix: Draw (HDC, const rect * dstrect, const rect * srcrect, colorref crtransparent) {// select the dot matrix bitmap to the memory dchbitmap holdbmp = (hbitmap) selectObject (m_hmemdc, m_bitmap); // create a temporary dchdc htempdc = createcompatibledc (HDC); // create a temporary bitmap hbitmap htempbmp = createcompatiblebitmap (HDC, dstrect-> right-dstrect-> left, dstrect-> bottom-dstrect-> top); // select the temporary bitmap to the temporary dchbitmap holdtempbmp = (hbitmap) SelectObject (htempdc, htempbmp); // map the bitmap to the temporary dcstretchblt (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 ); // draw the temporary DC to the target dctransparentblt (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 ); // restore and release the SelectObject (m_hmemdc, holdbmp) Environment; SelectObject (htempdc, holdtempbmp); deleteobject (htempbmp); deletedc (htempdc );}