/// <summary> /// 将窗口置底 /// </summary> /// <param name="lpClassName">窗口类名</param> /// <param name="lpWindowName">窗口标题</param> public static void SendToBack(string lpClassName, string lpWindowName) { IntPtr hWnd = UnsafeNativeMethods.FindWindow(lpClassName, lpWindowName); if (hWnd != IntPtr.Zero) { UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_BOTTOM, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE); } }
/// <summary> /// 将窗口置顶 /// </summary> /// <param name="lpClassName">窗口类名</param> /// <param name="lpWindowName">窗口标题</param> public static void BringToFront(string lpClassName, string lpWindowName) { IntPtr hWnd = UnsafeNativeMethods.FindWindow(lpClassName, lpWindowName); if (hWnd != IntPtr.Zero) { UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_TOP, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE); } }
/// <summary> /// 设置控件NoTopMost(将窗口放在所有“TopMost”类型窗口的后面,其它类型窗口的前面) /// </summary> /// <param name="hWnd">要设置的窗口</param> public static void SetNoTopMost(IntPtr hWnd) { try { UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_NOTOPMOST, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOACTIVATE); } catch { } }
internal void MoveToScreenCenter(HandleRef hWnd) { // Create an IntPtr to store a handle to the monitor. IntPtr hMonitor = IntPtr.Zero; // Get the monitor to use based on the location of the parent window if (_hwndOwnerWindow != IntPtr.Zero) { // we have a owner hwnd; center on the screen on // which our owner hwnd is. // We use MONITOR_DEFAULTTONEAREST to get the monitor // nearest to the window if the window doesn't intersect // any display monitor. hMonitor = SafeNativeMethods.MonitorFromWindow( new HandleRef(this, _hwndOwnerWindow), // window to find monitor location for NativeMethods.MONITOR_DEFAULTTONEAREST); // get the monitor nearest to the window // Only move the window if we got a valid monitor... otherwise let Windows // position the dialog. if (hMonitor != IntPtr.Zero) { // Now, create another RECT and fill it with the bounds of the parent window. NativeMethods.RECT dialogRect = new NativeMethods.RECT(); SafeNativeMethods.GetWindowRect(hWnd, ref dialogRect); Size dialogSize = new Size((dialogRect.right - dialogRect.left), /*width*/ (dialogRect.bottom - dialogRect.top)); /*height*/ // create variables that will receive the new position of the dialog double x = 0; double y = 0; // Call into a static function in System.Windows.Window to calculate // the actual new position Window.CalculateCenterScreenPosition(hMonitor, dialogSize, ref x, ref y); // Call SetWindowPos to actually move the window. UnsafeNativeMethods.SetWindowPos(hWnd, // handle to the window to move NativeMethods.NullHandleRef, // window to precede this one in zorder (int)Math.Round(x), (int)Math.Round(y), // new X and Y positions 0, 0, // new width and height, if applicable // Flags: // SWP_NOSIZE: Retains current size // SWP_NOZORDER: retains current zorder // SWP_NOACTIVATE: does not activate the window NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE); } } }
internal void MoveToScreenCenter(HandleRef hWnd) { IntPtr intPtr = IntPtr.Zero; if (this._hwndOwnerWindow != IntPtr.Zero) { intPtr = SafeNativeMethods.MonitorFromWindow(new HandleRef(this, this._hwndOwnerWindow), 2); if (intPtr != IntPtr.Zero) { NativeMethods.RECT rect = default(NativeMethods.RECT); SafeNativeMethods.GetWindowRect(hWnd, ref rect); Size currentSizeDeviceUnits = new Size((double)(rect.right - rect.left), (double)(rect.bottom - rect.top)); double a = 0.0; double a2 = 0.0; Window.CalculateCenterScreenPosition(intPtr, currentSizeDeviceUnits, ref a, ref a2); UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.NullHandleRef, (int)Math.Round(a), (int)Math.Round(a2), 0, 0, 21); } } }
/// <summary> /// 设置控件(包括其非工作区元素)相对于其父控件的大小和位置(以像素为单位)。 /// </summary> /// <param name="hWnd">控件句柄。</param> /// <param name="value">相对于父控件的 System.Drawing.Rectangle,表示控件(包括其非工作区元素)的大小和位置(以像素为单位)。</param> public static void SetBounds(IntPtr hWnd, Rectangle value) { UnsafeNativeMethods.SetWindowPos(hWnd, IntPtr.Zero, value.X, value.Y, value.Width, value.Height, NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE); }
/// <summary> /// 设置控件的高度和宽度。 /// </summary> /// <param name="hWnd">控件句柄。</param> /// <param name="value">System.Drawing.Size,表示控件的高度和宽度(以像素为单位)。</param> public static void SetSize(IntPtr hWnd, Size value) { UnsafeNativeMethods.SetWindowPos(hWnd, IntPtr.Zero, 0, 0, value.Width, value.Height, NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE); }
/// <summary> /// 设置该控件的左上角相对于其容器的左上角的坐标。 /// </summary> /// <param name="hWnd">控件句柄。</param> /// <param name="value">System.Drawing.Point,它表示控件的左上角相对于其容器的左上角。</param> public static void SetLocation(IntPtr hWnd, Point value) { UnsafeNativeMethods.SetWindowPos(hWnd, IntPtr.Zero, value.X, value.Y, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE); }
/// <summary> /// 设置一个值,该值指示是否显示该控件及其所有父控件。 /// </summary> /// <param name="hWnd">控件句柄。</param> /// <param name="value">如果显示该控件及其所有父控件,则为 true;否则为 false。默认为 true。</param> public static void SetVisible(IntPtr hWnd, bool value) { UnsafeNativeMethods.SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOZORDER | NativeMethods.SWP_NOACTIVATE | (value ? NativeMethods.SWP_SHOWWINDOW : NativeMethods.SWP_HIDEWINDOW)); }
/// <summary> /// 将控件发送到 Z 顺序的后面。 /// </summary> /// <param name="hWnd">控件句柄。</param> public static void SendToBack(IntPtr hWnd) { UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_BOTTOM, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE); }
/// <summary> /// 将控件带到 Z 顺序的前面。 /// </summary> /// <param name="hWnd">控件句柄。</param> public static void BringToFront(IntPtr hWnd) { UnsafeNativeMethods.SetWindowPos(hWnd, NativeMethods.HWND_TOP, 0, 0, 0, 0, NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOMOVE); }