driveListBox.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "main.h"
  2. #include ".\DriveListBox.h"
  3. #include "resource.h"
  4. #include "..\..\General\gen_ml\graphics.h"
  5. #include <strsafe.h>
  6. DriveListBox::DriveListBox(int controlId)
  7. {
  8. m_hwnd = NULL;
  9. m_parentHwnd= NULL;
  10. hInstance = NULL;
  11. bmpNormal = NULL;
  12. bmpSelected = NULL;
  13. driveResId = NULL;
  14. bgndResId = NULL;
  15. this->controlId = controlId;
  16. SetRect(&rcItem, 0,0,0,68); // hardcoded height
  17. clrNormalBG = RGB(0,0,0);
  18. clrSelected1 = RGB(0,0,0);
  19. clrSelected2 = RGB(255,255,255);
  20. clrTextSel = RGB(255,255,255);
  21. clrTextNorm = RGB(0,255,0);
  22. }
  23. DriveListBox::~DriveListBox(void)
  24. {
  25. DestroyImages();
  26. }
  27. void DriveListBox::DestroyImages(void)
  28. {
  29. if (bmpNormal) DeleteObject(bmpNormal);
  30. bmpNormal = NULL;
  31. if (bmpSelected) DeleteObject(bmpSelected);
  32. bmpSelected = NULL;
  33. }
  34. void DriveListBox::SetColors(COLORREF clrNormalBG, COLORREF clrSelected1, COLORREF clrSelected2, COLORREF clrTextSel, COLORREF clrTextNorm)
  35. {
  36. this->clrNormalBG = clrNormalBG;
  37. this->clrSelected1 = clrSelected1;
  38. this->clrSelected2 = clrSelected2;
  39. this->clrTextSel = clrTextSel;
  40. this->clrTextNorm = clrTextNorm;
  41. ReloadImages();
  42. }
  43. void DriveListBox::SetImages(HINSTANCE hInstance, int bgndResId, int driveResId)
  44. {
  45. this->hInstance = hInstance;
  46. this->bgndResId = bgndResId;
  47. this->driveResId = driveResId;
  48. ReloadImages();
  49. }
  50. HWND DriveListBox::GetHWND(void)
  51. {
  52. return m_hwnd;
  53. }
  54. void DriveListBox::ReloadImages(void)
  55. {
  56. DestroyImages();
  57. if (!hInstance) return;
  58. HBITMAP bmpBck = NULL, bmpDrive = NULL;
  59. if (bgndResId)
  60. {
  61. bmpBck = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(bgndResId), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
  62. if (bmpBck) bmpBck = PatchBitmapColors24(bmpBck, clrSelected1, clrSelected2, Filter1);
  63. }
  64. if (driveResId)
  65. {
  66. bmpDrive = (HBITMAP)LoadImage(hInstance, MAKEINTRESOURCE(driveResId), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
  67. }
  68. CreateBitmaps(bmpBck, bmpDrive);
  69. if (bmpBck) DeleteObject(bmpBck);
  70. if (bmpDrive) DeleteObject(bmpDrive);
  71. }
  72. void DriveListBox::CreateBitmaps(HBITMAP bmpBck, HBITMAP bmpDrive)
  73. {
  74. if (rcItem.right == 0 || rcItem.bottom == 0) return;
  75. HBITMAP bmpDriveMask = NULL;
  76. if (bmpDrive) bmpDriveMask = CreateBitmapMask(bmpDrive, RGB(255, 0, 255));
  77. HDC hdc = GetDC(m_hwnd);
  78. HBITMAP bmp;
  79. for (int i = 0; i < 2; i++)
  80. {
  81. bmp = CreateCompatibleBitmap(hdc, rcItem.right, rcItem.bottom);
  82. HDC memDstDC = CreateCompatibleDC (hdc);
  83. HDC memSrcDC = CreateCompatibleDC (hdc);
  84. HBITMAP obmp1 = (HBITMAP)SelectObject(memDstDC, bmp);
  85. HBITMAP obmp2 = (HBITMAP)SelectObject(memSrcDC, bmpBck);
  86. if (i == 0 )
  87. {
  88. for (int i = 0; i < rcItem.right; i++)
  89. {
  90. BitBlt(memDstDC, i, 0, 2, rcItem.bottom, memSrcDC, 0, 0, SRCCOPY);
  91. }
  92. }
  93. else
  94. {
  95. HBRUSH hb = CreateSolidBrush(clrNormalBG);
  96. FillRect(memDstDC, &rcItem, hb);
  97. DeleteObject(hb);
  98. }
  99. BITMAP bm;
  100. GetObject(bmpDrive, sizeof(BITMAP), &bm);
  101. RECT r1 = {
  102. max(2, (rcItem.right - bm.bmWidth) / 2),
  103. max(2, (rcItem.bottom - 16 - bm.bmHeight) / 2),
  104. min(rcItem.right - 4, bm.bmWidth),
  105. min(rcItem.bottom -18, bm.bmHeight)
  106. };
  107. SelectObject(memSrcDC, bmpDriveMask);
  108. BitBlt(memDstDC, r1.left, r1.top, r1.right, r1.bottom, memSrcDC, 0,0, SRCAND);
  109. SelectObject(memSrcDC, bmpDrive);
  110. BitBlt(memDstDC, r1.left, r1.top, r1.right, r1.bottom, memSrcDC, 0,0, SRCPAINT);
  111. SelectObject(memDstDC, obmp1);
  112. SelectObject(memSrcDC, obmp2);
  113. DeleteDC(memDstDC);
  114. DeleteDC(memSrcDC);
  115. if (i == 0) bmpSelected = bmp;
  116. else bmpNormal = bmp;
  117. }
  118. ReleaseDC(m_hwnd, hdc);
  119. DeleteObject(bmpDriveMask);
  120. }
  121. void DriveListBox::Init(HWND hwnd)
  122. {
  123. m_hwnd = hwnd;
  124. m_parentHwnd = GetParent(hwnd);
  125. }
  126. int DriveListBox::HandleMsgProc(UINT uMsg, WPARAM wParam, LPARAM lParam)
  127. {
  128. switch (uMsg)
  129. {
  130. case WM_DRAWITEM:
  131. if (wParam == (WPARAM)controlId) DrawItem((DRAWITEMSTRUCT *)lParam);
  132. break;
  133. case WM_MEASUREITEM:
  134. if (wParam == (WPARAM)controlId && MeasureItem((LPMEASUREITEMSTRUCT)lParam)) return 1;
  135. break;
  136. case WM_CTLCOLORLISTBOX:
  137. SetBkColor((HDC)wParam, clrNormalBG);
  138. return NULL;
  139. }
  140. return FALSE;
  141. }
  142. void DriveListBox::DrawItem(LPDRAWITEMSTRUCT di)
  143. {
  144. if(di->CtlType == ODT_LISTBOX)
  145. {
  146. if (di->itemID == -1) return;
  147. RECT r;
  148. r=di->rcItem;
  149. if (!bmpSelected || !bmpNormal || ((r.right - r.left) != rcItem.right))
  150. {
  151. SetRect(&rcItem, 0,0,r.right - r.left, rcItem.bottom);
  152. ReloadImages();
  153. }
  154. HBITMAP bmp;
  155. int color;
  156. if (di->itemState & ODS_SELECTED)
  157. {
  158. bmp = bmpSelected;
  159. color = clrTextSel;
  160. }
  161. else
  162. {
  163. bmp = bmpNormal;
  164. color = clrTextNorm;
  165. }
  166. RECT rc;
  167. GetClientRect(di->hwndItem, &rc);
  168. HRGN rgnW = CreateRectRgn(rc.left, rc.top, rc.right, rc.bottom);
  169. // HRGN rgn = CreateRoundRectRgn(r.left +2, r.top + 2, r.right - 2, r.bottom -2, 10, 8);
  170. HRGN rgn = CreateRectRgn(r.left +2, r.top + 2, r.right - 2, r.bottom -2);
  171. CombineRgn(rgn, rgn, rgnW, RGN_AND);
  172. SelectClipRgn(di->hDC, rgn);
  173. DeleteObject(rgn);
  174. DeleteObject(rgnW);
  175. if (bmp)
  176. {
  177. HDC hdcbmp = CreateCompatibleDC(di->hDC);
  178. HBITMAP obmp = (HBITMAP)SelectObject(hdcbmp,bmp);
  179. StretchBlt(di->hDC,
  180. r.left,
  181. r.top,
  182. rcItem.right,
  183. rcItem.bottom,
  184. hdcbmp,
  185. 0,
  186. 0,
  187. rcItem.right,
  188. rcItem.bottom,
  189. SRCCOPY);
  190. SelectObject(hdcbmp,obmp);
  191. DeleteDC(hdcbmp);
  192. }
  193. InflateRect(&r, -2, -2);
  194. if ( (di->itemState & ODS_SELECTED) && GetFocus() == di->hwndItem)
  195. {
  196. DrawFocusRect(di->hDC, &r);
  197. }
  198. SetBkMode(di->hDC, TRANSPARENT);
  199. SetTextColor(di->hDC, color);
  200. RECT textRect = {r.left + 2, r.bottom - 20, r.right - 2, r.bottom - 6};
  201. wchar_t str[256] = {0};
  202. INT nType;
  203. nType = 0xFFFF & ((DWORD)di->itemData >> 16);
  204. StringCchPrintfW(str, 256, WASABI_API_LNGSTRINGW(IDS_X_DRIVE_X),
  205. (nType) ? Drive_GetTypeString(nType) : L"",
  206. (CHAR)(0xFF & di->itemData));
  207. DrawTextW(di->hDC, str,-1,&textRect,DT_BOTTOM|DT_SINGLELINE|DT_CENTER);
  208. }
  209. return;
  210. }
  211. int DriveListBox::MeasureItem(LPMEASUREITEMSTRUCT mi)
  212. {
  213. mi->itemHeight = rcItem.bottom;
  214. return 1;
  215. }