当前位置: 移动技术网 > IT编程>开发语言>c# > 基于C#实现的仿windows左侧伸缩菜单效果

基于C#实现的仿windows左侧伸缩菜单效果

2019年07月18日  | 移动技术网IT编程  | 我要评论

本文所述为基于c#实现的折叠菜单,风格仿照windows打开我的电脑后左侧的伸缩菜单效果,并且同样是蓝色的效果,看着和windows的效果一样漂亮,可以实现折叠、展开等功能。这在学习c#界面编程的时候能用上,其主要实现代码如下:

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;
using system.runtime.interopservices;
namespace likesxp
{
  public partial class frm_main : form
  {
    public frm_main()
    {
      initializecomponent();
    }
    private static panel var_panel = new panel();//创建静态字段
    private static picturebox var_pict = //创建静态字段
      new picturebox();
    private static int var_i = 0;//创建静态字段
    private font var_font = new font("宋体", 9); //创建字体字段
    private void picturebox_1_click(object sender, eventargs e)
    {
      var_i = convert.toint16((//得到控件中的数据
        (picturebox)sender).tag.tostring());
      switch (var_i)
      {
        case 1:
          {
            var_panel = panel_gut_1;//得到面板对象引用
            //var_pict = picturebox_1;//得到picturebox对象引用
            break;
          }
        case 2:
          {
            var_panel = panel_gut_2;//得到面板对象引用
            var_pict = picturebox_2;//得到picturebox对象引用
            break;
          }
        case 3:
          {
            var_panel = panel_gut_3;//得到面板对象引用
            var_pict = picturebox_3;//得到picturebox对象引用
            break;
          }
      }
      if (convert.toint16(var_panel.tag.tostring()) == 0 || convert.toint16(var_panel.tag.tostring()) == 2)
      {
        var_panel.tag = 1;//设置为隐藏标识
        var_pict.image = properties.resources.朝下按钮;//设置图像属性
        var_panel.visible = false;//隐藏面板
      }
      else
      {
        if (convert.toint16(var_panel.tag.tostring()) == 1)
        {
          var_panel.tag = 2;//设置为显示标识
          var_pict.image = properties.resources.朝上按钮;//设置图像属性
          var_panel.visible = true;//显示面板
        }
      }
    }
    private void form1_load(object sender, eventargs e)
    {
      //picturebox_1.image = properties.resources.朝上按钮;//设置图像信息
      picturebox_2.image = properties.resources.朝上按钮;//设置图像信息
      picturebox_3.image = properties.resources.朝上按钮;//设置图像信息
      var_font = label_1.font;//得到字体对象
    }
    private void label_1_mouseenter(object sender, eventargs e)
    {
      ((label)sender).forecolor = color.gray;//设置控件文字字颜色
      ((label)sender).font = //设置控件字体
        new font(var_font, var_font.style | fontstyle.underline);
    }
    private void label_1_mouseleave(object sender, eventargs e)
    {
      ((label)sender).forecolor = color.black;//设置控件文字颜色
      ((label)sender).font = //设置控件字体
        new font(var_font, var_font.style);
    }
    private void picturebox1_click(object sender, eventargs e)
    {
      if (convert.toint16(panel1.tag.tostring()) == 1)
      {
        timer1.start();
        convert.todatetime("").toshortdatestring();
        // picturebox1.image = properties.resources.朝下按钮;//设置图像属性
        //panel1.visible = false;//隐藏面板
      }
      else
      {
        if (convert.toint16(panel1.tag.tostring()) == 0)
        {
          timer1.start();
          //panel1.tag = 1;//设置为显示标识
          //picturebox1.image = properties.resources.朝上按钮;//设置图像属性
          //panel1.visible = true;//显示面板
        }
      }
    }
    private void timer1_tick(object sender, eventargs e)
    {
      long longwidth = panel1.width;
      //如果panel目前是隐藏的
      if (convert.toint16(panel1.tag.tostring()) == 0)
      {
        if (longwidth == 200)
        {
          timer1.enabled = false;
          panel1.tag = 1;//设置为显示标识
        }
        else
        {
          panel1.width += 10;
        }
      }
      //如果panel目前是显示的
      if (convert.toint16(panel1.tag.tostring()) == 1)
      {
        if (longwidth == 0)
        {
          timer1.enabled = false;
          panel1.tag = 0;//设置为隐藏标识
        }
        else
        {
          panel1.width -= 10;
        }
      }
    }
  }
}

这里只展示了主要的功能代码,其他的实现细节读者可以自己进一步完善。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网