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 }

C#利用win32API创建窗体的更多相关文章
- java中利用JFrame创建窗体 【转】
1. 一个简单的swing public class Test(){ public static void main(String[] args){ JFrame frame = new JFrame ...
- Java中利用JFrame创建窗体
1. 一个简单例子: public class Test(){ public static void main(String[] args){ JFrame frame = new JFrame(); ...
- Qt创建窗体的过程
版权声明 本文为原创作品,请尊重作者的劳动成果.转载必须保持文章完整性,并以超链接形式注明原始作者“ tingsking18”和 主站点地址,方便其他朋友提问和指正. QT源码解析(一) QT创建窗口 ...
- QT源码解析(七)Qt创建窗体的过程,作者“ tingsking18 ”(真正的创建QPushButton是在show()方法中,show()方法又调用了setVisible方法)
前言:分析Qt的代码也有一段时间了,以前在进行QT源码解析的时候总是使用ue,一个函数名在QTDIR/src目录下反复的查找,然后分析函数之间的调用关系,效率实在是太低了,最近总结出一个更简便的方法, ...
- 【翻译】利用Qt设计师窗体在运行时创建用户界面(Creating a user interface from a Qt Designer form at run-time)
利用Qt设计师窗体在运行时创建用户界面 我们利用Calculator窗体例子中创建的窗体(Form)来展示当一个应用(application)已经生成后,是可以在其运行时产生与例子中相同的用户界面. ...
- 利用WPF创建含多种交互特性的无边框窗体
咳咳,标题一口气读下来确实有点累,让我先解释一下.另外文章底部有演示程序的下载. 本文介绍利用WPF创建一个含有以下特性的窗口: 有窗口阴影,比如QQ窗口外围只有几像素的阴影: 支持透明且无边框,为了 ...
- C# 学习笔记(一) Winform利用Assembly反射动态创建窗体
1. 添加Reflection //添加对Reflection程序集引用 using System.Reflection; // 引用窗体创建方法CreateForm,传入参数 private voi ...
- Ext JS 如何动态加载JavaScript创建窗体
JavaScript不需要编译即可运行,这让JavaScript构建的应用程序可以变得很灵活.我们可以根据需要动态从服务器加载JavaScript脚本来创建和控制UI来与用户交互.下面结合Ext JS ...
- Oracle Sales Cloud:管理沙盒(定制化)小细节1——利用公式创建字段并显示在前端页面
Oracle Sales Cloud(Oracle 销售云)是一套基于Oracle云端的CRM管理系统.由于 Oracle 销售云是基于 Oracle 云环境的,它与传统的管理系统相比,显著特点之一便 ...
- 利用Oracle创建表空间和用户
本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6217152.html 第一步,创建表空间 以SYS/sys账户和SYSDBA身份登录 ...
随机推荐
- OpenHarmony父子组件单项同步使用:@Prop装饰器
@Prop装饰的变量可以和父组件建立单向的同步关系.@Prop装饰的变量是可变的,但是变化不会同步回其父组件. 说明: 从API version 9开始,该装饰器支持在ArkTS卡片中使用. 概述 ...
- 如何在OpenHarmony上使用SeetaFace2人脸识别库?
简介 相信大部分同学们都已了解或接触过OpenAtom OpenHarmony(以下简称"OpenHarmony")了,但你一定没在OpenHarmony上实现过人脸识别功能,跟着 ...
- DEB打包教程
一.deb简介 deb是一种安装包的格式,linux上常见的安装包主要是deb.rpm 二.deb简单使用 # deb安装 sudo dpkg -i webcamera_1.0_amd64.deb # ...
- 【译】Visual Studio 中的 GitHub Copilot:2023年回顾
在快速发展的软件开发世界中,保持领先是至关重要的.在 Visual Studio 中引入AI,特别是 GitHub Copilot,已经彻底改变了开发人员的编码方式.通过将 Copilot 集成到 V ...
- Python根据主播直播时间段判定订单销售额归属
写在前面:最近在群里看到一个这样的直播电商的场景觉得还是挺有趣的,于是就想用Python来实现. 需求描述:根据主播直播时间段结合销售订单的付款时间判断所属销售的归属 生成主播在线直播时间段数据 fr ...
- 【4】Spring框架的起源
在我们的<Java Spring框架入门教程>中对 Spring 框架进行了十分详尽的介绍和剖析,但在学习 Spring Boot 之前,在这里回顾一下 Spring 是怎么出现的. Sp ...
- jenkins 持续集成和交付——maven小栗子(四)
前言 因为前面一节中介绍了自由风格模式,这里介绍一个专门为maven打造的模式,没错就是使用插件.这里写这个只是为了说明,如果我们构建一些不是那么复杂的项目,那么可以去使用一些插件,这样可以更加方便, ...
- 重新整理数据结构与算法(c#系列)—— 树的前中后序遍历查找[十七]
前言 树的前中后序遍历 是根据前中后序的顺序来查找,找到了则弹出. 正文 节点模型: public class HeroNode { private int no; private string na ...
- .net core的依赖注入学习
依赖注入(Dependency Injection,DI),简称DI,它可以降低各模块之间的耦合 首先需要安装两个Nuget包: Microsoft.Extensions.DependencyInje ...
- 力扣615(MySQL)-平均工资:部门与公司比较(困难)
题目: 给如下两个表,写一个查询语句,求出在每一个工资发放日,每个部门的平均工资与公司的平均工资的比较结果 (高 / 低 / 相同). 表: salary employee_id 字段是表 emplo ...