CCompatibleDC.cpp 7.8 KB

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