Dock winform to screen side and resizing the screen WorkingArea - c#

I need a control panel, which is docked to the right side of my primary screen, here's how I've done that:
var PrimaryScreen = Screen.PrimaryScreen;
var WorkingArea = PrimaryScreen.WorkingArea;
this.Width = WorkingArea.Width / 6;
this.Height = WorkingArea.Height;
int X = WorkingArea.Width - this.Width;
int Y = WorkingArea.Location.Y;
this.Location = new Point(X, Y);
This works exactly, how I want it too, with one slight problem. I would need to simultaneously resize the primary screen workingarea, so other maximized forms / apps won't overlap my panel. Also, my panel should be always visible.
I had found a post here, exactly concerning this topic:
Reserve screen area in Windows 7
This is how it was suggested there:
public class WorkArea
{
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SystemParametersInfoA")]
private static extern Int32 SystemParametersInfo(Int32 uAction, Int32 uParam, IntPtr lpvParam, Int32 fuWinIni);
private const Int32 SPI_SETWORKAREA = 47;
public WorkArea(Int32 Left, Int32 Right, Int32 Top, Int32 Bottom)
{
_WorkArea.Left = Left;
_WorkArea.Top = Top;
_WorkArea.Bottom = Bottom;
_WorkArea.Right = Right;
}
public struct RECT
{
public Int32 Left;
public Int32 Right;
public Int32 Top;
public Int32 Bottom;
}
private RECT _WorkArea;
public void SetWorkingArea()
{
IntPtr ptr = IntPtr.Zero;
ptr = Marshal.AllocHGlobal(Marshal.SizeOf(_WorkArea));
Marshal.StructureToPtr(_WorkArea, ptr, false);
int i = SystemParametersInfo(SPI_SETWORKAREA, 0, ptr, 0);
}
}
But the post is over 10 years old, and the solution sadly didn't work for me.
So my question would be: What is the best way to accomplish that? I am open to any suggestions, since I am still learning ;)

Related

Compute total width of title bar buttons for 3rd party window on windows 10

My initial approach to this was using GetSystemMetrics with SystemMetric.SM_CXSIZE and some simple math based on which buttons are available (times 3, or times 1), via WindowStyle.
[DllImport("user32.dll")]
private static extern int GetSystemMetrics(SystemMetric smIndex);
This has an issue on Windows 10, where the calculated width is approximately 70% of actual. So the width covers just two buttons - maximize and close. Windows 7 and 8.1 are fine, same DPI setting, where it covers all buttons.
I checked a few existing questions on Stack Overflow, and had most success with this one from 2011:
How do I compute the non-client window size in WPF?
Unfortunately, while the suggested approach does work in windows 8.1, it calculates 0 on Windows 10 (latest version, all recommended updates). Is there a way that works on all OS from 7 to 10?
Code was taken from the above answer and modified to calculate total width of window's control buttons, by window handle (hwnd), and changed marshalling to RECT from Rectangle (then I get correct values of left/right).
public static int GetControlButtonsWidth(IntPtr hwnd)
{
// Create and initialize the structure
TITLEBARINFOEX tbi = new TITLEBARINFOEX();
tbi.cbSize = Marshal.SizeOf(typeof(TITLEBARINFOEX));
// Send the WM_GETTITLEBARINFOEX message
SendMessage(hwnd, WM_GETTITLEBARINFOEX, IntPtr.Zero, ref tbi);
int sum = tbi.rgrect.Sum(r => r.right - r.left);
// Return the filled-in structure
return sum;
}
internal const int WM_GETTITLEBARINFOEX = 0x033F;
internal const int CCHILDREN_TITLEBAR = 5;
[StructLayout(LayoutKind.Sequential)]
internal struct TITLEBARINFOEX
{
public int cbSize;
public RECT rcTitleBar;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = CCHILDREN_TITLEBAR + 1)]
public int[] rgstate;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = CCHILDREN_TITLEBAR + 1)]
public RECT[] rgrect;
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr SendMessage(
IntPtr hWnd,
int uMsg,
IntPtr wParam,
ref TITLEBARINFOEX lParam);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left, top, right, bottom;
}
You can use DwmGetWindowAttribute, the combined width for those 3 buttons should be 185 pixels on Windows 10, at 125% DPI. Note that if your application is not DPI aware, then the result will still be the same, 185 for example.
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("dwmapi.dll")]
public static extern int DwmGetWindowAttribute(
IntPtr hwnd, int attr, out RECT ptr, int size);
public void foo()
{
int DWMWA_CAPTION_BUTTON_BOUNDS = 5;
RECT rc;
if (0 != DwmGetWindowAttribute(this.Handle, DWMWA_CAPTION_BUTTON_BOUNDS,
out rc, Marshal.SizeOf(typeof(RECT))))
{
//error
}
int width = rc.right - rc.left;
}

