//********************************************************************* void CCxNDArrayDimGrid::SetRowHeight () { if (m_bIsPrinting) { ASSERT_VALID (m_pPrintDC); // map to printer metrics HDC hDCFrom = ::GetDC(NULL); int nYMul = m_pPrintDC->GetDeviceCaps(LOGPIXELSY); // pixels in print dc int nYDiv = ::GetDeviceCaps(hDCFrom, LOGPIXELSY); // pixels in screen dc ::ReleaseDC(NULL, hDCFrom); TEXTMETRIC tm; m_pPrintDC->GetTextMetrics (&tm); m_PrintParams.m_nRowHeight = tm.tmHeight + ::MulDiv (2 * TEXT_VMARGIN, nYMul, nYDiv); m_PrintParams.m_nLargeRowHeight = m_PrintParams.m_nRowHeight; } else { CClientDC dc (this); HFONT hfontOld = SetCurrFont (&dc); TEXTMETRIC tm; dc.GetTextMetrics (&tm); m_nRowHeight = tm.tmHeight + 2 * TEXT_VMARGIN; m_nLargeRowHeight = m_nRowHeight; ::SelectObject (dc.GetSafeHdc (), hfontOld); } }
void CBigIcon::CenterText(CClientDC &dc, LPCSTR lpszStatus, int top) { int nMargin; CRect rect; TEXTMETRIC tm; // Leave a horizontal margin equal to the widest character VERIFY(dc.GetTextMetrics(&tm)); nMargin = tm.tmMaxCharWidth; // Compute the opaque rect rect.left = nMargin; rect.right = m_sizeBitmap.cx - nMargin; rect.top = top; rect.bottom = rect.top + tm.tmHeight; // We need to compute where to draw the text so it is centered // horizontally int x = rect.left; CSize extent = CIntlWin::GetTextExtent(0, dc.m_hDC, lpszStatus, XP_STRLEN(lpszStatus)); if (extent.cx < rect.Width()) x += (rect.Width() - extent.cx) / 2; // Draw opaquely so we can avoid erasing the old text dc.ExtTextOut(x, rect.top, ETO_OPAQUE, &rect, lpszStatus, strlen(lpszStatus), NULL); }
//****************************************************************************** void CBCGPRibbonCommandsListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS) { ASSERT (lpMIS != NULL); CClientDC dc (this); CFont* pOldFont = (CFont*) dc.SelectStockObject (DEFAULT_GUI_FONT); ASSERT_VALID (pOldFont); TEXTMETRIC tm; dc.GetTextMetrics (&tm); lpMIS->itemHeight = tm.tmHeight + 6; dc.SelectObject (pOldFont); }
int myframe::OnCreate(LPCREATESTRUCT lp) { if (CWnd::OnCreate (lp) == -1) return -1; CClientDC dc (this); CButton *but; but=new CButton; but->Create("Show fonts",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,10,100,50),this,10); fonttitle.Create("",WS_VISIBLE | WS_CHILD,CRect(50,100,500,500),this); TEXTMETRIC tm; dc.GetTextMetrics (&tm); m_cxChar = tm.tmAveCharWidth; m_cyChar = tm.tmHeight + tm.tmExternalLeading; return 0; }
BOOL CDlgCalcDlg::OnInitDialog() { CDialog::OnInitDialog(); // // Set the application's icon. // SetIcon(m_hIcon, TRUE); SetIcon(m_hIcon, FALSE); // // Remove the Size and Maximize commands from the system menu. // CMenu* pMenu = GetSystemMenu (FALSE); pMenu->DeleteMenu (SC_SIZE, MF_BYCOMMAND); pMenu->DeleteMenu (SC_MAXIMIZE, MF_BYCOMMAND); // // Initialize m_rect with the coordinates of the control representing // the calculator's output window. Then destroy the control. // CWnd* pWnd = GetDlgItem (IDC_DISPLAYRECT); pWnd->GetWindowRect (&m_rect); pWnd->DestroyWindow (); ScreenToClient (&m_rect); // // Initialize m_cxChar and m_cyChar with the average character width // and height. // TEXTMETRIC tm; CClientDC dc (this); dc.GetTextMetrics (&tm); m_cxChar = tm.tmAveCharWidth; m_cyChar = tm.tmHeight - tm.tmDescent; // // Initialize the calculator's output window and return. // DisplayXRegister (); return TRUE; }
int CUndoBar::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CBCGPPopupMenuBar::OnCreate(lpCreateStruct) == -1) return -1; CFont* pMenuFont = (CFont*) &CBCGPMenuBar::GetMenuFont (); //------------------ // Set label height: //------------------ CClientDC dc (this); CFont* pOldFont = dc.SelectObject (pMenuFont); TEXTMETRIC tm; dc.GetTextMetrics (&tm); m_nLabelHeight = tm.tmHeight + 2; dc.SelectObject (pOldFont); CRect rectDummy (0, 0, 0, 0); m_wndList.Create (WS_VISIBLE | WS_CHILD | LBS_NOINTEGRALHEIGHT | LBS_NOTIFY | WS_VSCROLL | LBS_MULTIPLESEL, rectDummy, this, ID_LIST); m_wndList.ModifyStyleEx (0, WS_EX_CLIENTEDGE); m_wndList.SetFont (pMenuFont); CUndoButton* pUndoButton = GetParentButton (); ASSERT_VALID (pUndoButton); for (POSITION pos = pUndoButton->m_lstActions.GetHeadPosition (); pos != NULL;) { m_wndList.AddString (pUndoButton->m_lstActions.GetNext (pos)); } return 0; }
int CMainWindow::OnCreate (LPCREATESTRUCT lpcs) { if (CWnd::OnCreate (lpcs) == -1) return -1; // // Create an 8-point MS Sans Serif font to use in the controls. // m_fontMain.CreatePointFont (80, _T ("MS Sans Serif")); // // Compute the average width and height of a character in the font. // CClientDC dc (this); CFont* pOldFont = dc.SelectObject (&m_fontMain); TEXTMETRIC tm; dc.GetTextMetrics (&tm); m_cxChar = tm.tmAveCharWidth; m_cyChar = tm.tmHeight + tm.tmExternalLeading; dc.SelectObject (pOldFont); // // Create the controls that will appear in the FontView window. // CRect rect (m_cxChar * 2, m_cyChar, m_cxChar * 48, m_cyChar * 2); m_wndLBTitle.Create (_T ("Typefaces"), WS_CHILD | WS_VISIBLE | SS_LEFT, rect, this); rect.SetRect (m_cxChar * 2, m_cyChar * 2, m_cxChar * 48, m_cyChar * 18); m_wndListBox.CreateEx (WS_EX_CLIENTEDGE, _T ("listbox"), NULL, WS_CHILD | WS_VISIBLE | LBS_STANDARD, rect, this, IDC_LISTBOX); rect.SetRect (m_cxChar * 2, m_cyChar * 19, m_cxChar * 48, m_cyChar * 20); m_wndCheckBox.Create (_T ("Show TrueType fonts only"), WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, rect, this, IDC_CHECKBOX); rect.SetRect (m_cxChar * 2, m_cyChar * 21, m_cxChar * 66, m_cyChar * 25); m_wndGroupBox.Create (_T ("Sample"), WS_CHILD | WS_VISIBLE | BS_GROUPBOX, rect, this, (UINT) -1); rect.SetRect (m_cxChar * 4, m_cyChar * 22, m_cxChar * 64, (m_cyChar * 99) / 4); m_wndSampleText.Create (_T (""), WS_CHILD | WS_VISIBLE | SS_CENTER, rect, this, IDC_SAMPLE); rect.SetRect (m_cxChar * 50, m_cyChar * 2, m_cxChar * 66, m_cyChar * 4); m_wndPushButton.Create (_T ("Print Sample"), WS_CHILD | WS_VISIBLE | WS_DISABLED | BS_PUSHBUTTON, rect, this, IDC_PRINT); // // Set each control's font to 8-point MS Sans Serif. // m_wndLBTitle.SetFont (&m_fontMain, FALSE); m_wndListBox.SetFont (&m_fontMain, FALSE); m_wndCheckBox.SetFont (&m_fontMain, FALSE); m_wndGroupBox.SetFont (&m_fontMain, FALSE); m_wndPushButton.SetFont (&m_fontMain, FALSE); // // Fill the list box with typeface names and return. // FillListBox (); return 0; }
//************************************************************************************************ void CBCGPDateTimeCtrl::AdjustControl (CRect rectClient) { if (GetSafeHwnd () == NULL) { return; } CClientDC dc (this); CFont* pPrevFont = m_hFont == NULL ? (CFont*) dc.SelectStockObject (DEFAULT_GUI_FONT) : dc.SelectObject (CFont::FromHandle (m_hFont)); if (m_WidestDate == COleDateTime ()) { BuidWidestDate (&dc); } SetPartsOrder (); m_iPartNum = m_bCheckBoxIsAvailable ? 1 : 0; m_CurrPartType = m_arPartsOrder [m_iPartNum]; m_rectText = rectClient; m_rectText.InflateRect (0, -2); if (m_bAutoResize) { // Calculate text size: TEXTMETRIC tm; dc.GetTextMetrics (&tm); m_iControlHeight = tm.tmHeight + 6; } else { m_iControlHeight = rectClient.Height (); } if (m_bCheckBoxIsAvailable) { m_rectCheck = CRect (rectClient.left + 1, rectClient.top + 1, rectClient.left + m_iControlHeight - 1, rectClient.bottom - 1); m_rectText.left = m_rectCheck.right + 2; } m_rectText.top = rectClient.top; m_rectText.bottom = rectClient.top + m_iControlHeight; // Calculate parts: CalcPartRects (&dc); // Adjust control size: m_rectText.right = m_arPartRects [m_iPartsNumber - 1].right + 2; m_iControlWidth = m_rectText.right; if (m_spinButton) { m_iControlWidth += iSpinWidth; } if (!m_bAutoResize) { m_iControlWidth = rectClient.Width (); m_iControlHeight = rectClient.Height (); } if (m_dropCalendar && m_showDate) { if (m_bAutoResize) { m_iControlWidth += iDropButtonWidth; } int iDropStart = rectClient.left + m_iControlWidth - iDropButtonWidth - 1; if (m_spinButton) { iDropStart -= iSpinWidth + 1; } m_rectDropButton = CRect ( iDropStart, rectClient.top, iDropStart + iDropButtonWidth, rectClient.top + m_iControlHeight - 2); } if (m_bAutoResize) { SetWindowPos (NULL, -1, -1, m_iControlWidth + 2, m_iControlHeight + 2, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOMOVE); } // Adjust spin button: if (m_spinButton) { if (m_wndSpin.GetSafeHwnd () == NULL) { m_wndSpin.Create (WS_CHILD | WS_VISIBLE | UDS_ALIGNRIGHT | UDS_AUTOBUDDY, CRect (0, 0, 0, 0), this, iSpinID); } m_wndSpin.SetWindowPos (NULL, rectClient.left + m_iControlWidth - iSpinWidth - 2, rectClient.top, iSpinWidth, m_iControlHeight - 2, SWP_NOZORDER); m_wndSpin.ShowWindow (SW_SHOW); m_wndSpin.EnableWindow (IsWindowEnabled () && m_bIsChecked); } else if (m_wndSpin.GetSafeHwnd () != NULL) { m_wndSpin.SetWindowPos (NULL, 0, 0, 0, 0, SWP_NOZORDER); m_wndSpin.ShowWindow (SW_HIDE); } if (pPrevFont != NULL) { dc.SelectObject (pPrevFont); } //----------------------------- // Adjust min./max date values: //----------------------------- COleDateTime minAllowedDate (iFirstAllowedYear, 1, 1, 0, 0, 0); COleDateTime maxAllowedDate (iLastAllowedYear, 12, 31, 23, 59, 59); COleDateTime emptyDate; m_MinDate = max (m_MinDate, minAllowedDate); if (m_MaxDate != emptyDate) { m_MaxDate = min (m_MaxDate, maxAllowedDate); } else { m_MaxDate = maxAllowedDate; } }
//***************************************************************************** void CBCGPCaptionBar::RecalcLayout () { CClientDC dc (NULL); CFont* pOldFont = dc.SelectObject ( m_hFont == NULL ? &globalData.fontRegular : CFont::FromHandle (m_hFont)); ASSERT (pOldFont != NULL); TEXTMETRIC tm; dc.GetTextMetrics (&tm); int nTextHeight = tm.tmHeight + 2; CSize sizeImage = GetImageSize (); const int nButtonVertMargin = CBCGPVisualManager::GetInstance()->GetMessageBarMargin(); const int nBorderSize = m_bIsMessageBarMode ? 0 : m_nBorderSize; //------------------------------------------------------------------- // the height is set to the default (provided by the user in Create) // or calculated if it is -1 //------------------------------------------------------------------- if (m_nDefaultHeight != -1) { m_nCurrentHeight = m_nDefaultHeight; } else { if (!m_strBtnText.IsEmpty () && m_bIsMessageBarMode) { nTextHeight += 2 * nButtonVertMargin; } m_nCurrentHeight = max (nTextHeight, sizeImage.cy) + m_nMargin * 2 + nBorderSize; } if (m_bIsMessageBarMode) { m_nCurrentHeight += CBCGPVisualManager::GetInstance()->GetMessageBarMargin() * 2; } // for left and center alignment: icon, button, text // for right alignment: text, button, icon CRect rectClient; GetClientRect (rectClient); if (rectClient.IsRectEmpty ()) { return; } if (m_bIsMessageBarMode) { CSize sizeImage = CBCGPMenuImages::Size (); const int nCloseButtonMargin = 4; sizeImage.cx += 2 * nCloseButtonMargin; sizeImage.cy += 2 * nCloseButtonMargin; m_rectClose = CRect ( CPoint (rectClient.right - sizeImage.cx, rectClient.top + nCloseButtonMargin), sizeImage); m_rectClose.right--; rectClient.DeflateRect (nCloseButtonMargin, nCloseButtonMargin); rectClient.right -= m_rectClose.Width (); } else { m_rectClose.SetRectEmpty(); } BOOL bButtonLeftOfIcon = FALSE; BOOL bTextLeftOfButton = FALSE; BOOL bTextLeftOfIcon = FALSE; BOOL bIconCenter = FALSE; BOOL bButtonCenter = FALSE; BOOL bButtonAfterText = FALSE; BOOL bTextCenter = FALSE; // differs from the current height, because the border size is non-client area int nBaseLine = rectClient.CenterPoint ().y; int nCenterOffset = rectClient.CenterPoint ().x; int nNextXOffsetLeft = rectClient.left + m_nMargin; int nNextXOffsetRight = rectClient.right - m_nMargin; int nNextXOffsetCenter = nCenterOffset; if (IsImageSet ()) { if (sizeImage.cy < rectClient.Height () || m_bIsMessageBarMode) { // center the icon if its height lesser than client area height m_rectImage.top = nBaseLine - sizeImage.cy / 2; } else { // otherwise, clip it from the buttom m_rectImage.top = rectClient.top + m_nMargin; } if (!m_bStretchImage) { m_rectImage.bottom = m_rectImage.top + sizeImage.cy; } else { m_rectImage.bottom = rectClient.bottom - m_nMargin; } switch (m_iconAlignment) { case ALIGN_LEFT: m_rectImage.left = nNextXOffsetLeft; m_rectImage.right = m_rectImage.left + sizeImage.cx; nNextXOffsetLeft = m_rectImage.right + m_nHorzElementOffset; break; case ALIGN_RIGHT: m_rectImage.left = nNextXOffsetRight - sizeImage.cx; m_rectImage.right = m_rectImage.left + sizeImage.cx; nNextXOffsetRight = m_rectImage.left - m_nHorzElementOffset; // only in this case button and text is at the left side of the icon bButtonLeftOfIcon = TRUE; bTextLeftOfIcon = TRUE; break; case ALIGN_CENTER: bIconCenter = TRUE; nNextXOffsetCenter -= sizeImage.cx / 2; if (m_btnAlignnment == ALIGN_LEFT) { bButtonLeftOfIcon = TRUE; } if (m_textAlignment == ALIGN_LEFT) { bTextLeftOfIcon = TRUE; } break; default: ASSERT (FALSE); } } else { m_rectImage.SetRectEmpty(); } int nButtonWidth = 0; if (!m_strBtnText.IsEmpty ()) { nButtonWidth = dc.GetTextExtent (m_strBtnText).cx + 2 * m_nHorzElementOffset; if (m_bIsMessageBarMode) { nButtonWidth += 2 * nButtonHorzMargin; } if (m_uiBtnID != 0 && m_bBtnEnabled && m_bBtnHasDropDownArrow) { nButtonWidth += nMenuArrowWidth; } // the button always has a height equivalent to the bar's height m_rectButton.top = rectClient.top; m_rectButton.bottom = rectClient.bottom; if (m_bIsMessageBarMode) { m_rectButton.DeflateRect (0, nButtonVertMargin); } switch (m_btnAlignnment) { case ALIGN_LEFT: if (!m_bIsMessageBarMode || m_textAlignment != ALIGN_LEFT) { m_rectButton.left = nNextXOffsetLeft; if (nNextXOffsetLeft == rectClient.left + m_nMargin) { m_rectButton.left = rectClient.left + m_nMargin; } m_rectButton.right = m_rectButton.left + nButtonWidth; nNextXOffsetLeft = m_rectButton.right + m_nHorzElementOffset; } else { bButtonAfterText = TRUE; } break; case ALIGN_RIGHT: m_rectButton.left = nNextXOffsetRight - nButtonWidth; if (nNextXOffsetRight == rectClient.right - m_nMargin) { m_rectButton.left = rectClient.right - nButtonWidth - m_nMargin; } m_rectButton.right = m_rectButton.left + nButtonWidth; nNextXOffsetRight = m_rectButton.left - m_nHorzElementOffset; // only in this case text at the left side of the button bTextLeftOfButton = TRUE; break; case ALIGN_CENTER: bButtonCenter = TRUE; nNextXOffsetCenter -= nButtonWidth / 2; if (m_textAlignment == ALIGN_LEFT) { bTextLeftOfButton = TRUE; } break; default: ASSERT (FALSE); return; } } else { m_rectButton.SetRectEmpty(); } CSize sizeText (0, 0); if (!m_strText.IsEmpty ()) { sizeText = GetTextSize (&dc, m_strText); m_rectText.top = nBaseLine - sizeText.cy / 2; m_rectText.bottom = m_rectText.top + sizeText.cy; switch (m_textAlignment) { case ALIGN_LEFT: m_rectText.left = nNextXOffsetLeft; nNextXOffsetLeft += sizeText.cx + 2 * m_nMargin; break; case ALIGN_RIGHT: m_rectText.left = nNextXOffsetRight - sizeText.cx; break; case ALIGN_CENTER: bTextCenter = TRUE; nNextXOffsetCenter -= sizeText.cx / 2; break; default: ASSERT (FALSE); return; } m_rectText.right = m_rectText.left + sizeText.cx; AdjustRectToMargin (m_rectText, rectClient, m_nMargin); m_rectDrawText = m_rectText; } if (bIconCenter) { m_rectImage.left = nNextXOffsetCenter; m_rectImage.right = m_rectImage.left + sizeImage.cx; nNextXOffsetCenter = m_rectImage.right + m_nHorzElementOffset; } if (bButtonAfterText) { m_rectButton.left = nNextXOffsetLeft; m_rectButton.right = m_rectButton.left + nButtonWidth; if (m_rectButton.right + m_nMargin > rectClient.right) { m_rectButton.right = rectClient.right - m_nMargin; m_rectButton.left = m_rectButton.right - nButtonWidth; } } else if (bButtonCenter) { m_rectButton.left = nNextXOffsetCenter; m_rectButton.right = m_rectButton.left + nButtonWidth; nNextXOffsetCenter = m_rectButton.right + m_nHorzElementOffset; } if (bTextCenter) { m_rectText.left = nNextXOffsetCenter; m_rectText.right = m_rectText.left + sizeText.cx; AdjustRectToMargin (m_rectText, rectClient, m_nMargin); m_rectDrawText = m_rectText; } if (IsImageSet ()) { // do not retain image size if it should be stretched AdjustRectToMargin (m_rectImage, rectClient, m_nMargin, !m_bStretchImage); if (m_rectImage.left < rectClient.left || m_rectImage.right > rectClient.right) { m_rectImage.SetRectEmpty (); } } CRect rectButtonTemp = m_rectButton; if (!m_strBtnText.IsEmpty () && IsImageSet ()) { CheckRectangle (rectButtonTemp, m_rectImage, bButtonLeftOfIcon); } if (!m_strBtnText.IsEmpty ()) { AdjustRectToMargin (rectButtonTemp, rectClient, m_nMargin); if (m_rectButton.Width () + m_rectImage.Width () + 2 * m_nMargin > rectClient.Width ()) { m_rectButton.SetRectEmpty (); } } if (!m_strText.IsEmpty ()) { CheckRectangle (m_rectDrawText, m_rectImage, bTextLeftOfIcon); CheckRectangle (m_rectDrawText, rectButtonTemp, bTextLeftOfButton || bButtonAfterText); } dc.SelectObject (pOldFont); m_bTextIsTruncated = m_rectDrawText.Width () < sizeText.cx; UpdateTooltips (); }