Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c#桌面应用开发

作者:整整ー世♂陰霾そ   发布日期:2026-02-16   浏览:17

using System;
using System.Windows.Forms;

namespace DesktopAppExample
{
    public class Program
    {
        [STAThread]
        static void Main()
        {
            // 创建一个Windows窗体应用程序
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // 创建主窗口
            Form mainForm = new Form();
            mainForm.Text = "C# 桌面应用示例";
            mainForm.Size = new System.Drawing.Size(400, 300);

            // 添加一个按钮到主窗口
            Button button = new Button();
            button.Text = "点击我";
            button.Location = new System.Drawing.Point(150, 120);
            button.Click += (sender, e) => 
            {
                MessageBox.Show("你点击了按钮!");
            };

            // 将按钮添加到主窗口的控件集合中
            mainForm.Controls.Add(button);

            // 运行应用程序
            Application.Run(mainForm);
        }
    }
}

解释说明:

  1. 命名空间和类:代码使用了SystemSystem.Windows.Forms命名空间,定义了一个名为Program的类。
  2. 入口点Main方法是程序的入口点。通过[STAThread]属性指定线程模型为单线程公寓(STA),这是Windows Forms应用程序的要求。
  3. 启用视觉样式Application.EnableVisualStyles()启用了Windows XP及更高版本的视觉样式。
  4. 设置文本渲染模式Application.SetCompatibleTextRenderingDefault(false)确保使用GDI+进行文本渲染。
  5. 创建主窗口:创建了一个Form对象作为主窗口,并设置了窗口标题和大小。
  6. 添加按钮:创建了一个Button对象,并设置了按钮的位置和文本。还为按钮的Click事件添加了一个事件处理程序,当按钮被点击时会弹出一个消息框。
  7. 运行应用程序:最后调用Application.Run(mainForm)启动应用程序的消息循环,并显示主窗口。

这个示例展示了如何创建一个简单的C#桌面应用程序,包含一个按钮和一个消息框。

上一篇:c# 替换字符串

下一篇:c# datagridview合并单元格

大家都在看

c# 二进制

c# 创建目录

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站