Move current Form to left side of screen - c#

I have a Windows Forms that represents a menu.
If I click on an Item, I want to move the Form to the top left of the Screen and extend the window to the buttom of the screen.
Like GIMP do it. GIMP has multiple windows and 2 of them are docked left and right of the screen.
It looks like:
How can I do this?

Using the Screen.FromControl function to determine the current screen, then set the parameters accordingly:
Rectangle r = Screen.FromControl(this).WorkingArea;
this.Bounds = new Rectangle(r.Left, 0, this.Width, r.Height);

Related

How to create small window in the corner of the screen

I am trying to create a bot using c#, in Visual Studio. What i am struggling to do is that i am trying to create a small box in the corner of the screen while the bot is running, a like the one in the picture:
Also, i would like it to be permanently in the corner of screen, so it cant be a window. What i mean by this is that if i was to click away from the box and away from the screen then the box should still be visible.
If you want the window to be placed in the lower right corner of the screen, this code will help:
protected override void OnLoad(EventArgs e)
{
this.TopMost = true;
var screen = Screen.FromPoint(this.Location);
this.Location = new Point(screen.WorkingArea.Right - this.Width, screen.WorkingArea.Top);
base.OnLoad(e);
}
You want the TopMost property on a regular Form.

Getting Bottom Left location of scroll able Screen

Description Created a form in Winform C# app.
Added a panel (PBack) with dock type fill. (Scrollable)
Then added a picturebox(pbDraw) in panel(PBack) that height depends upon image size.
I want to add a control on the bottom left of the current screen view. (assume client scrolled down)
What i tried
Rectangle rect = Screen.GetWorkingArea(pbDraw);
ctrl.Top = rect.Top + rect.Height;
ctrl.Top = Screen.PrimaryScreen.WorkingArea.Top + Screen.PrimaryScreen.WorkingArea.Height;
ctrl.Top = Screen.FromControl(pbDraw).WorkingArea.Top+ Screen.FromControl(pbDraw).WorkingArea.Height;
Issue control is adding at top of pbDraw (0,0) and not on current screen bounds top.
As far as I understand, Control.Top takes the scrolled view into account all by itself.
Gets or sets the distance, in pixels, between the top edge of the control and the top edge of its container's client area.
So, you should be able to assign the coordinates relevant to the pBack and its client view:
ctrl.Top = pBack.Height - ctrl.Height;
Assuming that ctrl is a child of pBack this code should place it at the bottom of the current (scrolled) part of pBack
UPDATE:
As you said in the comments, ctrl is actually a child of pbDraw. In this case, you'll need to take the scrolling into account. For that, you can use Panel.VerticalScroll:
//scroll position + panel height - control height
ctrl.Top = pBack.VerticalScroll.Value + pBack.Height - ctrl.Height;
If I were you, I'd add ctrl to the panel, on top of the picture box. This will make it easier to calculate offsets relative to the panel.

WPF determine element is visible on screen

I'm using a Popup to show validation messages to the User. If the element (e.g. a Textbox) is scrolled out of the View the following Method is working great and i can easy hide the Popup.
Determine control is Visible
But if i move the window partial out of the screen so i can't see the element anymore this Method still returns true. In this case the popup is still visibile to the User (The popup allways stays within screen bounds) but the element is not.
How can i check if a element is visibile within the screen?
This can be done using PrimaryScreenWidth and PrimaryScreenHeight available in SystemParameters. You need to calculate the position of control relative to screen and check whether that point exist in screen bounds.
Point locationFromWindow = this.textBox.TransformToVisual(this).Transform(new Point(0, 0));
Point point = this.textBox.PointToScreen(new Point(0, 0));
Rect rect = new Rect(0, 0, SystemParameters.PrimaryScreenWidth, SystemParameters.PrimaryScreenHeight);
if (!rect.Contains(point))
{
// Outside screen bounds.
}

Programmatically Maximize Window On Half Of Screen

I want to maximize a random window on the left side of my screen. Can I use Windows Aero functions from my code ? This window can be maximized like that with the mouse. I just want to do that programmatically.
I use C# and I can get the IntPtr of the window.
If possible without faking mouse or keyboard input.
This can be done without p/invoke.
Try this:
Rectangle rect = Screen.PrimaryScreen.WorkingArea;
rect.Width = rect.Width / 2;
Bounds = rect;
This will put the current window on the left of the primary screen.
Then just add this to put it on the right of the screen.
Location = new Point(rect.Width, 0);
It's not exactly the same but fakes it well:
ShowWindow(handle, SW_MAXIMIZE);
// for a split second you might see a maximized window here
MoveWindow(handle, 0, 0, Screen.PrimaryScreen.WorkingArea.Width / 2, Screen.PrimaryScreen.WorkingArea.Height, true);
Will require heavy lifting for real-time placements, especially for non-child processes. Example - www.ishadow.com/vdm. For manual "fixing" of maximized windows position MoveWindow(hWnd, startPos, 0, winWidth, winHeight, true) after ShowWindow(hWnd, SW_MAXIMIZE) usually (try Task Manager on Windows 10) works as pointed above.

Windows Forms Screenshot of whole screen, except the calling window?

Just to take on a challenge, I decided to write an application to magnify only a particular section of the screen (under the mouse, following the mouse). The best way I can think to do this is to take a screenshot of the space under the form, enlarge, and paint on the form. I'm using this section of code to snap the picture inside a timer:
timer1.Stop();
//Reposition window
this.Left = Cursor.Position.X - this.Width / 2;
this.Top = Cursor.Position.Y - this.Height / 2;
//Bitmap to save image to
Bitmap bmpScreenshot = new Bitmap(this.Width, this.Height, PixelFormat.Format32bppArgb);
Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot);
//Grab section of screen shot
this.Opacity = 0.01;
gfxScreenshot.CopyFromScreen(this.Left, this.Top, 0, 0, this.Size, CopyPixelOperation.SourceCopy);
this.Opacity = 1;
//Save to class level so Paint can paint it
frame = bmpScreenshot;
//Make Double Buffered Panel repaint
dbPanel1.Invalidate();
timer1.Start();
In order to get the desktop (or whatever windows/contents are present) beneath the form, I set it's opacity to 0.01 (so that it's still clickable, which a click closes the app. Absolute 0 is not clickable) before taking the picture and then 1 after taking the snapshot... This creates a flicker. Is there a way I can get a bitmap of what's under the form without changing opacity or hiding/showing the form? I want a live image of what's under the form, not just a snapshot of when the app started.
I'm not looking for other apps that do this, I'm working on this so when I approach these problems I can learn a solution. I've tried changing the opacity (to all sorts of levels), using show and hide, SetVisibleCore(bool) on the window, but nothing lowers the flicker to an acceptable level. Any thoughts?
I would think you'd be better off displaying the magnified image offset from the cursor (look at the iPhone text selection/editing interface for example), and add some smart handling for the screen edges. That way you don't have to keep changing opacity, you just update the image and or placement of your form.

Categories