WPF fullscreen mode - c#

I am creating an WPF application which has following XAML structure.
<Window>
<ScrollViewer>
<Grid>
...
...
...
</Grid>
</ScrollViewer>
</Window>
I want to run application on fullscreen on the press of 'F' button and for that i tried following code.
private void window1_KeyUp(object sender, KeyEventArgs e)
{
if(e.Key == Key.F)
{
if(!isFullScreen)
{
height = mePlayer.Height;
width = mePlayer.Width;
mePlayer.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
mePlayer.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
this.Background = new SolidColorBrush(Colors.Black);
this.WindowStyle = WindowStyle.None;
this.WindowState = WindowState.Maximized;
isFullScreen = !isFullScreen;
}
else
{
mePlayer.Height = height;
mePlayer.Width = width;
this.Background = new SolidColorBrush(Colors.White);
this.WindowStyle = WindowStyle.SingleBorderWindow;
isFullScreen = !isFullScreen;
}
}
}
I am Facing following two problems.
When i press F key for full screen, window goes to full screen mode but task bar is still visible
In full screen mode scroll bar becomes visible.
I don't know why this is happening. I think scroll bar becomes visible because of the taskbar. Any help would greatly appreciated.
Here is the screen shot of what is happening.

I'm not sure why you're doing all the extra stuff but doing this seems to be sufficient and working fine:
private void window1_KeyUp(object sender, KeyEventArgs e)
{
if(e.Key == Key.F)
{
if(!isFullScreen)
{
this.WindowStyle = WindowStyle.None;
this.WindowState = WindowState.Maximized;
this.SC.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
this.SC.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
isFullScreen = !isFullScreen;
}
else
{
this.WindowStyle = WindowStyle.SingleBorderWindow;
this.WindowState = WindowState.Normal;
this.SC.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
this.SC.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
isFullScreen = !isFullScreen;
}
}
}
SC is my ScrollViewer.

Related

Cursor = Cursors.None for popup in code

Im trying to set the cursor to none in code for a popup but I cant get it to work. The cursor is still shown when it is over the popup. What am I doing wrong?
public void SubWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
TextBlock popupText = new TextBlock();
popupText.Text = "Complete" ;
popupText.Background = Brushes.Transparent;
popupText.Foreground = Brushes.White;
popupText.Width = 130;
popupText.FontSize = 30;
popupText.IsHitTestVisible = false;
popupText.Cursor = Cursors.None;
Popup Popup = new Popup();
Popup.AllowsTransparency = true;
Popup.PlacementRectangle = new Rect(1086, 16, 0, 0);
Popup.IsHitTestVisible = false;
Popup.Cursor = Cursors.None;
Popup_Text.Child = popupText;
Popup.IsOpen = true;
}
Don't set the IsHitTestVisible property of the TextBlock to false:
TextBlock popupText = new TextBlock();
popupText.Text = "Complete";
popupText.Background = Brushes.Transparent;
popupText.Foreground = Brushes.White;
popupText.Width = 130;
popupText.Height = 130;
popupText.FontSize = 30;
//popupText.IsHitTestVisible = false;
popupText.Cursor = Cursors.None;
Popup Popup = new Popup();
//Popup.AllowsTransparency = true;
Popup.PlacementRectangle = new Rect(1086, 16, 0, 0);
Popup.IsHitTestVisible = false;
Popup.Cursor = Cursors.None;
Popup.Child = popupText;
Popup.IsOpen = true;
Also note that your app can only change the cursor when the cursor is actually over one of your app's elements. The "background" of a transparent Popup does not belong to your application so Cursors.None will only apply when you move the mouse pointer over the actual text in the TextBlock.

WPF Window -- Trying to create two size options: constant "normal dimensions" and full-screen

