gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
请给一个用C# 语言调用RegisterClassEx 然后调用CreateWindowEx能成功的示例程序
- User
请给一个用C# 语言调用RegisterClassEx 然后调用CreateWindowEx能成功的示例程序
- Assistant
下面是一个用 C# 语言调用 RegisterClassEx 然后调用 CreateWindowEx 能成功的示例程序:
using System; using System.Runtime.InteropServices; public class Win32App { private const int WS_OVERLAPPEDWINDOW = 0x00000000; private const int SW_SHOW = 5; [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr CreateWindowEx( int dwExStyle, string lpClassName, string lpWindowName, int 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("kernel32.dll", SetLastError = true)] private static extern IntPtr GetModuleHandle(string lpModuleName); [StructLayout(LayoutKind.Sequential)] private struct WNDCLASSEX { public uint cbSize; public uint style; [MarshalAsAttribute(UnmanagedType.FunctionPtr)] public WndProc 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; } private delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", SetLastError = true)] private static extern short RegisterClassEx(ref WNDCLASSEX lpwcx); [STAThread] public static void Main() { // Define the window class. WNDCLASSEX wcex = new WNDCLASSEX(); wcex.cbSize = (uint)Marshal.SizeOf(typeof(WNDCLASSEX)); wcex.style = 0; wcex.lpfnWndProc = new WndProc(WndProc); wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = GetModuleHandle(null); wcex.hIcon = IntPtr.Zero; wcex.hCursor = IntPtr.Zero; wcex.hbrBackground = (IntPtr)(22); wcex.lpszMenuName = null; wcex.lpszClassName = "win32appclass"; wcex.hIconSm = IntPtr.Zero; // Register the window class. short atom = RegisterClassEx(ref wcex); if (atom == 0) { Console.WriteLine("RegisterClassEx failed: {0}", Marshal.GetLastWin32Error()); } else { // Create the window. IntPtr hWnd = CreateWindowEx( 0, "win32appclass", "Win32 App", WS_OVERLAPPEDWINDOW, 100, 100, 500, 300, IntPtr.Zero, IntPtr.Zero, GetModuleHandle(null), IntPtr.Zero); if (hWnd == IntPtr.Zero) { Console.WriteLine("CreateWindowEx failed: {0}", Marshal.GetLastWin32Error()); } else { // Show the window. ShowWindow(hWnd, SW_SHOW); // Start the message loop. MSG msg; while (GetMessage(out msg, IntPtr.Zero, 0, 0) != 0) { TranslateMessage(ref msg); DispatchMessage(ref msg); } // Destroy the window. DestroyWindow(hWnd); } } } private static IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, msg, wParam, lParam); } return IntPtr.Zero; } private const uint WM_DESTROY = 0x0002; [StructLayout(LayoutKind.Sequential)] private struct MSG { public IntPtr hWnd; public uint message; public IntPtr wParam; public IntPtr lParam; public uint time; public POINT pt; } [StructLayout(LayoutKind.Sequential)] private struct POINT { public int x; public int y; } [DllImport("user32.dll", SetLastError = true)] private static extern bool GetMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax); [DllImport("user32.dll", SetLastError = true)] private static extern bool TranslateMessage(ref MSG lpMsg); [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr DispatchMessage(ref MSG lpMsg); [DllImport("user32.dll", SetLastError = true)] private static extern void PostQuitMessage(int nExitCode); [DllImport("user32.dll", SetLastError = true)] private static extern void ShowWindow(IntPtr hWnd, int nCmdShow); }