白骑士的C#教学实战项目篇 4.2 图形用户界面(GUI)应用

系列目录

上一篇:白骑士的C#教学实战项目篇 4.1 控制台应用程序

在这一部分,我们将从简单的控制台应用程序过渡到图形用户界面(GUI)应用程序。GUI 应用程序更加直观和用户友好,是现代软件开发的核心内容。我们将介绍如何使用 Windows Forms 和 Windows Presentation Foundation (WPF) 开发桌面应用程序,并通过开发一个记事本应用项目来巩固所学知识。

图形用户界面(GUI)应用程序使用户可以通过图形元素与应用程序进行交互。这些图形元素包括窗口、按钮、文本框等。在 C# 中,主要有两种常见的 GUI 开发框架:Windows Forms 和 Windows Presentation Foundation (WPF)。

使用 Windows Forms 开发桌面应用

Windows Forms 是 .NET 框架的一部分,用于创建具有图形界面的桌面应用程序。它简单易用,适合快速开发小型应用程序。

创建一个简单的 Windows Forms 应用程序

  1. **新建项目:**在 Visual Studio 中,选择 "创建新项目",选择 "Windows Forms App (.NET Framework)" 模板。

  2. **设计界面:**使用拖放方式添加控件,例如按钮和文本框。

  3. 编写代码:

    cs 复制代码
    using System;
    using System.Windows.Forms;
    
    
    public class MainForm : Form
    {
        private Button button;
        private TextBox textBox;
    
        public MainForm()
        {
            button = new Button();
            button.Text = "Click Me";
            button.Location = new System.Drawing.Point(50, 50);
            button.Click += Button_Click;
    
            textBox = new TextBox();
            textBox.Location = new System.Drawing.Point(50, 100);
            textBox.Width = 200;
    
            Controls.Add(button);
            Controls.Add(textBox);
        }
    
        private void Button_Click(object sender, EventArgs e)
        {
            textBox.Text = "Hello, Windows Forms!";
        }
    
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }

在这个示例中,创建了一个包含按钮和文本框的简单窗体应用程序。当用户点击按钮时,文本框中会显示 "Hello, Windows Forms!"。

使用 WPF 开发现代界面应用

Windows Presentation Foundation (WPF) 是一个用于构建现代桌面应用程序的 UI 框架,提供了更丰富的图形和动画支持。

创建一个简单的 WPF 应用程序

  1. **新建项目:**在 Visual Studio 中,选择 "创建新项目",选择 "WPF App (.NET Core)" 模板。

  2. 设计界面: 在 XAML 文件中定义界面元素:

    XML 复制代码
    <Window x:Class="WpfApp.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="200" Width="300">
        <Grid>
            <Button Name="button" Content="Click Me" Width="100" Height="30" VerticalAlignment="Top" Margin="100,50,0,0" Click="Button_Click"/>
            <TextBox Name="textBox" Width="200" Height="30" VerticalAlignment="Top" Margin="50,100,0,0"/>
        </Grid>
    </Window>
  3. 编写代码:

    cs 复制代码
    using System.Windows;
    
    
    namespace WpfApp
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                textBox.Text = "Hello, WPF!";
            }
        }
    }

在这个示例中,创建了一个包含按钮和文本框的简单 WPF 应用程序。当用户点击按钮时,文本框中会显示 "Hello, WPF!"。

实践项目:开发一个记事本应用

现在我们将综合运用 Windows Forms 和 WPF 技术,开发一个简单的记事本应用程序。这个应用程序将包括基本的文本编辑功能,如新建、打开、保存和文本编辑。

