2

I have a window and inside the window, i have a usercontrol. I only want to maximize or expand (to full screen) the User Control on the hit of F11. My existing code works for my window,

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

But i need to have this feature for my UserControl. Is there a similar way to maximize the User Control? Please give me some suggestions. Thanking in advance.

voonna
  • 741
  • 3
  • 10
  • 24
  • 1
    I don't understand your question... a `UserControl` does *not* have a window border or buttons to remove. Please *clearly* outline exactly what you want. – Sheridan Sep 04 '13 at 14:58
  • Sheridan, I have edited my question. Does this help you understanding my question? – voonna Sep 04 '13 at 15:12
  • 2
    As Sheridan already wrote, your question is not clear. A UserControl is a child of a window and can only have the space provided by the parent window. Do you want your usercontrol to fill the complete space? Then set its VerticalAlignment and HorizontalAlignment to Stretch. Do you want to control the maximize state of the window in your user control? Then get access to your window (by passing it or using VisualTreeHelper) and set the appropiate flags. Otherwise please give a better explanation of what you want to achieve. – dowhilefor Sep 04 '13 at 15:13
  • 2
    @voonna, I don't see any difference to be honest. I'm asking for more information because your current question does not make sense. Please see dowhilefor's comment that further explains the problem. Also, just so you know, if you want to send a notification to somebody, you can add the '@' symbol before their name (and remove spaces if applicable) when you write a question, answer, or comment (like I did with your name at the start of this comment). – Sheridan Sep 04 '13 at 15:18

2 Answers2

3

I don't think you need PINvoke to get fullscreen without windows toolbar. The idea is to take your user control and place it inside a new window which you make full screen. This is how I have done it.

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);
  }
}

Implementation of RemoveChild (and similiar for AddChild) can be found in this answer : https://stackoverflow.com/a/19318405

Community
  • 1
  • 1
1

There is no such thing for User Controls.

There are only two things I can think that I am not sure that you want to achieve.

1) You want a full screen without the Window Toolbar and in order to this you have to call PINvokes

WPF doesn't have a built-in property to hide the title bar's Close button, but you can do it with a few lines of P/Invoke.

First, add these declarations to your Window class:

    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);

And there you go: no more Close button. You also won't have a window icon on the left side of the title bar, which means no System menu, even when you right-click the title bar -- they all go together.

Note that Alt+F4 will still close the Window. If you don't want to allow the window to close before the background thread is done, then you could also override OnClosing and set Cancel to true.

2) You want to show this User control in a dialog which you will then have to use Window class and put the Usercontrol inside as the Child

What you currently have is correct for Windows, yes. If that's not the case then I can only think that you are aiming for the first one?

123 456 789 0
  • 10,565
  • 4
  • 43
  • 72