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

TDHAppBar - 一个将 Windows.Forms.Form 转换为应用程序桌面工具栏的库

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.79/5 (13投票s)

2007年12月17日

CPOL

4分钟阅读

viewsIcon

68713

downloadIcon

2413

本文介绍了一个类,该类可用于将任何 Windows.Forms.Form 转换为已停靠的应用程序桌面工具栏。也就是说,通过使用 AppBar.ApplicationDesktopToolbar 类,可以将您的工具栏小程序浮动或停靠在桌面的边缘。

Screenshot -

背景

最近,我一直在寻找能够为桌面停靠小型工具栏小程序启用代码。我偶然发现了 CodeProject 上一篇 2003 年的文章,C# does Shell, Part 3,起初它似乎正是我想要的。(我绝无意贬低 "arikp" 的文章或代码:它是一篇写得很好且信息丰富的文章;只是代码不是我完成当前任务所需的。)事实上,在这篇文章中,我将读者引导回 "arikp" 的原始文章,本文完全是它的衍生作品。

幸运的是,在我上面引用的文章的评论区,早在 2006 年 12 月,"GWB@s1c" 发布了代码,解决了我的特定问题。

由于 "GWB@s1c" 没有撰写关于他的代码的文章(而且由于我添加了我自己的特定观点),我提交这篇文章是为了让 CodeProject 社区更容易获得这段代码。

'AppBar.ApplicationDesktopToolbar' 类

TDHAppBar 严格来说是本文以及我基于原始代码编写的项目/程序集的名称;命名空间和类名是 AppBar.ApplicationDesktopToolbar,与 "GWB@s1c" 去年 12 月发布的原始代码示例相同。我的目的是对原始代码进行最少的更改,并清楚地划分我的更改;因此,我保留了原始的命名空间和类名(同时给我的项目起了一个独特的名字,以防 "GWB@s1c" 有一天选择提交一篇关于他某个版本代码的文章)。

使用 'AppBar.ApplicationDesktopToolbar' 类

要使用 AppBar.ApplicationDesktopToolbar,请在您的项目中添加对类库 TDHAppBar.dll 的引用。示例如下:

using AppBar;
// 
private AppBar.ApplicationDesktopToolbar appBar = null;
//
private void cmnuMain_Dock_Click(object sender, System.EventArgs e)
{
    AppBar.AppBarEdges theAppBarEdge = AppBar.AppBarEdges.Right;    // example

    if (this.appBar == null)
    {
        appBar = new AppBar.ApplicationDesktopToolbar(
            this, theAppBarEdge,
            // HorizConfig delegate
            new AppBar.AltitudeConfigDelegate(this.Configure_Horizontal),
            // VertConfig delegate
            new AppBar.AltitudeConfigDelegate(this.Configure_Vertical),  
            // AutoHideConfig delegate
            new AppBar.AutoHideChangedDelegate(this.Configure_AutoHide), 
            // Docking dimensions (max height OR max width, depending upon .Edge)
            106, 82,  
            false);    // set .AutoHide
    }
    else
    if (this.appBar.Edge != theAppBarEdge)
    {
        appBar.Edge = theAppBarEdge;    // Change the Docked .Edge
    }

}

private void Configure_AutoHide(bool asAutoHide)
{
    // do something
}
private void Configure_Horizontal(bool asDocked)
{
    // do something
}

private void Configure_Vertical(bool asDocked)
{
    // do something
}

private void cmnuMain_DockAutoHide_Click(object sender, System.EventArgs e)
{
    if (this.appBar != null)
    {
        this.cmnuMain_DockAutoHide.Checked = !this.cmnuMain_DockAutoHide.Checked;
        this.appBar.AutoHide = this.cmnuMain_DockAutoHide.Checked;
    }
    else
    {
        this.cmnuMain_DockAutoHide.Checked = false;
        this.cmnuMain_DockAutoHide.Enabled = false;
    }
}

