Jump to content
dummzeuch

How can I move a window to the left edge of a monitor?

Recommended Posts

Pressing Win+Left will dock the current window to the left edge of the current monitor.

How can do that from code?

I tried to set the form bounds, in particular Left to 0 but that leaves a gap of about 7 pixels. Setting it to -7 still leaves that gap.

 

My Google fu has left me (again). All I can find is how to use the keyboard combination for that.

Share this post


Link to post

Setting form's Left works fine for me. I have autogenerated manifest, enabled runtime themes and "Per monitor v2" DPI Awarrness.

Instead of setting Left to -7, I set it to minus (GetSystemMetrics(SM_CXSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER)), which is -8 on my PC.

 

 

 

  • Thanks 1

Share this post


Link to post
2 hours ago, dummzeuch said:

Pressing Win+Left will dock the current window to the left edge of the current monitor.

How can do that from code?

I tried to set the form bounds, in particular Left to 0 but that leaves a gap of about 7 pixels. Setting it to -7 still leaves that gap.

 

My Google fu has left me (again). All I can find is how to use the keyboard combination for that.

Couldn't you just set the ScreenSnap property to true to allow the user to snap the form to a screen edge?

Share this post


Link to post
2 minutes ago, PeterBelow said:

Couldn't you just set the ScreenSnap property to true to allow the user to snap the form to a screen edge?

This is not about letting the user do anything but the program positioning its window.

(To allow the user to do that, the ScreenSnap property is no longer necessary in Windows 10.)

Share this post


Link to post
1 hour ago, Vandrovnik said:

Setting form's Left works fine for me. I have autogenerated manifest, enabled runtime themes and "Per monitor v2" DPI Awarrness.

Instead of setting Left to -7, I set it to minus (GetSystemMetrics(SM_CXSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER)), which is -8 on my PC.

Hm, that seems to be kind of a magical value, because if I use that rather then the -7, it works. It's also -8 on my PC.

Share this post


Link to post
  WindowState := TWindowState.wsMaximized;
  var L := Left;
  WindowState := TWindowState.wsNormal;
  Left := L;

 

Share this post


Link to post

OK, so

Left := -(GetSystemMetrics(SM_CXSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER));

works for Left

 

(actually it's

Left := Form.Monitor.WorkAreaRect.Left - (GetSystemMetrics(SM_CXSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER));

because the left edge of the monitor might not be at  pixel 0).

 

It does not work for the height though:

Height := Form.Monitor.WorkAreaRect.Height + 2* (GetSystemMetrics(SM_CXSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER));

still leaves a gap to the bottom border of the monitor. But that's consistent with Windows + Left (but not with maximizing the window)

Edited by dummzeuch

Share this post


Link to post
12 minutes ago, Vandrovnik said:

For the "y" direction, there is SM_CYSIZEFRAME.

That doesn't work either. If I subtract if from the top, the title bar gets partly clipped and if I add it to the height, it has no effect.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×