Bring window to top - c#

I am trying to bring my Form to the top, take screenshot, save it and then minimize it back using the following code.
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = FormBorderStyle.None;
this.TopMost = true;
Application.DoEvents();
string keys = "%" + "{PrtSc}";
SendKeys.SendWait(keys);
Clipboard.GetImage().Save(imagePath, ImageFormat.Jpeg);
this.FormBorderStyle = FormBorderStyle.Sizable;
this.WindowState = FormWindowState.Minimized;
this.TopMost = false;
I thought the code will always bring the window on top and take the screenshot. However I noticed that its not 100% working:
The machine is too slow, so when the code that bring the window to top executes, the entire window does not appear.
Sometimes It does not bring it to top, so the pic contains some user activity.
How can I bring it completely to the top and wait till its done complete and then take a screenshot and reverse it.
Please note that there is a single form in the application.

Perhaps add a timer. run the first part of the code(up until and including:"Application.DoEvents();"), and start the timer. On the tick code check the "this.TopMost" status and only if it is the topmost form take the screen shot and stop the timer.

Related

How to run winform with showing taskbar?

I have a C#.2017 project, Home form is none border (set in properties). It always start with maximize and startposition is windowsdefaultlocation/manual (I tried). I try many code but it still runs and hide taskbar.
I want to run the form in none border, maximize/full screen mode, the form doesn't hide the taskbar of windows 10.
Tried this links:
https://social.msdn.microsoft.com/Forums/windows/en-US/e81dc341-720e-474a-9c37-75eac3a130cb/howto-show-window-form-on-top-of-taskbar-in-every-resolution?forum=winforms
https://www.c-sharpcorner.com/UploadFile/shubham0987/display-app-in-full-screen-with-windows-taskbar/
How to display a Windows Form in full screen on top of the taskbar?
private void Form1_Load(object sender, EventArgs e)
{
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Width = Screen.PrimaryScreen.WorkingArea.Width;
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
//Screen currentScreen = Screen.FromHandle(this.Handle);
//this.Size = new System.Drawing.Size(currentScreen.Bounds.Width, currentScreen.Bounds.Height);
}
It doesn't help me anything. If you have some solution better help me please.
Much thank to all.
Setting a Forms WindowState to Maximized when your BorderStyle is none will always lay over the taskbar. This is a typical behaviour of a "Fullscreen" application.
However you are on the right track. If you want to have a semi-fullscreen experience without laying over the taskbar you have to set the Location and Size of your Form manually.
Important here is that you not only set these values manually but also take away control from the OS itself as it will always try to position a Form by some ruleset.
private void Form1_Load(object sender, EventArgs e)
{
//Hiding the Border to simulate a fullscreen-experience
this.FormBorderStyle = FormBorderStyle.None;
//Telling the operating system that we want to set the start position manually
this.StartPosition = FormStartPosition.Manual;
//Actually setting our Width, Height, and Location
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Width = Screen.PrimaryScreen.WorkingArea.Width;
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
}
Just a little side node: You might want to think about people with multiple screens and on which screen your application should appear (maybe let the user decide by some setting etc).
Maybe this one is quite similar to something I need:
this.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size ;

Keep my form above the taskbar?

