首页 > 编程语言 >C# GUI(Winform)案例

C# GUI(Winform)案例

时间:2022-11-10 02:55:40浏览次数:63  
标签:控件 ofd C# GUI System private using void Winform

项目知识点补充

① C# 项目没有 sln 怎么用 VS 打开

② 临时测试,程序是否运行成功

// 控制台输出内容 Console.WriteLine(...);

// 比如下面就是测试 vlc 控件是否运行成功,输出 mp4 文件名
private void openbutton_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "视频文件(*.mp4)|*.mp4";

    if(ofd.ShowDialog() == DialogResult.OK)
    {
        Console.WriteLine(ofd.FileName);
    }
}

③ 控件名改了,但代码没变

对代码控件名部分使用 ctrl + H,更改成新的控件名

④ 控件添加动作(如 click)

要点击 GUI 界面的控件并 Enter,出来代码再做修改,直接复制粘贴代码是没有用的

案例一:音乐播放器

参考:C#使用axWindowsMediaPlayer实现音乐播放器

示例图片

代码示例

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Video_player
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<string> listSong = new List<string>();
        private void openbutton_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "请选择文件";
            ofd.Multiselect = true;
            ofd.InitialDirectory = @"C:\Users\14805\Desktop\music";
            ofd.Filter = "mp3文件|*.mp3";
            ofd.ShowDialog();
            //获得我们在文件夹中选择所有文件的全路径
            string[] path = ofd.FileNames;
            for (int i = 0; i<path.Length; i++)
            {
                //将音乐文件的文件名加载到ListBox中
                listBox1.Items.Add(Path.GetFileName(path[i]));
                //将音乐文件的全路径存储到泛型集合中
                listSong.Add(path[i]);
            }
        }

        private void playbutton_Click(object sender, EventArgs e)
        {
            if (playbutton.Text=="播放")
            {
                // axWindowsMediaPlayer1.URL = listSong[listBox1.SelectedIndex];
                axWindowsMediaPlayer1.Ctlcontrols.play();
                playbutton.Text = "暂停";
            }
            else
            {
                axWindowsMediaPlayer1.Ctlcontrols.pause();
                playbutton.Text = "播放";
            }
        }

        private void lastbutton_Click(object sender, EventArgs e)
        {
            // 获得当前选中歌曲的索引
            int index = listBox1.SelectedIndex;
            index--;
            if (index < 0)
            {
                index = listBox1.Items.Count - 1;
            }
            //将重新改变后的索引重新的赋值给当前选中项
            listBox1.SelectedIndex = index;
            axWindowsMediaPlayer1.URL = listSong[index];
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }

        private void nextbutton_Click(object sender, EventArgs e)
        {
            int index = listBox1.SelectedIndex;
            index++;
            if (index==listBox1.Items.Count)
            {
                index = 0;
            }
            listBox1.SelectedIndex = index;
            axWindowsMediaPlayer1.URL = listSong[index];
            axWindowsMediaPlayer1.Ctlcontrols.play();
        }
    }
}

步骤:
① 添加 TableLayoutPanel 控件
用于布局接下来的控件

② 添加控件(ListBox、axWindowsMediaPlayer、FlowLayoutPanel、Button)
ListBox:RowSpan 属性设置 2(跨列)
axWindowsMediaPlayer:在 COM 组件中找到
FlowLayoutPanel:为多个 Button 控件安放位置,因为 TableLayoutPanel 一个位置只能有一个控件
Button:共四个,分别为 openbutton、playbutton、lastbutton、nextbutton,作用如其名

思考
明白了先布局后布置控件的步骤是重要的,而且在布置控件的过程中,我发现有些控件很难摆放到自己想要的位置,说白了还是自己不熟练(或者根本不明白各属性的作用),这个小项目只有 5% 是按照自己想法完成的,大部分还是参考网上大佬的,毕竟是速成嘛

案例二:视频播放器

大步骤一:下载并尝试 vlc 控件

参考:C# winform vlc 视频播放

示例图片

代码示例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace vlc_test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void openbutton_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "视频文件(*.mp4)|*.mp4";

            if(ofd.ShowDialog() == DialogResult.OK)
            {
                Console.WriteLine(ofd.FileName);

                vlcControl1.SetMedia(new System.IO.FileInfo(ofd.FileName));
            }
        }

        private void playbutton_Click(object sender, EventArgs e)
        {
            if(playbutton.Text == "播放")
            {
                vlcControl1.Play();
                playbutton.Text = "暂停";
            } else
            {
                vlcControl1.Pause();
                playbutton.Text = "播放";
            }
        }

        private void stopbutton_Click(object sender, EventArgs e)
        {
            vlcControl1.Stop();
        }
    }
}

细分步骤
① 更改调试功能 Any CPUx64

