【问题标题】:WPF: Remove the title/control boxWPF:删除标题/控制框
【发布时间】:2011-05-05 07:54:43
【问题描述】:

我刚刚从 WinForms 切换到 wpf,在 WinForms 中删除整个标题框非常简单,只需设置 title="" 和 ControlBox=false。

现在有很多关于如何使用 wpf 执行此操作的建议,所有这些建议都使用本机 Win32 调用。 尽管它们确实移除了控制框,但它们仍然在顶部留下了较粗的边框。

我确定使用某种本地调用是可行的,但是如何?

【问题讨论】:

    标签: c# .net wpf .net-3.5 .net-4.0


    【解决方案1】:

    设置 WindowStyle = none

    【讨论】:

      【解决方案2】:

      好吧,试试这个

      WindowStyle="none"
      

      像这样:

      <Window x:Class="Test.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow"  WindowStyle="None"
          MinHeight="350" MaxHeight="350" MinWidth="525" MaxWidth="525">
      <Grid>
      
      </Grid>
      </Window>
      

      编辑:

      它看起来有点愚蠢,但是这样(最小和最大高度/宽度大小相同)可以防止窗口被调整大小

      【讨论】:

      • 哦,我现在觉得有点傻。无论如何,我现在意识到我以前曾尝试过该解决方案,但它不起作用的原因是因为我还设置了 ResizeMode=NoResize,这完全消除了边框。所以我想真正的问题是:如何获得 WindowStyle=None 外观,同时防止窗口被调整大小?
      【解决方案3】:

      这是一种替代方法。 要删除 Max-/minimize,您需要像这样更改ResizeMode

      <Window x:Class="MyWpfApplication.MainWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              Title="" Height="350" Width="525" ResizeMode="NoResize">
          <Grid>
      
          </Grid>
      </Window>
      

      之后,您可以通过添加此按钮 (read more here) 删除关闭按钮

      private const int GWL_STYLE = -16;
      private const int WS_SYSMENU = 0x80000;
      [DllImport("user32.dll", SetLastError = true)]
      private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
      [DllImport("user32.dll")]
      private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
      
      public MainWindow()
      {
          SourceInitialized += Window_SourceInitialized;
      }
      
      void Window_SourceInitialized(object sender, EventArgs e)
      {
          var hwnd = new WindowInteropHelper(this).Handle;
          SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
      }
      

      【讨论】:

      • 但我们也会让您拥有标题栏。 WindowsStyle="none" 完全符合图片中显示的内容,而且更简单;-)
      • @Tokk,是的,看到了。改变了我的答案,说这是另一种方式! :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多