public IWindow CreateWindow(CreateWindowArguments arguments) { var nativeWindow = new NativeWindow(arguments.Width, arguments.Height, _eventManager); var wndClassExA = new WNDCLASSEXA { CbClsExtra = 0, CbSize = Marshal.SizeOf <WNDCLASSEXA>(), HCursor = IntPtr.Zero, HIcon = IntPtr.Zero, LpFnWndProc = nativeWindow.WindowProcedureFunctionPointer, CbWndExtra = 0, HIconSm = IntPtr.Zero, HInstance = Marshal.GetHINSTANCE(GetType().Module), HbrBackground = IntPtr.Zero, LpszClassName = arguments.Title + "class", Style = 0 }; if (User32.RegisterClassExA(ref wndClassExA) == 0) { throw new Win32Exception(Marshal.GetLastWin32Error(), "RegisterClassExA failed"); } const WindowStyles wsStyle = WindowStyles.WS_OVERLAPPEDWINDOW | WindowStyles.WS_VISIBLE; RECT windowRect = default; windowRect.Left = 100; windowRect.Right = arguments.Width + windowRect.Left; windowRect.Top = 100; windowRect.Bottom = arguments.Height + windowRect.Top; User32.AdjustWindowRect(ref windowRect, wsStyle, false); nativeWindow.Handle = User32.CreateWindowExA( 0, wndClassExA.LpszClassName, arguments.Title, wsStyle, arguments.X, arguments.Y, windowRect.Right - windowRect.Left, windowRect.Bottom - windowRect.Top, IntPtr.Zero, IntPtr.Zero, wndClassExA.HInstance, IntPtr.Zero ); if (nativeWindow.Handle == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error(), "CreateWindowExA failed"); } nativeWindow.ShowWindow(); return(nativeWindow); }
private void Init(int x, int y, int width, int height, WindowSizeType sizeType, WindowType type, WindowStartupPosition startupPosition) { // register window class var wcex = new WNDCLASSEXA(); wcex.cbSize = (UINT)Marshal.SizeOf <WNDCLASSEXA>(); wcex.style = CS_HREDRAW | CS_VREDRAW; wndProcDelegate = new WndProcDelegate(WndProc); #if CS2X Marshal.GetFunctionPointerForDelegate <WndProcDelegate>(wndProcDelegate, out _, out wcex.lpfnWndProc); #else wcex.lpfnWndProc = Marshal.GetFunctionPointerForDelegate <WndProcDelegate>(wndProcDelegate); #endif wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = Application.hInstance; //wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINDOWSDESKTOPVCPP)); //wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); var guid = Guid.NewGuid(); string guidString = guid.ToString(); fixed(char *guidStringPtr = guidString) { wcex.lpszClassName = (LPCSTR)guidStringPtr; // set class name to guid atom = RegisterClassExA(&wcex); } // create window RECT rect; if (startupPosition == WindowStartupPosition.CenterScreen) { HWND desktop = GetDesktopWindow(); GetClientRect(desktop, &rect); x = (rect.right / 2) - (width / 2); y = (rect.bottom / 2) - (height / 2); } else if (startupPosition == WindowStartupPosition.Default) { x = unchecked ((int)CW_USEDEFAULT); y = unchecked ((int)CW_USEDEFAULT); } DWORD windowStyle = WS_OVERLAPPEDWINDOW; switch (type) { case WindowType.Tool: windowStyle ^= WS_MAXIMIZEBOX; windowStyle ^= WS_MINIMIZEBOX; windowStyle ^= WS_THICKFRAME; // disable window resize break; case WindowType.Popup: windowStyle = WS_POPUPWINDOW; break; } byte *title = stackalloc byte[1]; title[0] = 0; hWnd = CreateWindowExA(0, (LPCSTR)atom, (LPCSTR)title, windowStyle, x, y, width, height, HWND.Zero, HMENU.Zero, Application.hInstance, IntPtr.Zero); if (hWnd == HWND.Zero) { throw new Exception("CreateWindowExA failed"); } // adjust working area / client size and position if (sizeType == WindowSizeType.WorkingArea) { if (GetWindowRect(hWnd, &rect) == 0) { throw new Exception("GetWindowRect failed"); } int rectWidth = rect.right - rect.left; int rectHeight = rect.bottom - rect.top; RECT clientRect; if (GetClientRect(hWnd, &clientRect) == 0) { throw new Exception("GetWindowRect failed"); } int clientRectWidth = clientRect.right - clientRect.left; int clientRectHeight = clientRect.bottom - clientRect.top; int offsetX = (rectWidth - clientRectWidth); int offsetY = (rectHeight - clientRectHeight); width += offsetX; height += offsetY; UINT flags = SWP_NOMOVE; if (startupPosition == WindowStartupPosition.CenterScreen) { flags = 0; x -= offsetX / 2; y -= offsetY; } if (SetWindowPos(hWnd, HWND.Zero, x, y, width, height, flags) == 0) { throw new Exception("SetWindowPos failed"); } } // track window windows.Add(this); }