I am developping a simple windows application (in C#) and I want it to display a form that is maximized in my second monitor. To do so, I am doing the following on the "Load" event of the form:
private void FormTest_Load(object sender, EventArgs e)
{
Screen[] screens = Screen.AllScreens;
this.WindowState = FormWindowState.Normal;
this.Location = screens[1].WorkingArea.Location;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
The problem I have is that when I execute this, the form only takes part of the screen. My primary screen has a resolution of 1024x768 and my secondary screen has a resolution of 1920x1080, and it seems that the form is taking the size of the primary screen in my secondary screen.
Also, I have a button that runs the following code to maximize the screen or turn it back to normal:
private void ChangeSize() {
if (this.WindowState == System.Windows.Forms.FormWindowState.Maximized)
{
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
}
else
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
}
When I click on the button twice (first to de-maximize the form, and then to maximize it again) the form does cover the entire secondary screen perfectly, but if I try to just run that function in code twice (for the sake of testing) right after the code in "FormTest_Load", the screen will still not cover correctly the entire screen.
I am probably making a noob mistake here but I have struggled with this for some time, so I would really appreciate if anyone can shed some light on what is wrong with my code.
I've tried to get the same result as you describe (changing resolutions, etc), but all works fine in my computer and screens. May be... what is exactly the variable screens? I've assumed that is Screen.AllScreens, like this:
protected override void OnLoad(EventArgs e)
{
Screen[] screens = Screen.AllScreens;
int screenNumber = 1;
this.WindowState = FormWindowState.Normal;
this.Location = screens[screenNumber].WorkingArea.Location;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
As I said, it works fine for me.
Also, you can check the screen settings of your computer: is the size of text and apps set to 100% (recommended value)?
Related
I am creating the application which should be always on top and not minimizable / maximizable.
For now I have the following code snippet:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.Manual;
}
private void Form1_Load( object sender, EventArgs e )
{
this.Location = new Point( Screen.PrimaryScreen.WorkingArea.Right - this.Width + 8 );
this.Height = Screen.PrimaryScreen.Bounds.Height;
this.MinimizeBox = false;
this.MaximizeBox = false;
this.TopMost = true;
}
}
Now this application is docked or placed on the right side of the screen. Now I need to adjust the resolution of everything else on the screen. I'll try to explain with the following image:
Explanation:
There is an application which is always on top. "My app" is the application which I am working on and my idea is to "share the same spot" on the screen like said earlier.
I tried to look for some solutions like this one:
https://www.c-sharpcorner.com/article/how-to-change-screen-resolution-in-C-Sharp/
but this only changes the resolution of the whole display. I need to leave resolution as it is in my application and resize everything else.
Is there a way to do it?
PS: If it is confusing in some way please let me know I will update the question.
Thanks
Before starting the question, I apologize for not using English well.
I don't want to use Border and Minimized/Maximized/Closed buttons in the existing form.
FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
Then, I created Custom Minimized/Maximized/Closed Button.
//Closed
private void imgLblCloseProgram_Click(object sender, EventArgs e) {
this.Close();
}
//Maximized
private void imgLblMaximumProgram_Click(object sender, EventArgs e) {
if(this.WindowState == FormWindowState.Maximized) {
this.WindowState = FormWindowState.Normal;
} else if(this.WindowState == FormWindowState.Normal) {
//this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea; //★★★
this.WindowState = FormWindowState.Maximized; //
}
}
private void tbPnlTopBar_DoubleClick(object sender, EventArgs e) {
imgLblMaximumProgram_Click(imgLblMaximumProgram, e);
}
//Minimized
private void imgLblMinimumProgram_Click(object sender, EventArgs e) {
this.WindowState = FormWindowState.Minimized; //
}
When Clear the annotation, Maximizes without taking up space on the taskbar.
However, if more than one monitors are used, they do not work on screens other than the main screen.
In addition, for the Maximized/Minimized button, there seems to be a built-in animation provided by Windows.
When the Maximized/Minimized was changed via FormWindowState, I confirmed that the animation did not work.
The biggest reason why I want the Low-cost Maximized Button Event is that when I use more than one monitors, I have a issue.
Therefore, I need a way to maximize the form while displaying TaskBar on two or more monitors.
p.s. I want the animation of the maximized/minimized button as well, so the best way to run the maximized/minimized button's event is.
I solved this.
when I use more than one monitors, I have a issue(they do not work on screens other than the main screen).
If you still want to use FormWindowState.Maximized on more than one monitor, this is likely to be an appropriate solution.
this.MaximizedBounds = new Rectangle(new Point(Screen.PrimaryScreen.WorkingArea.X, Screen.PrimaryScreen.WorkingArea.Y), new Size(Screen.FromHandle(this.Handle).WorkingArea.Width, Screen.FromHandle(this.Handle).WorkingArea.Height));
However, there is an error on the screen with higher resolution than the main monitor.
I have a project that automates/controls other Window applications by simulating keystrokes. To simulate keystrokes, I need to bring the window of that application to the front. When this is in progress, you can see the whole process on the screen.
However, I want to have a fake full screen to cover that up. If this is possible, I would also like to display something on the fake full screen, such as the progress bar.
Any suggestion would be helpful. Thanks
What you want to achieve is to make your main form be displayed in "windowed full screen" mode that is right ?
So in order to do that, you need to set your window size to your screen size.
A way to do it would be the following, in your WPF class :
this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
Or
this.WindowState = WindowState.Maximized;
To bring a window forward, you can also use :
myWindow.Activate();
or
this.TopMost = true;
I hope this will be helpful.
To prevent other windows overlaying your window when it is deactivated you can add the method, I'd use the solution at WPF Always On Top (I've it used before and it worked well for me), to the Deactivated event of the window:
private void Window_Deactivated(object sender, EventArgs e)
{
Window window = (Window)sender;
window.Topmost = true;
}
If you would like to show a progress bar in a fullscreen window when your operation is in progress, you could do something like:
private Window CreateFullScreenLoadingBar()
{
Window fullscreenWindow = new YourLoadingBarWindow();
fullscreenWindow.WindowStyle = WindowStyle.None;
fullscreenWindow.WindowState = WindowState.Maximized;
fullscreenWindow.ResizeMode = ResizeMode.NoResize;
fullscreenWindow.WindowStartupLocation = WindowStartupLocation.CenterOwner;
fullscreenWindow.Deactivated += delegate (object sender, EventArgs e)
{
Window window = (Window)sender;
window.Topmost = true;
};
}
Usage:
...
Window loadingBar = CreateFullScreenLoadingBar();
fullscreenWindow.Show();
fullscreenWindow.Activate();
// Your code
loadingBar.Close();
...
Where YourLoadingBarWindow is a window containing a progress bar or whatever you want the user to see.
I've successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs when trying to start with the app minimized. At first the problem was that the app would be minimized but would still appear in the alt-tab dialog. Changing the FormBorderStyle to one of the ToolWindow options (from the "None" option) fixed this, but introduced another problem. When the app first starts the titlebar of the minimized window is visible just above the start menu:
Opening the form and the closing it causes it to hide properly. I've tried lots of variations, but here's essentially how it's working right now...
WindowState is set to Minimized in the Designer. After some initialization in the constructor I have the following lines:
this.Visible = false;
this.ShowInTaskbar = false;
When the NotifyIcon is double-clicked I have the following:
this.WindowState = FormWindowState.Normal;
this.Visible = true;
this.ShowInTaskbar = true;
Like I said, I've tried lots of minor variations on this (this.Hide(), etc.). Is there a way to have the NotifyIcon be the primary component such that I can completely start and dispose of the form while leaving the NotifyIcon running? There's got to be a way to start the app with the form minimized without any of the weirdness. Please help me find it!
The right way to do this is to prevent the form from getting visible in the first place. That requires overriding SetVisibleCore(). Let's assume a context menu for the NotifyIcon with a Show and Exit command. You can implement it like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
notifyIcon1.ContextMenuStrip = contextMenuStrip1;
this.showToolStripMenuItem.Click += showToolStripMenuItem_Click;
this.exitToolStripMenuItem.Click += exitToolStripMenuItem_Click;
}
private bool allowVisible; // ContextMenu's Show command used
private bool allowClose; // ContextMenu's Exit command used
protected override void SetVisibleCore(bool value) {
if (!allowVisible) {
value = false;
if (!this.IsHandleCreated) CreateHandle();
}
base.SetVisibleCore(value);
}
protected override void OnFormClosing(FormClosingEventArgs e) {
if (!allowClose) {
this.Hide();
e.Cancel = true;
}
base.OnFormClosing(e);
}
private void showToolStripMenuItem_Click(object sender, EventArgs e) {
allowVisible = true;
Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
allowClose = true;
Application.Exit();
}
}
Note a wrinkle with the Load event, it won't fire until the main form is first shown. So be sure to do initialization in the form's constructor, not the Load event handler.
I'm reading all the answers and see hacks and black magic... (no offense, mates)
No hacks needed. You don't even have to set "ShowInTaskbar=false" and other stuff. Just do this:
//"Form Shown" event handler
private void Form_Shown(object sender, EventArgs e)
{
//to minimize window
this.WindowState = FormWindowState.Minimized;
//to hide from taskbar
this.Hide();
}
NOTE: I strongly recommend NOT TOUCHING the "ShowInTaskbar" property. For example, if your application registers system-wide hotkeys or other similar stuff (hooks, etc) - setting ShowInTaskBar=false and minimizing your app will prevent Windows from sending some messages to your window... And your hooks/hotkeys/etc will stop working.
In the constructor, remove these two lines:
this.Visible = false;
this.ShowInTaskbar = false;
and add after InitializeComponent();:
this.WindowState = FormWindowState.Minimized;
In designer, set ShowInTaskbar to false & FormWindowState to Normal.
EDIT:
If you post the same in Load event, the window does get minimized but still shows minimized on the desktop. I think this is a bug.
When minimizing an application and you want to hide it from Alt+Tab:
You also need to set the Opacity to stop the titlebar showing near the Start Menu when you set the Border Style to a Tool Window.
On Minimize Event:
this.Visible = false;
this.Opacity = 0;
this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
this.ShowInTaskbar = false;
On Normalize Event:
this.Visible = true;
this.Opacity = 100;
this.FormBorderStyle = FormBorderStyle.FixedSingle; //or whatever it was previously set to
this.ShowInTaskbar = true;
Move the following code from the Form's constructor to Form_Main_Load(). With the same setup on Notification_Icon when Form_Resize().
// Hide the Form to System Tray
this.WindowState = FormWindowState.Minimized;
This "quick and dirty fix" worked for me:
$form1.FormBorderStyle = "fixedtoolwindow"
$form1.top = -1000000
$form1.Left = -1000000
$form1.Width = 10
$form1.Height = 10
$form1.WindowState = "normal"
$form1.ShowInTaskbar = $False
$form1.Opacity = 0
$form1.Hide()
Hope it helps someone else...
So I am making a game on Visual Studio C# and I want the form to
be automatically maximized to any user's computer screen when compiled?
How can I do that?
You can do it using one of the following --
Set the form WindowState = FormWindowState.Maximized;
Get the screen resolution using following code and set the size of your forms accordingly
int height = Screen.PrimaryScreen.Bounds.Height;
int width = Screen.PrimaryScreen.Bounds.Width;
Set the WindowState property of your form to Maximized.
That will cause your form to be maximumized when it's opened.
You can use this.WindowState = FormWindowState.Maximized;
Go To Load Form As View Code and use This Code :
C#:
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
VB:
Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
If you are looking for something that will maximize your window on a first click and normalizes your window on a second click, this will help.
private void maximiseButton_Click(object sender, EventArgs e)
{
//normalises window
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
this.CenterToScreen();
}
//maximises window
else
{
this.WindowState = FormWindowState.Maximized;
this.CenterToScreen();
}
}
correct in VS2010:
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
On the Form Move Event add this:
private void Frm_Move (object sender, EventArgs e)
{
Top = 0; Left = 0;
Size = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
}