C#利用win32API创建窗体
效果图
代码实现

1 using System; 2 using System.Runtime.InteropServices; 3 //using System.Windows.Forms; 4 5 namespace win32API 6 { 7 class Program 8 { 9 public const int COLOR_WINDOW = 5; 10 const uint WS_TABSTOP = 0x00010000; 11 const uint WS_VISIBLE = 0x10000000; 12 const uint WS_CHILD = 0x40000000; 13 const uint BS_DEFPUSHBUTTON = 0x0001; 14 const uint ES_AUTOHSCROLL = 0x0080; 15 const uint WM_COMMAND = 0x0111; 16 const uint BN_CLICKED = 0x0; 17 18 /// <summary> 19 /// 按钮 20 /// </summary> 21 static IntPtr hButton; 22 23 /// <summary> 24 /// 处理窗口消息 25 /// </summary> 26 /// <param name="hWnd">表示接收消息的窗口句柄(handle)。窗口句柄是操作系统分配给每个窗口的唯一标识符,用于识别窗口</param> 27 /// <param name="msg">表示接收到的消息类型。消息是操作系统或其他应用程序发送给窗口的通知或指令。</param> 28 /// <param name="wParam">表示与消息相关的附加信息,通常用于传递消息的参数。它的具体含义取决于接收到的消息类型。</param> 29 /// <param name="lParam">也是用于传递消息的参数,通常与 wParam 一起使用。它的具体含义也取决于接收到的消息类型。</param> 30 /// <returns></returns> 31 public static IntPtr WindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) 32 { 33 switch (msg) 34 { 35 case 0x0010: // WM_CLOSE 36 NativeMethods.DestroyWindow(hWnd); 37 return IntPtr.Zero; 38 case 0x0002: // WM_DESTROY 39 return IntPtr.Zero; 40 Environment.Exit(0);// 处理按钮点击消息 41 case WM_COMMAND: 42 if (lParam == hButton && NativeMethods.HIWORD(wParam) == BN_CLICKED) 43 { 44 Button_Click(hWnd, wParam, lParam); 45 } 46 return IntPtr.Zero; 47 //默认 48 default: 49 return NativeMethods.DefWindowProc(hWnd, msg, wParam, lParam); 50 } 51 } 52 53 static void Main(string[] args) 54 { 55 NativeMethods.WNDCLASS wc = new NativeMethods.WNDCLASS(); 56 wc.lpfnWndProc = WindowProc; 57 wc.lpszClassName = "MyWindowClass"; 58 wc.hCursor = NativeMethods.LoadCursor(IntPtr.Zero, 32512); // 加载系统光标 59 wc.hbrBackground = (IntPtr)(COLOR_WINDOW + 1); // 设置背景颜色 60 61 ushort classAtom = NativeMethods.RegisterClass(ref wc); 62 if (classAtom == 0) 63 throw new Exception("Failed to register window class."); 64 //创建窗口 65 IntPtr hWnd = NativeMethods.CreateWindowEx(0, wc.lpszClassName, "Hello, win32API!", 0xCF0000, 100, 100, 800, 600, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); 66 if (hWnd == IntPtr.Zero) 67 throw new Exception("Failed to create window."); 68 // 创建按钮 69 hButton = NativeMethods.CreateWindowEx( 70 0, 71 "BUTTON", // 按钮类名 72 "Click Me", // 按钮文本 73 WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // 按钮样式 74 10, // X 坐标 75 10, // Y 坐标 76 100, // 宽度 77 30, // 高度 78 hWnd, // 父窗口句柄 79 IntPtr.Zero, 80 Marshal.GetHINSTANCE(typeof(Program).Module), 81 IntPtr.Zero); 82 83 // 创建文本框 84 IntPtr hTextBox = NativeMethods.CreateWindowEx( 85 0, 86 "EDIT", // 文本框类名 87 "文本框", // 默认文本 88 WS_TABSTOP | WS_VISIBLE | WS_CHILD | ES_AUTOHSCROLL, // 文本框样式 89 10, // X 坐标 90 50, // Y 坐标 91 200, // 宽度 92 30, // 高度 93 hWnd, // 父窗口句柄 94 IntPtr.Zero, 95 Marshal.GetHINSTANCE(typeof(Program).Module), 96 IntPtr.Zero); 97 NativeMethods.ShowWindow(hWnd, 1); // 显示窗口 98 NativeMethods.UpdateWindow(hWnd); // 更新窗口 99 100 MSG msg; 101 while (NativeMethods.GetMessage(out msg, IntPtr.Zero, 0, 0)) 102 { 103 NativeMethods.TranslateMessage(ref msg); 104 NativeMethods.DispatchMessage(ref msg); 105 } 106 } 107 108 // 处理按钮点击事件 109 private static void Button_Click(IntPtr hWnd, IntPtr wParam, IntPtr lParam) 110 { 111 Console.WriteLine("Button clicked!"); 112 //MessageBox.Show("Button clicked!"); 113 } 114 } 115 116 [StructLayout(LayoutKind.Sequential)] 117 public struct MSG 118 { 119 public IntPtr hwnd; 120 public uint message; 121 public IntPtr wParam; 122 public IntPtr lParam; 123 public uint time; 124 public POINT pt; 125 } 126 127 [StructLayout(LayoutKind.Sequential)] 128 public struct POINT 129 { 130 public int x; 131 public int y; 132 } 133 134 public class NativeMethods 135 { 136 // 定义提取高16位的方法 137 public static int HIWORD(IntPtr n) 138 { 139 int v = n.ToInt32(); 140 return (v >> 16) & 0xffff; 141 } 142 [DllImport("user32.dll", CharSet = CharSet.Unicode)] 143 public static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName); 144 145 [DllImport("user32.dll", CharSet = CharSet.Unicode)] 146 public static extern ushort RegisterClass([In] ref WNDCLASS lpWndClass); 147 148 [DllImport("user32.dll", CharSet = CharSet.Unicode)] 149 public static extern bool GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax); 150 151 [DllImport("user32.dll", CharSet = CharSet.Unicode)] 152 public static extern IntPtr DispatchMessage([In] ref MSG lpmsg); 153 154 [DllImport("user32.dll", CharSet = CharSet.Unicode)] 155 public static extern bool TranslateMessage([In] ref MSG lpMsg); 156 157 158 159 // 窗口过程函数委托 160 public delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); 161 162 // 窗口类结构 163 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 164 public struct WNDCLASS 165 { 166 public uint style; 167 public WndProc lpfnWndProc; 168 public int cbClsExtra; 169 public int cbWndExtra; 170 public IntPtr hInstance; 171 public IntPtr hIcon; 172 public IntPtr hCursor; 173 public IntPtr hbrBackground; 174 public string lpszMenuName; 175 public string lpszClassName; 176 } 177 178 // 调用创建窗口的Win32 API 179 [DllImport("user32.dll", SetLastError = true,CharSet =CharSet.Unicode)] 180 public static extern IntPtr CreateWindowEx( 181 uint dwExStyle, string lpClassName, string lpWindowName, uint dwStyle, 182 int x, int y, int nWidth, int nHeight, 183 IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam); 184 185 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 186 public static extern bool DestroyWindow(IntPtr hWnd); 187 188 [DllImport("user32.dll", CharSet = CharSet.Unicode)] 189 public static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam); 190 191 [DllImport("user32.dll", CharSet = CharSet.Unicode)] 192 public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 193 194 [DllImport("user32.dll", CharSet = CharSet.Unicode)] 195 public static extern bool UpdateWindow(IntPtr hWnd); 196 } 197 198 }
点击按钮就会事件响应
使用winform的MessageBox类项目设置
点击双击项目设置项目文件,加一个设置项
1 <Project Sdk="Microsoft.NET.Sdk"> 2 3 <PropertyGroup> 4 <OutputType>Exe</OutputType> 5 <TargetFramework>net6.0-windows</TargetFramework> 6 <ImplicitUsings>enable</ImplicitUsings> 7 <Nullable>enable</Nullable> 8 <!--添加或修改这一项--> 9 <UseWindowsForms>true</UseWindowsForms> 10 </PropertyGroup> 11 12 </Project>
可以手动更改,也可以在项目上右键属性更改,结果都是反映到项目文件的修改
然后重新生成项目,就会发现以来里面添加了winform框架的引用
在引入名称空间using System.Windows.Forms;
在按钮事件处理中就能显示提示框了
1 // 处理按钮点击事件 2 private static void Button_Click(IntPtr hWnd, IntPtr wParam, IntPtr lParam) 3 { 4 Console.WriteLine("Button clicked!"); 5 MessageBox.Show("Button clicked!"); 6 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?