65.9K
CodeProject 正在变化。 阅读更多。
Home

如何使用 WTL 多窗格状态栏控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.69/5 (9投票s)

2002年9月27日

2分钟阅读

viewsIcon

99119

downloadIcon

2569

本文介绍了如何在应用程序中使用WTL的CMultiPaneStatusBarCtrl类。

Multipane Status Bar Sample

引言

本文介绍了如何在应用程序中使用WTL的CMultiPaneStatusBarCtrl类。 本文包含的示例项目是一个向导生成的SDI应用程序,该应用程序增强了具有三个窗格的状态栏,该状态栏显示状态消息以及当前日期和时间。

状态栏

使用状态栏的所有相关代码都包含在示例项目的mainframe.h文件中。 状态栏在OnCreate方法中初始化,该方法还会调用WTL类中错误的解决方法。 另一种方法提供一个计时器处理程序,用于在状态栏窗格中显示日期和时间。

基础

状态栏在使用应用程序时向用户提供各种类型的反馈和信息。 WTL向导生成的应用程序定义了一个单窗格状态栏。 它处理显示“就绪”的基本功能,并切换到简单模式以显示菜单提示。 另一方面,多窗格状态栏分为多个独立控制的段。

初始化

请注意,您必须包含atlctrlx.h头文件。 它定义了CMultiPaneStatusBarCtrl类。 您还必须包含atlmisc.h,因为它定义了AtlLoadIconImage()CString类。

定义和初始化应用程序的多窗格状态栏控件需要以下步骤

  1. 为每个状态栏窗格创建一个资源定义
  2. 定义一个成员变量:CMultiPaneStatusBarCtrl m_status;
  3. 对WTL向导创建的“简单”状态栏进行子类化
  4. 枚举并设置状态栏窗格
  5. 调用解决方法方法,SetPaneWidths()
  6. 为各个状态栏窗格设置图标(如果需要)

以下来自resource.h的资源定义与状态栏窗格及其图标有关。 它们是在导入图标时由资源编辑器创建的,它们用于标识状态栏窗格以及图标。 如果您不导入或为应用程序创建状态栏图标,则必须手动输入类似的值。

#define IDR_DEFAULT 201
#define IDR_DATE    202
#define IDR_TIME    203

此外,WTL定义了一个默认窗格标识符ID_DEFAULT_PANE,它用作显示“就绪”等状态消息的可变宽度窗格。

这是来自mainframe.hOnCreate()方法。 请注意,在传递给SetPaneWidths()的数组中,默认窗格的宽度设置为0。

LRESULT OnCreate(UINT, WPARAM, LPARAM, BOOL&)
{ 
    CreateSimpleStatusBar();

    // subclass the status bar as multipane

    m_status.SubclassWindow(m_hWndStatusBar);

    // set status bar panes. ID_DEFAULT_PANE is defined by WTL

    int arrPanes[] = { ID_DEFAULT_PANE, IDR_DATE, IDR_TIME };

    m_status.SetPanes(arrPanes, 
        sizeof(arrPanes) / sizeof(int), false);

    // set status bar pane widths using local workaround

    int arrWidths[] = { 0, 90, 60 };
    SetPaneWidths(arrWidths, sizeof(arrWidths) / sizeof(int));

    // set the status bar pane icons

    m_status.SetPaneIcon(ID_DEFAULT_PANE, 
        AtlLoadIconImage(IDR_DEFAULT, LR_DEFAULTCOLOR));

    m_status.SetPaneIcon(IDR_DATE, 
        AtlLoadIconImage(IDR_DATE, LR_DEFAULTCOLOR));

    m_status.SetPaneIcon(IDR_TIME, 
        AtlLoadIconImage(IDR_TIME, LR_DEFAULTCOLOR));

    // initialize date/time and start a 1 second timer

    OnTimer(0, 0, 0, bHandled);
    SetTimer(1, 1000);

    return 0; 
}

解决方法

SetPaneWidths()是一个解决方法,用于解决CMultiPaneStatusBarCtrl::SetPanes()方法中的一个错误。 该错误将默认窗格之后的所有窗格的宽度限制为总共100像素。 此解决方法允许任意窗格宽度。

void SetPaneWidths(int* arrWidths, int nPanes)
{ 
    // find the size of the borders

    int arrBorders[3];
    m_status.GetBorders(arrBorders);

    // calculate right edge of default pane (0)

    arrWidths[0] += arrBorders[2];
    for (int i = 1; i < nPanes; i++)
        arrWidths[0] += arrWidths[i];

    // calculate right edge of remaining panes (1 thru nPanes-1)

    for (int j = 1; j < nPanes; j++)
        arrWidths[j] += arrBorders[2] + arrWidths[j - 1];

    // set the pane widths

    m_status.SetParts(m_status.m_nPanes, arrWidths); 
}

计时器处理程序

这是计时器处理程序的代码。 您还必须为WM_TIMER消息创建一个消息映射条目。

LRESULT OnTimer(UINT, WPARAM, LPARAM, BOOL&)
{ 
    // get the current date and time

    SYSTEMTIME st;
    ::GetLocalTime(&st);

    CString str;

    // Pane 1: Display the date

    str.Format("%i/%i/%i", st.wMonth, st.wDay, st.wYear);
    m_status.SetPaneText(IDR_DATE, str);

    // Pane 2: Display the time

    str.Format("%i:%02i", st.wHour, st.wMinute);
    m_status.SetPaneText(IDR_TIME, str);

    return 0; 
}

使用条款

本文提供的示例应用程序可免费用于任何目的。

本软件按“现状”分发,不提供任何形式的担保。

© . All rights reserved.