简体   繁体   English

如何计算CMFCRibbonStatusBarPane的大小

[英]How to calculate the size of a CMFCRibbonStatusBarPane

I have MFC ribbon based application that includes three status bars as follows; 我有一个基于MFC功能区的应用程序,其中包括以下三个状态栏。

CString LongString;
LongString.Format("%0*lf", 60, 0.0);
m_pStatusWnd = new CMFCRibbonStatusBarPane(ID_STATUSBAR_PANE1, _T(""), TRUE,NULL, LongString);
m_pStatusCommand = new CMFCRibbonStatusBarPane(ID_INDICATOR_PROMPT, _T("Command"), TRUE, NULL, "000000000000000000000000000000");
m_pStatusSnap = new CMFCRibbonStatusBarPane(ID_INDICATOR_SNAP, _T("Snaps"), TRUE, NULL, "00000000000000000000");
ui.GetStatusBar().AddElement(m_pStatusWnd, "Status");
ui.GetStatusBar().AddExtendedElement(m_pStatusCommand, "Command");
ui.GetStatusBar().AddExtendedElement(m_pStatusSnap, "Snaps");
ui.GetStatusBar().RecalcLayout();

The two extended status panes on the bottom right side of the screen have fixed sizes, but I'd like the left side status pane to take the remaining space to the edge of the frame. 屏幕右下方的两个扩展状态窗格具有固定的大小,但我希望左侧状态窗格将剩余空间带到框架的边缘。 The only method I can find for setting the pane width is the SetAlmostLargeText method, which isn't really that useful as the value will vary based on the size of the frame. 我可以找到设置窗格宽度的SetAlmostLargeText方法是SetAlmostLargeText方法,该方法实际上并不实用,因为值会根据框架的大小而变化。 From testing, if I give a value too large, the status pane simply does not display any content. 从测试来看,如果我给出的值太大,状态窗格将根本不显示任何内容。 Similarly, if I resize the frame and make it smaller, the content on the left pane is lost entirely once it goes below the AlmostLargeTextSize 同样,如果我调整框架大小并使其变小,左窗格中的内容一旦低于AlmostLargeTextSize就会完全丢失

Is there a method of calculating the size of the left CMFCRibbonStatusBarPane based on available frame space? 有没有一种根据可用的帧空间计算左CMFCRibbonStatusBarPane大小的方法? Nearest I can find is CMFCRibbonBaseElement::GetSize which I could possibly figure out the maximum available text size from when used in conjunction with average font size metrics, but it seems rather crude. 我最近能找到的是CMFCRibbonBaseElement::GetSize ,我可以找出与平均字体大小指标一起使用时的最大可用文本大小,但它看起来相当粗糙。

I got this working as follows; 我的工作如下:

void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
    CMyMainFrame::OnSize(nType, cx, cy);
    if (!m_MyFrameInitialised)
      return;  // Only proceed if frame creation has been completed
    CString SnapStr = m_pStatusSnap->GetAlmostLargeText();
    CString CommandStr = m_pStatusCommand->GetAlmostLargeText();
    int SnapChars = SnapStr.GetLength(),CommandChars = CommandStr.GetLength(),  StatusChars = 0;
    CDC *pDC = GetWindowDC();
    CSize SnapSize = m_pStatusSnap->GetSize(pDC);
    pDC->LPtoDP(&SnapSize);
    double CharWidth = SnapSize.cx;  
    CharWidth /= SnapChars;
    double TotalChars = cx / CharWidth;
    StatusChars = TotalChars - (SnapChars + CommandChars);
    CString LongStatusString;
    LongStatusString.Format("%0*.0lf", StatusChars, 0.0);
    m_pStatusWnd->SetAlmostLargeText(LongStatusString);
    ui.GetStatusBar().RecalcLayout();
}

Basically, when the frame window gets sized at any time after initially being created, calculate the size of the width of the frame in terms of status pane characters, subtract the number of characters of the known panes, and set the left pane based on the result. 基本上,当框架窗口在最初创建后随时可以调整大小时,请根据状态窗格字符来计算框架的宽度大小,减去已知窗格的字符数,然后根据结果。

Seems a bit cumbersome for a UI that is supposed to offer a high level of abstraction, but it was the best I could come up with. 对于应该提供高级抽象的UI来说似乎有点麻烦,但它是我能想到的最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM