GoodDC.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // CGoodDC.cpp: implementation of the CGoodDC class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "gooddc.h"
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. BOOL DisplayBMP(CDC* pDC,int nX1,int nY1,int nXX,int nYY,CString strBMPFile,BOOL bStretch)
  10. {
  11. OFSTRUCT of;
  12. unsigned fh = OpenFile ((char const*)strBMPFile, &of, OF_READ);
  13. if(fh==-1) return FALSE;
  14. DWORD size = (DWORD)(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
  15. HANDLE hDIBInfo = GlobalAlloc(GMEM_MOVEABLE,size);
  16. BITMAPFILEHEADER bf;
  17. LPBITMAPINFOHEADER lpbi;
  18. lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIBInfo);
  19. _lread(fh,(LPSTR)&bf,sizeof (bf));
  20. _lread(fh,(LPSTR)lpbi,sizeof(BITMAPINFOHEADER));
  21. WORD nClrs = (WORD)lpbi->biClrUsed;
  22. size = ((((lpbi->biWidth*lpbi->biBitCount)+31)&~31)>>3)*lpbi->biHeight;
  23. if(nClrs==0 && lpbi->biBitCount!=24) nClrs = 1<<lpbi->biBitCount;
  24. if(lpbi->biClrUsed == 0) lpbi->biClrUsed = (DWORD)nClrs;
  25. if(lpbi->biSizeImage == 0) lpbi->biSizeImage = size;
  26. WORD Size = (WORD)lpbi->biSize;
  27. WORD Width = (WORD)lpbi->biWidth;
  28. WORD Height = (WORD)lpbi->biHeight;
  29. WORD ClrUsed = (WORD)lpbi->biClrUsed;
  30. WORD BitCount = (WORD)lpbi->biBitCount;
  31. DWORD SizeImage = lpbi->biSizeImage;
  32. WORD SizeRGB = nClrs*sizeof(RGBQUAD);
  33. WORD SizeHeader = Size+SizeRGB;
  34. GlobalUnlock(hDIBInfo);
  35. hDIBInfo = GlobalReAlloc(hDIBInfo,SizeHeader+SizeImage, 0);
  36. lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIBInfo);
  37. _lread(fh,(LPSTR)(lpbi)+Size,SizeRGB);
  38. if(bf.bfOffBits!=0L) _llseek(fh,bf.bfOffBits,SEEK_SET);
  39. DWORD ul = SizeImage;
  40. BYTE *hp = (BYTE *)((LPSTR)lpbi + SizeHeader);
  41. while(ul>30000L)
  42. {
  43. _lread(fh,(LPSTR)hp,30000L);
  44. ul -= 30000L;
  45. hp += 30000L;
  46. }
  47. _lread(fh,(LPSTR)hp,(WORD)ul);
  48. _lclose(fh);
  49. HPALETTE hPalette,hOldPal;
  50. HANDLE hPalHeader = NULL;
  51. HDC hDC = pDC->m_hDC;
  52. LPBITMAPINFOHEADER lpHeader = lpbi;
  53. WORD wDIBUse = DIB_RGB_COLORS;
  54. if(ClrUsed)
  55. {
  56. WORD size = sizeof(LOGPALETTE) + ClrUsed * sizeof(PALETTEENTRY);
  57. NPLOGPALETTE npPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED,size);
  58. npPal->palVersion = 0x300;
  59. npPal->palNumEntries = ClrUsed;
  60. RGBQUAD FAR *lpRGB = (RGBQUAD FAR *)((LPSTR)lpbi + Size);
  61. for(WORD i=0;i<ClrUsed;i++,lpRGB++)
  62. {
  63. npPal->palPalEntry[i].peRed = lpRGB->rgbRed;
  64. npPal->palPalEntry[i].peGreen = lpRGB->rgbGreen;
  65. npPal->palPalEntry[i].peBlue = lpRGB->rgbBlue;
  66. npPal->palPalEntry[i].peFlags = 0;
  67. }
  68. hPalette = CreatePalette((LPLOGPALETTE)npPal);
  69. LocalFree((HANDLE)npPal);
  70. hOldPal = SelectPalette(hDC,hPalette,FALSE);
  71. RealizePalette(hDC);
  72. if(BitCount!=24)
  73. {
  74. size = Size+ClrUsed*sizeof(WORD);
  75. hPalHeader = GlobalAlloc(GMEM_MOVEABLE,size);
  76. LPBITMAPINFOHEADER lpPalInfo;
  77. lpPalInfo = (LPBITMAPINFOHEADER)GlobalLock(hPalHeader);
  78. *lpPalInfo = *lpbi;
  79. WORD FAR *lpTable = (WORD FAR *)((LPSTR)lpPalInfo + Size);
  80. for(WORD i=0;i<ClrUsed;i++) *lpTable++ = i;
  81. GlobalUnlock(hPalHeader);
  82. lpHeader = (LPBITMAPINFOHEADER) GlobalLock(hPalHeader);
  83. wDIBUse = DIB_PAL_COLORS;
  84. }
  85. }
  86. HDC hMemDC = CreateCompatibleDC(hDC);
  87. HBITMAP hDDBitmap = CreateCompatibleBitmap(hDC,Width,Height);
  88. SetDIBits(hDC,hDDBitmap,0,Height,(LPSTR)lpbi + SizeHeader,
  89. (LPBITMAPINFO)lpHeader,wDIBUse);
  90. HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC,hDDBitmap);
  91. if(bStretch)
  92. {
  93. StretchBlt(hDC,nX1,nY1,nXX,nYY,hMemDC,0,0,Width,Height,SRCCOPY);
  94. }
  95. else
  96. {
  97. Width = min(Width,nXX);
  98. Height = min(Height,nYY);
  99. BitBlt(hDC,nX1,nY1,Width,Height,hMemDC,0,0,SRCCOPY);
  100. }
  101. SelectObject(hMemDC,hOldBitmap);
  102. DeleteDC(hMemDC);
  103. DeleteObject(hDDBitmap);
  104. if(ClrUsed)
  105. {
  106. SelectPalette(hDC,hOldPal,FALSE);
  107. DeleteObject(hPalette);
  108. if(BitCount!=24)
  109. {
  110. GlobalUnlock(hPalHeader);
  111. GlobalFree(hPalHeader);
  112. }
  113. }
  114. GlobalUnlock(hDIBInfo);
  115. GlobalFree(hDIBInfo);
  116. return TRUE;
  117. }
  118. void DrawColorButton(LPDRAWITEMSTRUCT lpDIS,COLORREF clrButton)
  119. {
  120. CDC* pDC = CDC::FromHandle(lpDIS->hDC);
  121. CRect rectButton = lpDIS->rcItem;
  122. UINT nStyle = DFCS_BUTTONPUSH;
  123. if(lpDIS->itemState & ODS_SELECTED) nStyle |= DFCS_PUSHED;
  124. if(lpDIS->itemState & ODS_DISABLED) nStyle |= DFCS_INACTIVE;
  125. pDC->DrawFrameControl(rectButton,DFC_BUTTON,nStyle);
  126. CRect rectColor = lpDIS->rcItem;
  127. rectColor.InflateRect(-1,-1,-1,-1);
  128. if(lpDIS->itemState & ODS_SELECTED) rectColor.InflateRect(-1,-1,1,1);
  129. if(lpDIS->itemState & ODS_DISABLED)
  130. {
  131. CPen pen(PS_SOLID,1,RGB(128,128,128));
  132. CPen* pOldPen = pDC->SelectObject(&pen);
  133. pDC->SetBkColor(RGB(192,192,192));
  134. pDC->SetTextColor(clrButton);
  135. CBrush* pBrush = pDC->GetHalftoneBrush();
  136. CBrush* pOldBrush = pDC->SelectObject(pBrush);
  137. pDC->Rectangle(rectColor);
  138. pDC->SelectObject(pOldBrush);
  139. pDC->SelectObject(pOldPen);
  140. }
  141. else
  142. {
  143. CBrush brush(clrButton);
  144. CBrush* pOldBrush = pDC->SelectObject(&brush);
  145. pDC->SelectStockObject(BLACK_PEN);
  146. pDC->Rectangle(rectColor);
  147. pDC->SelectObject(pOldBrush);
  148. }
  149. }
  150. void CGoodDC::Create(CDC* pDC)
  151. {
  152. m_pOldDC = pDC;
  153. m_pOldDC->GetClipBox(m_rect);
  154. m_rect.right ++;
  155. m_rect.bottom ++;
  156. CreateCompatibleDC(m_pOldDC);
  157. if( m_rect.Width() > 0 )
  158. {
  159. m_bitmap.CreateCompatibleBitmap(m_pOldDC,m_rect.Width(),m_rect.Height());
  160. m_pOldBitmap = SelectObject(&m_bitmap);
  161. OffsetViewportOrg(-m_rect.left,-m_rect.top);
  162. IntersectClipRect(m_rect);
  163. CPoint point(0,0);
  164. LPtoDP(&point);
  165. SetBrushOrg(point);
  166. //2011-11-01 add
  167. DeleteObject( m_bitmap );
  168. }
  169. //2011-11-02 add
  170. DeleteObject( m_pOldDC );
  171. }
  172. void CGoodDC::Destroy(void)
  173. {
  174. SelectClipRgn(NULL);
  175. SetViewportOrg(0,0);
  176. m_pOldDC->BitBlt(m_rect.left,m_rect.top,m_rect.Width(),m_rect.Height(),this, 0, 0, SRCCOPY);
  177. SelectObject(m_pOldBitmap);
  178. }
  179. void CGoodDC::Destroy(CRect rect)
  180. {
  181. SelectClipRgn(NULL);
  182. SetViewportOrg(0,0);
  183. CRect rc1,rc2,rc3,rc4;
  184. //////////////////////////////////////
  185. // 2 //
  186. // //
  187. // /////////////// //
  188. // 1 // // 3 //
  189. // // // //
  190. // ////////////// //
  191. // //
  192. // 4 //
  193. // //
  194. //////////////////////////////////////
  195. rc1.left = m_rect.left;
  196. rc1.right = rect.left;
  197. rc1.top = m_rect.top;
  198. rc1.bottom = m_rect.bottom;
  199. rc2.left = m_rect.left;
  200. rc2.right = m_rect.right;
  201. rc2.top = m_rect.top;
  202. rc2.bottom = rect.top;
  203. rc3.left = rect.right;
  204. rc3.right = m_rect.right;
  205. rc3.top = m_rect.top;
  206. rc3.bottom = m_rect.bottom;
  207. rc4.left = m_rect.left;
  208. rc4.right = m_rect.right;
  209. rc4.top = rect.bottom;
  210. rc4.bottom = m_rect.bottom;
  211. m_pOldDC->BitBlt(rc1.left,rc1.top,rc1.Width()/1,rc1.Height(),this, 0,0, SRCCOPY);
  212. m_pOldDC->BitBlt(rc2.left,rc2.top,rc2.Width()/1,rc2.Height(),this, 0,rc2.top, SRCCOPY);
  213. m_pOldDC->BitBlt(rc3.left,rc3.top,rc3.Width()/1,rc3.Height(),this, rc3.left,rc3.top, SRCCOPY);
  214. m_pOldDC->BitBlt(rc4.left,rc4.top,rc4.Width()/1,rc4.Height(),this, 0,rc4.top, SRCCOPY);
  215. SelectObject(m_pOldBitmap);
  216. //if( m_rect.left<rect.left )
  217. //{
  218. // rc1.left = m_rect.left;
  219. // rc1.right = m_rect.right;
  220. // rc1.top = m_rect.top;
  221. // rc1.bottom = rect.top;
  222. // rc2.left = m_rect.left;
  223. // rc2.right = rect.left;
  224. // rc2.top = m_rect.top;
  225. // rc2.bottom = m_rect.bottom;
  226. // rc3.left = rect.right;
  227. // rc3.right = m_rect.right;
  228. // rc3.top = m_rect.top;
  229. // rc3.bottom = m_rect.bottom;
  230. // rc4.left = m_rect.left;
  231. // rc4.right = m_rect.right;
  232. // rc4.top = rect.bottom;
  233. // rc4.bottom = m_rect.bottom;
  234. //m_pOldDC->BitBlt(rc1.left,rc1.top,rc1.Width()/1,rc1.Height(),this, 0,0, SRCCOPY);
  235. //m_pOldDC->BitBlt(rc2.left,rc2.top,rc2.Width()/1,rc2.Height(),this, 0,rc2.top, SRCCOPY);
  236. //m_pOldDC->BitBlt(rc3.left,rc3.top,rc3.Width()/1,rc3.Height(),this, rc3.left,rc3.top, SRCCOPY);
  237. //m_pOldDC->BitBlt(rc4.left,rc4.top,rc4.Width()/1,rc4.Height(),this, 0,rc4.top, SRCCOPY);
  238. //SelectObject(m_pOldBitmap);
  239. }