private void cmnuMain_Float_Click(object sender, System.EventArgs e)
{
    if (this.appBar != null)
    {
        if (!this.appBar.UnDock(AppBar.Altitude.Horizontal))    // example
        {
            this.appBar.Dispose();
        }
        this.appBar = null;
    }
}

TDHAppBar 项目是用 Visual Studio 2003 (.NET 1.1) 编写(并编译)的。如果需要,将其转换为 .NET 1.0 应该是一个简单的过程,只需将 ApplicationDesktopToolbar.csShellApi.cs 源代码文件添加到项目的即可。

'AppBar.ApplicationDesktopToolbar' 类的关注点

该类的一些方法确实有可能引发异常。这些异常与未能将 Form 注册为 AppBar 或未能 "注销" 它有关。例如,类构造函数本身就有可能引发异常(因此,上面的示例代码实际上应该考虑这一点)。.Edge 属性的 set 访问器也有可能引发异常,因为它通过先 "注销" AppBar 然后创建/注册一个新的停靠到新边缘的 AppBar 来将 AppBar Form 设置到不同的桌面边缘。

我版本的 AppBar.ApplicationDesktopToolbar 类的构造函数记录了被转换为 AppBar 的 Form 的各种属性。这些值用于在执行 .Undock().Dispose() 方法时将 Form 恢复到其原始状态。.Undock() 方法的一些签名允许为其中一些属性提供新值。

在我的类版本中,原始 Form 的 [.FormBorderStyle == FormBorderStyle.None]; 不是必需的 - 该类在将 Form 转换为 AppBar 时会进行此更改。并且,在将其转换回标准 Form 时,该类会恢复此属性值。原始 Form 的 [.TopMost == true]; 也不是必需的 - 该类会在必要时设置此属性。

AppBar 命名空间中值得关注的成员有:

  • AppBar.Altitude - 这个 enum 的元素指示 AppBar.ApplicationDesktopToolbar 类的实例在 UnDocking AppBar Form 时(即将其转换回标准 Form)要调用哪个配置委托(如果已传递给构造函数)。
  • AppBar.AppBarEdges - 这个 enum 的元素指示 AppBar.ApplicationDesktopToolbar 类的实例应该将 AppBar Form 停靠到哪个桌面边缘。
  • public delegate void AltitudeConfigDelegate(bool asDocked) - 这个 delegate 的适当实例(如果已传递给构造函数)由 AppBar.ApplicationDesktopToolbar 类的实例在必要时调用,以在垂直和水平方向之间重新配置 AppBar Form。
  • public delegate void AutoHideChangedDelegate(bool asAutoHide) - 这个 delegate (如果已传递给构造函数)由 AppBar.ApplicationDesktopToolbar 类的实例在其 .AutoHide 属性更改时调用。

AppBar.ApplicationDesktopToolbar 类接口中值得关注的成员有:

  • AutoHide - 这个属性 getset AppBar.ApplicationDesktopToolbar 类的实例是否被指示自动隐藏 AppBar Form。
  • Edge - 这个属性 getset AppBar.ApplicationDesktopToolbar 类的实例应将 AppBar Form 停靠(或重新停靠)到哪个桌面边缘。
  • UnDock() - 这个方法(有各种签名)将 AppBar Form 转换回标准的 Form。调用不带参数的 .UnDock() 会尝试将 Form 恢复到其被转换为 AppBar 之前的状态;其他签名可用于指定 Form 的新状态。
  • Dispose() - 在我的类版本中,此方法与不带参数的 .UnDock() 基本相同。

历史

  • 2007 年 12 月 17 日:将 TDHAppBar 项目 V1.0.001 提交到 The Code Project
  • 2007 年 12 月 18 日:V1.0.002
    • 修复了 .AutoHide 模式的一个小麻烦 -- 当 AppBar Form 被设置为 .AutoHide 时,上下文菜单可能会在您有机会做出选择之前消失。
© . All rights reserved.