⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmessagebox.cpp

📁 读取XML信息
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			m_pDlgItemArray[i] = NULL;
		}
	}

	if (m_lpszMessage)
		delete [] m_lpszMessage;
	m_lpszMessage = NULL;

	if (m_lpszCaption)
		delete [] m_lpszCaption;
	m_lpszCaption = NULL;
}

///////////////////////////////////////////////////////////////////////////////
// AddCheckBox
void CXDialogTemplate::AddCheckBox(HDC hdc, 
								   int& x, 
								   int& y, 
								   CXRect& rect, 
								   CXRect& mbrect, 
								   CXRect& buttonrow, 
								   CXRect& checkboxrect, 
								   LPCTSTR lpszButtonCaption)
{
	x = 2 * ButtonSpacing + 5;
	y += ButtonHeight + (2 * ButtonSpacing);

	::DrawText(hdc, lpszButtonCaption, -1, &checkboxrect,
		DT_LEFT | DT_NOPREFIX | DT_CALCRECT | DT_SINGLELINE);

	int w = checkboxrect.Width();
	w += w/3;

	SetRect(&rect, x, y, x + w, y + DoNotAskAgainHeight);
	AddItem(CXDialogItem::CHECKBOX, IdDoNotAskAgian, &rect, lpszButtonCaption);
	buttonrow.bottom = y + DoNotAskAgainHeight;
	mbrect.bottom = buttonrow.bottom + SpacingSize;
	if (mbrect.Width() < (x + w))
		mbrect.right = mbrect.left + x + w;
}

//-[UK
void CXDialogTemplate::LoadUserDefinedButtonStrings( 
	const XMSGBOXPARAMS::CUserDefinedButtonCaptions& udcs )
{
	TRACE(_T("in CXDialogTemplate::LoadUserDefinedButtonStrings\n"));

	_tcscpy(szAbort,          udcs.szAbort          );
	_tcscpy(szCancel,         udcs.szCancel         );
	_tcscpy(szContinue,       udcs.szContinue       );
	_tcscpy(szDoNotAskAgain,  udcs.szDoNotAskAgain  );
	_tcscpy(szDoNotTellAgain, udcs.szDoNotTellAgain );
	_tcscpy(szDoNotShowAgain, udcs.szDoNotShowAgain );
	_tcscpy(szHelp,           udcs.szHelp           );
	_tcscpy(szIgnore,         udcs.szIgnore         );
	_tcscpy(szIgnoreAll,      udcs.szIgnoreAll      );
	_tcscpy(szNo,             udcs.szNo             );
	_tcscpy(szNoToAll,        udcs.szNoToAll        );
	_tcscpy(szOK,             udcs.szOK             );
	if (szReport[0] == _T('\0'))
		_tcscpy(szReport,     udcs.szReport			);
	_tcscpy(szRetry,          udcs.szRetry          );
	_tcscpy(szSkip,           udcs.szSkip           );
	_tcscpy(szSkipAll,        udcs.szSkipAll        );
	_tcscpy(szTryAgain,       udcs.szTryAgain       );
	_tcscpy(szYes,            udcs.szYes            );
	_tcscpy(szYesToAll,       udcs.szYesToAll       );
}
//-]UK

///////////////////////////////////////////////////////////////////////////////
// LoadButtonStrings
void CXDialogTemplate::LoadButtonStrings()
{
	TRACE(_T("in CXDialogTemplate::LoadButtonStrings\n"));
	_tcscpy(szAbort,          _T("&Abort"));
	_tcscpy(szCancel,         _T("Cancel"));
	_tcscpy(szContinue,       _T("&Continue"));
	_tcscpy(szDoNotAskAgain,  _T("Don't ask me again"));
	_tcscpy(szDoNotTellAgain, _T("Don't tell me again"));
	_tcscpy(szDoNotShowAgain, _T("Don't show again"));
	_tcscpy(szHelp,           _T("&Help"));
	_tcscpy(szIgnore,         _T("&Ignore"));
	_tcscpy(szIgnoreAll,      _T("I&gnore All"));
	_tcscpy(szNo,             _T("&No"));
	_tcscpy(szNoToAll,        _T("N&o to All"));
	_tcscpy(szOK,             _T("OK"));
	if (szReport[0] == _T('\0'))
		_tcscpy(szReport,     _T("Re&port"));
	_tcscpy(szRetry,          _T("&Retry"));
	_tcscpy(szSkip,           _T("&Skip"));
	_tcscpy(szSkipAll,        _T("S&kip All"));
	_tcscpy(szTryAgain,       _T("&Try Again"));
	_tcscpy(szYes,            _T("&Yes"));
	_tcscpy(szYesToAll,       _T("Y&es to All"));
}

