/* * GetClientRectangle */ /// <summary> /// Modifies the specified client rectangle so that the window /// looked properly in any window state. /// </summary> /// <exception cref="NuGenInvalidHWndException"> /// Specified <paramref name="hWnd"/> does not represent a window. /// </exception> protected virtual Rectangle GetClientRectangle(IntPtr hWnd, Rectangle rectangleToModify) { if (!User32.IsWindow(hWnd)) { throw new NuGenInvalidHWndException(hWnd); } if (Window.GetState(hWnd) == FormWindowState.Maximized) { RECT clientRect = new RECT(0, 0, 100, 100); User32.AdjustWindowRectEx( ref clientRect, Window.GetStyle(hWnd), false, Window.GetExStyle(hWnd) ); Rectangle clientRectangle = (Rectangle)clientRect; int x = Math.Abs(clientRectangle.X); int offset = 2; rectangleToModify.X += x - offset; rectangleToModify.Width -= ((x * 2) - (offset * 2)) - 1; rectangleToModify.Height -= ((x * 2) - (offset * 2)) - 1; rectangleToModify.Y += x - offset - 1; return(rectangleToModify); } return(rectangleToModify); }
/// <summary> /// Tweaks the window bounds so that it looked properly in any window state. /// </summary> /// <exception cref="NuGenInvalidHWndException"> /// Specified <paramref name="hWnd"/> does not represent a window. /// </exception> protected virtual void AdjustBounds(IntPtr hWnd, ref int width, ref int height, BoundsSpecified specified) { if (!User32.IsWindow(hWnd)) { throw new NuGenInvalidHWndException(hWnd); } RECT rect = new RECT(0, 0, width, height); User32.AdjustWindowRectEx( ref rect, Window.GetStyle(hWnd), false, Window.GetExStyle(hWnd) ); if ((specified & BoundsSpecified.Height) == BoundsSpecified.Height) { height -= rect.Height - height; } if ((specified & BoundsSpecified.Width) == BoundsSpecified.Width) { width -= rect.Width - width; } }
/// <summary> /// Resizes the window. /// </summary> /// <param name="width"> The width. </param> /// <param name="height"> The height. </param> /// <exception cref="Win32Exception"> Thrown when a Window 32 error condition occurs. </exception> public void Resize(int width, int height) { RECT windowRect; windowRect.LeftTop.X = 0; windowRect.LeftTop.Y = 0; windowRect.RightBottom.X = width; windowRect.RightBottom.Y = height; uint windowStyles = _borderStyle switch { FormBorderStyle.None => 0, FormBorderStyle.Fixed => WS.CAPTION | WS.SYSMENU | WS.OVERLAPPED | WS.MINIMIZEBOX | WS.MAXIMIZEBOX, FormBorderStyle.Sizable => WS.CAPTION | WS.SYSMENU | WS.OVERLAPPED | WS.MINIMIZEBOX | WS.MAXIMIZEBOX | WS.SIZEFRAME, _ => throw new ArgumentOutOfRangeException() }; // ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault windowStyles |= _windowState switch { FormWindowState.Normal => 0, FormWindowState.Maximized => WS.MAXIMIZE, _ => throw new ArgumentOutOfRangeException() }; uint windowStylesEx = WSEX.LEFT | WSEX.LTRREADING | WSEX.WINDOWEDGE | WSEX.APPWINDOW; if (_borderStyle == FormBorderStyle.None) { windowStylesEx &= ~WSEX.WINDOWEDGE; } if (!User32.AdjustWindowRectEx(ref windowRect, windowStyles, false, windowStylesEx)) { throw new Win32Exception(Kernel32.GetLastError(), $"{nameof(User32.AdjustWindowRectEx)} failed!"); } if (!User32.SetWindowPos( _hWnd, IntPtr.Zero, 0, 0, windowRect.RightBottom.X - windowRect.LeftTop.X, windowRect.RightBottom.Y - windowRect.LeftTop.Y, SetWindowPosFlags.DoNotActivate | SetWindowPosFlags.IgnoreMove | SetWindowPosFlags.IgnoreZOrder)) { throw new Win32Exception(Kernel32.GetLastError(), $"{nameof(User32.SetWindowPos)} failed!"); } }
private void InitializeWindow() { AutoHelpers.LogInvariant("Initializing window data for viewport area..."); IntPtr hWnd = app.GetWindowHandle(); User32.RECT lpRect; User32.GetClientRect(hWnd, out lpRect); int style = User32.GetWindowLong(hWnd, User32.GWL_STYLE); int exStyle = User32.GetWindowLong(hWnd, User32.GWL_EXSTYLE); Verify.IsTrue(User32.AdjustWindowRectEx(ref lpRect, style, false, exStyle)); this.clientTopLeft = new Point(); this.clientTopLeft.X = Math.Abs(lpRect.left); this.clientTopLeft.Y = Math.Abs(lpRect.top); AutoHelpers.LogInvariant("Top left corner of client area is at X:{0} Y:{1}", this.clientTopLeft.X, this.clientTopLeft.Y); }
private void CreateWindowInternal() { 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 = User32.GetSystemMetrics(SystemMetrics.SM_CXSCREEN); var screenHeight = User32.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 Rect(0, 0, Width, Height); // Adjust according to window styles User32.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 = User32.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; } User32.ShowWindow(hwnd, ShowWindowCommand.Normal); Handle = hwnd; Width = windowWidth; Height = windowHeight; }
/// <summary> /// Creates a window. /// </summary> /// <param name="w"> The width. </param> /// <param name="h"> The height. </param> /// <returns> /// The new window. /// </returns> /// <exception cref="Win32Exception"> Thrown when a Window 32 error condition occurs. </exception> internal IntPtr CreateWindow(int w, int h) { RECT windowRect; windowRect.LeftTop.X = 0; windowRect.LeftTop.Y = 0; windowRect.RightBottom.X = w; windowRect.RightBottom.Y = h; uint windowStyles = _borderStyle switch { FormBorderStyle.None => WS.POPUP, FormBorderStyle.Fixed => WS.CAPTION | WS.SYSMENU | WS.OVERLAPPED | WS.MINIMIZEBOX | WS.MAXIMIZEBOX, FormBorderStyle.Sizable => WS.CAPTION | WS.SYSMENU | WS.OVERLAPPED | WS.MINIMIZEBOX | WS.MAXIMIZEBOX | WS.SIZEFRAME, _ => throw new ArgumentOutOfRangeException() }; // ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault windowStyles |= _windowState switch { FormWindowState.Normal => 0, FormWindowState.Maximized => WS.MAXIMIZE, _ => throw new ArgumentOutOfRangeException() }; uint windowStylesEx = WSEX.LEFT | WSEX.LTRREADING | WSEX.WINDOWEDGE | WSEX.APPWINDOW; if (_borderStyle == FormBorderStyle.None) { windowStylesEx &= ~WSEX.WINDOWEDGE; } if (!User32.AdjustWindowRectEx(ref windowRect, windowStyles, false, windowStylesEx)) { throw new Win32Exception(Kernel32.GetLastError(), $"{nameof(User32.AdjustWindowRectEx)} failed!"); } const int CW_USEDEFAULT = unchecked ((int)0x80000000); if ((_hWnd = User32.CreateWindowEx( windowStylesEx, _wndClassEx.lpszClassName, WindowTitle, windowStyles, CW_USEDEFAULT, 0, windowRect.RightBottom.X - windowRect.LeftTop.X, windowRect.RightBottom.Y - windowRect.LeftTop.Y, IntPtr.Zero, IntPtr.Zero, _wndClassEx.hInstance, IntPtr.Zero)) == IntPtr.Zero) { throw new Win32Exception(Kernel32.GetLastError(), $"{nameof(User32.CreateWindowEx)} failed!"); } if (_borderStyle == FormBorderStyle.None) { User32.SetWindowLongPtr(_hWnd, WLF.GWL_STYLE, (IntPtr)0); if (!User32.SetWindowPos( _hWnd, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.DoNotActivate | SetWindowPosFlags.IgnoreMove | SetWindowPosFlags.IgnoreZOrder | SetWindowPosFlags.IgnoreResize | SetWindowPosFlags.FrameChanged)) { throw new Win32Exception(Kernel32.GetLastError(), $"{nameof(User32.SetWindowPos)} failed!"); } } Device.RegisterDevice(HIDUsagePage.Generic, HIDUsage.Mouse, RawInputDeviceFlags.None, _hWnd); return(_hWnd); }