【问题标题】:Docking a WPF window to left/right sides of the screen windows 7/8将 WPF 窗口停靠到屏幕窗口的左侧/右侧 7/8
【发布时间】:2014-05-17 18:09:39
【问题描述】:

在窗口窗体和 WPF 中都有这个 .WindowState 我可以设置为

Minimized, maximizedNormal

我想知道是否已经实现了类似的方法来将窗口停靠在屏幕的左侧/右侧,例如 Win + / -> Windows 7/8 的键?

我用谷歌搜索过,似乎人们调用 Win32 API 并自己实现它

我应该编写自定义对接代码还是可以设置一个简单的标志,例如WindowState

【问题讨论】:

标签: c# .net wpf


【解决方案1】:

不,没有可以设置的标志。 Windows 改变了窗口的矩形,而 WPF 窗口除了矩形已经改变之外,不知道发生了什么。

自行更改矩形非常简单。 (实际上替换 Windows 的功能有点难度。)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;


namespace StackOverflowWPF
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      public MainWindow()
      {
         InitializeComponent();
      }

      private void Button_Click_1(object sender, RoutedEventArgs e)
      {
         this.Top = 0;
         this.Left = 0;
         System.Drawing.Rectangle screenBounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
         this.Width = screenBounds.Width / 2;
         this.Height = screenBounds.Height;
      }
   }
}

【讨论】:

  • 窗口最大化状态不仅仅是不同的界限。
  • 不是从 WPF 窗口的角度来看的。 Windows 自己跟踪矩形。 WPF 窗口不知道发生了什么,只是矩形发生了变化。
  • 我根据@Banana 所说的内容稍微改进了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-30
  • 2019-05-20
  • 1970-01-01
  • 1970-01-01
  • 2013-08-21
  • 2021-08-11
相关资源
最近更新 更多