DwmGetWindowAttribute returns 0 with PInvoke

I'm trying to do screen captures by capturing a specific window and in order to accurately figure out the size of the window to capture I want to use DwmGetWindowAttribute(). When I call this function with PInvoke on Windows 10 the Rect structure is always empty even though the result value is 0 (success). The Window handle passed in is valid as well because there is fallback code that calls GetWindowRect() which works (albeit with border problems).
I'm a bit at a loss. I used this same code a while back (perhaps on Windows 8.1?) and the same code seemed to be working but now no matter what I do the call to the function always returns an empty structure.
Here's the relevant code.
Definitions:
[DllImport("dwmapi.dll")]
static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out Rect pvAttribute, int cbAttribute);
[Flags]
public enum DwmWindowAttribute : uint
{
DWMWA_NCRENDERING_ENABLED = 1,
DWMWA_NCRENDERING_POLICY,
DWMWA_TRANSITIONS_FORCEDISABLED,
DWMWA_ALLOW_NCPAINT,
DWMWA_CAPTION_BUTTON_BOUNDS,
DWMWA_NONCLIENT_RTL_LAYOUT,
DWMWA_FORCE_ICONIC_REPRESENTATION,
DWMWA_FLIP3D_POLICY,
DWMWA_EXTENDED_FRAME_BOUNDS,
DWMWA_HAS_ICONIC_BITMAP,
DWMWA_DISALLOW_PEEK,
DWMWA_EXCLUDED_FROM_PEEK,
DWMWA_CLOAK,
DWMWA_CLOAKED,
DWMWA_FREEZE_REPRESENTATION,
DWMWA_LAST
}
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
public Rectangle ToRectangle()
{
return Rectangle.FromLTRB(Left, Top, Right, Bottom);
}
}
Code to do the capture:
public static Rectangle GetWindowRectangle(IntPtr handle)
{
Rectangle rected = Rectangle.Empty;
Rect rect = new Rect();
if (Environment.OSVersion.Version.Major < 6)
{
GetWindowRect(handle, out rect);
rected = rect.ToRectangle();
}
else
{
int size = Marshal.SizeOf(typeof(Rect));
int res = DwmGetWindowAttribute(handle, (int)DwmWindowAttribute.DWMWA_EXTENDED_FRAME_BOUNDS, out rect, size);
Debug.WriteLine(res.ToString("x") + " " + size + " " + handle + " " + (int) DwmWindowAttribute.DWMWA_EXTENDED_FRAME_BOUNDS);
// allow returning of desktop and aero windows
if (rected.Width == 0)
{
GetWindowRect(handle, out rect);
rected = rect.ToRectangle();
Debug.WriteLine("Using GetWindowRect");
}
}
Debug.WriteLine(rected.ToString());
return rected;
}
It feels like something simple is missing here. Any ideas?
Based on the Rick Strahl original code as well as Hans Passant correction I created a more compact version of GetWindowsRectangle. I tested it on Windows 10, here's the code in case it helps someone in the future:
public static Rectangle GetWindowRectangle(IntPtr handle)
{
Rect rect = new Rect();
if (Environment.OSVersion.Version.Major >= 6)
{
int size = Marshal.SizeOf(typeof(Rect));
DwmGetWindowAttribute(handle, (int)DwmWindowAttribute.DWMWA_EXTENDED_FRAME_BOUNDS, out rect, size);
}
else if (Environment.OSVersion.Version.Major < 6 || rect.ToRectangle().Width == 0)
{
GetWindowRect(handle, out rect);
}
return rect.ToRectangle();
}
Use GetWindowRect instead of DwmGetWindowAttribute to receive RECT of the window.
[DllImport("user32.dll", SetLastError = true)]
public static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);

SendInput not working perfectly on extended screen

