【问题标题】:WPF User Control full sreen on maximize?WPF用户控制全屏最大化?
【发布时间】:2015-12-08 18:10:09
【问题描述】:

我有一个窗口,在窗口内,我有一个用户控件。我只想在按下 F11 时最大化或扩展(全屏)用户控件。 我现有的代码适用于我的窗口,

if (e.Key == Key.F11)
{
    WindowStyle = WindowStyle.None;
    WindowState = WindowState.Maximized;
    ResizeMode = ResizeMode.NoResize;
}

但我需要为我的 UserControl 提供此功能。有没有类似的方法来最大化用户控制?请给我一些建议。提前致谢。

【问题讨论】:

  • 我不明白你的问题...UserControl 没有有要删除的窗口边框或按钮。请清楚地准确地概述您想要的内容。
  • Sheridan,我已经编辑了我的问题。这是否有助于您理解我的问题?
  • 正如 Sheridan 已经写的那样,您的问题并不清楚。 UserControl 是窗口的子窗口,只能拥有父窗口提供的空间。你想让你的用户控件填满整个空间吗?然后将其 VerticalAlignment 和 Horizo​​ntalAlignment 设置为 Stretch。您想在用户控件中控制窗口的最大化状态吗?然后访问您的窗口(通过传递它或使用 VisualTreeHelper)并设置适当的标志。否则,请更好地解释您想要实现的目标。
  • @voonna,老实说,我看不出有什么不同。我要求提供更多信息,因为您当前的问题没有意义。请参阅 dowhilefor 的评论,该评论进一步解释了该问题。另外,请注意,如果您想向某人发送通知,您可以在编写问题、答案或评论时在他们的姓名前添加“@”符号(如果适用,请删除空格)(就像我对您在此评论开头的名字)。

标签: c# wpf wpf-controls fullscreen


【解决方案1】:

我认为您不需要 PINvoke 即可在没有 Windows 工具栏的情况下获得全屏。这个想法是让您的用户控制并将其放置在您制作全屏的新窗口中。 我就是这样做的。

private Window _fullScreenWindow;
private DependencyObject _originalParent;

if (e.Key == Key.F11)
{
  if(_fullScreenWindow == null) //go fullscreen
  {
    _fullScreenWindow = new Window(); //create full screen window
    _originalParent = _myUserControl.Parent;
    RemoveChild(_originalParent, this); //remove the user control from current parent
    _fullScreenWindow.Content = this; //add the user control to full screen window
    _fullScreenWindow.WindowStyle = WindowStyle.None;
    _fullScreenWindow.WindowState = WindowState.Maximized;
    _fullScreenWindow.ResizeMode = ResizeMode.NoResize;

    _fullScreenWindow.Show();
  }
  else //exit fullscreen
  { 
    var parent = Parent;
    RemoveChild(parent, this);
    _fullScreenWindow.Close();
    _fullScreenWindow = null;

    AddChild(viewerControlParent, this);
  }
}

RemoveChild(和 AddChild 类似)的实现可以在这个答案中找到: https://stackoverflow.com/a/19318405

【讨论】:

    【解决方案2】:

    用户控件没有这样的东西。

    我认为只有两件事我不确定你想要实现。

    1)您想要一个没有窗口工具栏的全屏,为此您必须调用 PINvokes

    WPF 没有隐藏标题栏的关闭按钮的内置属性,但您可以通过几行 P/Invoke 来实现。

    首先,将这些声明添加到您的 Window 类中:

        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);
        Then put this code in the Window's Loaded event:
    
    var hwnd = new WindowInteropHelper(this).Handle;
    SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
    

    然后你去:不再有关闭按钮。标题栏左侧也不会出现窗口图标,这意味着没有系统菜单,即使您右键单击标题栏也是如此——它们都在一起。

    请注意,Alt+F4 仍将关闭窗口。如果您不想让窗口在后台线程完成之前关闭,那么您也可以覆盖 OnClosing 并将 Cancel 设置为 true。

    2)您想在对话框中显示此用户控件,然后您必须使用 Window 类并将用户控件放入其中作为孩子

    您目前拥有的对于 Windows 来说是正确的,是的。如果不是这样,那么我只能认为您的目标是第一个?

    【讨论】:

      猜你喜欢
      • 2011-06-23
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 2017-12-17
      • 2020-04-21
      相关资源
      最近更新 更多