当前位置: 代码网 > it编程>编程语言>C# > 基于C#设计一个带导航菜单的主界面

基于C#设计一个带导航菜单的主界面

2024年05月28日 C# 我要评论
在c#中,可以使用windows forms或wpf来设计带有导航菜单的主界面。一、关于导航菜单导航菜单是一种用户界面元素,用于帮助用户在应用程序的不同部分之间进行导航。它通常包含一个或多个菜单项,每

  在c#中,可以使用windows forms或wpf来设计带有导航菜单的主界面。

一、关于导航菜单

导航菜单是一种用户界面元素,用于帮助用户在应用程序的不同部分之间进行导航。它通常包含一个或多个菜单项,每个菜单项都表示一个特定的功能或操作。当用户单击某个菜单项时,应用程序会执行相应的操作或显示与该操作相关的子菜单。

导航菜单可以出现在应用程序的顶部、底部或侧边,具体取决于应用程序的设计和平台。例如,在web应用程序中,导航菜单通常出现在页面的顶部或侧边,而在移动应用程序中,导航菜单可能出现在屏幕的底部或顶部。

导航菜单的主要目的是提供一个直观且一致的方式来访问应用程序的主要功能和特性。它可以帮助用户快速找到他们需要的信息或完成特定的任务,从而提高应用程序的可用性和用户体验。

二、设计一个带导航菜单的主界面

在窗体中添加一个menustrip控件,用来设计菜单栏;添加一个splitcontainer控件,用来将窗体分为两部分;添加一个listview控件和3个button控件,并将它们加入sqlitcontainer1的左侧部分,用来制作窗体左侧的导航栏;添加一个imagelist组件,用来为listview列表项提供图标。

1.resources.designer.cs设计

在图片资源管理器中设计项目需要的背景图片和imagelist组件的图片。

如何加载图片资源详见本文作者写的其他文章:c#手动改变自制窗体的大小

// resources.designer.cs
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:4.0.30319.42000
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
 
namespace _176.properties {
    using system;
    
    
    /// <summary>
    ///   一个强类型的资源类,用于查找本地化的字符串等。
    /// </summary>
    // 此类是由 stronglytypedresourcebuilder
    // 类通过类似于 resgen 或 visual studio 的工具自动生成的。
    // 若要添加或移除成员,请编辑 .resx 文件,然后重新运行 resgen
    // (以 /str 作为命令选项),或重新生成 vs 项目。
    [global::system.codedom.compiler.generatedcodeattribute("system.resources.tools.stronglytypedresourcebuilder", "17.0.0.0")]
    [global::system.diagnostics.debuggernonusercodeattribute()]
    [global::system.runtime.compilerservices.compilergeneratedattribute()]
    internal class resources {
        
        private static global::system.resources.resourcemanager resourceman;
        
        private static global::system.globalization.cultureinfo resourceculture;
        
        [global::system.diagnostics.codeanalysis.suppressmessageattribute("microsoft.performance", "ca1811:avoiduncalledprivatecode")]
        internal resources() {
        }
        
        /// <summary>
        ///   返回此类使用的缓存的 resourcemanager 实例。
        /// </summary>
        [global::system.componentmodel.editorbrowsableattribute(global::system.componentmodel.editorbrowsablestate.advanced)]
        internal static global::system.resources.resourcemanager resourcemanager {
            get {
                if (object.referenceequals(resourceman, null)) {
                    global::system.resources.resourcemanager temp = new global::system.resources.resourcemanager("_176.properties.resources", typeof(resources).assembly);
                    resourceman = temp;
                }
                return resourceman;
            }
        }
        
        /// <summary>
        ///   重写当前线程的 currentuiculture 属性,对
        ///   使用此强类型资源类的所有资源查找执行重写。
        /// </summary>
        [global::system.componentmodel.editorbrowsableattribute(global::system.componentmodel.editorbrowsablestate.advanced)]
        internal static global::system.globalization.cultureinfo culture {
            get {
                return resourceculture;
            }
            set {
                resourceculture = value;
            }
        }
        
