MessageBox.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #include "StdAfx.h"
  2. #include "Resource.h"
  3. #include "Information.h"
  4. //////////////////////////////////////////////////////////////////////////////////
  5. //时间标识
  6. #define IDI_MESSAGE 100 //消息标识
  7. //窗口位置
  8. #define SMB_WINDOW_WIDTH 230 //最小宽度
  9. //图标偏移
  10. #define SMB_ICON_LEFT 20 //图标偏移
  11. #define SMB_ICON_WIDTH 32 //图标宽度
  12. #define SMB_ICON_HEIGHT 32 //图标高度
  13. //字符偏移
  14. #define SMB_STRING_TOP 25 //字符偏移
  15. #define SMB_STRING_LEFT 20 //字符偏移
  16. #define SMB_STRING_RIGHT 40 //字符偏移
  17. #define SMB_STRING_WIDTH 600 //最大宽度
  18. #define SMB_STRING_HEIGHT 600 //最大宽度
  19. //按钮偏移
  20. #define SMB_BUTTON_TOP 25 //按钮偏移
  21. #define SMB_BUTTON_LEFT 12 //按钮偏移
  22. #define SMB_BUTTON_RIGHT 12 //按钮偏移
  23. #define SMB_BUTTON_BOTTOM 12 //按钮偏移
  24. #define SMB_BUTTON_SPACE 20 //按钮间隔
  25. #define SMB_BUTTON_WIDTH 70 //按钮偏移
  26. #define SMB_BUTTON_HEIGHT 22 //按钮偏移
  27. //////////////////////////////////////////////////////////////////////////////////
  28. BEGIN_MESSAGE_MAP(CInformation, CSkinDialog)
  29. ON_WM_PAINT()
  30. ON_WM_TIMER()
  31. END_MESSAGE_MAP()
  32. //////////////////////////////////////////////////////////////////////////////////
  33. //构造函数
  34. CInformation::CInformation(CWnd * pParentWnd) : CSkinDialog(IDD_MESSAGE_BOX,pParentWnd)
  35. {
  36. //设置变量
  37. m_uType=MB_OK;
  38. //辅助变量
  39. m_nElapse=0;
  40. m_nBtCount=0;
  41. m_hResInstance=NULL;
  42. return;
  43. }
  44. //析构函数
  45. CInformation::~CInformation()
  46. {
  47. }
  48. //初始化消息
  49. BOOL CInformation::OnInitDialog()
  50. {
  51. __super::OnInitDialog();
  52. //设置标题
  53. SetWindowText(m_strCaption);
  54. //设置资源
  55. AfxSetResourceHandle(GetModuleHandle(NULL));
  56. //获取信息
  57. INT nTBorder=m_SkinAttribute.m_EncircleInfoView.nTBorder;
  58. INT nBBorder=m_SkinAttribute.m_EncircleInfoView.nBBorder;
  59. INT nLBorder=m_SkinAttribute.m_EncircleInfoView.nLBorder;
  60. INT nRBorder=m_SkinAttribute.m_EncircleInfoView.nRBorder;
  61. //按钮区域
  62. CreateBoxButton();
  63. INT nButtonAreaHeight=SMB_BUTTON_HEIGHT+SMB_BUTTON_TOP+SMB_BUTTON_BOTTOM;
  64. INT nButtonAreaWidth=m_nBtCount*SMB_BUTTON_WIDTH+(m_nBtCount+1)*SMB_BUTTON_SPACE+SMB_BUTTON_LEFT+SMB_BUTTON_RIGHT;
  65. //字符空间
  66. CClientDC ClientDC(this);
  67. CRect rcString(0,0,SMB_STRING_WIDTH,SMB_STRING_HEIGHT);
  68. ClientDC.SelectObject(CSkinResourceManager::GetInstance()->GetDefaultFont());
  69. ClientDC.DrawText(m_strString,rcString,DT_CALCRECT|DT_EXPANDTABS|DT_NOCLIP|DT_WORD_ELLIPSIS);
  70. //字符区域
  71. INT nStringAreaHeight=rcString.Height()+SMB_STRING_TOP;
  72. INT nStringAreaWidth=rcString.Width()+SMB_ICON_LEFT+SMB_ICON_WIDTH+SMB_STRING_LEFT+SMB_STRING_RIGHT;
  73. //窗口区域
  74. INT nWindowAreaHeight=nStringAreaHeight+nButtonAreaHeight;
  75. INT nWindowAreaWidth=__max(SMB_WINDOW_WIDTH,__max(nStringAreaWidth,nButtonAreaWidth));
  76. //输出位置
  77. if (nWindowAreaWidth>nStringAreaWidth)
  78. {
  79. m_rcString.top=SMB_STRING_TOP+nTBorder;
  80. m_rcString.left=SMB_ICON_LEFT+SMB_ICON_WIDTH+SMB_STRING_LEFT+(nWindowAreaWidth-nStringAreaWidth)/2;
  81. m_rcString.right=m_rcString.left+rcString.Width();
  82. m_rcString.bottom=m_rcString.top+rcString.Height()+nTBorder;
  83. }
  84. else
  85. {
  86. m_rcString.top=SMB_STRING_TOP+nTBorder;
  87. m_rcString.left=SMB_ICON_LEFT+SMB_ICON_WIDTH+SMB_STRING_LEFT;
  88. m_rcString.right=m_rcString.left+rcString.Width();
  89. m_rcString.bottom=m_rcString.top+rcString.Height();
  90. }
  91. //调整窗口
  92. INT nWindowWidth=nWindowAreaWidth+nLBorder+nRBorder;
  93. INT nWindowHeight=nWindowAreaHeight+nTBorder+nBBorder;
  94. SetWindowPos(NULL,0,0,nWindowWidth,nWindowHeight,SWP_NOMOVE|SWP_NOZORDER|SWP_NOCOPYBITS);
  95. //计算位置
  96. INT nYButton=nTBorder+nBBorder+nWindowAreaHeight-SMB_BUTTON_BOTTOM-SMB_BUTTON_HEIGHT;
  97. INT nXButton=(nWindowAreaWidth-m_nBtCount*SMB_BUTTON_WIDTH-(m_nBtCount+1)*SMB_BUTTON_SPACE)/2+SMB_BUTTON_SPACE;
  98. //调整按钮
  99. for (UINT i=0;i<m_nBtCount;i++)
  100. {
  101. m_btButton[i].SetWindowPos(NULL,nXButton,nYButton,0,0,SWP_NOSIZE|SWP_NOZORDER|SWP_SHOWWINDOW);
  102. nXButton+=SMB_BUTTON_SPACE+SMB_BUTTON_WIDTH;
  103. }
  104. //默认按钮
  105. UINT uDefaultIndex=(m_uType&MB_DEFMASK)>>8;
  106. if (uDefaultIndex>=m_nBtCount) uDefaultIndex=0;
  107. m_btButton[uDefaultIndex].SetFocus();
  108. m_btButton[uDefaultIndex].SetButtonStyle(m_btButton[uDefaultIndex].GetButtonStyle()|BS_DEFPUSHBUTTON);
  109. //居中窗口
  110. SetActiveWindow();
  111. SetForegroundWindow();
  112. CenterWindow(GetParent());
  113. //设置时间
  114. if (m_nElapse!=0) SetTimer(IDI_MESSAGE,1000,NULL);
  115. return FALSE;
  116. }
  117. //命令消息
  118. BOOL CInformation::OnCommand(WPARAM wParam, LPARAM lParam)
  119. {
  120. //按钮标识
  121. UINT uButtonID=LOWORD(wParam);
  122. //存在判断
  123. for (UINT i=0;i<m_nBtCount;i++)
  124. {
  125. //获取标识
  126. UINT uExistID=GetWindowLong(m_btButton[i],GWL_ID);
  127. //标识处理
  128. if (uButtonID==uExistID)
  129. {
  130. EndDialog(uButtonID);
  131. return TRUE;
  132. }
  133. }
  134. //默认处理
  135. if (uButtonID==IDOK)
  136. {
  137. UINT uDefaultIndex=(m_uType&MB_DEFMASK)>>8;
  138. if (uDefaultIndex>=m_nBtCount) uDefaultIndex=0;
  139. EndDialog(GetWindowLong(m_btButton[uDefaultIndex],GWL_ID));
  140. return TRUE;
  141. }
  142. //取消处理
  143. if (uButtonID==IDCANCEL)
  144. {
  145. EndDialog(GetWindowLong(m_btButton[m_nBtCount-1],GWL_ID));
  146. return TRUE;
  147. }
  148. //默认处理
  149. EndDialog(uButtonID);
  150. return TRUE;
  151. }
  152. //显示消息
  153. INT CInformation::ShowMessageBox(LPCTSTR pszString, UINT uType, UINT nElapse)
  154. {
  155. return ShowMessageBox(TEXT("系统提示"),pszString,uType,nElapse);
  156. }
  157. //显示消息
  158. INT CInformation::ShowMessageBox(LPCTSTR pszCaption, LPCTSTR pszString, UINT uType, UINT nElapse)
  159. {
  160. //设置变量
  161. m_uType=uType;
  162. m_nElapse=nElapse;
  163. m_strString=pszString;
  164. m_strCaption=pszCaption;
  165. //设置资源
  166. m_hResInstance=AfxGetResourceHandle();
  167. AfxSetResourceHandle(GetModuleHandle(SHARE_CONTROL_DLL_NAME));
  168. //显示窗口
  169. INT nResult=(INT)DoModal();
  170. return nResult;
  171. }
  172. //激活按钮
  173. VOID CInformation::CreateBoxButton()
  174. {
  175. //变量定义
  176. UINT uButtonID[3]={0,0,0};
  177. LPCTSTR pszString[3]={NULL,NULL,NULL};
  178. //设置按钮
  179. switch (m_uType&MB_TYPEMASK)
  180. {
  181. case MB_OKCANCEL:
  182. {
  183. m_nBtCount=2;
  184. uButtonID[0]=IDOK;
  185. uButtonID[1]=IDCANCEL;
  186. pszString[0]=TEXT("确定(&O)");
  187. pszString[1]=TEXT("取消(&C)");
  188. break;
  189. }
  190. case MB_YESNO:
  191. {
  192. m_nBtCount=2;
  193. uButtonID[0]=IDYES;
  194. uButtonID[1]=IDNO;
  195. pszString[0]=TEXT("是(&Y)");
  196. pszString[1]=TEXT("否(&N)");
  197. break;
  198. }
  199. case MB_YESNOCANCEL:
  200. {
  201. m_nBtCount=3;
  202. uButtonID[0]=IDYES;
  203. uButtonID[1]=IDNO;
  204. uButtonID[2]=IDCANCEL;
  205. pszString[0]=TEXT("是(&Y)");
  206. pszString[1]=TEXT("否(&N)");
  207. pszString[2]=TEXT("取消(&C)");
  208. break;
  209. }
  210. case MB_RETRYCANCEL:
  211. {
  212. m_nBtCount=2;
  213. uButtonID[0]=IDRETRY;
  214. uButtonID[1]=IDCANCEL;
  215. pszString[0]=TEXT("重试(&R)");
  216. pszString[1]=TEXT("取消(&C)");
  217. break;
  218. }
  219. case MB_ABORTRETRYIGNORE:
  220. {
  221. m_nBtCount=3;
  222. uButtonID[0]=IDABORT;
  223. uButtonID[1]=IDRETRY;
  224. uButtonID[2]=IDIGNORE;
  225. pszString[0]=TEXT("中止(&A)");
  226. pszString[1]=TEXT("重试(&R)");
  227. pszString[2]=TEXT("忽略(&I)");
  228. break;
  229. }
  230. default:
  231. {
  232. m_nBtCount=1;
  233. uButtonID[0]=IDOK;
  234. pszString[0]=TEXT("确定(&O)");
  235. break;
  236. }
  237. }
  238. //显示按钮
  239. CRect rcButton(0,0,SMB_BUTTON_WIDTH,SMB_BUTTON_HEIGHT);
  240. for (UINT i=0;i<m_nBtCount;i++) m_btButton[i].Create(pszString[i],WS_CHILD,rcButton,this,uButtonID[i]);
  241. return;
  242. }
  243. //绘画消息
  244. VOID CInformation::OnPaint()
  245. {
  246. CPaintDC dc(this);
  247. //获取位置
  248. CRect rcClient;
  249. GetClientRect(&rcClient);
  250. //加载图标
  251. HICON hIcon=NULL;
  252. UINT uIconType=(m_uType&MB_ICONMASK);
  253. if (uIconType==MB_ICONHAND) hIcon=LoadIcon(NULL,IDI_HAND);
  254. else if (uIconType==MB_ICONQUESTION) hIcon=LoadIcon(NULL,IDI_QUESTION);
  255. else if (uIconType==MB_ICONEXCLAMATION) hIcon=LoadIcon(NULL,IDI_EXCLAMATION);
  256. else hIcon=LoadIcon(NULL,IDI_ASTERISK);
  257. //绘画图标
  258. INT nYPos=(m_rcString.bottom+m_rcString.top)/2-SMB_ICON_HEIGHT/2;
  259. DrawIconEx(dc,SMB_ICON_LEFT,nYPos,hIcon,SMB_ICON_WIDTH,SMB_ICON_HEIGHT,0,NULL,DI_NORMAL);
  260. //绘画字体
  261. dc.SetBkMode(TRANSPARENT);
  262. dc.SetTextColor(RGB(10,10,10));
  263. dc.SelectObject(CSkinResourceManager::GetInstance()->GetDefaultFont());
  264. dc.DrawText(m_strString,&m_rcString,DT_EXPANDTABS|DT_NOCLIP|DT_WORD_ELLIPSIS);
  265. return;
  266. }
  267. //时间消息
  268. VOID CInformation::OnTimer(UINT_PTR nIDEvent)
  269. {
  270. //倒数时间
  271. if (nIDEvent==IDI_MESSAGE)
  272. {
  273. //删除判断
  274. if (m_nElapse==0)
  275. {
  276. PostMessage(WM_CLOSE,0,0);
  277. return;
  278. }
  279. //设置界面
  280. TCHAR szTitle[32]=TEXT("");
  281. _sntprintf(szTitle,CountArray(szTitle),TEXT("%s (%ld 秒)"),m_strCaption,m_nElapse--);
  282. SetWindowText(szTitle);
  283. return;
  284. }
  285. __super::OnTimer(nIDEvent);
  286. }
  287. //////////////////////////////////////////////////////////////////////////////////
  288. //显示消息
  289. extern "C" SHARE_CONTROL_CLASS INT ShowInformation(LPCTSTR pszString, UINT uType, UINT nElapse)
  290. {
  291. CInformation Information;
  292. return Information.ShowMessageBox(pszString,uType,nElapse);
  293. }
  294. //显示消息
  295. extern "C" SHARE_CONTROL_CLASS INT ShowInformationEx(LPCTSTR pszCaption, LPCTSTR pszString, UINT uType, UINT nElapse)
  296. {
  297. CInformation Information;
  298. return Information.ShowMessageBox(pszCaption,pszString,uType,nElapse);
  299. }
  300. //////////////////////////////////////////////////////////////////////////////////