Mouse stimulation using SendInput works perfectly on MainDisplay. However when I use SendInput for extended screen (e.g. Second screen placed to the left of the main display in my case. Issues is replicable irrespective of the extended display any place around main display but with different resolution then main display):
If I use SendInput on extended screen, the mouse position has offset in both X and Y position, ever so slightly ranging from 40 to 80 points in x and 10 to 20 points in Y based on if X (width) and Y(height) of extended screen is different to main display width/height)
Thanks in advance for any support as to why difference on extended screen
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(ref Win32Point pt);
[DllImport("user32.dll")]
internal static extern bool SetCursorPos(int X, int Y);
[StructLayout(LayoutKind.Sequential)]
internal struct Win32Point
{
public Int32 X;
public Int32 Y;
};
internal enum SendInputEventType : int
{
InputMouse,
InputKeyboard
}
[DllImport("user32.dll", SetLastError = true)]
private static extern uint SendInput(uint nInputs, ref Input pInputs, int cbSize);
public struct Input
{
public uint InputType;
public MouseInput MI;
}
public struct MouseInput
{
public int Dx;
public int Dy;
public uint MouseData;
public uint DwFlags;
public uint Time;
public IntPtr DwExtraInfo;
}
public enum MouseEventInfo
{
mouseEventfMove = 0x0001,
mouseEventfLeftdown = 0x0002,
mouseEventfLeftup = 0x0004,
mouseEventfRightdown = 0x0008,
mouseEventfRightup = 0x0010,
mouseEventfWheel = 0x0800,
mouseEventfAbsolute = 0x8000,
wheelDelta = 0x0078
}
static int CalculateAbsoluteCoordinateX(int x, System.Drawing.Rectangle currentBounds)
{
return ((currentBounds.X + x) * 65536) / (currentBounds.Width);
}
static int CalculateAbsoluteCoordinateY(int y, System.Drawing.Rectangle currentBounds)
{
return (((currentBounds.Y + y) * 65536) / currentBounds.Height);
}
// for me screen at index 0 (screen no 1) is main display. Screen id 2
//placed to the left of the main display as per resolution screen i.e.at
//index 1 (Screen.AllScreens[1]) is extended display and Bound.X is a -ve value
public static int ScreenId = 2;
public static System.Drawing.Rectangle CurrentBounds
{
get
{
return SysForms.Screen.AllScreens[ScreenId - 1].Bounds;
}
}
public static void ClickLeftMouseButton(int x, int y)
{
Input mouseInput = new Input();
mouseInput.InputType = SendInputEventType.InputMouse;
mouseInput.MI.Dx = CalculateAbsoluteCoordinateX(x, CurrentBounds);
mouseInput.MI.Dy = CalculateAbsoluteCoordinateY(y, CurrentBounds);
mouseInput.MI.MouseData = 0;
mouseInput.MI.DwFlags = MouseEventInfo.mouseEventfMove | MouseEventInfo.mouseEventfAbsolute;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
mouseInput.MI.DwFlags = MouseEventInfo.mouseEventfLeftdown;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
mouseInput.MI.DwFlags = MouseEventFlags.mouseEventfLeftup;
SendInput(1, ref mouseInput, Marshal.SizeOf(new INPUT()));
}
//Below is code of the WPF MainWindow for testing. Two buttons with click event.
// For main display with screenid as 1 both setcursor position and sendinput
//work perfectly, as I get the MousePosition, but when I apply this to
//extended screen (currently with two screen, main display is screen 1 in my
//case and screen 2 is extended screen, they put the mouse at two different positions.
//I have my doubts the way I am using the extended screen Bounds.X, but
//haven't will able to fix the issue
int x = 600;
int y = 300;
private void btnSend_Click(object sender, RoutedEventArgs e)
{
SetCursorPos(SysForms.Screen.AllScreens[ScreenId - 1].Bounds.X + x, SysForms.Screen.AllScreens[screenId - 1].Bounds.Y + y);
}
private void btnSend1_Click(object sender, RoutedEventArgs e)
{
ClickLeftMouseButton(x, y);
}
Found the issue. While using SendInput, the conversion of x,y in absolute value must be done in relation to Main/Primary screen.
Thus the changes:
static int CalculateAbsoluteCoordinateX(int x, System.Drawing.Rectangle currentBounds)
{
return ((currentBounds.X + x) * 65536) / (SystemParameters.PrimaryScreenWidth);
}
static int CalculateAbsoluteCoordinateY(int y, System.Drawing.Rectangle currentBounds)
{
return (((currentBounds.Y + y) * 65536) / SystemParameters.PrimaryScreenHeight);
}

InputBox Position

