分享一个 C# Winfrom 下的 OutlookBar 控件的使用

最近在上网的时候,发现了这个C# 下的 OutlookBar 控件,看了一下感觉还真不错,特此记录一下。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace OutLookBarDemo
{
    internal class BandTagInfo
    {
        public OutlookBar outlookBar;
        public int index;

        public BandTagInfo(OutlookBar ob, int index)
        {
            outlookBar=ob;
            this.index=index;
        }
    }

    public class OutlookBar : Panel
    {
        private int buttonHeight;
        private int selectedBand;
        private int selectedBandHeight;

        public int ButtonHeight
        {
            get
            {
                return buttonHeight;
            }

            set
            {
                buttonHeight=value;
                // do recalc layout for entire bar
            }
        }

        public int SelectedBand
        {
            get
            {
                return selectedBand;
            }
            set
            {
                SelectBand(value);
            }
        }

        public OutlookBar()
        {
            buttonHeight=25;
            selectedBand=0;
            selectedBandHeight=0;
        }

        public void Initialize()
        {
            //parent Panel 必须存在不能删除
            //this.BorderStyle = BorderStyle.None;
            Parent.SizeChanged+=new EventHandler(SizeChangedEvent);
        }

        public void AddBand(string caption, ContentPanel content)
        {
            content.outlookBar=this;
            int index=Controls.Count;
            BandTagInfo bti=new BandTagInfo(this, index);
            BandPanel bandPanel=new BandPanel(caption, content, bti);
            Controls.Add(bandPanel);
            UpdateBarInfo();
            RecalcLayout(bandPanel, index);
        }

        public void SelectBand(int index)
        {
            selectedBand=index;
            RedrawBands();
        }

        private void RedrawBands()
        {
            for (int i=0; i<Controls.Count; i++)
            {
                BandPanel bp=Controls[i] as BandPanel;
                RecalcLayout(bp, i);
            }
        }

        private void UpdateBarInfo()
        {
            selectedBandHeight=ClientRectangle.Height-(Controls.Count * buttonHeight);
        }

        private void RecalcLayout(BandPanel bandPanel, int index)
        {
            int vPos=(index <= selectedBand) ? buttonHeight*index : buttonHeight*index+selectedBandHeight;
            int height=selectedBand==index ? selectedBandHeight+buttonHeight : buttonHeight;

            // the band dimensions
            bandPanel.Location=new Point(0, vPos);
            bandPanel.Size=new Size(ClientRectangle.Width, height);

            // the contained button dimensions
            bandPanel.Controls[0].Location=new Point(0, 0);
            bandPanel.Controls[0].Size=new Size(ClientRectangle.Width, buttonHeight);

            // the contained content panel dimensions
            bandPanel.Controls[1].Location=new Point(0, buttonHeight);
            bandPanel.Controls[1].Size=new Size(ClientRectangle.Width-2, height-8);
        }

        private void SizeChangedEvent(object sender, EventArgs e)
        {
            Size=new Size(Size.Width, ((Control)sender).ClientRectangle.Size.Height);
            UpdateBarInfo();
            RedrawBands();
        }
    }

    internal class BandPanel : Panel
    {
        public BandPanel(string caption, ContentPanel content, BandTagInfo bti)
        {

            BandButton bandButton=new BandButton(caption, bti);
            Controls.Add(bandButton);
            Controls.Add(content);
        }
    }

    internal class BandButton : Button
    {
        private BandTagInfo bti;

        public BandButton(string caption, BandTagInfo bti)
        {
            Text=caption;
            FlatStyle=FlatStyle.Standard;
            Visible=true;
            this.bti=bti;
            Click+=new EventHandler(SelectBand);
        }

        private void SelectBand(object sender, EventArgs e)
        {
            bti.outlookBar.SelectBand(bti.index);
        }
    }

    public abstract class ContentPanel : Panel
    {
        public OutlookBar outlookBar;

        public ContentPanel()
        {
            // initial state
            Visible=true;
        }
    }

    public class IconPanel : ContentPanel
    {
        protected int iconSpacing;
        protected int margin;

        public int IconSpacing
        {
            get
            {
                return iconSpacing;
            }
        }

        public int Margin
        {
            get
            {
                return margin;
            }
        }

        public IconPanel()
        {
            margin=20;
            //这里是调节图标间距的
            iconSpacing = 32 + 15 + 20;    // icon height + text height + margin
            BackColor=Color.LightBlue;
            AutoScroll=true;
        }

        public void AddIcon(string caption, Image image, EventHandler onClickEvent)
        {
            int index=Controls.Count/2;    // two entries per icon
            PanelIcon panelIcon=new PanelIcon(this, image, index, onClickEvent);
            Controls.Add(panelIcon);

            Label label=new Label();
            label.Text=caption;
            label.Visible=true;
            label.Location = new Point(0, margin + image.Size.Height + index * iconSpacing+5);
            label.Size=new Size(Size.Width, 15);
            label.TextAlign=ContentAlignment.BottomCenter;
            label.Click+=onClickEvent;
            label.Tag=panelIcon;
            Controls.Add(label);
        }
    }

    public class PanelIcon : PictureBox
    {
        public int index;
        public IconPanel iconPanel;

        private Color bckgColor;
        private bool mouseEnter;

        public int Index
        {
            get
            {
                return index;
            }
        }

        public PanelIcon(IconPanel parent, Image image, int index, EventHandler onClickEvent)
        {
            this.index=index;
            this.iconPanel=parent;
            Image=image;
            Visible=true;
            Location=new Point(iconPanel.outlookBar.Size.Width/2-image.Size.Width/2,
                            iconPanel.Margin + index*iconPanel.IconSpacing);
            Size=image.Size;
            Click+=onClickEvent;
            Tag=this;

            MouseEnter+=new EventHandler(OnMouseEnter);
            MouseLeave+=new EventHandler(OnMouseLeave);
            MouseMove+=new MouseEventHandler(OnMouseMove);

            bckgColor=iconPanel.BackColor;
            mouseEnter=false;
        }

        private void OnMouseMove(object sender, MouseEventArgs args)
        {
            if ( (args.X < Size.Width-2) &&
                (args.Y < Size.Width-2) &&
                (!mouseEnter) )
            {
                BackColor=Color.LightCyan;
                BorderStyle=BorderStyle.FixedSingle;
                Location=Location-new Size(1, 1);
                mouseEnter=true;
            }
        }

        private void OnMouseEnter(object sender, EventArgs e)
        {
        }

        private void OnMouseLeave(object sender, EventArgs e)
        {
            if (mouseEnter)
            {
                BackColor=bckgColor;
                BorderStyle=BorderStyle.None;
                Location=Location+new Size(1, 1);
                mouseEnter=false;
            }
        }
    }
}