My overall goal is to render a second (or third, fourth...) mouse cursor. To this end, I have created a frameless, topmost, transparent window. I can draw on this window (I have 4 buttons on it to show that it's properly covering the whole desktop) - but when I click on the taskbar, it is brought to the top and overlays my buttons and drawn line.
How can I keep my window above the taskbar?
Alternatively, is there a way that I can draw on the "final" version of the screen?
Here's the code from my .Designer.cs file:
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.White;
this.CausesValidation = false;
this.ClientSize = new System.Drawing.Size(332, 332);
this.ControlBox = false;
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Screen";
this.ShowIcon = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultBounds;
this.TopMost = true;
this.TransparencyKey = System.Drawing.Color.White;
Without analysing the exact motivation of doing this and whether this approach is the best way through, you should note that the taskbar's behaviour is equivalent to the one of any other window: TopMost can be used without any restriction. On the other hand, it is considered outside the "WorkingArea" and thus, depending upon the properties you are using, it might be ignored. Take a look at the sample codes below to understand this better.
Main Form covering all the available space above the taskbar:
this.ClientSize = new System.Drawing.Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Left, Screen.PrimaryScreen.WorkingArea.Top);
Main Form covering the whole available space of the screen:
this.ClientSize = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Left, Screen.PrimaryScreen.WorkingArea.Top);
Thus, you can locate your form on top of the taskbar without any problem by bearing the mind that it represents the difference between WorkingArea and Bounds of the screen.
CLARIFICATION: this answer highlights what might be the reason for the taskbar to be treated differently than any other part of the screen. It does not imply that you have to make your form as big as the whole screen to put it over the taskbar; you can put it over the taskbar whenever you want with an as small size as you wish. What you have to bear in mind is that WorkingArea does not provide the whole dimensions of the screen, but just the area ABOVE the taskbar; if you want to bring the taskbar into consideration, consider Bounds.
I'll go with varocarbas answer,but there is something that i want to add which i think might be useful.
A few things are there that i guess may be helpful,when i read your question the first idea that struck my mind was to create a GlobalHook to monitor message WM_CBT,this message is send to GlobalHook procedure when the system is about to Maximize,Minimize,Restore or Focus a Window.When any hook is trapped you can override any required events to block that window from becoming the topmost window,thus giving your Form a chance to remain the topmost Window.This method will be highly helpful if it worked as i am expecting.
If you are interested in this, here are the methods that are required to make a GlobalHook work.
SetWindowsHookEx(),used to tell the system to create a GlobalHook.
HookProc,Represents the method called when a hook catches a monitored event.
HookType,Enumerates the valid hook types passed into a call to SetWindowsHookEx.
CallNextHook.If using interoperability is messy,try this open source library that contains WindowsAPI functions in a managed wrapper.Hope this helps you,meanwhile i'm also trying to get the stuff working for you.
I've been struggling for a few hours with this same problem. I thought of a nice solution: just change the bottom padding of the form dynamically whenever the form is maximized.
If Me.WindowState = FormWindowState.Maximized then
Dim pd As New Padding
pd.Left = Me.Padding.Left
pd.Right = Me.Padding.Right
pd.Top = Me.Padding.Top
pd.Bottom = Me.Height - Screen.PrimaryScreen.WorkingArea.Height
Me.Padding = pd
end if
This way the form is not above the taskbar, but at least your controls are!
Note: in designing your forms you can leave the bottom-padding 0.
If you want your form to be able to minimize/normal resize, then you may want to reset the bottom padding to 0 in another form event.
I actually found the solution about 15 minutes after I posted. (Isn't that always the way? Research for hours, but as soon as you ask for help, you figure it out on your own. :)
I have an event that is calling my code with position updates. If I simply call "BringToFront" in this event, it works just fine. The event is called many times per second, but if I throttle my behavior back to 1/4 second, it still works great.

PrimaryScreen.Bounds.Height is not the full screen

I have a windows form. I'm setting it's size using the following code. My desired behaviour is to have it take the full width and half the height of the primary display.
this.Location = new Point(0, 0);
this.WindowState = FormWindowState.Normal;
this.Size = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height / 2);
The problem is there is a space on the right form (it's not taking up the full screen). Does anyone have any suggestions as to why?
Thanks
Try use
Screen.PrimaryScreen.WorkingArea
Two ways to make it fullscreen:
// maximize the form
this.WindowState = FormWindowState.Maximized;
or
// hide the border
this.FormBorderStyle = FormBorderStyle.None;
You can try them together but have to notice the order, hide the border first, then set the window state to maximized.

Get size of window in maximized version in winforms

How can I get the size a form would have in Maximized windowstate without maximizing it ?
I am doing a snapshot of a map control. I want to maximize the window when doing the snapshot but without the user noticing. It seems that the form windows state doesn't change the form size when it is hidden :
this.Hide();
this.WindowState = FormWindowState.Maximized;
// Snapshot but this.Size didn't change
this.WindowState = FormWindowState.Normal;
this.Show();
It works fine when not hiding.
So I'm trying to set the size manually but need to know the Maximized state width and height:
// x & y ???
this.Size = new Size(x,y);
You can read the workingarea-screen size.
Try the SuspendLayout() function to suspend layout updates to your screen. By calling ResumeLayout() you can resume this.

C# - Why won't a fullscreen winform app ALWAYS cover the taskbar?

I'm using Windows Vista and C#.net 3.5, but I had my friend run the program on XP and has the same problem.
So I have a C# program that I have running in the background with an icon in the SystemTray. I have a low level keyboard hook so when I press two keys (Ctr+windows in this case) it'll pull of the application's main form. The form is set to be full screen in the combo key press even handler:
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
So it basically works. When I hit CTR+Windows it brings up the form, no matter what program I have given focus to. But sometimes, the taskbar will still show up over the form, which I don't want. I want it to always be full screen when I hit that key combo.
I figure it has something to do with what application has focus originally. But even when I click on my main form, the taskbar sometimes stays there. So I wonder if focus really is the problem. It just seems like sometimes the taskbar is being stubborn and doesn't want to sit behind my program.
Anyone have any ideas how I can fix this?
EDIT: More details-
I'm trying to achieve the same effect that a web browser has when you put it into fullscreen mode, or when you put powerpoint into presentation mode.
In a windows form you do that by putting the border style to none and maximizing the window. But sometimes the window won't cover the taskbar for some reason. Half the time it will.
If I have the main window topmost, the others will fall behind it when I click on it, which I don't want if the taskbar is hidden.
Try this (where this is your form):
this.Bounds = Screen.PrimaryScreen.Bounds;
this.TopMost = true;
That'll set the form to fullscreen, and it'll cover the taskbar.
I've tried so many solutions, some of them works on Windows XP and all of them did NOT work on Windows 7. After all I write a simple method to do so.
private void GoFullscreen(bool fullscreen)
{
if (fullscreen)
{
this.WindowState = FormWindowState.Normal;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
}
else
{
this.WindowState = FormWindowState.Maximized;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
}
}
the order of code is important and will not work if you change the place of WindwosState and FormBorderStyle.
One of the advantages of this method is leaving the TOPMOST on false that allow other forms to come over the main form.
It absolutely solved my problem.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F11)
if (FormBorderStyle == FormBorderStyle.None)
{
FormBorderStyle = FormBorderStyle.Sizable;
WindowState = FormWindowState.Normal;
}
else
{
SuspendLayout();
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
ResumeLayout();
}
}
As far as I know, the taskbar is either above or below windows based on the "Keep the taskbar on top of other windows" setting. (At least, that's the wording in XP.) I suppose you could try to see if you can detect this setting and toggle it if needed?
Try resizing the form and bringing it to the front of the z-order like so:
Rectangle screenRect = Screen.GetBounds(this);
this.Location = screenRect.Location;
this.Size = screenRect.Size;
this.BringToFront();

Categories

Resources