使用 Windows Forms 开发记事本应用

  1. **设计界面:**在设计视图中添加菜单栏、文本框和相关控件。

  2. 编写代码:

    cs 复制代码
    using System;
    using System.IO;
    using System.Windows.Forms;
    
    
    public class NotepadForm : Form
    {
        private MenuStrip menuStrip;
        private ToolStripMenuItem fileMenu;
        private ToolStripMenuItem newMenuItem;
        private ToolStripMenuItem openMenuItem;
        private ToolStripMenuItem saveMenuItem;
        private TextBox textBox;
    
        public NotepadForm()
        {
            menuStrip = new MenuStrip();
            fileMenu = new ToolStripMenuItem("File");
            newMenuItem = new ToolStripMenuItem("New", null, NewFile);
            openMenuItem = new ToolStripMenuItem("Open", null, OpenFile);
            saveMenuItem = new ToolStripMenuItem("Save", null, SaveFile);
    
            fileMenu.DropDownItems.Add(newMenuItem);
            fileMenu.DropDownItems.Add(openMenuItem);
            fileMenu.DropDownItems.Add(saveMenuItem);
    
            menuStrip.Items.Add(fileMenu);
    
            textBox = new TextBox();
            textBox.Multiline = true;
            textBox.Dock = DockStyle.Fill;
    
            Controls.Add(textBox);
            Controls.Add(menuStrip);
            MainMenuStrip = menuStrip;
        }
    
        private void NewFile(object sender, EventArgs e)
        {
            textBox.Clear();
        }
    
        private void OpenFile(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    textBox.Text = File.ReadAllText(openFileDialog.FileName);
                }
            }
        }
    
        private void SaveFile(object sender, EventArgs e)
        {
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    File.WriteAllText(saveFileDialog.FileName, textBox.Text);
                }
            }
        }
    
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new NotepadForm());
        }
    }

使用 WPF 开发记事本应用

  1. 设计界面: 在 XAML 文件中定义界面元素:

    XML 复制代码
    <Window x:Class="NotepadApp.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Notepad" Height="450" Width="800">
        <DockPanel>
            <Menu DockPanel.Dock="Top">
                <MenuItem Header="File">
                    <MenuItem Header="New" Click="NewFile"/>
                    <MenuItem Header="Open" Click="OpenFile"/>
                    <MenuItem Header="Save" Click="SaveFile"/>
                </MenuItem>
            </Menu>
            <TextBox Name="textBox" AcceptsReturn="True" AcceptsTab="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"/>
        </DockPanel>
    </Window>
  2. 编写代码:

    cs 复制代码
    using System;
    using System.IO;
    using System.Windows;
    
    
    namespace NotepadApp
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void NewFile(object sender, RoutedEventArgs e)
            {
                textBox.Clear();
            }
    
            private void OpenFile(object sender, RoutedEventArgs e)
            {
                Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
    
                if (openFileDialog.ShowDialog() == true)
                {
                    textBox.Text = File.ReadAllText(openFileDialog.FileName);
                }
            }
    
            private void SaveFile(object sender, RoutedEventArgs e)
            {
                Microsoft.Win32.SaveFileDialog saveFileDialog = new Microsoft.Win32.SaveFileDialog();
    
                if (saveFileDialog.ShowDialog() == true)
                {
                    File.WriteAllText(saveFileDialog.FileName, textBox.Text);
                }
            }
        }
    }

总结

在本节中,我们从基本的控制台应用程序逐步过渡到图形用户界面(GUI)应用程序,通过 Windows Forms 和 WPF 这两种不同的框架来开发桌面应用程序。通过开发一个记事本应用项目,我们综合运用了所学知识,展示了如何设计和实现一个实际的应用程序。继续练习和扩展这些项目,可以帮助您进一步提高 C# 编程技能,为更复杂的项目打下坚实的基础。

下一篇:白骑士的C#教学实战项目篇 4.3 Web开发​​​​​​​

相关推荐
Tony Bai22 分钟前
高并发后端:坚守 Go,还是拥抱 Rust?
开发语言·后端·golang·rust
wjs202443 分钟前
Swift 类型转换
开发语言
秃了也弱了。1 小时前
python实现定时任务:schedule库、APScheduler库
开发语言·python
weixin_440730501 小时前
java数组整理笔记
java·开发语言·笔记
Thera7772 小时前
状态机(State Machine)详解:原理、优缺点与 C++ 实战示例
开发语言·c++
niucloud-admin2 小时前
java服务端——controller控制器
java·开发语言
夏幻灵3 小时前
JAVA基础:基本数据类型和引用数据类型
java·开发语言
cike_y3 小时前
Spring-Bean的作用域&Bean的自动装配
java·开发语言·数据库·spring
十八度的天空4 小时前
第01节 Python的基础语法
开发语言·python
我是唐青枫4 小时前
深入理解 C#.NET Interlocked.Increment:原子操作的核心
c#·.net