示例#1
0
void CKofDlgImpl::OnChangeVisualManager()
{
	if (m_Dlg.GetSafeHwnd () == NULL)
	{
		return;
	}

	CRect rectWindow;
	m_Dlg.GetWindowRect (rectWindow);

	BOOL bZoomed = m_Dlg.IsZoomed ();

	if (IsOwnerDrawCaption ())
	{
		BOOL bChangeBorder = FALSE;

		if ((m_Dlg.GetStyle () & WS_BORDER) == WS_BORDER && m_bHasBorder)
		{
			bChangeBorder = TRUE;
			m_bWindowPosChanging = TRUE;
			m_Dlg.ModifyStyle (WS_BORDER, 0, SWP_FRAMECHANGED);
			m_bWindowPosChanging = FALSE;
		}

		m_bIsWindowRgn = CKofStyleHelper::GetInstance()->OnSetWindowRegion (
			&m_Dlg, rectWindow.Size ());

		if (bZoomed && bChangeBorder)
		{
			m_Dlg.ShowWindow (SW_MINIMIZE);
			m_Dlg.ShowWindow (SW_MAXIMIZE);
		}
	}
	else
	{
		BOOL bChangeBorder = FALSE;

		if ((m_Dlg.GetStyle () & WS_BORDER) == 0 && m_bHasBorder)
		{
			bChangeBorder = TRUE;
			m_bWindowPosChanging = TRUE;
			m_Dlg.ModifyStyle (0, WS_BORDER, SWP_FRAMECHANGED);
			m_bWindowPosChanging = FALSE;
		}

		if (m_bIsWindowRgn)
		{
			m_bIsWindowRgn = FALSE;
			m_Dlg.SetWindowRgn (NULL, TRUE);
		}

		if (bZoomed && bChangeBorder)
		{
			NCCALCSIZE_PARAMS params;
			ZeroMemory(&params, sizeof (NCCALCSIZE_PARAMS));
			params.rgrc[0].left   = rectWindow.left;
			params.rgrc[0].top    = rectWindow.top;
			params.rgrc[0].right  = rectWindow.right;
			params.rgrc[0].bottom = rectWindow.bottom;

			m_Dlg.CalcWindowRect (&params.rgrc[0], CFrameWnd::adjustBorder);

			if ((m_Dlg.GetStyle () & WS_CAPTION) == WS_CAPTION)
			{
				params.rgrc[0].top += GetCaptionHeight ();
			}

			m_Dlg.SetWindowPos (NULL, params.rgrc[0].left, params.rgrc[0].top, 
				params.rgrc[0].right - params.rgrc[0].left, params.rgrc[0].bottom - params.rgrc[0].top,
				SWP_NOACTIVATE | SWP_NOZORDER);
		}
		else
		{
			m_Dlg.SetWindowPos (NULL, -1, -1, rectWindow.Width () + 1, rectWindow.Height () + 1,
				SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
			m_Dlg.SetWindowPos (NULL, -1, -1, rectWindow.Width (), rectWindow.Height (),
				SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
		}
	}

	UpdateCaption ();
	UpdateCaptionButtons();
}
void CBCGPMessageBox::PreInitDialog ()
{
	CBCGPDialog::PreInitDialog ();

	// Setting font

	NONCLIENTMETRICS ncm;
	ZeroMemory (&ncm, sizeof (ncm));
	ncm.cbSize = sizeof (ncm);
	::SystemParametersInfo (SPI_GETNONCLIENTMETRICS, 0, (LPVOID)&ncm, 0);
	m_fntText.CreateFontIndirect (&ncm.lfMessageFont);

	// Calculating gaps, spaces and margins

	{
		MapDialogRect (&m_rectClientMargins);

		CRect rectTemp (m_cxIconSpacing, m_cyVerticalSpacing, m_cxButtonSpacing, m_cyCheckBoxHeight);
		MapDialogRect (&rectTemp);
		m_cxIconSpacing = rectTemp.left;
		m_cyVerticalSpacing = rectTemp.top;
		m_cxButtonSpacing = rectTemp.right;
		m_cyCheckBoxHeight = rectTemp.bottom;

		rectTemp.TopLeft () = m_szButton;
		MapDialogRect (&rectTemp);
		m_szButton = rectTemp.TopLeft ();
	}

	// Creating buttons

	m_bHasCancelButton = false;
	m_ptNextButtonPos.x = 0;
	m_ptNextButtonPos.y = 0;

	HINSTANCE hInst = ::LoadLibrary (_T("user32.dll"));

	if (!m_Params.bIgnoreStandardButtons)
	{
		switch (m_Params.dwStyle & MB_TYPEMASK)
		{
		case MB_OK:
			AddButton (IDOK, hInst);
			break;
		case MB_OKCANCEL:
			AddButton (IDOK, hInst);
			AddButton (IDCANCEL, hInst);
			break;
		case MB_ABORTRETRYIGNORE:
			AddButton (IDABORT, hInst);
			AddButton (IDRETRY, hInst);
			AddButton (IDIGNORE, hInst);
			break;
		case MB_YESNO:
			AddButton (IDYES, hInst);
			AddButton (IDNO, hInst);
			break;
		case MB_YESNOCANCEL:
			AddButton (IDYES, hInst);
			AddButton (IDNO, hInst);
			AddButton (IDCANCEL, hInst);
			break;
		case MB_RETRYCANCEL:
			AddButton (IDRETRY, hInst);
			AddButton (IDCANCEL, hInst);
			break;

		case MB_CANCELTRYCONTINUE:
			AddButton (IDCANCEL, hInst);
			AddButton (IDTRYAGAIN, hInst);
			AddButton (IDCONTINUE, hInst);
			break;
		}
	}

	for (int iBtn = 0; iBtn < BCGP_MSGBOXPARAMS::UserButtonsCount; ++iBtn)
	{
		if (m_Params.uiUserButtonID [iBtn] != 0)
		{
			LPCTSTR pszCaption = m_Params.lpszUserButtonText[iBtn];
			AddButton (m_Params.uiUserButtonID [iBtn], pszCaption == NULL ? hInst : m_Params.hInstance, pszCaption);
		}
	}

	if (hInst != NULL)
	{
		::FreeLibrary (hInst);
	}

	int nBtnCount = (int)m_arrButtons.GetSize ();
	ASSERT (nBtnCount != 0);

	if (nBtnCount == 1 && (m_Params.dwStyle & MB_TYPEMASK) == MB_OK)
	{
		m_bHasCancelButton = true;
	}

	// Calculate window size

	int nMinClientWidth = m_ptNextButtonPos.x;

	CRect rectScreen;
	::SystemParametersInfo (SPI_GETWORKAREA, 0, (LPVOID)&rectScreen, 0);

	int nMaxMessageBoxWidth = (rectScreen.Width () / 2) + 80;

	m_rectText.SetRect (0, 0, nMaxMessageBoxWidth, rectScreen.Height() - m_szButton.cy - m_cxButtonSpacing);
	if (!m_rectIcon.IsRectEmpty ())
	{
		m_rectText.right -= (m_rectIcon.Width () + m_cxIconSpacing);
	}


	HDC hdcScreen = ::GetDC (NULL); // Screen DC
	HFONT hOldFont =  (HFONT)::SelectObject (hdcScreen, m_fntText);

	m_dwDrawTextFlags = DT_NOPREFIX | DT_WORDBREAK | DT_EXPANDTABS;

	::DrawText (hdcScreen, m_strMessageText, -1, &m_rectText, m_dwDrawTextFlags | DT_CALCRECT);

	m_dwDrawTextFlags |= (m_bRightAlignment ? DT_RIGHT : DT_LEFT);

	// Calculate minimal width for message box window

	if (m_Params.bShowCheckBox)
	{
		SIZE szCheckBox;
		::GetTextExtentPoint32 (hdcScreen, m_strCheckBox, m_strCheckBox.GetLength (), &szCheckBox);
		szCheckBox.cx += 2 * m_cyCheckBoxHeight;
		if (m_hMessageIcon != NULL)
		{
			szCheckBox.cx += m_rectIcon.Width ();
		}
		nMinClientWidth = max (nMinClientWidth, szCheckBox.cx);
	}

	::SelectObject (hdcScreen, hOldFont);
	::ReleaseDC (NULL, hdcScreen);

	CSize szTotal (m_rectText.Width (), m_rectText.Height ());
	if (m_hMessageIcon != NULL)
	{
		CSize szTextOffset (m_rectIcon.Width () + m_cxIconSpacing, 0);

		if (m_rectIcon.Height () > m_rectText.Height ())
		{
			// Center text vertically against icon
			szTextOffset.cy = (m_rectIcon.Height () - m_rectText.Height()) / 2;
		}

		m_rectText.OffsetRect (szTextOffset);

		szTotal.cx += m_rectIcon.Width () + m_cxIconSpacing;
		szTotal.cy = max (szTotal.cy, m_rectIcon.Height ());
	}

	if (szTotal.cx < nMinClientWidth)
	{
		szTotal.cx = nMinClientWidth;
	}
	szTotal.cy += m_cyVerticalSpacing;

	if (m_bRightAlignment)
	{
		m_rectText.OffsetRect (szTotal.cx - m_rectText.right, 0);
	}

	// Adjusting buttons

	int yButtons = szTotal.cy + m_rectClientMargins.top;

	// Add check box if necessary

	m_cySeparatorLine = -1;

	if (m_Params.bShowCheckBox)
	{
		int xCheckBox = m_rectClientMargins.left;
		if (m_hMessageIcon != NULL)
		{
			xCheckBox += m_rectIcon.Width () + m_cxIconSpacing;
		}

		CRect rectCheckBox (xCheckBox, yButtons, m_rectClientMargins.left + szTotal.cx, yButtons + m_cyCheckBoxHeight);
		int yOffset = rectCheckBox.Height () + m_cyVerticalSpacing;

		if (m_Params.bShowSeparator)
		{
			m_cySeparatorLine = rectCheckBox.top;
			rectCheckBox.OffsetRect (0, m_cyVerticalSpacing);
			yOffset += m_cyVerticalSpacing;
		}

		m_wndDontAskCheckBox.Create (m_strCheckBox,
			WS_VISIBLE | WS_CHILD | WS_TABSTOP | BS_AUTOCHECKBOX,
			rectCheckBox, this, 0);

		if (m_Params.bIsChecked)
		{
			m_wndDontAskCheckBox.SetCheck(TRUE);
		}

		yButtons += yOffset;
		szTotal.cy += yOffset;
	}

	m_rectIcon.OffsetRect (m_rectClientMargins.left, m_rectClientMargins.top);
	m_rectText.OffsetRect (m_rectClientMargins.left, m_rectClientMargins.top);

	CRect rectButtons (m_rectClientMargins.left, yButtons, m_rectClientMargins.right + szTotal.cx, yButtons);
	AdjustControls (&m_arrButtons[0], (UINT)m_arrButtons.GetSize (), rectButtons, TA_CENTER);

	szTotal.cy += m_szButton.cy; // place for buttons

	CRect rectClient (0, 0, szTotal.cx, szTotal.cy);
	rectClient.InflateRect (m_rectClientMargins);

	AdjustWindowRectEx (&rectClient, 
		::GetWindowLong (GetSafeHwnd (), GWL_STYLE) | WS_CAPTION | WS_BORDER,
		false,
		::GetWindowLong (GetSafeHwnd (), GWL_EXSTYLE)
		);

	SetWindowPos (NULL, 0, 0, rectClient.Width (), rectClient.Height (),
		SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOSENDCHANGING);

	if (!m_bHasCancelButton)
	{
		GetSystemMenu (FALSE)->EnableMenuItem (SC_CLOSE, MF_GRAYED);
		UpdateCaptionButtons();
	}
}