1
0

infoBox.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "main.h"
  2. #include ".\infoBox.h"
  3. MLInfoBox::MLInfoBox(void)
  4. {
  5. oldWndProc = NULL;
  6. m_hwnd = NULL;
  7. bodyBrush = NULL;
  8. headerBrush = NULL;
  9. headerText[0] = 0;
  10. SetColors(RGB(0,0,0), RGB(255,255,255), RGB(0,60,0));
  11. SetRect(&rcBody, 0,0,0,0);
  12. drawHeader = TRUE;
  13. SetRect(&rcHeader, 0,0,0,20); // default height
  14. headerFont = NULL;
  15. }
  16. MLInfoBox::~MLInfoBox(void)
  17. {
  18. SetWindowLong(m_hwnd, GWLP_WNDPROC, (LONGX86)(LONG_PTR)oldWndProc);
  19. oldWndProc = NULL;
  20. if (headerBrush) DeleteObject(headerBrush);
  21. headerBrush = NULL;
  22. if (bodyBrush) DeleteObject(bodyBrush);
  23. bodyBrush = NULL;
  24. if (headerFont) DeleteObject(headerFont);
  25. headerFont = NULL;
  26. }
  27. void MLInfoBox::SetColors(COLORREF bodyBG, COLORREF headerFG, COLORREF headerBG)
  28. {
  29. this->bodyBG = bodyBG;
  30. this->headerFG = headerFG;
  31. this->headerBG = headerBG;
  32. if (headerBrush) DeleteObject(headerBrush);
  33. headerBrush = NULL;
  34. headerBrush = CreateSolidBrush(headerBG);
  35. if (bodyBrush) DeleteObject(bodyBrush);
  36. bodyBrush = NULL;
  37. bodyBrush = CreateSolidBrush(bodyBG);
  38. }
  39. void MLInfoBox::Init(HWND hwnd)
  40. {
  41. m_hwnd = hwnd;
  42. HDC hdc = GetDC(hwnd);
  43. long lfHeight;
  44. lfHeight = -MulDiv(8, GetDeviceCaps(hdc, LOGPIXELSY), 72);
  45. headerFont = CreateFontW(lfHeight, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, L"Arial");
  46. ReleaseDC(hwnd, hdc);
  47. SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONGX86)(LONG_PTR)this);
  48. oldWndProc= (WNDPROC)(LONG_PTR)SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONGX86)(LONG_PTR)newWndProc);
  49. RECT rc;
  50. GetWindowRect(hwnd, &rc);
  51. SetSize(rc.right - rc.left, rc.bottom - rc.top);
  52. }
  53. void MLInfoBox::SetSize(int cx, int cy)
  54. {
  55. int offset = 0;
  56. if (drawHeader)
  57. {
  58. SetRect(&rcHeader, 0,0, cx, rcHeader.bottom);
  59. offset = rcHeader.bottom;
  60. }
  61. SetRect(&rcBody, 0, offset, cx, cy);
  62. }
  63. LRESULT CALLBACK MLInfoBox::newWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam,LPARAM lParam)
  64. {
  65. MLInfoBox *box = (MLInfoBox*)(LONG_PTR)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
  66. switch(uMsg)
  67. {
  68. case WM_SIZE:
  69. if (SIZE_MINIMIZED != wParam)
  70. {
  71. box->SetSize(LOWORD(lParam), HIWORD(lParam));
  72. }
  73. break;
  74. case WM_ERASEBKGND:
  75. {
  76. HDC hdc = GetDC(hwndDlg);
  77. SetTextColor(hdc, box->headerFG);
  78. SetBkColor(hdc, box->headerBG);
  79. RECT txtRect;
  80. SetRect(&txtRect, box->rcHeader.left + 8, box->rcHeader.top + 2, box->rcHeader.right -2, box->rcHeader.bottom -2);
  81. HFONT oldFont = (HFONT)SelectObject(hdc, box->headerFont);
  82. GetWindowTextW(hwndDlg, box->headerText, CAPTION_LENGTH);
  83. DrawTextW(hdc, box->headerText, -1, &txtRect, DT_VCENTER | DT_LEFT | DT_SINGLELINE);
  84. SelectObject(hdc, oldFont);
  85. ReleaseDC(hwndDlg, hdc);
  86. }
  87. return TRUE;
  88. break;
  89. case WM_PAINT:
  90. {
  91. PAINTSTRUCT pt;
  92. HDC hdc = BeginPaint(hwndDlg, &pt);
  93. RECT drawRect ;
  94. if(box->drawHeader && IntersectRect(&drawRect, &box->rcHeader, &pt.rcPaint))
  95. {
  96. FillRect(hdc, &drawRect, box->headerBrush);
  97. SetTextColor(hdc, box->headerFG);
  98. SetBkColor(hdc, box->headerBG);
  99. SetRect(&drawRect, box->rcHeader.left + 8, box->rcHeader.top + 2, box->rcHeader.right -2, box->rcHeader.bottom -2);
  100. HFONT oldFont = (HFONT)SelectObject(hdc, box->headerFont);
  101. GetWindowTextW(hwndDlg, box->headerText, CAPTION_LENGTH);
  102. DrawTextW(hdc, box->headerText, -1, &drawRect, DT_VCENTER | DT_LEFT | DT_SINGLELINE);
  103. SelectObject(hdc, oldFont);
  104. ValidateRect(hwndDlg, &drawRect);
  105. }
  106. if(IntersectRect(&drawRect, &box->rcBody, &pt.rcPaint))
  107. {
  108. FillRect(hdc, &drawRect, box->bodyBrush);
  109. ValidateRect(hwndDlg, &drawRect);
  110. }
  111. EndPaint(hwndDlg, &pt);
  112. }
  113. break;
  114. }
  115. return CallWindowProc(box->oldWndProc, hwndDlg, uMsg, wParam, lParam);
  116. }