首页
学习
活动
专区
工具
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”的窗口。您可以根据需要修改此示例以满足您的需求。

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

相关·内容

  • ue4 插件开发(ue4性能优化)

    UE4引擎和UE4项目是由各个模块组成的,其主要编程语言是C++。 插件也是一个模块,说到插件必须讲到耦合性与独立性,耦合性是对模块间关联程度的度量,模块间关联越强,则耦合性强,独立性差。 庞大的UE4引擎各模块相互参差作用,所以想要读懂并拆解并非易事,而创建UE4游戏项目实则只引入了引擎的部分模块,再想想如果很多游戏项目都需要用到这一个自定义功能时,是不是每个项目都要开发一次这个功能,个人或者一家公司内部还好,只要把项目代码复制给另一个项目即可,但是全球这么多的UE4开发者怎么办,开发者的结晶势必要发挥它最大的用处,把自己的研发成果分享出去,为了解决这类问题,就要使得模块独立起来,那么UE4插件就该闪亮登场了。

    02

    window32api_win32api与硬件设备

    作者:浪子花梦,一个有趣的程序员 ~ . Win32API 相关文章如下: Win32利用CreateEvent 实现简单的 —— 线程同步 Win32消息处理机制与窗口制作 Win32远程线程注入 .dll 文件 Win32删除目录下的所有文件 —— 递归遍历 (一)Win32服务程序编写 —— 使用SC命令创建与删除 (二)Win32服务程序编写 —— 使用命令行参数创建与删除 Win32使用快照、psapi.dll、wtsapi32.dll、ntdll.dll 四种方式实现 —— 枚举进程 (一)Win32进程通信 —— 自定义消息实现 (二)Win32进程通信 —— 内存映射文件 (三)Win32进程通信 —— 数据复制消息 (四)Win32进程通信 —— 剪贴板的使用 (五)Win32进程通信 —— 匿名管道 (六)Win32进程通信 —— 邮槽的使用

    01
    领券