예제 #1
0
        public WindowManager(
            [Import] Lazy <IDispatchManager> dispatchManager
            )
        {
            _lpClassName     = (WCHAR *)(Marshal.StringToHGlobalUni($"TerraFX.Interop.Provider.Win32.UI.Window.{EntryModuleHandle}"));
            _lpWindowName    = (WCHAR *)(Marshal.StringToHGlobalUni($"TerraFX Win32 Window"));
            _dispatchManager = dispatchManager;

            var wndClassEx = new WNDCLASSEX()
            {
                cbSize        = unchecked ((uint)(Marshal.SizeOf <WNDCLASSEX>())),
                style         = CS.VREDRAW | CS.HREDRAW,
                lpfnWndProc   = WndProc,
                cbClsExtra    = 0,
                cbWndExtra    = 0,
                hInstance     = EntryModuleHandle,
                hIcon         = HICON.NULL,
                hCursor       = HCURSOR.NULL,
                hbrBackground = (IntPtr)(COLOR.WINDOW + 1),
                lpszMenuName  = LPWSTR.NULL,
                lpszClassName = _lpClassName,
                hIconSm       = HICON.NULL
            };

            var classAtom = RegisterClassEx(ref wndClassEx);

            if (classAtom == 0)
            {
                ExceptionUtilities.ThrowExternalExceptionForLastError(nameof(RegisterClassEx));
            }

            _classAtom = classAtom;
        }
예제 #2
0
        /// <summary>Closes the instance.</summary>
        public void Close()
        {
            var succeeded = CloseWindow(_hWnd);

            if (!succeeded)
            {
                ExceptionUtilities.ThrowExternalExceptionForLastError(nameof(CloseWindow));
            }
        }
예제 #3
0
        /// <summary>Activates the instance.</summary>
        public void Activate()
        {
            var previousActiveWindow = SetActiveWindow(_hWnd);

            if (previousActiveWindow == HWND.NULL)
            {
                ExceptionUtilities.ThrowExternalExceptionForLastError(nameof(SetActiveWindow));
            }
        }
        public static void ThrowExternalExceptionForLastErrorStringTest(
            [Values(null, "", "methodName")] string methodName
            )
        {
            var errorCode = Marshal.GetLastWin32Error();

            Assert.That(() => ExceptionUtilities.ThrowExternalExceptionForLastError(methodName),
                        Throws.InstanceOf <ExternalException>()
                        .With.Property("ErrorCode").EqualTo(errorCode)
                        );
        }
예제 #5
0
        /// <summary>Shows the instance.</summary>
        public void Show()
        {
            if (!_isVisible)
            {
                var succeeded = ShowWindow(_hWnd, SW.SHOW);

                if (!succeeded)
                {
                    ExceptionUtilities.ThrowExternalExceptionForLastError(nameof(ShowWindow));
                }
            }
        }
예제 #6
0
        private void Dispose(bool isDisposing)
        {
            if (_hWnd != HWND.NULL)
            {
                var succeeded = DestroyWindow(_hWnd);

                if (!succeeded)
                {
                    ExceptionUtilities.ThrowExternalExceptionForLastError(nameof(DestroyWindow));
                }

                _hWnd = null;
            }
        }
예제 #7
0
        /// <summary>Initializes a new instance of the <see cref="DispatchManager" /> class.</summary>
        public DispatchManager()
        {
            var succeeded = QueryPerformanceFrequency(out var lpFrequency);

            if (!succeeded)
            {
                ExceptionUtilities.ThrowExternalExceptionForLastError(nameof(QueryPerformanceFrequency));
            }

            const double ticksPerSecond = Timestamp.TicksPerSecond;

            _tickFrequency = (ticksPerSecond / unchecked ((ulong)(lpFrequency)));

            _dispatchers = new ConcurrentDictionary <Thread, Dispatcher>();
        }
예제 #8
0
        /// <summary>Initializes a new instance of the <see cref="Window" /> class.</summary>
        internal Window(IDispatchManager dispatchManager, LPWSTR lpClassName, LPWSTR lpWindowName, HINSTANCE hInstance)
        {
            var hWnd = CreateWindowEx(
                WS_EX.OVERLAPPEDWINDOW,
                lpClassName,
                lpWindowName,
                WS.OVERLAPPEDWINDOW,
                unchecked ((int)(CW.USEDEFAULT)),
                unchecked ((int)(CW.USEDEFAULT)),
                unchecked ((int)(CW.USEDEFAULT)),
                unchecked ((int)(CW.USEDEFAULT)),
                HWND.NULL,
                HMENU.NULL,
                hInstance,
                null
                );

            if (hWnd == HWND.NULL)
            {
                ExceptionUtilities.ThrowExternalExceptionForLastError(nameof(CreateWindowEx));
            }

            _hWnd       = hWnd;
            _dispatcher = dispatchManager.DispatcherForCurrentThread;
            _properties = new PropertySet();

            var succeeded = GetWindowRect(_hWnd, out var lpRect);

            if (!succeeded)
            {
                ExceptionUtilities.ThrowExternalExceptionForLastError(nameof(GetWindowRect));
            }

            _bounds = new Rectangle(lpRect.left, lpRect.top, (lpRect.right - lpRect.left), (lpRect.bottom - lpRect.top));

            var activeWindow = GetActiveWindow();

            _isActive = (activeWindow == _hWnd);

            _isVisible = IsWindowVisible(hWnd);
        }