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

在 C#.NET 中创建浮动/滑动/移动菜单

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.70/5 (8投票s)

2014年11月14日

CPOL
viewsIcon

44831

downloadIcon

2802

C#.NET 中的浮动/滑动菜单或面板

引言

在这里,我将告诉您如何在 C# 中为您的 Windows 应用程序创建滑动/浮动面板。这是一种非常简单的技术。如果您喜欢,请尝试一下...

Using the Code

请遵循以下步骤

  1. 启动 Windows 窗体应用程序
  2. 添加一个面板(例如:Panel1)并将其停靠到 顶部
  3. 将两个按钮控件(例如:Button1Button2)放置在面板内,并将其停靠到 Panel1 的左侧和右侧。
  4. 将另一个面板(例如:Panel2)放置在 Panel1 内,并将 DockStyle 设置为“Fill”。
  5. 将一个“用户控件窗体”(例如:UserControl1)添加到您的项目中,并将所有控件放置在上面(例如:Panel1,2,3,4,5 & button1,2,3....17)。
  6. 将两个 Timer 控件添加到您的项目中(例如:timer1timer2)并设置 Interval = 5
  7. 最后,编写如下代码
//
//Form1

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 FloatingMenu
{
    public partial class Form1 : Form
    {
        UserControl usrCtrl = new UserControl1();           //Create an instance of UserControl1

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            usrCtrl.Left = usrCtrl.Top = 0;                //Set the location of UserControl1
            panel2.Controls.Add(usrCtrl);                  //Adding UserControl1 to Panel2
            usrCtrl.Show();                                //Shows UserControl1 inside Panel2
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            timer1.Start();                                //Enables timer1
        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            timer1.Stop();                                 //Disables timer1
        }

        private void button2_MouseDown(object sender, MouseEventArgs e)
        {
            timer2.Start();                                //Enables timer2
        }

        private void button2_MouseUp(object sender, MouseEventArgs e)
        {
            timer2.Stop();                                 //Disables timer2
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (usrCtrl.Left < 0)
            {
                usrCtrl.Left = usrCtrl.Left + 5;           //Move UserControl1 to right side
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (usrCtrl.Right >= panel2.Left + panel2.Width)
            {
                usrCtrl.Left = usrCtrl.Left - 5;           //Move UserControl1 to left side
            }
        }
    }
}

//End of code.

感谢您使用我的技巧。

享受编程吧...

© . All rights reserved.