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

WinForms 的 Windows Ribbon,第 19 部分 – 最近使用的项目

starIconstarIconstarIconstarIconstarIcon

5.00/5 (11投票s)

2010年3月2日

Ms-PL

2分钟阅读

viewsIcon

27054

downloadIcon

1089

在本文中,我将介绍如何使用 Ribbon 最近使用的项目控件。

这系列 CodeProject 文章基于我首先在我的 博客上发表的一系列帖子。

WinForms 的 Windows Ribbon 库现在支持在应用程序菜单中使用最近使用的项目。这篇文章的结果是另一个示例,“16-RecentItems”,可以在项目站点上找到。

image

什么是最近使用的项目?

最近使用的项目是在应用程序菜单中显示的列表中的项目。它们不一定是文件名,也不一定是最近使用的,尽管建议这样做。

每个项目都有三个属性

  • 标签 - 项目名称,通常是文件名(不含路径)。
  • 标签说明 - 项目工具提示,通常是完整的文件名路径。
  • 固定 - 布尔值,指示是否应将最近使用的项目从列表中移除。

更多详细信息可在 MSDN 上的 最近使用的项目 中找到。

使用最近使用的项目 - Ribbon 标记

命令 部分

<Application.Commands>
  ...
  <Command Name="cmdRecentItems" Id="1005" 
                LabelTitle="Recent Items" />
</Application.Commands>

视图 部分

<Application.Views>
  <Ribbon>
    <Ribbon.ApplicationMenu>
      <ApplicationMenu CommandName="cmdApplicationMenu">
        <ApplicationMenu.RecentItems>
          <RecentItems CommandName="cmdRecentItems" EnablePinning="true" MaxCount="7" />
        </ApplicationMenu.RecentItems>
        ...
      </ApplicationMenu>
    </Ribbon.ApplicationMenu>
  </Ribbon>
</Application.Views>

注意事项

  • “最近使用的项目”标签可以更改为您需要的任何内容(例如,“一周中的几天”)。
  • EnablePinning 属性设置为 false 将隐藏应用程序菜单中的图钉。
  • MaxCount 属性指定在应用程序菜单上显示的项目数量。

使用最近使用的项目 - 代码隐藏

初始化

private Ribbon _ribbon;
private RibbonRecentItems _ribbonRecentItems;

List<RecentItemsPropertySet> _recentItems;

public Form1()
{
    InitializeComponent();

    _ribbon = new Ribbon();
    _ribbonRecentItems = new RibbonRecentItems(_ribbon, 
                            (uint)RibbonMarkupCommands.cmdRecentItems);

    _ribbonRecentItems.OnExecute += 
           new OnExecuteEventHandler(_recentItems_OnExecute);
}

private void Form1_Load(object sender, EventArgs e)
{
    _ribbon.InitFramework(this);

    InitRecentItems();
}

private void InitRecentItems()
{
    // prepare list of recent items
    _recentItems = new List();
    _recentItems.Add(new RecentItemsPropertySet()
                     {
                         Label = "Recent item 1",
                         LabelDescription = "Recent item 1 description",
                         Pinned = true
                     });
    _recentItems.Add(new RecentItemsPropertySet()
                     {
                         Label = "Recent item 2",
                         LabelDescription = "Recent item 2 description",
                         Pinned = false
                     });

    _ribbonRecentItems.RecentItems = _recentItems;
}

RibbonRecentItems 是用于处理最近使用的项目功能的辅助类。它具有一个名为RecentItems 的属性,类型为IList<RecentItemsPropertySet>。此属性包含最近使用的项目的列表。请注意,提供此列表并在需要时更新它(添加/删除项目,更改固定状态)是用户的责任。

响应项目点击

void _recentItems_OnExecute(PropertyKeyRef key, PropVariantRef currentValue, 
                  IUISimplePropertySet commandExecutionProperties)
{
    if (key.PropertyKey == RibbonProperties.RecentItems)
    {
        // go over recent items
        object[] objectArray = (object[])currentValue.PropVariant.Value;
        for (int i = 0; i < objectArray.Length; ++i)
        {
            IUISimplePropertySet propertySet = objectArray[i] as IUISimplePropertySet;

            if (propertySet != null)
            {
                PropVariant propLabel;
                propertySet.GetValue(ref RibbonProperties.Label, 
                                     out propLabel);
                string label = (string)propLabel.Value;

                PropVariant propLabelDescription;
                propertySet.GetValue(ref RibbonProperties.LabelDescription, 
                                     out propLabelDescription);
                string labelDescription = (string)propLabelDescription.Value;

                PropVariant propPinned;
                propertySet.GetValue(ref RibbonProperties.Pinned, 
                                     out propPinned);
                bool pinned = (bool)propPinned.Value;

                // update pinned value
                _recentItems[i].Pinned = pinned;
            }
        }
    }
    else if (key.PropertyKey == RibbonProperties.SelectedItem)
    {
        // get selected item index
        uint selectedItem = (uint)currentValue.PropVariant.Value;

        // get selected item label
        PropVariant propLabel;
        commandExecutionProperties.GetValue(ref RibbonProperties.Label, 
                                            out propLabel);
        string label = (string)propLabel.Value;

        // get selected item label description
        PropVariant propLabelDescription;
        commandExecutionProperties.GetValue(ref RibbonProperties.LabelDescription, 
                                            out propLabelDescription);
        string labelDescription = (string)propLabelDescription.Value;

        // get selected item pinned value
        PropVariant propPinned;
        commandExecutionProperties.GetValue(ref RibbonProperties.Pinned, 
                                            out propPinned);
        bool pinned = (bool)propPinned.Value;
    }
}

我知道,需要一些解释。OnExecute 事件在两种情况下被调用

  1. 当用户点击其中一个项目时。
  2. 当用户更改多个项目的固定状态,然后关闭菜单(通过选择其中一个项目或点击菜单外部)时。

当用户点击一个项目时,currentValue 参数包含所选项目的索引,commandExecutionProperties 参数包含所选项目的属性。上面的代码显示了如何提取它们。

当用户更改多个项目的固定状态时,currentValue 参数包含项目的新的状态。更新他们自己列表中的项目是用户的责任。否则,用户下次打开菜单时,用户的更改将不会出现。

目前就到这里为止。

© . All rights reserved.