Detect Change of Resolution C# Winforms

Detect change of resolution c# WinForms

Handle the following event:

Microsoft.Win32.SystemEvents.DisplaySettingsChanged

You may refer to this page for more details.

You may also wanna see the msdn article on SystemEvents class.

Why can not I adjust the form to the size of the monitor? (Windows Forms)

You should set the Anchor and Dock properties on the controls in the forms.

The Anchor property controls which edges of control are "bound" or "tied" to the corresponding edges of its form.
For example, if you set Anchor to Bottom, the distance between the control's bottom edge and the bottom of its parent will not change, so the control will move down as you resize the form.
If you set Anchor to Top | Bottom, the control will resize vertically as you resize the form.

To make a control resize with the form, set the Anchor to all four sides, or set Dock to Fill.

How to detect if SetForegroundWindow changes screen resolution?

I managed to solve the issue. Instead of trying to detect fullscreen applications, I simply send an inactivation message to the foreground application, which triggers an early resolution change:

SendMessage(GetForegroundWindow(), WM_ACTIVATEAPP, false, GetCurrentThreadId());

This exact same message is also sent during application switches, so I essentially emulate one before it actually happens. I have not encountered any side effects yet.

Mind you however, that this does not solve DWM issues. Windows 7 automatically disables DWM composition for compatibility launches, or when it detects direct access to the primary display surface. It does not allow you to re-enable, and I do not see an easy solution to this problem. Thankfully this issue will eventually go away since DWM composition is always enabled in Windows 8 and newer.

Windows Desktop Size changed event?

You're looking for the SystemEvents.DisplaySettingsChanged event.

When Screen Resolution Changes the Panel and its Child Control display inside the WinForm

Form the form Load event call the ResizeForm() function in such a way

Resolution objFormResizer = new Resolution();
objFormResizer.ResizeForm(this, 768, 1024);

public class Resolution
{
float heightRatio = new float();
float widthRatio = new float();
int standardHeight, standardWidth;
public void ResizeForm(Form objForm, int DesignerHeight, int DesignerWidth)
{
standardHeight = DesignerHeight;
standardWidth = DesignerWidth;
int presentHeight = Screen.PrimaryScreen.WorkingArea.Height;//.Bounds.Height;
int presentWidth = Screen.PrimaryScreen.Bounds.Width;
heightRatio = (float)((float)presentHeight / (float)standardHeight);
widthRatio = (float)((float)presentWidth / (float)standardWidth);
objForm.AutoScaleMode = AutoScaleMode.None;
objForm.Scale(new SizeF(widthRatio, heightRatio));
foreach (Control c in objForm.Controls)
{
if (c.HasChildren)
{
ResizeControlStore(c);
}
else
{
c.Font = new Font(c.Font.FontFamily, c.Font.Size * heightRatio, c.Font.Style, c.Font.Unit, ((byte)(0)));
}
}
objForm.Font = new Font(objForm.Font.FontFamily, objForm.Font.Size * heightRatio, objForm.Font.Style, objForm.Font.Unit, ((byte)(0)));
}

private void ResizeControlStore(Control objCtl)
{
if (objCtl.HasChildren)
{
foreach (Control cChildren in objCtl.Controls)
{
if (cChildren.HasChildren)
{
ResizeControlStore(cChildren);

}
else
{
cChildren.Font = new Font(cChildren.Font.FontFamily, cChildren.Font.Size * heightRatio, cChildren.Font.Style, cChildren.Font.Unit, ((byte)(0)));
}
}
objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * heightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
}
else
{
objCtl.Font = new Font(objCtl.Font.FontFamily, objCtl.Font.Size * heightRatio, objCtl.Font.Style, objCtl.Font.Unit, ((byte)(0)));
}
}
}

this class slove my issue.......hope so you also enjoy Happy Coding



Related Topics



Leave a reply



Submit