search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

C# 製作 Outlook Menu – jashliao部落格

C# 製作 Outlook Menu

 

資料來源: https://www.codeproject.com/Articles/3950/An-Outlook-Bar-Implementation

 

 

OutlookBar.cs

using System;

using System.Drawing;

using System.Windows.Forms;

 

namespace OutlookBar

{

        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 must exist!

                        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 AddBand(Image im, string caption, ContentPanel content)

        {

            content.outlookBar = this;

            int index = Controls.Count;

            BandTagInfo bti = new BandTagInfo(this, index);

            BandPanel bandPanel = new BandPanel(im,caption, content, bti);

            Controls.Add(bandPanel);

            UpdateBarInfo();

            RecalcLayout(bandPanel, index);

        }

        public void DelAllBand()

        {

            Controls.Clear();

        }

                public void SelectBand(int index)

                {

                        selectedBand=index;

                        RedrawBands();

                }

 

                private void RedrawBands()

                {

                        for (int i=0; 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(Image im, string caption, ContentPanel content, BandTagInfo bti)

                {

                        BandButton bandButton=new BandButton(im,caption, bti);

                        Controls.Add(bandButton);

                        Controls.Add(content);

                }

        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(Image im,string caption, BandTagInfo bti)

                {

            Image = im;

                        Text=caption;

            ImageAlign = ContentAlignment.MiddleLeft;

            TextAlign = ContentAlignment.MiddleCenter;

                        FlatStyle=FlatStyle.Standard;

                        Visible=true;

                        this.bti=bti;

                        Click+=new EventHandler(SelectBand);

                }

        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=10;

                        iconSpacing=32+15+10;  // 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);

                        label.Size=new Size(Size.Width, 15);

                        label.TextAlign=ContentAlignment.TopCenter;

                        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;

                        }

                }

        }

}

 

Form1.cs

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

 

namespace OutlookBar

{

        ///

        /// Summary description for Form1.

        ///

        public class Form1 : System.Windows.Forms.Form

    {

        private IContainer components;

        private ImageList imageList1;

        private OutlookBar outlookBar1;

                public Form1()

                {

                        //

                        // Required for Windows Form Designer support

                        //

                        InitializeComponent();

 

                        //

                        // TODO: Add any constructor code after InitializeComponent call                             

                        //

 

                }

 

                public void PanelEvent(object sender, EventArgs e)

                {

                        Control ctrl=(Control)sender;

                        PanelIcon panelIcon=ctrl.Tag as PanelIcon;

            MessageBox.Show(“#” + outlookBar1.SelectedBand+ “,” + panelIcon.Index.ToString(), “Panel Event”);

                }

 

                ///

                /// Clean up any resources being used.

                ///

                protected override void Dispose( bool disposing )

                {

                        if( disposing )

                        {

                                if (components != null)

                                {

                                        components.Dispose();

                                }

                        }

                        base.Dispose( disposing );

                }

 

                #region Windows Form Designer generated code

                ///

                /// Required method for Designer support – do not modify

                /// the contents of this method with the code editor.

                ///

                private void InitializeComponent()

                {

            this.components = new System.ComponentModel.Container();

            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));

            this.outlookBar1 = new OutlookBar();

            this.imageList1 = new System.Windows.Forms.ImageList(this.components);

            this.SuspendLayout();

            //

            // outlookBar1

            //

            this.outlookBar1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

            this.outlookBar1.ButtonHeight = 25;

            this.outlookBar1.Dock = System.Windows.Forms.DockStyle.Fill;

            this.outlookBar1.Location = new System.Drawing.Point(0, 0);

            this.outlookBar1.Name = “outlookBar1”;

            this.outlookBar1.SelectedBand = 0;

            this.outlookBar1.Size = new System.Drawing.Size(304, 259);

            this.outlookBar1.TabIndex = 0;

            //

            // imageList1

            //

            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject(“imageList1.ImageStream”)));

            this.imageList1.TransparentColor = System.Drawing.Color.Transparent;

            this.imageList1.Images.SetKeyName(0, “home.png”);

            this.imageList1.Images.SetKeyName(1, “envelope.png”);

            //

            // Form1

            //

            this.AutoScaleBaseSize = new System.Drawing.Size(7, 20);

            this.ClientSize = new System.Drawing.Size(304, 259);

            this.Controls.Add(this.outlookBar1);

            this.Font = new System.Drawing.Font(“新細明體”, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(136)));

            this.Name = “Form1”;

            this.Text = “Form1”;

            this.Load += new System.EventHandler(this.Form1_Load);

            this.Resize += new System.EventHandler(this.Form1_Resize);

            this.ResumeLayout(false);

 

                }

                #endregion

 

                ///

                /// The main entry point for the application.

                ///

                [STAThread]

                static void Main()

                {

                        Application.Run(new Form1());

                }

        public void CreateMenu()

        {

            IconPanel iconPanel1 = new IconPanel();

            IconPanel iconPanel2 = new IconPanel();

            IconPanel iconPanel3 = new IconPanel();

            outlookBar1.DelAllBand();

            outlookBar1.AddBand(imageList1.Images[0], “Outlook Shortcuts”, iconPanel1);

            outlookBar1.AddBand(“My Shortcuts”, iconPanel2);

            outlookBar1.AddBand(“Other Shortcuts”, iconPanel3);

 

            iconPanel1.AddIcon(“Outlook Today”, imageList1.Images[1], new EventHandler(PanelEvent));

            iconPanel1.AddIcon(“Calendar”, imageList1.Images[1], new EventHandler(PanelEvent));

            iconPanel1.AddIcon(“Contacts”, imageList1.Images[1], new EventHandler(PanelEvent));

            iconPanel1.AddIcon(“Tasks”, imageList1.Images[1], new EventHandler(PanelEvent));

 

            iconPanel2.AddIcon(“Calendar”, imageList1.Images[1], new EventHandler(PanelEvent));

            iconPanel2.AddIcon(“Outlook Today”, imageList1.Images[1], new EventHandler(PanelEvent));

            iconPanel2.AddIcon(“Contacts”, imageList1.Images[1], new EventHandler(PanelEvent));

            iconPanel2.AddIcon(“Tasks”, imageList1.Images[1], new EventHandler(PanelEvent));

 

            iconPanel3.AddIcon(“Calendar”, imageList1.Images[1], new EventHandler(PanelEvent));

            iconPanel3.AddIcon(“Outlook Today”, imageList1.Images[1], new EventHandler(PanelEvent));

            iconPanel3.AddIcon(“Tasks”, imageList1.Images[1], new EventHandler(PanelEvent));

            iconPanel3.AddIcon(“Contacts”, imageList1.Images[1], new EventHandler(PanelEvent));

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            outlookBar1.Initialize();

            CreateMenu();

            outlookBar1.SelectBand(2);

        }

 

        private void Form1_Resize(object sender, EventArgs e)

        {

            CreateMenu();

        }

        }

}

 

 

 

 

 

 



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