OutlookBar 组件代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OutLookBarDemo
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();

            #region 初始化 OutLookBar
            outlookBar.BorderStyle = BorderStyle.FixedSingle;
            outlookBar.Initialize();
            IconPanel iconPanel1 = new IconPanel();
            IconPanel iconPanel2 = new IconPanel();
            IconPanel iconPanel3 = new IconPanel();

            outlookBar.AddBand("工具条A", iconPanel1);
            outlookBar.AddBand("工具条B", iconPanel2);
            outlookBar.AddBand("工具条C", iconPanel3);

            //0
            iconPanel1.AddIcon("信息查询", Image.FromFile(@"Image\1.ico"), new EventHandler(PanelEventA));
            //1
            iconPanel1.AddIcon("购物车管理", Image.FromFile(@"Image\2.ico"), new EventHandler(PanelEventA));
            //2
            iconPanel2.AddIcon("电子邮件", Image.FromFile(@"Image\3.ico"), new EventHandler(PanelEventB));
            //3
            iconPanel2.AddIcon("密码管理", Image.FromFile(@"Image\4.ico"), new EventHandler(PanelEventB));
            //4
            iconPanel3.AddIcon("时间设置", Image.FromFile(@"Image\4.ico"), new EventHandler(PanelEventC));
            outlookBar.SelectBand(0);
            #endregion
        }

        public void PanelEventA(object sender, EventArgs e)
        {
            Control ctrl = (Control)sender;
            PanelIcon panelIcon = ctrl.Tag as PanelIcon;
            string clickInfo = string.Empty;
            switch (panelIcon.Index)
            {
                case 0:
                    clickInfo = "信息查询";
                    break;
                case 1:
                    clickInfo = "购物车管理";
                    break;
            }
            this.label1.Text = string.Format("您选择了 {0}", clickInfo);
        }

        public void PanelEventB(object sender, EventArgs e)
        {
            Control ctrl = (Control)sender;
            PanelIcon panelIcon = ctrl.Tag as PanelIcon;
            string clickInfo = string.Empty;

            switch (panelIcon.Index)
            {
                case 0:
                    clickInfo = "电子邮件";
                    break;
                case 1:
                    clickInfo = "密码管理";
                    break;
            }
            this.label1.Text = string.Format("您选择了 {0}", clickInfo);
        }

        public void PanelEventC(object sender, EventArgs e)
        {
            Control ctrl = (Control)sender;
            PanelIcon panelIcon = ctrl.Tag as PanelIcon;
            string clickInfo = string.Empty;
            switch (panelIcon.Index)
            {
                case 0:
                    clickInfo = "时间设置";
                    break;
            }
            this.label1.Text = string.Format("您选择了 {0}", clickInfo);
        }
    }
}

调用代码

虽然 调用和获取 这块有点费劲,但是还是不错的一款组件。

-==源码下载==-

原文地址:https://www.cnblogs.com/belx/p/9194792.html

时间: 2024-07-31 05:03:09

分享一个 C# Winfrom 下的 OutlookBar 控件的使用的相关文章

福利到~分享一个基于jquery的智能提示控件intellSeach.js

一.需求 我们经常会遇到[站内搜索]的需求,为了提高用户体验,我们希望能做到像百度那样的即时智能提示.例如:某公司人事管理系统,想搜索李XX,只要输入“李”,系统自然会提示一些姓李的员工,这样方便用户使用.说白了,就是用户边输入,系统会提示相关的结果:或者,当用户点击搜索框时,就推荐一些内容,如360.百度都会提示今天的主要新闻或搜索量大的内容. jquery 已经有一个这样的插件了,叫 autocomplete, 但我觉得不好用.关于autocomplete的介绍也很多,有兴趣的朋友可以去试试

