Exemple #1
0
        //private readonly WindowStyles _windowFullscreenStyle = WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_GROUP | WindowStyles.WS_TABSTOP;

        public unsafe Window(string title, int width, int height, WindowFlags flags = WindowFlags.None)
        {
            Title = title;

            int  x         = CW_USEDEFAULT;
            int  y         = CW_USEDEFAULT;
            bool resizable = (flags & WindowFlags.Resizable) != WindowFlags.None;

            _windowWindowedStyle = WindowStyles.WS_CAPTION | WindowStyles.WS_SYSMENU | WindowStyles.WS_MINIMIZEBOX | WindowStyles.WS_CLIPSIBLINGS | WindowStyles.WS_BORDER | WindowStyles.WS_DLGFRAME | WindowStyles.WS_THICKFRAME | WindowStyles.WS_GROUP | WindowStyles.WS_TABSTOP;

            if (resizable)
            {
                _windowWindowedStyle |= WindowStyles.WS_SIZEBOX | WindowStyles.WS_MAXIMIZEBOX;
            }

            _windowStyle = _windowWindowedStyle;

            RawRect windowRect = new RawRect(0, 0, width, height);

            // Adjust according to window styles
            AdjustWindowRectEx(ref windowRect, _windowStyle, false, WindowExStyles.WS_EX_OVERLAPPEDWINDOW);

            int windowWidth  = windowRect.Right - windowRect.Left;
            int windowHeight = windowRect.Bottom - windowRect.Top;

            bool centerWindow = true;

            if (centerWindow)
            {
                if (windowWidth > 0 && windowHeight > 0)
                {
                    int screenWidth  = GetSystemMetrics(SystemMetrics.SM_CXSCREEN);
                    int screenHeight = GetSystemMetrics(SystemMetrics.SM_CYSCREEN);

                    // Place the window in the middle of the screen.WS_EX_APPWINDOW
                    x = (screenWidth - windowWidth) / 2;
                    y = (screenHeight - windowHeight) / 2;
                }
            }

            IntPtr hwnd;

            fixed(char *lpWndClassName = WndClassName)
            {
                fixed(char *lpWindowName = Title)
                {
                    hwnd = CreateWindowExW(
                        (uint)WindowExStyles.WS_EX_OVERLAPPEDWINDOW,
                        (ushort *)lpWndClassName,
                        (ushort *)lpWindowName,
                        (uint)_windowStyle,
                        x,
                        y,
                        windowWidth,
                        windowHeight,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        null);
                }
            }

            if (hwnd == IntPtr.Zero)
            {
                return;
            }

            ShowWindow(hwnd, ShowWindowCommand.Normal);
            Handle = hwnd;

            GetClientRect(hwnd, out windowRect);
            Extent = new Size(windowRect.Right - windowRect.Left, windowRect.Bottom - windowRect.Top);
        }