How to open the Interaction.InputBox to center of the form? I know there is a code for the position of the InputBox
Interaction.InputBox("Question?", "Title", "Default Text", x,y);
I will be using this InputBox in different form of different sizes. Is there a way to open the InputBox in the center of the form? Or I have to position them individually on each form?
Is it possible also to reposition the OKbutton and Cancelbutton of InputBox?
Here is something simple to calculate the center of a form, the extra offset is for the size of the input box.
{
int x = this.Left + (this.Width / 2) - 200;
int y = this.Top + (this.Height / 2) - 100;
}
Pass these into the input box for x and y
If you want full customisation then creating your own form is the best way to go as indicated in Fabio's comment.
However, if you just want to approximately centre the box and you will be doing it many times then you could write your own extension method to show and position the input box for you:
public static class FormExtensions
{
public static string CentredInputBox(this Form form, string prompt, string title = "", string defaultResponse = "")
{
const int approxInputBoxWidth = 370;
const int approxInputBoxHeight = 158;
int left = form.Left + (form.Width / 2) - (approxInputBoxWidth / 2);
left = left < 0 ? 0 : left;
int top = form.Top + (form.Height / 2) - (approxInputBoxHeight / 2);
top = top < 0 ? 0 : top;
return Microsoft.VisualBasic.Interaction.InputBox(prompt, title, defaultResponse, left, top);
}
}
Usage from within a form:
this.CentredInputBox("MyPrompt", "MyTitle", "MyDefaultResponse");
It's not perfect because if the box is bigger than normal for some reason then it won't quite be in the centre, and I think its size is variable depending on how much text is in it. However, it shouldn't be far off in normal usage.
To center your InputBox, you can try using Win32 functions to deal with it. This code works for you:
[DllImport("user32")]
private static extern int SetWindowPos(IntPtr hwnd, IntPtr afterHwnd, int x, int y, int cx, int cy, int flag);
[DllImport("user32")]
private static extern IntPtr FindWindow(string className, string caption);
[DllImport("user32")]
private static extern int GetWindowRect(IntPtr hwnd, out RECT rect);
//RECT structure
public struct RECT {
public int left, top, right, bottom;
}
public void ShowCenteredInputBox(string prompt, string title, string defaultReponse){
BeginInvoke((Action)(() => {
while (true) {
IntPtr hwnd = FindWindow(null, title + "\n\n\n");//this is just a trick to identify your InputBox from other window with the same caption
if (hwnd != IntPtr.Zero) {
RECT rect;
GetWindowRect(hwnd, out rect);
int w = rect.right - rect.left;
int h = rect.bottom - rect.top;
int x = Left + (Width - w) / 2;
int y = Top + (Height - h) / 2;
SetWindowPos(hwnd, IntPtr.Zero, x, y, w, h, 0x40);//SWP_SHOWWINDOW = 0x40
break;
}
};
}));
Microsoft.VisualBasic.Interaction.InputBox(prompt, title + "\n\n\n", defaultResponse,0,0);
}
Of course you can also change the position of the buttons, label and TextBox on your InputBox but it's very nasty and tricky, we can say that it's not simple. The recommended solution for you is to create new standard form in System.Windows.Forms.Form, add controls to it and use the method ShowDialog() to show your form.. Of course it requires more code to do but it allows you to fully customize the look and feel and its behaviors.
You can set the InputBox's starting position. There's a property for that
InputBox ib = new InputBox();
ib.StartPosition = FormStartPosition.CenterParent;
Where as FormStartPosition is an enum, from which you can select your desired position!
You can sipmly use -1 for x and y:
Interaction.InputBox("Question?", "Title", "Default Text", -1,-1);

Why do I get a height and Width of 0?

Why do I get a heigh and Width of 0 with the below:
static void Main(string[] args)
{
Process notePad = new Process();
notePad.StartInfo.FileName = "notepad.exe";
notePad.Start();
IntPtr handle = notePad.Handle;
RECT windowRect = new RECT();
GetWindowRect(handle, ref windowRect);
int width = windowRect.Right - windowRect.Left;
int height = windowRect.Bottom - windowRect.Top;
Console.WriteLine("Height: " + height + ", Width: " + width);
Console.ReadLine();
}
Here is my definition of GetWindowRect:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);
This is my definition for RECT:
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
Thanks all for any help.
You are passing a process handle to a function, GetWindowRect, that expects a window handle. Naturally, this fails. You should send Notepad.MainWindowHandle instead.
You may be querying the size before notepad has fully started up. Try this:
notePad.Start();
notePad.WaitForInputIdle(); // Waits for notepad to finish startup
IntPtr handle = notePad.Handle;
I like using pinvoke.net to sanity check all my PInvokes. GetWindowRect is described well at: http://pinvoke.net/default.aspx/user32/GetWindowRect.html

Categories

Resources