///////////////////////////////////////////////////////////////////////////////
// LoadButtonStringsFromResources
void CXDialogTemplate::LoadButtonStringsFromResources(HINSTANCE hInstance)
{
	TRACE(_T("in CXDialogTemplate::LoadButtonStringsFromResources\n"));

	_ASSERTE(hInstance);

	LoadButtonStrings();	// initialize all strings in case LoadString fails

	if (::LoadString(hInstance, IDS_ABORT, szAbort, MaxButtonStringSize) == 0)
		_tcscpy(szAbort, _T("&Abort"));
	szAbort[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_CANCEL, szCancel, MaxButtonStringSize) == 0)
		_tcscpy(szCancel, _T("Cancel"));
	szCancel[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_CONTINUE, szContinue, MaxButtonStringSize) == 0)
		_tcscpy(szContinue, _T("&Continue"));
	szContinue[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_DONOTASKAGAIN, szDoNotAskAgain, MaxButtonStringSize) == 0)
		_tcscpy(szDoNotAskAgain, _T("Don't ask me again"));
	szDoNotAskAgain[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_DONOTTELLAGAIN, szDoNotTellAgain, MaxButtonStringSize) == 0)
		_tcscpy(szDoNotTellAgain, _T("Don't tell me again"));
	szDoNotTellAgain[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_DONOTSHOWAGAIN, szDoNotShowAgain, MaxButtonStringSize) == 0)
		_tcscpy(szDoNotShowAgain, _T("Don't show again"));
	szDoNotShowAgain[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_HELP, szHelp, MaxButtonStringSize) == 0)
		_tcscpy(szHelp, _T("&Help"));
	szHelp[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_IGNORE, szIgnore, MaxButtonStringSize) == 0)
		_tcscpy(szIgnore, _T("&Ignore"));
	szIgnore[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_IGNOREALL, szIgnoreAll, MaxButtonStringSize) == 0)
		_tcscpy(szIgnoreAll, _T("I&gnore All"));
	szIgnoreAll[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_NO, szNo, MaxButtonStringSize) == 0)
		_tcscpy(szNo, _T("&No"));
	szNo[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_NOTOALL, szNoToAll, MaxButtonStringSize) == 0)
		_tcscpy(szNoToAll, _T("N&o to All"));
	szNoToAll[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_OK, szOK, MaxButtonStringSize) == 0)
		_tcscpy(szOK, _T("OK"));
	szOK[MaxButtonStringSize-1] = _T('\0');

	if (szReport[0] == _T('\0'))
		if (::LoadString(hInstance, IDS_REPORT, szReport, MaxButtonStringSize) == 0)
			_tcscpy(szReport, _T("Re&port"));
	szReport[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_RETRY, szRetry, MaxButtonStringSize) == 0)
		_tcscpy(szRetry, _T("&Retry"));
	szRetry[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_SKIP, szSkip, MaxButtonStringSize) == 0)
		_tcscpy(szSkip, _T("&Skip"));
	szSkip[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_SKIPALL, szSkipAll, MaxButtonStringSize) == 0)
		_tcscpy(szSkipAll, _T("S&kip All"));
	szSkipAll[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_TRYAGAIN, szTryAgain, MaxButtonStringSize) == 0)
		_tcscpy(szTryAgain, _T("&Try Again"));
	szTryAgain[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_YES, szYes, MaxButtonStringSize) == 0)
		_tcscpy(szYes, _T("&Yes"));
	szYes[MaxButtonStringSize-1] = _T('\0');

	if (::LoadString(hInstance, IDS_YESTOALL, szYesToAll, MaxButtonStringSize) == 0)
		_tcscpy(szYesToAll, _T("Y&es to All"));
	szYesToAll[MaxButtonStringSize-1] = _T('\0');
}