分享一个Winform里面的HTML编辑控件Zeta HTML Edit Control,汉化附源码

我们知道,Web开发上有很多HTML的编辑控件,如FCKEditor.CKEditor.kindeditor等等,很多都做的很好,而虽然Winform里面有WebBrowser控件,但是默认这个控件是不允许编辑内容的,可以显示网页而已.Winform开发里面,有些使用RichTextBox控件来编辑HTML,也有一些结合WebBrowser控件来实现内容的编辑,其中我觉得做的最好的应该是Zeta HTML Edit Control(http://www.codeproject.com/Artic

自定义下拉刷新控件

一.功能效果 1.在很多app中,在信息展示页面,当我们向下拖拽时,页面会加载最新的数据,并有一个短暂的提示控件出现,有些会有加载进度条,有些会记录加载日期.条目,有些还带有加载动画.其基本实现原理都相仿,本文中将探讨其实现原理,并封装出一个简单的下拉刷新控件 2.自定义刷新工具简单的示例 二.系统提供的下拉刷新工具 1.iOS6.0以后系统提供了自己的下拉刷新的控件:UIRefreshControl .例如,refreshControl,作为UITableViewController中的一个属

Android仿苹果版QQ下拉刷新实现(一) ——打造简单平滑的通用下拉刷新控件

前言: 因为公司人员变动原因,导致了博主四个月没有动安卓,一直在做IOS开发,如今接近年前,终于可以花一定的时间放在安卓上了.好了,废话不多说,今天我们要带来的效果是苹果版本的QQ下拉刷新.首先看一下目标效果以及demo效果:      因为此效果实现的步骤较多,所以今天博主要实现以上效果的第一步——打造一个通用的下拉刷新控件,具体效果如下: GIF图片比较大,还希望读者能耐心等待一下下从效果图中可以看出,我们的下拉刷新的滑动还是很流畅的,可能大多数开发者用的是XListview或者PullTo

Android SwipeRefreshLayout 官方下拉刷新控件介绍

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24521483 以下App基本都有下拉刷新的功能,曾经基本都使用XListView或者自己写一个下拉刷新,最近Google提供了一个官方的下拉刷新控件SwipeRefreshLayout,我感觉还不错啊,见惯了传统的下拉刷新,这个反而给人耳目一新的感觉(貌似知乎的APP已经使用这样的下拉刷新了). Google也在官方站点给出了V4的兼容包: 顺便看一眼API呗: 和XlistV

Win7 64bit系统WinFrom下未能加载文件或程序集“System.Data.SQLite”的解决办法

在Win7 64bit系统下调试程序时出现如下错误: 未能加载文件或程序集”System.Data.SQLite”或它的某一个依赖项.试图加载格式不正确的程序. 事实上System.Data.SQLite.dll控件在默认情况下是32bit的,在64bit系统下得使用另一个控件. 可以到这里http://sourceforge.net/projects/sqlite-dotnet2/files/下载一个安装程序包,安装完后在D:\Program Files (x86)\SQLite.NET\bi

android官方下拉刷新控件SwipeRefreshLayout的使用

可能开发安卓的人大多数都用过很多下拉刷新的开源组件,但是今天用了官方v4支持包的SwipeRefreshLayout觉得效果也蛮不错的,特拿出来分享. 简介:SwipeRefreshLayout组件只接受一个子组件:即需要刷新的那个组件.它使用一个侦听机制来通知拥有该组件的监听器有刷新事件发生,换句话说我们的Activity必须实现通知的接口.该Activity负责处理事件刷新和刷新相应的视图.一旦监听者接收到该事件,就决定了刷新过程中应处理的地方.如果要展示一个“刷新动画”,它必须调用setR

[Android]下拉刷新控件RefreshableView的实现

需求:自定义一个ViewGroup,实现可以下拉刷新的功能.下拉一定距离后(下拉时显示的界面可以自定义任何复杂的界面)释放手指可以回调刷新的功能,用户处理完刷新的内容后,可以调用方法onCompleteRefresh()通知刷新完毕,然后回归正常状态.效果如下:     源代码:RefreshableView(https://github.com/wangjiegulu/RefreshableView) 分析: 我们的目的是不管什么控件,只要在xml中外面包一层标签,那这个标签下面的所有子标签所

WinForm多窗体间操作,ComboBox下拉菜单控件

1.通过操作一个窗体打开另一个窗体 已有窗体为Form1.Form1中有一个按钮,对该按钮编写点击事件: Form2 f2 = new Form2();f2.Show(); 通过这段代码来打开Form2. 在实际应用中会出现代开Form2后需要Form1隐藏的情况,此时可以加入代码:this.Hide();来实现隐藏Form1. [不能关闭Form1,如果关闭Form1,那么Form2也会关闭,因为Form1是主窗体] 2.在窗体之间进行值的传递 在某些情况下,需要用到某个已经获取到的值,为了提