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;
}
}
}
Related
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.
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.
I'd like to show an instance of a form class for a specific time. The form needs to be topmost and not steal focus. Here is my code:
public class mSplashForm : Form
{
public mSplashForm()
{
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.LightBlue;
this.Opacity = 0.92D;
this.ShowInTaskbar = false;
this.MinimumSize = new System.Drawing.Size(5, 5);
}
}
public static void mSplash(int time = 500)
{
mSplashForm SF = new mSplashForm();
Application.EnableVisualStyles();
SF.Width = 500;
SF.Height = 100;
SF.Left = 500;
SF.Top = 500;
SetWindowPos(SF.Handle, HWND_TOPMOST, SF.Left, SF.Top, SF.Width, SF.Height, SWP_NOACTIVATE);
ShowWindow(SF.Handle, mEnumShowWindowCommands.ShowNoActivate);
Application.DoEvents();
Thread.Sleep(time);
SF.Close();
}
It works, but the form is not shown in the right position defined using Top and Left parameters. What is wrong please?
You've got your form set to start in FormStartPosition.WindowsDefaultLocation. Add this into your mSplash function:
SF.StartPosition = FormStartPosition.Manual;
This is why it's trying to position successively down the page (as per your comment) on each opening.
set the start position to manual:
this.StartPosition = FormStartPosition.Manual;
Try this
SF.StartPosition = FormStartPosition.Manual;
SF.Width = 500;
SF.Height = 100;
SF.Left = 500;
SF.Top = 500;
I want to use fully transparent Modal form in my application, with being able to fill it with partially-transparent image; For this, I used to remove all visible elements from the form and got the code below.
class WinScreenshotWindow : Form
{
public WinScreenshotWindow()
{
// Create from without erasing background with a color
// Going not to use transparent form instead, it will produce context menu bugs in textboxes for child form
this.SuspendLayout();
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPosition.Manual;
this.ControlBox = false;
this.Visible = false;
this.Size = new Size(100, 100);
this.Location = new Point(200, 200);
this.ResumeLayout();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
// Erase Background Windows message:
}
protected override void OnPaint(PaintEventArgs e)
{
Rectangle clientRect = e.ClipRectangle;
e.Graphics.FillRectangle(Brushes.Transparent, clientRect);
}
}
static void Main()
{
Form form = new Form();
form.Size = new Size(400, 400);
form.Show();
var ww = new WinScreenshotWindow();
ww.ShowDialog(form);
}
But the result is something strange:
When I remove filling in OnPaint(), it is not visible at all.
The question is - why does this happen? If the background is transparent why do it shows the form in such way? And what can be done in this situation?
Any help appreciated.
Wouldn't it be easier to open a borderless form with a red backcolor and set the TransparencyKey = red?
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();
}
}