///////////////////////////////////////////////////////////////////////////////
// MsgBoxDlgProc
BOOL CALLBACK CXDialogTemplate::MsgBoxDlgProc(HWND hwnd,
											  UINT message,
											  WPARAM wParam,
											  LPARAM lParam)
{
	CXDialogTemplate* Me = (CXDialogTemplate*) (UINT_PTR)::GetWindowLong(hwnd, GWL_USERDATA);
	HWND hwndChild;

	switch (message)
	{
		case WM_INITDIALOG:
		{
			::SetWindowLong(hwnd, GWL_USERDATA, (LONG)lParam);	// save it for the others
			Me = (CXDialogTemplate*) lParam;
			_ASSERTE(Me);

			HDC hdc = ::CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
			_ASSERTE(hdc);

			::SelectObject(hdc, Me->m_hFont);
			::DeleteDC(hdc);
			UINT nID;

			for (nID = FirstControlId; nID < Me->m_nMaxID; nID++)
			{
				hwndChild = ::GetDlgItem(hwnd, nID);
				if (::IsWindow(hwndChild))
					::SendMessage(hwndChild, WM_SETFONT, (WPARAM)Me->m_hFont, 0);
				else
					break;
			}
			for (nID = 1; nID <= ID_XMESSAGEBOX_LAST_ID; nID++)
			{
				hwndChild = ::GetDlgItem(hwnd, nID);
				if (hwndChild && ::IsWindow(hwndChild))
				{
					::SendMessage(hwndChild, WM_SETFONT, (WPARAM)Me->m_hFont, 0);

					// disbale all buttons if disabled seconds specified
					if (Me->m_nDisabledSeconds)
					{
						TCHAR szClassName[MAX_PATH];
						::GetClassName(hwndChild, szClassName, countof(szClassName)-2);
						if (_tcsicmp(szClassName, _T("Button")) == 0)
						{
							LONG nStyle = ::GetWindowLong(hwndChild, GWL_STYLE);
							nStyle |= WS_DISABLED;
							::SetWindowLong(hwndChild, GWL_STYLE, nStyle);
						}
						::EnableMenuItem(GetSystemMenu(hwnd, FALSE), SC_CLOSE, MF_GRAYED);
					}
				}
			}

			hwndChild = ::GetDlgItem(hwnd, IdExHelp);
			if (hwndChild && ::IsWindow(hwndChild))
				::SendMessage(hwndChild, WM_SETFONT, (WPARAM)Me->m_hFont, 0);

			hwndChild = ::GetDlgItem(hwnd, IdDoNotAskAgian);
			if (hwndChild && ::IsWindow(hwndChild))
			{
				::SendMessage(hwndChild, WM_SETFONT, (WPARAM)Me->m_hFont, 0);
				CheckDlgButton(hwnd, IdDoNotAskAgian, 0);
			}

			hwndChild = ::GetDlgItem(hwnd, IdExReport);
			if (hwndChild && ::IsWindow(hwndChild))
				::SendMessage(hwndChild, WM_SETFONT, (WPARAM)Me->m_hFont, 0);

			hwndChild = ::GetDlgItem(hwnd, Me->m_nDefId);
			if (hwndChild && ::IsWindow(hwndChild))
				::SetFocus(hwndChild);

			// disable close button just like real MessageBox
			if (!Me->Option(CancelOrOkButton))
				::EnableMenuItem(GetSystemMenu(hwnd, FALSE), SC_CLOSE, MF_GRAYED);

			if (Me->m_hIcon)
			{
				HWND hwndIcon;

				hwndIcon = ::GetDlgItem(hwnd, 1000);
				::SetWindowLong(hwndIcon, GWL_WNDPROC, (LONG)(UINT_PTR) IconProc);
				::SetWindowLong(hwndIcon, GWL_USERDATA, (LONG)(UINT_PTR) Me->m_hIcon);
			}

			if (Me->m_nStyle & MB_TOPMOST)
				::SetWindowPos(hwnd, HWND_TOPMOST, 0,0,0,0, SWP_NOMOVE|SWP_NOSIZE);

			if (Me->m_nStyle & MB_SETFOREGROUND)
				::SetForegroundWindow(hwnd);

			if (Me->m_X || Me->m_Y)
			{
				// caller has specified screen coordinates
				CXRect dlgRect;
				::GetWindowRect(hwnd, &dlgRect);
				::MoveWindow(hwnd, Me->m_X, Me->m_Y, dlgRect.Width(), dlgRect.Height(), TRUE);
			}
			else if (GetParent(hwnd) != NULL)
			{
				// code to center dialog (necessary if not called by MFC app)
				// [suggested by Tom Wright]
				CXRect mainRect, dlgRect;
				::GetWindowRect(::GetParent(hwnd), &mainRect);
				::GetWindowRect(hwnd, &dlgRect);
				int x = (mainRect.right  + mainRect.left)/2 - dlgRect.Width()/2;
				int y =	(mainRect.bottom + mainRect.top) /2 - dlgRect.Height()/2;
				::MoveWindow(hwnd, x, y, dlgRect.Width(), dlgRect.Height(), TRUE);
			}

			// display seconds countdown if timeout specified
			if (Me->m_nTimeoutSeconds > 0)
				::SetTimer(hwnd, 1, 1000, NULL);

			if (Me->m_nDisabledSeconds > 0)
				::SetTimer(hwnd, 2, Me->m_nDisabledSeconds*1000, NULL);

			return FALSE;
		}

		case WM_COMMAND:
		{
			// user clicked on something - stop the timer
			::KillTimer(hwnd, 1);

			if (Me->GetDefaultButtonId())
			{
				HWND hwndDefButton = ::GetDlgItem(hwnd, Me->GetDefaultButtonId());
				if (hwndDefButton && ::IsWindow(hwndDefButton))
				{
					if (Me->m_szDefaultButton[0] != _T('\0'))
					{
						::SetWindowText(hwndDefButton, Me->m_szDefaultButton);
					}
				}
			}

			switch(wParam)
			{
				case IDCLOSE:
					return TRUE;

				case IDCANCEL:
					if (Me->Option(CancelButton))
						::EndDialog(hwnd, IDCANCEL);
					else if (Me->Option(OkButton))
						::EndDialog(hwnd, IDOK);
					return TRUE;

				case IdExHelp:
				{
					TCHAR szBuf[_MAX_PATH*2];
					memset(szBuf, 0, sizeof(szBuf));
					::GetModuleFileName(NULL, szBuf, countof(szBuf) - 1);
					if (_tcslen(szBuf) > 0)
					{
						TCHAR *cp = _tcsrchr(szBuf, _T('.'));
						if (cp)
						{
							_tcscpy(cp, _T(".hlp"));
							::WinHelp(hwnd, szBuf,
								(Me->m_nHelpId == 0) ? HELP_PARTIALKEY : HELP_CONTEXT,
								Me->m_nHelpId);
						}
					}
					return FALSE;
				}

				case IdExReport:
				{
					if (Me->m_lpReportFunc)
						Me->m_lpReportFunc(Me->m_lpszMessage, Me->m_dwReportUserData);
					return FALSE;
				}

				case IdDoNotAskAgian: //IdDoNotAskAgian & DoNotTellAgain share the same id!!
					return FALSE;

				default:
					hwndChild = ::GetDlgItem(hwnd, IdDoNotAskAgian);
					BOOL bFlag = FALSE;

					if (hwndChild && ::IsWindow(hwndChild))
						bFlag = (BOOL)::SendMessage(hwndChild, BM_GETCHECK, 0, 0);

					if (Me->Option(DoNotAskAgain))
						wParam |= bFlag ? MB_DONOTASKAGAIN : 0;
					else if (Me->Option(DoNotTellAgain))
						wParam |= bFlag ? MB_DONOTTELLAGAIN : 0;
					else if (Me->Option(DoNotShowAgain))
						wParam |= bFlag ? MB_DONOTSHOWAGAIN : 0;

#ifndef XMESSAGEBOX_DO_NOT_SAVE_CHECKBOX

					// save return code in ini file if Do Not Ask style and
					// module name were specified

					if (bFlag && Me->m_lpszModule &&
						(Me->m_lpszModule[0] != _T('\0')))
					{
						TCHAR szPathName[MAX_PATH*2];

						// get full path to ini file
						szPathName[0] = _T('\0');
						::GetModuleFileName(NULL, szPathName, countof(szPathName)-1);

						TCHAR *cp = _tcsrchr(szPathName, _T('\\'));
						if (cp != NULL)
							*(cp+1) = _T('\0');
						_tcscat(szPathName, XMESSAGEBOX_INI_FILE);

						// key is module name and line
						TCHAR szKey[MAX_PATH*2];
						_tcscpy(szKey, Me->m_lpszModule);
						TRACE(_T("szKey=<%s>\n"), szKey);

						encode(szKey);		// simple encoding to obscure module name

						TCHAR szLine[100];
						szLine[0] = _T('\0');
						_tcscat(szKey, _itot(Me->m_nLine, szLine, 10));

						TRACE(_T("szKey=<%s>\n"), szKey);

#ifndef XMESSAGEBOX_USE_PROFILE_FILE

						WriteRegistry(Me->m_szCompanyName, szKey, (DWORD)wParam);

#else

						// data string is hex value of XMessageBox return code
						TCHAR szData[100];
						_stprintf(szData, _T("%08X"), wParam);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -