1
0

banner.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "banner.h"
  2. #include "graphics.h"
  3. MLBanner::MLBanner(void)
  4. {
  5. bmpBck = NULL;
  6. bmpLogo = NULL;
  7. bmpLogoMask = NULL;
  8. bmpBanner = NULL;
  9. m_hwnd = NULL;
  10. oldWndProc = NULL;
  11. color1 = RGB(0,0,0);
  12. color2 = RGB(255,255,255);
  13. hInstance = NULL;
  14. logoResId = 0;
  15. bgndResId = 0;
  16. SetRect(&rcBanner, 0,0,0,0);
  17. }
  18. MLBanner::~MLBanner(void)
  19. {
  20. DestroyImages();
  21. SetWindowLong(m_hwnd, GWL_WNDPROC, (LONG)oldWndProc);
  22. oldWndProc = NULL;
  23. }
  24. void MLBanner::SetColors(int color1, int color2)
  25. {
  26. this->color1 = color1;
  27. this->color2 = color2;
  28. ReloadImages();
  29. }
  30. void MLBanner::SetImages(HINSTANCE hInstance, int bgndResId, int logoResId)
  31. {
  32. this->hInstance = hInstance;
  33. this->logoResId = logoResId;
  34. this->bgndResId = bgndResId;
  35. ReloadImages();
  36. }
  37. void MLBanner::ReloadImages(void)
  38. {
  39. DestroyImages();
  40. if (hInstance)
  41. {
  42. if (bgndResId)
  43. {
  44. bmpBck = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(bgndResId), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
  45. if (bmpBck) bmpBck = PatchBitmapColors24(bmpBck, color1, color2, Filter1);
  46. }
  47. if (logoResId)
  48. {
  49. bmpLogo = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(logoResId), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
  50. if (bmpLogo)
  51. {
  52. bmpLogoMask = CreateBitmapMask(bmpLogo, 1,1);
  53. }
  54. }
  55. }
  56. }
  57. void MLBanner::DestroyImages(void)
  58. {
  59. if (bmpBck) DeleteObject(bmpBck);
  60. bmpBck = NULL;
  61. if (bmpLogo) DeleteObject(bmpLogo);
  62. bmpLogo = NULL;
  63. if (bmpLogoMask) DeleteObject(bmpLogoMask);
  64. bmpLogoMask = NULL;
  65. if (bmpBanner) DeleteObject(bmpBanner);
  66. bmpBanner = NULL;
  67. }
  68. void MLBanner::UpdateBunnerBmp(void)
  69. {
  70. if (bmpBanner) DeleteObject(bmpBanner);
  71. HDC hdc = GetDC(m_hwnd);
  72. bmpBanner = CreateCompatibleBitmap(hdc, rcBanner.right, rcBanner.bottom);
  73. HDC memDstDC = CreateCompatibleDC (hdc);
  74. HDC memSrcDC = CreateCompatibleDC (hdc);
  75. SelectObject(memDstDC, bmpBanner);
  76. SelectObject(memSrcDC, bmpBck);
  77. for (int i = 0; i < rcBanner.right; i++)
  78. {
  79. BitBlt(memDstDC,
  80. i,0,
  81. 1, rcBanner.bottom,
  82. memSrcDC,
  83. 0,0,
  84. SRCCOPY);
  85. }
  86. BITMAP bm;
  87. GetObject(bmpLogo, sizeof(BITMAP), &bm);
  88. SelectObject(memSrcDC, bmpLogoMask);
  89. BitBlt(memDstDC,
  90. 6,
  91. max(2, (rcBanner.bottom - bm.bmHeight) / 2),
  92. min(rcBanner.right - 4, bm.bmWidth),
  93. min(rcBanner.bottom - 2, bm.bmHeight),
  94. memSrcDC,
  95. 0,0,
  96. SRCAND);
  97. SelectObject(memSrcDC, bmpLogo);
  98. BitBlt(memDstDC,
  99. 6,
  100. max(2, (rcBanner.bottom - bm.bmHeight) / 2),
  101. min(rcBanner.right - 4, bm.bmWidth),
  102. min(rcBanner.bottom - 2, bm.bmHeight),
  103. memSrcDC,
  104. 0,0,
  105. SRCPAINT);
  106. ReleaseDC(m_hwnd, hdc);
  107. DeleteDC(memDstDC);
  108. DeleteDC(memSrcDC);
  109. }
  110. void MLBanner::Init(HWND hwnd)
  111. {
  112. m_hwnd = hwnd;
  113. SetWindowLong(hwnd,GWL_USERDATA,(LONG)this);
  114. oldWndProc= (WNDPROC) SetWindowLong(hwnd, GWL_WNDPROC, (LONG)newWndProc);
  115. UpdateBunnerBmp();
  116. }
  117. BOOL CALLBACK MLBanner::newWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  118. {
  119. MLBanner *banner = (MLBanner*)GetWindowLong(hwndDlg, GWL_USERDATA);
  120. switch(uMsg)
  121. {
  122. case WM_SIZE:
  123. if (SIZE_MINIMIZED != wParam)
  124. {
  125. SetRect(&banner->rcBanner, 0,0,LOWORD(lParam),HIWORD(lParam));
  126. banner->UpdateBunnerBmp();
  127. }
  128. break;
  129. case WM_ERASEBKGND:
  130. {
  131. HDC hdc = GetDC(hwndDlg);
  132. if (banner->bmpBanner)
  133. {
  134. HDC memSrcDC = CreateCompatibleDC (hdc);
  135. SelectObject(memSrcDC, banner->bmpBanner);
  136. StretchBlt( hdc,
  137. banner->rcBanner.left,
  138. banner->rcBanner.top,
  139. banner->rcBanner.right - banner->rcBanner.left,
  140. banner->rcBanner.bottom - banner->rcBanner.top,
  141. memSrcDC,
  142. banner->rcBanner.left,
  143. banner->rcBanner.top,
  144. banner->rcBanner.right - banner->rcBanner.left,
  145. banner->rcBanner.bottom - banner->rcBanner.top,
  146. SRCCOPY);
  147. DeleteDC(memSrcDC);
  148. }
  149. ReleaseDC(hwndDlg, hdc);
  150. }
  151. return TRUE;
  152. case WM_PAINT:
  153. {
  154. PAINTSTRUCT pt;
  155. HDC hdc = BeginPaint(hwndDlg, &pt);
  156. if (!banner->bmpBanner)
  157. {
  158. SetRect(&banner->rcBanner, 0,0,pt.rcPaint.right - pt.rcPaint.left, pt.rcPaint.bottom - pt.rcPaint.top);
  159. banner->UpdateBunnerBmp();
  160. }
  161. if (banner->bmpBanner)
  162. {
  163. HDC memSrcDC = CreateCompatibleDC (hdc);
  164. SelectObject(memSrcDC, banner->bmpBanner);
  165. StretchBlt( hdc,
  166. pt.rcPaint.left,
  167. pt.rcPaint.top,
  168. pt.rcPaint.right - pt.rcPaint.left,
  169. pt.rcPaint.bottom - pt.rcPaint.top,
  170. memSrcDC,
  171. pt.rcPaint.left,
  172. pt.rcPaint.top,
  173. pt.rcPaint.right - pt.rcPaint.left,
  174. pt.rcPaint.bottom - pt.rcPaint.top,
  175. SRCCOPY);
  176. DeleteDC(memSrcDC);
  177. ValidateRect(hwndDlg, &pt.rcPaint);
  178. }
  179. EndPaint(hwndDlg, &pt);
  180. }
  181. break;
  182. }
  183. return CallWindowProc(banner->oldWndProc, hwndDlg, uMsg, wParam, lParam);
  184. }