using System;
using System.Runtime.InteropServices;
class Program
{
static void Main()
{
// 创建WNDCLASSEX结构体实例
WNDCLASSEX winClss = new WNDCLASSEX
{
cbSize = Marshal.SizeOf(typeof(WNDCLASSEX)),
style = CS_VREDRAW | CS_HREDRAW,
lpfnWndProc = WndProc, // 使用C#的WndProc作为窗口过程函数
hInstance = Marshal.GetHINSTANCE(typeof(Program).Module), // 获取当前模块句柄
hCursor = LoadCursor(IntPtr.Zero, IDC_ARROW),
hbrBackground = (IntPtr)(COLOR_BTNFACE + 1),
lpszClassName = "MyWindowClass"
};
// 注册窗口类
ushort atom = RegisterClassEx(ref winClss);
if (atom == 0)
{
Console.WriteLine("RegisterClassEx failed.");
return;
}
// 创建窗口
IntPtr hwnd = CreateWindowEx(
0,
"MyWindowClass",
"My Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero,
IntPtr.Zero);
if (hwnd == IntPtr.Zero)
{
Console.WriteLine("CreateWindowEx failed.");
return;
}
// 显示窗口
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
// 消息循环
MSG msg = new MSG();
while (GetMessage(out msg, IntPtr.Zero, 0, 0))
{
TranslateMessage(ref msg);
DispatchMessage(ref msg);
}
}
static IntPtr WndProc(IntPtr hwnd, uint uMsg, IntPtr wParam, IntPtr lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
const uint CS_HREDRAW = 2;
const uint CS_VREDRAW = 1;
const int IDC_ARROW = 32512;
const int COLOR_BTNFACE = 15;
const uint WS_OVERLAPPEDWINDOW = 0x00CF0000;
const int CW_USEDEFAULT = unchecked((int)0x80000000);
const uint SW_SHOW = 5;
[StructLayout(LayoutKind.Sequential)]
struct MSG
{
public IntPtr hWnd;
public uint message;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public int pt_x;
public int pt_y;
}
[StructLayout(LayoutKind.Sequential)]
struct WNDCLASSEX
{
public uint cbSize;
public uint style;
[MarshalAs(UnmanagedType.FunctionPtr)]
public WindowProc lpfnWndProc; // 窗口过程函数指针
public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;
public string lpszMenuName;
public string lpszClassName;
public IntPtr hIconSm;
}
delegate IntPtr WindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam); // 定义委托
[DllImport("user32.dll")]
static extern ushort RegisterClassEx(ref WNDCLASSEX lpWndClass);
[DllImport("user32.dll")]
static extern IntPtr CreateWindowEx(
uint dwExStyle,
string lpClassName,
string lpWindowName,
uint dwStyle,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hWndParent,
IntPtr hMenu,
IntPtr hInstance,
IntPtr lpParam);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
[DllImport("user32.dll")]
static extern bool UpdateWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax);
[DllImport("user32.dll")]
static extern bool TranslateMessage(ref MSG lpMsg);
[DllImport("user32.dll")]
static extern IntPtr DispatchMessage(ref MSG lpMsg);
[DllImport("user32.dll")]
static extern IntPtr LoadCursor(IntPtr hInstance, int lpCursorName);
[DllImport("user32.dll", SetLastError = true)]
static extern void PostQuitMessage(int nExitCode);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
}