I am creating a WPF application in which I would like the user to be able to choose between the loaded Window dimensions or full-screen. I have attempted this by doing the following:
Within Window in xaml, I set:
<Window x:Class= ...
...
StateChanged="Window1_StateChanged"
Height=520
Width = 1000>
Within my xaml.cs file, I set:
void Window1_StateChanged(object sender, EventArgs e)
{
Console.WriteLine("SC: " + this.WindowState);
if (this.WindowState == WindowState.Maximized)
{
Console.WriteLine("SC: " + this.WindowState);
this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
this.Height = SystemParameters.MaximizedPrimaryScreenHeight;
this.Width = SystemParameters.MaximizedPrimaryScreenWidth;
}
else if (this.WindowState == WindowState.Normal || this.WindowState == WindowState.Minimized)
{
MinHeight = 520;
MaxHeight = 520;
MinWidth = 1000;
MaxWidth = 1000;
Console.WriteLine("SC: " + this.WindowState);
}
}
However the window never maximizes. When I add these two lines in the WindowState.Maximized conditional, I end up losing my titlebar.
this.WindowState = WindowState.Normal;
this.WindowState = WindowState.Maximized;
Any ideas as to what's going on? Please let me know if you need more details.
Thanks a lot!

Full screen Windows Form goes beyond screen dimensions

I have a WinForms app (.NET 4) that needs to be shown either full screen or maximized without borders.
Using the following code in the Form_Shown event:
#if (DEBUG)
var debug = true;
#else
var debug = false;
#endif
this.Text = "";
this.ControlBox = false;
this.ShowInTaskbar = true;
//this.TopMost = debug;
this.TopLevel = true;
this.FormBorderStyle = FormBorderStyle.None;
if (debug) { this.Bounds = Screen.FromControl(this).WorkingArea; }
else { this.WindowState = FormWindowState.Maximized; }
If you look closely at the screenshot below, the top and bottom areas are cut off by a few pixels. Also, if maximized, the window still does not cover the task bar.
Please note that I have only one monitor attached. No secondary displays.
Any suggestions on how to address the two issues above would be appreciated.
UPDATE: The code above seems to work fine with forms without a MenuStrip or StatusStrip.
Here is the code I use for fullscreen. I create a FullScreen property for my form and when I need, I set this.FullScreen = true;
private bool fullScreen = false;
[DefaultValue(false)]
public bool FullScreen
{
get
{
return fullScreen;
}
set
{
fullScreen = value;
if (value)
{
//this.SuspendLayout();
this.WindowState = FormWindowState.Normal;
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
//this.ResumeLayout(true);
}
else
{
this.Activate();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
}
}
}

FullscreenBehaviour for Mahapps

