1
0

messageBox.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include "main.h"
  2. #include "./api__ml_online.h"
  3. #include "./resource.h"
  4. #include <windows.h>
  5. #include <strsafe.h>
  6. typedef struct __MESSAGEBOX
  7. {
  8. HWND hParent;
  9. LPCWSTR pszText;
  10. LPCWSTR pszCaption;
  11. UINT uType;
  12. LPCWSTR pszCheck;
  13. INT *checked;
  14. } MESSAGEBOX;
  15. static INT_PTR CALLBACK OmMessageBox_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  16. INT OmMessageBox(HWND hParent, LPCWSTR pszText, LPCWSTR pszCaption, UINT uType, LPCWSTR pszCheck, INT *checked)
  17. {
  18. MESSAGEBOX instance;
  19. instance.hParent = hParent;
  20. instance.pszText = pszText;
  21. instance.pszCaption = pszCaption;
  22. instance.uType = uType;
  23. instance.pszCheck = pszCheck;
  24. instance.checked = checked;
  25. return (INT)WASABI_API_DIALOGBOXPARAMW(IDD_MESSAGEBOX, hParent, OmMessageBox_DialogProc, (LPARAM)&instance);
  26. }
  27. static void OmMessgageBox_CenterWindow(HWND hwnd, HWND hCenter)
  28. {
  29. if (NULL == hwnd || NULL == hCenter)
  30. return;
  31. RECT centerRect, windowRect;
  32. if (!GetWindowRect(hwnd, &windowRect) || !GetWindowRect(hCenter, &centerRect))
  33. return;
  34. windowRect.left = centerRect.left + ((centerRect.right - centerRect.left) - (windowRect.right - windowRect.left))/2;
  35. windowRect.top = centerRect.top + ((centerRect.bottom - centerRect.top) - (windowRect.bottom - windowRect.top))/2;
  36. SetWindowPos(hwnd, NULL, windowRect.left, windowRect.top, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER);
  37. }
  38. static BOOL OmMessageBox_GetTextBox(HWND hwnd, LPCWSTR pszText, INT cchText, SIZE *sizeText)
  39. {
  40. HDC hdc = GetDCEx(hwnd, NULL, DCX_CACHE | DCX_NORESETATTRS);
  41. if (NULL == hdc) return FALSE;
  42. HFONT font = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0L);
  43. HFONT fontOrig = (HFONT)SelectObject(hdc, font);
  44. if (cchText < 0)
  45. cchText = lstrlen(pszText);
  46. BOOL resultOk = GetTextExtentPoint32(hdc, pszText, cchText, sizeText);
  47. SelectObject(hdc, fontOrig);
  48. ReleaseDC(hwnd, hdc);
  49. return resultOk;
  50. }
  51. static HICON OmMessageBox_GetIcon(UINT flags)
  52. {
  53. LPCWSTR iconName = NULL;
  54. switch(0x000000F0 & flags)
  55. {
  56. case MB_ICONHAND: iconName = IDI_HAND; break;
  57. case MB_ICONQUESTION: iconName = IDI_QUESTION; break;
  58. case MB_ICONEXCLAMATION:iconName = IDI_EXCLAMATION; break;
  59. case MB_ICONASTERISK: iconName = IDI_ASTERISK; break;
  60. }
  61. return (NULL != iconName) ? LoadIcon(NULL, iconName) : NULL;
  62. }
  63. static BOOL OmMessageBox_GetIconSize(UINT flags, SIZE *iconSize)
  64. {
  65. if (NULL == iconSize) return FALSE;
  66. iconSize->cx = 0;
  67. iconSize->cy = 0;
  68. HICON hIcon = OmMessageBox_GetIcon(flags);
  69. if (NULL == hIcon)
  70. return TRUE;
  71. ICONINFO iconInfo;
  72. if (!GetIconInfo(hIcon, &iconInfo) || FALSE == iconInfo.fIcon)
  73. return FALSE;
  74. BITMAP bm;
  75. if (NULL != iconInfo.hbmColor)
  76. {
  77. if (FALSE == GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bm))
  78. return FALSE;
  79. iconSize->cx = bm.bmWidth;
  80. iconSize->cy = bm.bmHeight;
  81. }
  82. else if (NULL != iconInfo.hbmMask)
  83. {
  84. if (FALSE == GetObject(iconInfo.hbmMask, sizeof(BITMAP), &bm))
  85. return FALSE;
  86. iconSize->cx = bm.bmWidth;
  87. iconSize->cy = bm.bmHeight/2;
  88. }
  89. return TRUE;
  90. }
  91. static BOOL OmMessageBox_GetButtonSize(HWND hwnd, SIZE *buttonSize)
  92. {
  93. if (NULL == buttonSize)
  94. return FALSE;
  95. if (FALSE != SendMessage(hwnd, (0x1600 + 0x0001) /*BCM_GETIDEALSIZE*/, 0, (LPARAM)buttonSize))
  96. return TRUE;
  97. return FALSE;
  98. }
  99. static INT_PTR OmMessageBox_OnInitDialog(HWND hwnd, HWND hFocus, LPARAM lParam)
  100. {
  101. MESSAGEBOX *pmb = (MESSAGEBOX*)lParam;
  102. SetWindowText(hwnd, pmb->pszCaption);
  103. SIZE textSize;
  104. SIZE maxSize;
  105. MONITORINFO mi;
  106. mi.cbSize = sizeof(MONITORINFO);
  107. HMONITOR hMonitor = MonitorFromWindow(pmb->hParent, MONITOR_DEFAULTTONEAREST);
  108. if (NULL != hMonitor &&
  109. GetMonitorInfo(hMonitor, &mi))
  110. {
  111. RECT rcFrame;
  112. SetRectEmpty(&rcFrame);
  113. AdjustWindowRectEx(&rcFrame, GetWindowStyle(hwnd), FALSE, GetWindowStyleEx(hwnd));
  114. maxSize.cx = mi.rcWork.right - mi.rcWork.left - (rcFrame.right - rcFrame.left) - 8*2;
  115. maxSize.cy = mi.rcWork.bottom - mi.rcWork.top - (rcFrame.bottom - rcFrame.top) - 8*2;
  116. }
  117. else
  118. {
  119. maxSize.cx = 1200;
  120. maxSize.cy = 800;
  121. }
  122. HWND hText = GetDlgItem(hwnd, IDC_TEXT);
  123. if (NULL != hText)
  124. {
  125. if (!OmMessageBox_GetTextBox(hText, pmb->pszText, -1, &textSize))
  126. ZeroMemory(&textSize, sizeof(SIZE));
  127. SetWindowPos(hText, NULL, 0, 0, textSize.cx, textSize.cy, SWP_NOACTIVATE | SWP_NOZORDER);
  128. }
  129. else
  130. ZeroMemory(&textSize, sizeof(SIZE));
  131. OmMessgageBox_CenterWindow(hwnd, pmb->hParent);
  132. SendMessage(hwnd, DM_REPOSITION, 0, 0L);
  133. return FALSE;
  134. }
  135. static void OmMessageBox_OnDestroy(HWND hwnd)
  136. {
  137. }
  138. static void OmMessageBox_OnCommand(HWND hwnd, INT commandId, INT eventId, HWND hControl)
  139. {
  140. switch(commandId)
  141. {
  142. case IDOK:
  143. case IDCANCEL:
  144. EndDialog(hwnd, commandId);
  145. break;
  146. }
  147. }
  148. static INT_PTR CALLBACK OmMessageBox_DialogProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  149. {
  150. switch(uMsg)
  151. {
  152. case WM_INITDIALOG: return OmMessageBox_OnInitDialog(hwnd, (HWND)wParam, lParam);
  153. case WM_DESTROY: OmMessageBox_OnDestroy(hwnd); break;
  154. case WM_COMMAND: OmMessageBox_OnCommand(hwnd, LOWORD(wParam), HIWORD(wParam), (HWND)lParam); break;
  155. }
  156. return 0;
  157. }