Programmatically Maximize Window On Half Of Screen - c#

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.

Related

Prevent rendering of the desktop selection rectangle

I'm working on small program that basically animates the wallpaper using OpenGL.
The wallpaper is all interactive(I use a global mouse hook for the mouse events), but to make it perfect when I hold down the left mouse button, I'd like the selection rectangle to not appear at all.
All I could change was disable the translucent selection rectangle using a registry key and a P/Invoke function to force the desktop to reload it's registry settings, resulting in this:
On the image you can see the selection outline which would be great if wasn't rendered when I hold and drag.
Is it possible to achieve using C#?
EDIT:
There is a partial solution to this problem that #Jonathan Potter provided:
W32.EnumChildWindows(W32.GetDesktopWindow(), (hwnd, param) =>
{
var WM_CANCELMODE = 0x001F;
var sb = new StringBuilder(256);
if (W32.GetClassName(hwnd, sb, 256) > 0)
{
if (sb.ToString() == "SysListView32")
W32.SendMessage(hwnd, WM_CANCELMODE, 0, IntPtr.Zero);
}
return true;
}, IntPtr.Zero);
The problem with this solution is that now whenever I try selecting anything on the desktop, it won't, since I am literally cancelling the selection just as I begin holding the mouse button.

Move current Form to left side of screen

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);

Replace Windows Shell or Restrict application window size

I'm working on an application for home based users. And this application involves completely replacing windows explorer for a custom application.
I would like to mimic the behavior of the taskbar. For example, when you click the maximize button in Notepad, it does not overlap the taskbar.
I already tried to use the api to AppBar however, AppBar api does not work when windows explorer is not running and other windows overlap my taskbar.
Any idea how I can do this? If I can restrict the size of maximized windows when ever helps me. The problem is that other applications are not always written by me.
I found an example using an win32 api SystemParametersInfo.
This way I can define an area where the other applications will fit and leave an space to my form even if other applications is maximized.
Link on github
public static void MakeNewDesktopArea()
{
// Save current Working Area size
m_rcOldDesktopRect.left = SystemInformation.WorkingArea.Left;
m_rcOldDesktopRect.top = SystemInformation.WorkingArea.Top;
m_rcOldDesktopRect.right = SystemInformation.WorkingArea.Right;
m_rcOldDesktopRect.bottom = SystemInformation.WorkingArea.Bottom;
// Make a new Workspace
WinAPI.RECT rc;
rc.left = SystemInformation.VirtualScreen.Left + 150;
rc.top = SystemInformation.VirtualScreen.Top; // We reserve the 24 pixels on top for our taskbar
rc.right = SystemInformation.VirtualScreen.Right;
rc.bottom = SystemInformation.VirtualScreen.Bottom - 101;
WinAPI.SystemParametersInfo((int)WinAPI.SPI.SPI_SETWORKAREA, 0, ref rc, 0);
}

How to monitor a window's position with Win32?

I need to draw an overlay using WPF on top of a number of Win32 windows. In order to draw the overlay in the correct place, I will need to hook into the window move, but have no idea how to do this. Which Win32 calls should I be looking at?
SetWinEventHook
var hook = SetWinEventHook(EVENT_SYSTEM_MOVESIZESTART,
EVENT_SYSTEM_MOVESIZEEND, NULL, WinEventProc,
0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);

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