How to add a dynamic switching ability from fullscreen to windowed mode and vice versa to Mahapps MetroWindow?
Starting with Normal Window
and after switching to fullscreen the top right window Buttons (Minimize/Maximize/Close) are still visible (but they shouldn't be visible as well as the title bar). The reserved space for the title bar seems to be still there.
The other way round initially from fullscreen state (no buttons, except the Hello button in the middle and no title bar => as expected)
... but when switching back to normal window state the title is back again but the top left buttons are missing.
Am I doing something wrong here in the code? I used an derrived Behaviour. The interesting part that is executed when switching is this:
private static void OnIsFullscreenChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var window = (MetroWindow)sender;
var oldValue = (bool)e.OldValue;
var newValue = (bool)e.NewValue;
if (newValue == oldValue || window == null)
{
return;
}
if (newValue)
{
window.Tag = window.WindowState;
window.Topmost = true;
window.UseNoneWindowStyle = true;
window.IgnoreTaskbarOnMaximize = true;
window.ShowTitleBar = false;
window.WindowStyle = WindowStyle.None;
window.WindowState = WindowState.Maximized;
}
else
{
window.Topmost = false;
window.UseNoneWindowStyle = false;
window.IgnoreTaskbarOnMaximize = false;
window.ShowTitleBar = true;
window.WindowStyle = WindowStyle.SingleBorderWindow;
window.WindowState = (WindowState)window.Tag;
}
}
Attaching a simular Behaviour to a default Window WPF control everything works as expected.
I attach the Behaviour this way:
<controls:MetroWindow ... local:FullscreenBehavior.IsFullscreen="{Binding Fullscreen}">
<!-- code above sets initial state depending on ViewModel value -->
<!-- code below fires mode switching when a defined key is pressed => executes OnIsFullscreenChanged method -->
<i:Interaction.Behaviors>
<behaviours:BorderlessWindowBehavior />
<behaviours:WindowsSettingBehaviour />
<behaviours:GlowWindowBehavior />
<modern:FullscreenBehavior FullscreenKey="{Binding FullscreenKey}" />
</i:Interaction.Behaviors>
...
EDIT: Set state of Window Buttons explicitly
When I extend the method to set the states to the correct value explicitly there seems to be another strange effect:
private static void OnIsFullscreenChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var window = (MetroWindow)sender;
var oldValue = (bool)e.OldValue;
var newValue = (bool)e.NewValue;
if (newValue == oldValue || window == null)
{
return;
}
if (newValue)
{
window.Tag = window.WindowState;
window.Topmost = true;
window.UseNoneWindowStyle = true;
window.IgnoreTaskbarOnMaximize = true;
window.ShowTitleBar = false;
window.ShowCloseButton = false;
window.ShowMaxRestoreButton = false;
window.ShowMinButton = false;
window.WindowStyle = WindowStyle.None;
window.WindowState = WindowState.Maximized;
}
else
{
window.Topmost = false;
window.UseNoneWindowStyle = false;
window.IgnoreTaskbarOnMaximize = false;
window.ShowTitleBar = true;
window.ShowCloseButton = true;
window.ShowMaxRestoreButton = true;
window.ShowMinButton = true;
window.ShowCloseButton = true;
window.ShowMaxRestoreButton = true;
window.WindowStyle = WindowStyle.SingleBorderWindow;
window.WindowState = (WindowState)window.Tag;
}
}
The window gets "sometimes" cut at the border and sometimes it looks right (like in the first picture at the top).
Also I don't know (yet) wheter the space of the title bar is no longer reserved when initially starting with fullscreen (there seems to be a difference, don't know why).
There is a little bug in the current 1.0 release. If you toggle the UseNoneWindowStyle, it doesn't bring back the buttons and toolbar. I'll fix this as soon as possible.
So, here is a little workaround for you.
public static readonly DependencyProperty ToggleFullScreenProperty =
DependencyProperty.Register("ToggleFullScreen",
typeof(bool),
typeof(MainWindow),
new PropertyMetadata(default(bool), ToggleFullScreenPropertyChangedCallback));
private static void ToggleFullScreenPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var metroWindow = (MetroWindow)dependencyObject;
if (e.OldValue != e.NewValue)
{
var fullScreen = (bool)e.NewValue;
if (fullScreen)
{
metroWindow.UseNoneWindowStyle = true;
metroWindow.IgnoreTaskbarOnMaximize = true;
metroWindow.ShowMinButton = false;
metroWindow.ShowMaxRestoreButton = false;
metroWindow.ShowCloseButton = false;
metroWindow.WindowState = WindowState.Maximized;
}
else
{
metroWindow.UseNoneWindowStyle = false;
metroWindow.ShowTitleBar = true; // <-- this must be set to true
metroWindow.IgnoreTaskbarOnMaximize = false;
metroWindow.ShowMinButton = true;
metroWindow.ShowMaxRestoreButton = true;
metroWindow.ShowCloseButton = true;
metroWindow.WindowState = WindowState.Normal;
}
}
}
public bool ToggleFullScreen
{
get { return (bool)GetValue(ToggleFullScreenProperty); }
set { SetValue(ToggleFullScreenProperty, value); }
}
Hope this helps.

opacity around the message

Is it possible to make a poup screen with high opacity around the popup screen in winform? If yes, how?
How do I make the pop up message or GUI screen to be in the middle of the computer screen?
Please remember that I don't have any source code yet.
An example:
To show your Form at center of screen, use StartPosition property of form to CenterScreen.
this.StartPosition = FormStartPosition.CenterScreen;
Now, to grey rest of the portion of screen.
Take a new form name it frmBlur and set these properties.
this.BackColor = SystemColors.ControlDark;
this.FormBorderStyle = FormBorderStyle.None;
this.Opacity = 0.8;
this.ShowInTaskbar = false;
this.TopMost = true;
this.WindowState = FormWindowState.Maximized;
Now, use the below code to display MessageBox or winform
private void button1_Click(object sender, EventArgs e)
{
using (frmBlur ob = new frmBlur())
{
ob.Show();
frmMessage f = new frmMessage();
f.TopMost = true;
f.ShowDialog();
}
}

Categories