Exemple #2
0
        private unsafe void PlatformConstruct()
        {
            int            x         = 0;
            int            y         = 0;
            WindowStyles   style     = 0;
            WindowExStyles styleEx   = 0;
            const bool     resizable = true;

            // Setup the screen settings depending on whether it is running in full screen or in windowed mode.
            //if (fullscreen)
            //{
            //style = User32.WindowStyles.WS_POPUP | User32.WindowStyles.WS_VISIBLE;
            //styleEx = User32.WindowStyles.WS_EX_APPWINDOW;

            //width = screenWidth;
            //height = screenHeight;
            //}
            //else
            {
                if (ClientSize.Width > 0 && ClientSize.Height > 0)
                {
                    int screenWidth  = GetSystemMetrics(SystemMetrics.SM_CXSCREEN);
                    int screenHeight = GetSystemMetrics(SystemMetrics.SM_CYSCREEN);

                    // Place the window in the middle of the screen.WS_EX_APPWINDOW
                    x = (screenWidth - ClientSize.Width) / 2;
                    y = (screenHeight - ClientSize.Height) / 2;
                }

                if (resizable)
                {
                    style = WindowStyles.WS_OVERLAPPEDWINDOW;
                }
                else
                {
                    style = WindowStyles.WS_POPUP | WindowStyles.WS_BORDER | WindowStyles.WS_CAPTION | WindowStyles.WS_SYSMENU;
                }

                styleEx = WindowExStyles.WS_EX_APPWINDOW | WindowExStyles.WS_EX_WINDOWEDGE;
            }
            style |= WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS;

            int windowWidth;
            int windowHeight;

            if (ClientSize.Width > 0 && ClientSize.Height > 0)
            {
                var rect = new RawRect(0, 0, ClientSize.Width, ClientSize.Height);

                // Adjust according to window styles
                AdjustWindowRectEx(&rect, (uint)style, 0, (uint)styleEx);

                windowWidth  = rect.Right - rect.Left;
                windowHeight = rect.Bottom - rect.Top;
            }
            else
            {
                x = y = windowWidth = windowHeight = CW_USEDEFAULT;
            }

            fixed(char *lpszClassName = Application.WindowClassName)
            {
                fixed(char *lpWindowName = Title)
                {
                    Handle = CreateWindowExW(
                        (uint)styleEx,
                        (ushort *)lpszClassName,
                        (ushort *)lpWindowName,
                        (uint)style,
                        x,
                        y,
                        windowWidth,
                        windowHeight,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        null
                        );

                    if (Handle == IntPtr.Zero)
                    {
                        return;
                    }
                }
            }

            ShowWindow(Handle, (int)ShowWindowCommand.Normal);
            ClientSize = new Size(windowWidth, windowHeight);
        }
Exemple #3
0
        private void PlatformConstruct()
        {
            var            x         = 0;
            var            y         = 0;
            WindowStyles   style     = 0;
            WindowExStyles styleEx   = 0;
            const bool     resizable = true;

            // Setup the screen settings depending on whether it is running in full screen or in windowed mode.
            //if (fullscreen)
            //{
            //style = User32.WindowStyles.WS_POPUP | User32.WindowStyles.WS_VISIBLE;
            //styleEx = User32.WindowStyles.WS_EX_APPWINDOW;

            //width = screenWidth;
            //height = screenHeight;
            //}
            //else
            {
                if (Width > 0 && Height > 0)
                {
                    var screenWidth  = GetSystemMetrics(SystemMetrics.SM_CXSCREEN);
                    var screenHeight = GetSystemMetrics(SystemMetrics.SM_CYSCREEN);

                    // Place the window in the middle of the screen.WS_EX_APPWINDOW
                    x = (screenWidth - Width) / 2;
                    y = (screenHeight - Height) / 2;
                }

                if (resizable)
                {
                    style = WindowStyles.WS_OVERLAPPEDWINDOW;
                }
                else
                {
                    style = WindowStyles.WS_POPUP | WindowStyles.WS_BORDER | WindowStyles.WS_CAPTION | WindowStyles.WS_SYSMENU;
                }

                styleEx = WindowExStyles.WS_EX_APPWINDOW | WindowExStyles.WS_EX_WINDOWEDGE;
            }
            style |= WindowStyles.WS_CLIPCHILDREN | WindowStyles.WS_CLIPSIBLINGS;

            int windowWidth;
            int windowHeight;

            if (Width > 0 && Height > 0)
            {
                var rect = new RawRect(0, 0, Width, Height);

                // Adjust according to window styles
                AdjustWindowRectEx(
                    ref rect,
                    style,
                    false,
                    styleEx);

                windowWidth  = rect.Right - rect.Left;
                windowHeight = rect.Bottom - rect.Top;
            }
            else
            {
                x = y = windowWidth = windowHeight = CW_USEDEFAULT;
            }

            var hwnd = CreateWindowEx(
                (int)styleEx,
                Application.WndClassName,
                Title,
                (int)style,
                x,
                y,
                windowWidth,
                windowHeight,
                IntPtr.Zero,
                IntPtr.Zero,
                IntPtr.Zero,
                IntPtr.Zero);

            if (hwnd == IntPtr.Zero)
            {
                return;
            }

            ShowWindow(hwnd, ShowWindowCommand.Normal);
            Handle = hwnd;
            Width  = windowWidth;
            Height = windowHeight;
        }