Пример #1
0
        private void Init(int x, int y, int width, int height, WindowSizeType sizeType, WindowType type, WindowStartupPosition startupPosition)
        {
            // get native HWND handle
            handle = new WindowInteropHelper(window).EnsureHandle();
            if (handle == IntPtr.Zero)
            {
                throw new Exception("WindowInteropHelper HWND failed");
            }

            // set form type
            switch (type)
            {
            case WindowType.Tool:
                window.ResizeMode = System.Windows.ResizeMode.NoResize;
                break;

            case WindowType.Popup:
                window.WindowStyle = System.Windows.WindowStyle.None;
                break;
            }

            // set form size
            SetSize(width, height, sizeType);

            // set form startup position
            switch (startupPosition)
            {
            case WindowStartupPosition.Custom:
                window.WindowStartupLocation = System.Windows.WindowStartupLocation.Manual;
                SetPosition(x, y);
                break;

            case WindowStartupPosition.CenterScreen:
                window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
                break;
            }

            // watch for close event
            window.Closed += Window_Closed;
        }
Пример #2
0
 public Window(int x, int y, int width, int height, WindowSizeType sizeType, WindowType type, WindowStartupPosition startupPosition)
 {
     window = new WPFWindow();
     Init(x, y, width, height, sizeType, type, startupPosition);
 }
Пример #3
0
        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);
        }
Пример #4
0
 public Window(Point2 position, Size2 size, WindowSizeType sizeType, WindowType type, WindowStartupPosition startupPosition)
 {
     window = new WPFWindow();
     Init(position.x, position.y, size.width, size.height, sizeType, type, startupPosition);
 }
Пример #5
0
        private void Init(int x, int y, int width, int height, WindowSizeType sizeType, WindowType type, WindowStartupPosition startupPosition)
        {
            // set form type
            switch (type)
            {
            case WindowType.Tool:
                form.MaximizeBox     = false;
                form.MinimizeBox     = false;
                form.FormBorderStyle = FormBorderStyle.FixedSingle;
                break;

            case WindowType.Popup:
                form.MaximizeBox     = false;
                form.MinimizeBox     = false;
                form.FormBorderStyle = FormBorderStyle.None;
                break;
            }

            // set form size
            SetSize(width, height, sizeType);

            // set form startup position
            switch (startupPosition)
            {
            case WindowStartupPosition.Custom:
                form.StartPosition = FormStartPosition.Manual;
                SetPosition(x, y);
                break;

            case WindowStartupPosition.CenterScreen:
                form.StartPosition = FormStartPosition.CenterScreen;
                break;
            }
        }