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

20 篇文章 0 订阅

        在这一部分,我们将从简单的控制台应用程序过渡到图形用户界面(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. 编写代码:
    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 文件中定义界面元素:
    <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. 编写代码:
    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. 编写代码:
    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 文件中定义界面元素:
    <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. 编写代码:
    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# 编程技能,为更复杂的项目打下坚实的基础。

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白骑士所长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值