        /// <summary>
        ///   查找 system.drawing.bitmap 类型的本地化资源。
        /// </summary>
        internal static system.drawing.bitmap _1 {
            get {
                object obj = resourcemanager.getobject("_1", resourceculture);
                return ((system.drawing.bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 system.drawing.bitmap 类型的本地化资源。
        /// </summary>
        internal static system.drawing.bitmap _2 {
            get {
                object obj = resourcemanager.getobject("_2", resourceculture);
                return ((system.drawing.bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 system.drawing.bitmap 类型的本地化资源。
        /// </summary>
        internal static system.drawing.bitmap _3 {
            get {
                object obj = resourcemanager.getobject("_3", resourceculture);
                return ((system.drawing.bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 system.drawing.bitmap 类型的本地化资源。
        /// </summary>
        internal static system.drawing.bitmap _4 {
            get {
                object obj = resourcemanager.getobject("_4", resourceculture);
                return ((system.drawing.bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 system.drawing.bitmap 类型的本地化资源。
        /// </summary>
        internal static system.drawing.bitmap _5 {
            get {
                object obj = resourcemanager.getobject("_5", resourceculture);
                return ((system.drawing.bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 system.drawing.bitmap 类型的本地化资源。
        /// </summary>
        internal static system.drawing.bitmap _6 {
            get {
                object obj = resourcemanager.getobject("_6", resourceculture);
                return ((system.drawing.bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 system.drawing.bitmap 类型的本地化资源。
        /// </summary>
        internal static system.drawing.bitmap _7 {
            get {
                object obj = resourcemanager.getobject("_7", resourceculture);
                return ((system.drawing.bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 system.drawing.bitmap 类型的本地化资源。
        /// </summary>
        internal static system.drawing.bitmap _8 {
            get {
                object obj = resourcemanager.getobject("_8", resourceculture);
                return ((system.drawing.bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 system.drawing.bitmap 类型的本地化资源。
        /// </summary>
        internal static system.drawing.bitmap _9 {
            get {
                object obj = resourcemanager.getobject("_9", resourceculture);
                return ((system.drawing.bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 system.drawing.bitmap 类型的本地化资源。
        /// </summary>
        internal static system.drawing.bitmap c_编程词典 {
            get {
                object obj = resourcemanager.getobject("c_编程词典", resourceculture);
                return ((system.drawing.bitmap)(obj));
            }
        }
 
        internal static bitmap getobject(string v)
        {
            return v switch
            {
                "c_编程词典" => c_编程词典,
                "_9" => _9,
                "_8" => _8,
                "_7" => _7,
                "_6" => _6,
                "_5" => _5,
                "_4" => _4,
                "_3" => _3,
                "_2" => _2,
                "_1" => _1,
                _ => null
            };
        }
    }
}

2.主要的设计步骤

(1)窗体静态设计

设计一个窗体;

在窗体中设计一个menustrip1控件,默认的控件位于form1的顶部,重要的设置menustrip1控件的dock属性设置为top;最后设计3个menuitem子控件:设置、打开、编辑;

在窗体剩余空间中添加一个 splitcontainer控件,之所以说剩余空间,就是要保证menustrip1控件不被 splitcontainer控件包含,这哈斯有menustrip1的dock属性保证的;

设计图片资源管理器,并给右侧的splitcontainer2添加背景图片,stretch布局;

在左侧的splitcontainer2控件里,添加3个button控件,并命名为:设置、打开、编辑;设置控件的dock属性为top,设置编辑控件的dock属性为top,最后,设置打开按钮的dock属性为top;结果是,设置按钮位于splitcontainer2控件顶部,编辑按钮位于splitcontainer2控件底部,打开按钮位于编辑按钮的顶部;这样的设置很重要;

在左侧的splitcontainer2控件里设置按钮和打开按钮之间添加 listview1控件,其dock属性设置为fill,这很重要;

添加imagelist1控件,并为其添加图片集;

设置 listview1控件的属性,右上角的尖角,选择大图标、imagelist1、imagelist1;

创建窗体、按钮1~3的click事件;

// form1.designer.cs
namespace _176
{
    partial class form1
    {
        /// <summary>
        ///  required designer variable.
        /// </summary>
        private system.componentmodel.icontainer components = null;
 
        /// <summary>
        ///  clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.dispose();
            }
            base.dispose(disposing);
        }
 
        #region windows form designer generated code
 
        /// <summary>
        ///  required method for designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void initializecomponent()
        {
            components = new system.componentmodel.container();
            system.componentmodel.componentresourcemanager resources = new system.componentmodel.componentresourcemanager(typeof(form1));
            imagelist1 = new imagelist(components);
            menustrip1 = new menustrip();
            toolstripmenuitem1 = new toolstripmenuitem();
            toolstripmenuitem2 = new toolstripmenuitem();
            toolstripmenuitem3 = new toolstripmenuitem();
            splitcontainer1 = new splitcontainer();
            listview1 = new listview();
            button3 = new button();
            button2 = new button();
            button1 = new button();
            menustrip1.suspendlayout();
            ((system.componentmodel.isupportinitialize)splitcontainer1).begininit();
            splitcontainer1.panel1.suspendlayout();
            splitcontainer1.suspendlayout();
            suspendlayout();
            // 
            // imagelist1
            // 
            imagelist1.colordepth = colordepth.depth32bit;
            imagelist1.imagestream = (imageliststreamer)resources.getobject("imagelist1.imagestream");
            imagelist1.transparentcolor = color.transparent;
            imagelist1.images.setkeyname(0, "1.png");
            imagelist1.images.setkeyname(1, "2.jpg");
            imagelist1.images.setkeyname(2, "3.png");
            imagelist1.images.setkeyname(3, "4.png");
            imagelist1.images.setkeyname(4, "5.png");
            imagelist1.images.setkeyname(5, "6.png");
            imagelist1.images.setkeyname(6, "7.png");
            imagelist1.images.setkeyname(7, "8.png");
            imagelist1.images.setkeyname(8, "9.png");
            // 
            // menustrip1
            // 
            menustrip1.items.addrange(new toolstripitem[] { toolstripmenuitem1, toolstripmenuitem2, toolstripmenuitem3 });
            menustrip1.location = new point(0, 0);
            menustrip1.name = "menustrip1";
            menustrip1.size = new size(434, 25);
            menustrip1.tabindex = 0;
            menustrip1.text = "menustrip1";
            // 
            // toolstripmenuitem1
            // 
            toolstripmenuitem1.name = "toolstripmenuitem1";
            toolstripmenuitem1.size = new size(44, 21);
            toolstripmenuitem1.text = "设置";
            // 
            // toolstripmenuitem2
            // 
            toolstripmenuitem2.name = "toolstripmenuitem2";
            toolstripmenuitem2.size = new size(44, 21);
            toolstripmenuitem2.text = "打开";
            // 
            // toolstripmenuitem3
            // 
            toolstripmenuitem3.name = "toolstripmenuitem3";
            toolstripmenuitem3.size = new size(44, 21);
            toolstripmenuitem3.text = "编辑";
            // 
            // splitcontainer1
            // 
            splitcontainer1.dock = dockstyle.fill;
            splitcontainer1.location = new point(0, 25);
            splitcontainer1.name = "splitcontainer1";
            // 
            // splitcontainer1.panel1
            // 
            splitcontainer1.panel1.controls.add(listview1);
            splitcontainer1.panel1.controls.add(button3);
            splitcontainer1.panel1.controls.add(button2);
            splitcontainer1.panel1.controls.add(button1);
            // 
            // splitcontainer1.panel2
            // 
            splitcontainer1.panel2.backgroundimage = properties.resources.c_编程词典;
            splitcontainer1.panel2.backgroundimagelayout = imagelayout.stretch;
            splitcontainer1.size = new size(434, 276);
            splitcontainer1.splitterdistance = 144;
            splitcontainer1.tabindex = 1;
            // 
            // listview1
            // 
            listview1.dock = dockstyle.fill;
            listview1.largeimagelist = imagelist1;
            listview1.location = new point(0, 23);
            listview1.name = "listview1";
            listview1.size = new size(144, 207);
            listview1.smallimagelist = imagelist1;
            listview1.tabindex = 3;
            listview1.usecompatiblestateimagebehavior = false;
            // 
            // button3
            // 
            button3.dock = dockstyle.bottom;
            button3.location = new point(0, 230);
            button3.name = "button3";
            button3.size = new size(144, 23);
            button3.tabindex = 2;
            button3.text = "编辑";
            button3.usevisualstylebackcolor = true;
            button3.click += button3_click;
            // 
            // button2
            // 
            button2.dock = dockstyle.bottom;
            button2.location = new point(0, 253);
            button2.name = "button2";
            button2.size = new size(144, 23);
            button2.tabindex = 1;
            button2.text = "打开";
            button2.usevisualstylebackcolor = true;
            button2.click += button2_click;
            // 
            // button1
            // 
            button1.dock = dockstyle.top;
            button1.location = new point(0, 0);
            button1.name = "button1";
            button1.size = new size(144, 23);
            button1.tabindex = 0;
            button1.text = "设置";
            button1.usevisualstylebackcolor = true;
            button1.click += button1_click;
            // 
            // form1
            // 
            autoscaledimensions = new sizef(7f, 17f);
            autoscalemode = autoscalemode.font;
            clientsize = new size(434, 301);
            controls.add(splitcontainer1);
            controls.add(menustrip1);
            mainmenustrip = menustrip1;
            name = "form1";
            startposition = formstartposition.centerscreen;
            text = "带导航菜单的窗体";
            load += form1_load;
            menustrip1.resumelayout(false);
            menustrip1.performlayout();
            splitcontainer1.panel1.resumelayout(false);
            ((system.componentmodel.isupportinitialize)splitcontainer1).endinit();
            splitcontainer1.resumelayout(false);
            resumelayout(false);
            performlayout();
        }
 
        #endregion
 
        private imagelist imagelist1;
        private menustrip menustrip1;
        private toolstripmenuitem toolstripmenuitem1;
        private toolstripmenuitem toolstripmenuitem2;
        private toolstripmenuitem toolstripmenuitem3;
        private splitcontainer splitcontainer1;
        private listview listview1;
        private button button3;
        private button button2;
        private button button1;
    }
}

(2)事件的动作设计

// form1.cs
namespace _176
{
    public partial class form1 : form
    {
        public form1()
        {
            initializecomponent();
        }
 
        /// <summary>
        /// 默认设置
        /// </summary>
        private void form1_load(object sender, eventargs e)
        {
            listview1.clear();
            listview1.largeimagelist = imagelist1;                                      //设置当前项以大图标的形式显示时用到的图像
            listview1.items.add("设置上下班时间", "设置上下班时间", 0);    //向listview1中添加项“设置上下班时间”
            listview1.items.add("是否启用短信提醒", "是否启用短信提醒", 1);//向listview1中添加项“是否启用短信提醒”
            listview1.items.add("设置密码", "设置密码", 2);               //向listview1中添加项“设置密码”
        }
 
        /// <summary>
        /// 设置
        /// </summary>
        private void button1_click(object sender, eventargs e)
        {
            listview1.dock = dockstyle.none;    //设置listview1的绑定属性为未绑定
            button1.dock = dockstyle.top;       //设置button1的绑定属性为上端绑定
            button2.dock = dockstyle.bottom;    //设置button2的绑定属性为底端绑定
            button3.sendtoback();               //将button3发送到z顺序的后面
            button3.dock = dockstyle.bottom;    //设置button3的绑定属性为底端绑定
            listview1.bringtofront();           //将listview1带到z顺序的前面
            listview1.dock = dockstyle.bottom;  //设置listview1的绑定属性为底端绑定
            listview1.clear();
            listview1.items.add("设置上下班时间", "设置上下班时间", 0);
            listview1.items.add("是否启用短信提醒", "是否启用短信提醒", 1);
            listview1.items.add("设置密码", "设置密码", 2);
        }
 
        /// <summary>
        /// 打开
        /// </summary>
        private void button2_click(object sender, eventargs e)
        {
            listview1.dock = dockstyle.none;  //设置listview1的绑定属性为未绑定
            button2.dock = dockstyle.top;     //设置button2的绑定属性为上端绑定
            button1.sendtoback();             //将控件button1发送到z顺序的后面
            button1.dock = dockstyle.top;     //设置button1的绑定属性为上端绑定
            button3.dock = dockstyle.bottom;  //设置button3的绑定属性为底端绑定
            listview1.dock = dockstyle.bottom;//设置listview1的绑定属性为底端绑定
            listview1.clear();
            listview1.items.add("近期工作记录", "近期工作记录", 3);
            listview1.items.add("近期工作计划", "近期工作计划", 4); 
        }
 
        /// <summary>
        /// 编辑
        /// </summary>
        private void button3_click(object sender, eventargs e)
        {
            listview1.dock = dockstyle.none;    //设置listview1的绑定属性为未绑定
            button3.sendtoback();               //将button3发送到z顺序的后面
            button3.dock = dockstyle.top;       //设置button3的绑定属性为上端绑定
            button2.sendtoback();               //将button2发送到z顺序的后面
            button2.dock = dockstyle.top;       //设置button2的绑定属性为上端绑定
            button1.sendtoback();               //将button1发送到z顺序的后面
            button1.dock = dockstyle.top;       //设置button1的绑定属性为上端绑定
            listview1.dock = dockstyle.bottom;  //设置listview1的绑定属性为底端绑定
            listview1.clear();
            listview1.items.add("编辑工作进度报告", "编辑工作进度报告", 5);
            listview1.items.add("编辑项目设计图", "编辑项目设计图", 6);
        }
    }
}

3.涉及到的主要知识点

(1)control.sendtoback 方法

将控件发送到 z 顺序的后面。

public void sendtoback ();

控件移动到 z 顺序的后面。 如果控件是另一个控件的子控件,则子控件将移动到 z 顺序的后面。 如果控件是顶级控件,则除非控件处于活动状态,否则此方法将无法正常工作。 顶级控件是一个控件,如 form,它不是另一个控件的子控件。 活动控件是具有输入焦点的可见控件。 若要将 sendtoback 方法与非活动顶级控件一起使用,请先对 控件调用 bringtofront 方法。

(2)control.bringtofront 方法

将控件带到 z 顺序的前面。

public void bringtofront ();

控件将移动到 z 顺序的前面。 如果控件是另一个控件的子控件,则子控件将移动到 z 顺序的前面。 bringtofront 不会使控件成为顶级控件,也不会引发 paint 事件。

(3)control.updatezorder 方法

按其父级的 z 顺序更新控件。

protected void updatezorder ();

updatezorder 方法更新控件在其父控件的 z 顺序中的位置。例如,如果此控件是添加到 control.controlcollection 中的新创建的控件,则 z 顺序将更新为添加到后面的新控件。

(4) listview控件的items.add方法

listview控件的items属性用来获取包含控件中所有项的集合,其add方法用来将项添加到listview列表中。

public virtual listviewltem add(string key,string text,int imagelndex)

参    数说    明参    数说    明
key项的名称imageindex要为该项显示的图像的索引
text项的文本返回值添加到集合中的listviewitem

5) z-order

c# 中的 z-order 是一种确定窗口或控件在屏幕上的堆叠顺序的方式。它由窗口或控件在坐标系中的 z 值决定。具有更高 z 值的窗口或控件位于具有较低 z 值的窗口或控件之上。

在 c# 中,您可以使用 sendtoback 方法将指定控件发送到 z-order 的末尾,这意味着它将位于所有其他控件之后。这在您想要创建一个透明窗口或控件,并且需要将其放在其他所有控件之后的情况下非常有用。

到此这篇关于基于c#设计一个带导航菜单的主界面的文章就介绍到这了,更多相关c#带导航菜单界面内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com