首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

从c#注册自定义win32窗口类

从您的问题中,我了解到您想要了解如何在C#中注册自定义Win32窗口类。以下是一个简单的示例,说明如何在C#中注册自定义Win32窗口类:

代码语言:csharp
复制
using System;
using System.Runtime.InteropServices;

public class CustomWindow : IDisposable
{
    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern ushort RegisterClass(ref WNDCLASS lpWndClass);

    [DllImport("user32.dll", SetLastError = true)]
    private 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", SetLastError = true)]
    private static extern bool DestroyWindow(IntPtr hwnd);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool UnregisterClass(string lpClassName, IntPtr hInstance);

    private const int CS_HREDRAW = 0x0002;
    private const int CS_VREDRAW = 0x0001;
    private const int CW_USEDEFAULT = unchecked((int)0x80000000);
    private const uint WS_OVERLAPPEDWINDOW = 0x00CF0000;
    private const uint WS_EX_APPWINDOW = 0x00040000;

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    private struct WNDCLASS
    {
        public uint style;
        public WndProcDelegate 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;
    }

    private delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);

    private readonly WNDCLASS _wndClass;
    private readonly WndProcDelegate _wndProc;
    private readonly IntPtr _hInstance;
    private readonly IntPtr _hwnd;

    public CustomWindow()
    {
        _wndProc = WndProc;
        _hInstance = Marshal.GetHINSTANCE(typeof(CustomWindow).Module);

        _wndClass = new WNDCLASS
        {
            style = CS_HREDRAW | CS_VREDRAW,
            lpfnWndProc = _wndProc,
            hInstance = _hInstance,
            lpszClassName = "CustomWindowClass",
        };

        ushort classAtom = RegisterClass(ref _wndClass);
        if (classAtom == 0)
        {
            throw new Exception("Failed to register window class.");
        }

        _hwnd = CreateWindowEx(
            WS_EX_APPWINDOW,
            "CustomWindowClass",
            "Custom Window",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            IntPtr.Zero,
            IntPtr.Zero,
            _hInstance,
            IntPtr.Zero);

        if (_hwnd == IntPtr.Zero)
        {
            throw new Exception("Failed to create window.");
        }
    }

    public void Show()
    {
        ShowWindow(_hwnd, 1);
        UpdateWindow(_hwnd);
    }

    public void Dispose()
    {
        DestroyWindow(_hwnd);
        UnregisterClass(_wndClass.lpszClassName, _hInstance);
    }

    private IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
    {
        switch (msg)
        {
            case 0x0010: // WM_CLOSE
                DestroyWindow(hWnd);
                break;
            default:
                return DefWindowProc(hWnd, msg, wParam, lParam);
        }

        return IntPtr.Zero;
    }

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool UpdateWindow(IntPtr hWnd);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr DefWindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
}

这个示例创建了一个名为“CustomWindowClass”的自定义窗口类,并使用该类创建了一个名为“Custom Window”的窗口。您可以根据需要修改此示例以满足您的需求。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券