link_control.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <windows.h>
  2. #include "link_control.h"
  3. static HCURSOR link_hand_cursor;
  4. static LRESULT link_handlecursor(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  5. {
  6. LRESULT ret = CallWindowProcW((WNDPROC)GetPropW(hwndDlg, L"link_proc"), hwndDlg, uMsg, wParam, lParam);
  7. // override the normal cursor behaviour so we have a hand to show it is a link
  8. if(uMsg == WM_SETCURSOR)
  9. {
  10. if((HWND)wParam == hwndDlg)
  11. {
  12. if(!link_hand_cursor)
  13. {
  14. link_hand_cursor = LoadCursor(NULL, IDC_HAND);
  15. }
  16. SetCursor(link_hand_cursor);
  17. return TRUE;
  18. }
  19. }
  20. return ret;
  21. }
  22. void link_startsubclass(HWND hwndDlg, UINT id){
  23. HWND ctrl = GetDlgItem(hwndDlg, id);
  24. SetPropW(ctrl, L"link_proc",
  25. (HANDLE)(LONG_PTR)SetWindowLongPtrW(ctrl, GWLP_WNDPROC, (LONG_PTR)link_handlecursor));
  26. }
  27. void link_handledraw(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  28. {
  29. if (uMsg == WM_DRAWITEM)
  30. {
  31. DRAWITEMSTRUCT *di = (DRAWITEMSTRUCT *)lParam;
  32. if (di->CtlType == ODT_BUTTON)
  33. {
  34. wchar_t wt[123] = {0};
  35. int y;
  36. RECT r;
  37. HPEN hPen, hOldPen;
  38. GetDlgItemTextW(hwndDlg, (INT)wParam, wt, 123);
  39. // draw text
  40. SetTextColor(di->hDC, (di->itemState & ODS_SELECTED) ? RGB(220, 0, 0) : RGB(0, 0, 220));
  41. r = di->rcItem;
  42. r.left += 2;
  43. DrawTextW(di->hDC, wt, -1, &r, DT_VCENTER | DT_SINGLELINE);
  44. memset(&r, 0, sizeof(r));
  45. DrawTextW(di->hDC, wt, -1, &r, DT_SINGLELINE | DT_CALCRECT);
  46. // draw underline
  47. y = di->rcItem.bottom - ((di->rcItem.bottom - di->rcItem.top) - (r.bottom - r.top)) / 2 - 1;
  48. hPen = CreatePen(PS_SOLID, 0, (di->itemState & ODS_SELECTED) ? RGB(220, 0, 0) : RGB(0, 0, 220));
  49. hOldPen = (HPEN) SelectObject(di->hDC, hPen);
  50. MoveToEx(di->hDC, di->rcItem.left + 2, y, NULL);
  51. LineTo(di->hDC, di->rcItem.right + 2 - ((di->rcItem.right - di->rcItem.left) - (r.right - r.left)), y);
  52. SelectObject(di->hDC, hOldPen);
  53. DeleteObject(hPen);
  54. }
  55. }
  56. }