国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#創建浮動工具欄功能示例

2020-01-24 03:00:42
字體:
來源:轉載
供稿:網友

所謂的浮動工具欄,效果圖如下:

也就是說,可以將工具欄拖出其原先的停靠位置,而且可以將拖出來的工具欄再拖放回去。

實現的基本思路如下

1、拖動出來以后,需要創建一個大小合適的窗口,作為工具欄新的停靠容器,這個窗口可以這樣設置:

FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;

ShowIcon = false;

ShowInTaskbar = false;

TopMost = true;

2、浮動工具欄可以擴展自.Net Framework提供的ToolStrip,它被拖動都某個位置,松開鼠標左鍵時,會觸發EndDarg事件,在這個事件中,我們將其從原來的停靠容器中移除,同時根據鼠標左鍵松開時,在鼠標所在位置上創建一個窗口,作為工具欄的新容器。

這個就是基本的思路了,下面是浮動工具欄FloatToolstrip 具體的實現代碼:

復制代碼 代碼如下:

代碼

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace FloatToolStripDemo
{
    public partial class FloatToolstrip : ToolStrip
    {
        private ToolStripPanel tsPanel;

        public FloatToolstrip()
        {
            InitializeComponent();
            this.EndDrag += new EventHandler(MyToolStrip_EndDrag);
            this.SizeChanged += new EventHandler(MyToolStrip_SizeChanged);
        }

        private ToolStripFloatWindow floatForm;
        public ToolStripFloatWindow FloatForm
        {
            get { return floatForm; }
            set
            {
                floatForm = value;
                if (floatForm != null)
                {
                    floatForm.LocationChanged += new EventHandler(floatForm_LocationChanged);
                    floatForm.FormClosing += new FormClosingEventHandler(floatForm_FormClosing);
                }
            }
        }

        void floatForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }

        private void floatForm_LocationChanged(object sender, EventArgs e)
        {
            //當floatwindws的位置移動到toolstrippanel中時,將this放置到 toolstripPanel上 
            if (this.floatForm == null)
            {
                return;
            }
            else
            {
                if (floatForm.HasCreated)
                {
                    Point currentPt = new Point(floatForm.Location.X, floatForm.Location.Y);
                    Point minPt = this.tsPanel.PointToScreen(tsPanel.Location);
                    Point maxPt;
                    if (this.tsPanel.Height <= 20)
                    {
                        maxPt = new Point(minPt.X + this.tsPanel.Width, minPt.Y + 20);

                    }
                    else
                    {
                        maxPt = new Point(minPt.X + this.tsPanel.Width, minPt.Y + this.tsPanel.Height);
                    }

                    if ((currentPt.X > minPt.X) && (currentPt.X < maxPt.X) && (currentPt.Y > minPt.Y - 25) && (currentPt.Y < maxPt.Y - 25))
                    {
                        this.floatForm.Controls.Remove(this);
                        this.tsPanel.SuspendLayout();
                        this.tsPanel.Controls.Add(this);
                        this.Location = this.tsPanel.PointToClient(currentPt);
                        this.tsPanel.ResumeLayout();
                        this.floatForm.Dispose();
                        this.floatForm = null;
                    }
                }
            }
        }

        public bool isFloating
        {
            get
            {
                return (floatForm != null);
            }
        }

        public ToolStripPanel ToolStripPanel
        {
            get
            {
                return this.tsPanel;
            }
            set
            {
                this.tsPanel = value;
            }
        }

        private void MyToolStrip_EndDrag(object sender, EventArgs e)
        {
            //判斷移除時
            if (this.tsPanel == null)
            {
                MessageBox.Show("請先設置ToolStripPanel屬性");
                return;
            }
            Point dockPoint = Cursor.Position;
            int openX, openY;
            openX = dockPoint.X;
            openY = dockPoint.Y;

            Point clientPt = this.tsPanel.Parent.PointToClient(dockPoint);
            if (clientPt.Y > tsPanel.Height)
            {
                ToolStripFloatWindow tsfw = new ToolStripFloatWindow();
                this.tsPanel.Controls.Remove(this);
                tsfw.Controls.Add(this);
                this.Left = 0;
                this.Top = 0;
                this.FloatForm = tsfw;
                Point newLoc = new Point(openX, openY);
                tsfw.Show();
                tsfw.Location = newLoc;
                tsfw.SetBounds(newLoc.X, newLoc.Y, this.ClientSize.Width, this.ClientSize.Height+25);
            }
        }

        private void MyToolStrip_SizeChanged(object sender, EventArgs e)
        {
            if (this.isFloating)
            {
                this.floatForm.Width = this.ClientSize.Width;
            }
        }
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 杭锦后旗| 西青区| 芷江| 桐乡市| 石屏县| 罗城| 古丈县| 淮南市| 海兴县| 遵义县| 巴林右旗| 织金县| 浏阳市| 新民市| 周宁县| 平潭县| 北安市| 岑溪市| 濮阳市| 辽源市| 鹤庆县| 丽水市| 普格县| 克山县| 惠水县| 房山区| 乌海市| 石阡县| 云林县| 隆尧县| 肥乡县| 金沙县| 信丰县| 股票| 江川县| 南昌县| 韩城市| 东宁县| 晋州市| 涿鹿县| 吴旗县|