② 工具 》NuGet 包管理器 》管理解决方案的 NuGet 程序包,并下载 Vlc.DotNet.Forms 插件,并经过一系列调整(具体细节观看参考视频

③ 写代码(轻车熟路了已经)

大步骤二:初步美化

参考视频:基于C#的扁平化音乐播放器UI实现——高响应、侧边栏、Drop Down按钮
参考文章:IU Moderno, Menú

示例图片

代码示例:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace beautiful_vlc
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            hideSubMenu();
        }

        private void hideSubMenu()
        {
            // 隐藏子菜单
            panelMediaSubMenu.Visible = false;
            panelPlaylistSubMenu.Visible = false;
            panelToolsSubMenu.Visible = false;
        }

        private void showSubMenu(Panel subMenu)
        {
            if (subMenu.Visible == false)
            {
                hideSubMenu();
                subMenu.Visible = true;
            }
            else
                subMenu.Visible = false;
        }

        private void btnMedia_Click(object sender, EventArgs e)
        {
            showSubMenu(panelMediaSubMenu);
        }
        #region MediaSubMenu

        #endregion

        private void btnPlaylist_Click(object sender, EventArgs e)
        {
            showSubMenu(panelPlaylistSubMenu);
        }
        #region PlaylistSubMenu

        #endregion

        private void btnEqualizer_Click(object sender, EventArgs e)
        {
            openChildFormInPanel(new Form3());
            //..
            //your codes
            //..
            hideSubMenu();
        }

        private void btnTools_Click(object sender, EventArgs e)
        {
            showSubMenu(panelToolsSubMenu);
        }
        #region ToolsSubMenu


        #endregion

        private void btnHelp_Click(object sender, EventArgs e)
        {
            hideSubMenu();
        }

        private Form activeForm = null;
        private void openChildFormInPanel(Form childForm)
        {
            if (activeForm != null)
                activeForm.Close();
            activeForm = childForm;
            childForm.TopLevel = false;
            childForm.FormBorderStyle = FormBorderStyle.None;
            childForm.Dock = DockStyle.Fill;
            panelChildForm.Controls.Add(childForm);
            panelChildForm.Tag = childForm;
            childForm.BringToFront();
            childForm.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openChildFormInPanel(new Form2());
            //..
            //your codes
            //..
            hideSubMenu();
        }
    }
}

细分步骤
跟着视频走即可

大步骤三:最终美化并打包

参考文章:Diseño IU
参考视频:去 YouTuBe 找!!!

思考
相比于 WPF,Winform 播放视频还得另下载 vlc 控件,真是麻烦,不过 WPF 在只能写代码上,可能会更麻烦。看了很多扁平化视频,包括下面的评论,我发现扁平化可能是最简单的入门项目,进阶还需要学习 mvvm 模式(mvc 模式的改进版),而且比较理解了不止有 WPF、Winform 这两个基于 .net 的框架,还要很多很多框架(甚至能跨平台),也比较能看懂 C# 是服务于这些框架的小 case 了,看来自顶向下学习总归是没错的(谢谢催稿的冯老师

标签:控件,ofd,C#,GUI,System,private,using,void,Winform
From: https://www.cnblogs.com/CourserLi/p/15736350.html

相关文章

  • C# GUI(Winform)测试
    测试连接Mysql数据库参考博客:VisualStudio2017,C#winform项目连接Mysql数据库代码如下:usingMySql.Data.MySqlClient;privatevoidbutton1_Click(objectsender,Ev......
  • Python: convert int to mode string
     def_convert_mode(mode:int):ifnot0<=mode<=0o777:raiseRuntimeErrorres=''forvinrange(0,9):ifmode>>v&1:......
  • 基于Python的批量处理execl文件内容
    今天遇到一个棘手的问题,在三个文件夹中将近60个execl表选出所需的特定三列数据,且表名,sheet名,表中的数据类型均不一致,故想到利用Python批量化处理技术手段进行处理。其原理......
  • Spring Security 知识点总结
    Security部分WebSecurityConfigurerAdaptersecurity配置的核心类在这里配置权限等信息authenticationauthentication是认证(登陆)authorizationauthorizati......
  • C系统级编程-复习
    数组对象类型ArrayofType,它是多个相同对象类型的一维派生类型,包含两要素:元素个数,元素的对象类型所谓多维数组,不过是元素的迭代衍生,本质还是一维的声明对象标识......
  • curl跟踪重定向
    curl -Li https://xxxi 输出响应头,这样就能定位302之类的L跟踪重定向到最后一种是通过302重定向,需要解析header里面的内容<?php$context=stream_context_create(......
  • Docker WSLl2 目录相关记录
    windows10中的docker是基于wls2的。想访问docker的配置文件的话可以通过wsl2的目录来访问。在资源管理器中输入\\wsl$就可以找到相关的目录。比如我要改一个容器中的目......
  • vi 常用命令(CentOS 默认编辑器)
     一、关于vivi是最强大的文本编辑器,没有之一。尽管 vi已经是古董级的软件,但还是有无数新人迎着困难去学习使用,可见其经典与受欢迎的程度。无论是小说中还是电视剧,真正......
  • Spring Security 知识点总结
    Security部分WebSecurityConfigurerAdaptersecurity配置的核心类在这里配置权限等信息authenticationauthentication是认证(登陆)authorizationauthorizati......
  • LeetCode 题解 394. 字符串解码
    题目描述给定一个经过编码的字符串,返回它解码后的字符串。编码规则为:k[encoded_string],表示其中方括号内部的encoded_string正好重复k次。注意k保证为正整数。......