1
0

banner.cpp 4.9 KB

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