Exemple #1
0
        public Window(string title, int width, int height)
        {
            var instance = Kernel32.GetModuleHandleW(null);
            var icon     = User32.LoadIconW(IntPtr.Zero, IDI.APPLICATION);

            wndproc = WndProc;

            // register the window class
            var wndclass = new WNDCLASSEXW {
                cbSize        = (uint)Marshal.SizeOf <WNDCLASSEXW>(),
                style         = CS.HREDRAW | CS.VREDRAW | CS.OWNDC,
                hInstance     = instance,
                hIcon         = icon,
                hIconSm       = icon,
                hCursor       = User32.LoadCursorW(IntPtr.Zero, IDC.ARROW),
                lpszClassName = ClassName,
                lpfnWndProc   = Marshal.GetFunctionPointerForDelegate(wndproc)
            };

            var atom = User32.RegisterClassExW(ref wndclass);

            if (atom == 0)
            {
                throw new InvalidOperationException("Failed to register window class");
            }

            // size the client area appropriately
            var styles = WS.OVERLAPPEDWINDOW;
            var rect   = new RECT {
                left = 0, top = 0, right = width, bottom = height
            };

            if (User32.AdjustWindowRectEx(ref rect, styles, 0, 0))
            {
                width  = rect.right - rect.left;
                height = rect.bottom - rect.top;
            }

            // create the window
            hwnd = User32.CreateWindowExW(
                0,
                new IntPtr(atom),
                title,
                styles,
                CW.USEDEFAULT,
                CW.USEDEFAULT,
                width,
                height,
                IntPtr.Zero,
                IntPtr.Zero,
                instance,
                IntPtr.Zero
                );
            if (hwnd == IntPtr.Zero)
            {
                throw new InvalidOperationException("Failed to create window");
            }
        }
Exemple #2
0
 public static extern ushort RegisterClassExW(ref WNDCLASSEXW lpwcx);