一、使用Windows Presentation Foundation(WPF)进行GUI界面开发
Windows Presentation Foundation是一种用于创建 Windows 客户端应用程序的 UI 框架。它使用 XAML 作为声明性语法来定义应用程序的用户界面,同时使用 .NET 框架提供程序逻辑。
通过WPF,可以创建具有各种特效、状态转换和动画的漂亮应用程序。
以下是一个简单的WPF示例:
//MainWindow.xaml代码
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock Text="Hello, WPF!" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="40"/>
</Grid>
</Window>
//MainWindow.xaml.cs代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
上述示例使用XAML语言定义了一个窗口,并且在窗口中添加了一个文本块。它还定义了窗口的代码逻辑。
二、使用Windows Forms进行GUI界面开发
Windows Forms是一个早期的GUI框架,适用于 .NET应用程序。它允许创建基于传统窗口和控件的应用程序。使用Windows Forms,可以创建像Visual Basic 6这样的传统应用程序。
以下是Windows Forms示例:
//MainWindow.cs代码
using System.Windows.Forms;
public class MainWindow : Form
{
public MainWindow()
{
Text = "Hello, Windows Forms!";
Label label = new Label();
label.Text = "Hello, Windows Forms!";
label.Dock = DockStyle.Fill;
Controls.Add(label);
}
}
//Program.cs代码
using System;
using System.Windows.Forms;
public static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainWindow());
}
}
上述示例创建了一个基本的Windows Forms窗口。
三、使用Windows API进行底层编程
Windows API是Windows操作系统提供的一组函数和数据结构。它允许进行低级别的操作,如访问系统资源和对窗口进行细粒度控制。
以下是Windows API示例:
//MainWindow.cpp代码
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"My Window Class";
RegisterClass(&wc);
HWND hWnd = CreateWindow(L"My Window Class", L"Hello, Windows API!", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
RECT rect;
GetClientRect(hWnd, &rect);
DrawText(hdc, L"Hello, Windows API!", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
上述示例使用Windows API创建了一个窗口,并在窗口中绘制一些文本。
四、使用.NET Framework Class Library进行通用编程
.NET Framework Class Library是.NET框架的核心组成部分之一。它包含大量的通用类,如文件和IO操作、网络通信、线程和进程管理等。
以下是.NET Framework Class Library示例:
//MainWindow.cs代码
using System;
using System.IO;
using System.Windows.Forms;
public class MainWindow : Form
{
public MainWindow()
{
Text = "Hello, .NET Framework Class Library!";
Label label = new Label();
label.Text = "Hello, .NET Framework Class Library!";
label.Dock = DockStyle.Fill;
Controls.Add(label);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "hello.txt");
using (StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine("Hello, .NET Framework Class Library!");
}
MessageBox.Show("File saved to desktop!");
}
}
//Program.cs代码
using System;
using System.Windows.Forms;
public static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainWindow());
}
}
上述示例在窗口中显示一些文本,并将文本写入桌面上的文件。
原创文章,作者:BHKHY,如若转载,请注明出处:https://www.506064.com/n/372307.html
微信扫一扫
支付宝扫一扫