form size to different resolutions - winforms

hoping you could help me out with the sizing if my forms. on my school laptop the form fits nicely to the resolution. however, on my personal laptop, the form does not fit the larger res and for some reason is more zoomed. I have sent pictures too

You get you screen resolution with following code:
Screen.PrimaryScreen.Bounds.Width;
Screen.PrimaryScreen.Bounds.Height;
you could adjust the position from every control in the form_load event with Control.Location or the size with Control.Size depending on the width and height of your screen.
For example
label1.Location.X = Screen.PrimaryScreen.Bounds.Width - 100;
label1.Location.Y = Screen.PrimaryScreen.Bounds.Height - 50;
If you want your control to always be fullscreen, you could also add this.WindowState = FormWindowState.Maximized; in the form_load event. then its easier to adjust the positions of the controls depending on screen size.

Related

wpf notifyicon context menu not centered on tray icon at 100% dpi

I'm using the wpf notifyicon (http://www.hardcodet.net/wpf-notifyicon)
When my laptop is at 100% dpi scaling, the left side of the context menu is centred on the tray icon, as expected.
When the laptop isn't at 100%, the context menu is pushed to the far right.
On high resolution laptop displays, 100% scaling is not the default.
Wherever my tray icon is positioned, that is, however far from the clock, the menu always pops up over the clock, as far to the bottom-right of the screen as is possible while remaining visible.
Note: I'm testing on a default installation of Windows 8.1. Also, the NotifyIcon that I'm using is the one that is generally recommended for anyone attempting tray functionality in WPF.
To reproduce: the problem exists in the windowless sample provided by hardcodet. I'm using wpf NotifyIcon without a window, and can reproduce easily in code or xaml. In fact, I cannot stop reproducing it. It occurs when dpi scaling is turned on, i.e. when a 1080p display is actually showing a lesser resolution, which is what windows does to stop applications having text too tiny to read.
Any ideas about how I can make the context menu appear in the expected place regardless of dpi?
Screen shots as suggested by kennyzx:
good behaviour. the m on red background (MEGAsync) has just been right-clicked
bad behaviour. the green tick, my notifyicon, has just been right-clicked and the menu appears over the clock
!good behaviour. the m on red background (MEGAsync) has just been right-clicked
!bad behaviour. the green tick, my notifyicon, has just been right-clicked and the menu appears over the clock
and some code:
var n = new TaskbarIcon();
n.Icon=new System.Drawing.Icon(#"C:\window - 64 - tick.ico");
n.ContextMenu = new System.Windows.Controls.ContextMenu();
n.ContextMenu.Items.Add(new System.Windows.Controls.MenuItem {Header="E_xit" });
Found the solution here: http://www.codeproject.com/Messages/4929452/Problem-with-context-menu-position.aspx
It is, with thanks to codeproject user Igorious:
Get the code for Wpf Notifyicon (http://www.hardcodet.net/wpf-notifyicon)).
In Hardcodet.Wpf.TaskbarNotification.TaskbarIcon.ShowContextMenu()
replace
ContextMenu.HorizontalOffset = cursorPosition.X;
ContextMenu.VerticalOffset = cursorPosition.Y;
with
var g = Graphics.FromHwnd(IntPtr.Zero);
var scaleX = g.DpiX / 96.0;
var scaleY = g.DpiY / 96.0;
ContextMenu.HorizontalOffset = cursorPosition.X / scaleX;
ContextMenu.VerticalOffset = cursorPosition.Y / scaleY;
Explanation (thanks to codeproject user yachting):
It's needed because WinApi.GetPhysicalCursorPos return the mouse position in pixel,
but WPF's measurement unit is device independent pixel (by definition, it's 1/96 inch)
You need to adjust the return value of GetPhysicalCursorPos by DPI (dots per inch) setting,
otherwise the position of the context menu will be incorrect if users set DPI other than the default 96.

How can I prevent my form from vertically stretching when "live"?

My form looks gorgeous (YMMV) at design-time:
...but gets "stretchy" vertically when running on the device and, in fact, is a little too tall for the screen:
Why would this happen, and how can I prevent it from happening?
Possibly noteworthy: Form's WindowState == Normal, FormBorderStyle = FixedDialog
please check your form settings: AutoScaleMode and Size settings.
Is this your first Windows CE application? You have to keep in mind that there are devices with different resolutions (ie QVGA, square like 320x320 pixels, etc), so you should adjust your layout to the screen size. Or make your form maximized and set AutoScroll to enbaled (if the content does not fit).
When you design your form, you are using pixel counts. These are transformed to twips (1/1440dpi) and again transformed (on the device) back to pixels (including a correction for the resolution, the dots-per-inch (dpi)). So a form with 240x240 pixels will have adifferent size on a 96dpi and a 102dpi display. This scaling is controlled by AutoScaleMode.

Silverlight child window getting cropped from edges when resizing browser window

I am using ordinary Silverlight ChildWindow in my application. When resizing the browser window, in particular when making its width smaller than that of the child window, the edges of the child window get cropped, so that it is impossible to close it via the close button as it is no more visible. I have tried a number of workarounds but nothing helped. In particular I have subscribed to the SizeChanged event of the child window and set its sizes relative to the layout root's sizes. Here is the code of the SizeChanged event handler:
// Get the dimensions of the application screen
Size appSize = Application.Current.RootVisual.RenderSize;
// Make the child window occupy the 90% of width and 40% of height of the entire screen
this.Width = appSize.Width * 9 / 10;
this.Height = appSize.Height * 4 / 10;
This code modifies the sizes of the child window, but it also updates the sizes of the overlay, so that it no longer covers the whole page, which is a very strange behavior.
Has anyone encountered this kind of problems? Please share any ideas.
Thanks in advance.
I had a similar problem. It occured because scale in a browser was higher than 100%.

What's the simplest way to calculate a controls' inner display rectangle (with Padding subtracted)?

A control reports its display rectangle in .DisplayRectangle -- what is the simplest way of finding the area available for the control if .Padding is set?
For example, a Label has .DisplayRectangle always equal to .ClientRectangle, even if .Padding is not (0,0,0,0). I want to obtain the size of the rectangle in .ClientRectangle after paying attention to the .Padding setting (and whatever additional properties might limit the "net" display area, for that matter).
Isn't there some method, or other framework-supported way to do it? Do I have to re-code all those rectangle calculations that probably are already in the framework code?
Padding isn't simply a straight modification to Display Rectangle. Some built-in controls factor it in, some do not.
Best is to read the documentation for Padding and see which parts apply to you.
The hopefully on-target simple answer is "No, there's no framework method. Build your own like below"
private Rectangle GetPaddedRectangle(Control control)
{
var rect = control.ClientRectangle;
var pad = control.Padding;
return new Rectangle( rect.X + pad.Left,
rect.Y + pad.Top,
rect.Width - (pad.Left+pad.Right),
rect.Height - (pad.Top+pad.Bottom));
}

Form height problem when FormBorderStyle is NONE

I have a borderless form (FormBorderStyle = None) with the height of 23 pixels (set in the designer)
When .NET draws my form at runtime - it draws it 38 pixels high (it adds the height of a title-bar for some reason).
MessageBox.Show(this.Height.ToString()); //this shows 38!! why?
To work it around I have to set "Height = 23;" in the Form_Load event.
private void MyForm_Load(object sender, EventArgs e)
{
this.Height = 23; //workaround. wtf??
}
You can try this yourself in Visual Studio 2010 (Winforms App, target Framework - 2.0).
Wtf?
Yeah, it is a bug, of sorts. Note how in the designer you set the size of the form with the Width and Height properties. Those properties include the size of the borders and the title bar. That's a problem however, your form may run on a machine where the user has increased, say, the title bar font size. That then would reduce the size of the window's client area. Or in other words, the form's ClientSize property would change on that machine. Leaving less room for the controls and messing up the design of your form pretty badly.
There's code inside the Form class that runs after the Handle is created, right before the Load event runs. It recalculates the Size of the form, using the same ClientSize you had on your machine. Now everything is good, the Height of the form won't match the one you set in the designer but the form otherwise looks the same and the layout of the controls is identical.
That same code also ensures that the window doesn't get too small. And that's where it falls over, it doesn't pay enough attention to the FormBorderStyle property. Clipping the height to the title bar size plus the client area height, as you found out. It also prevents the form getting too narrow, trying to make sure that the icon and min/max/close buttons are always visible. Even if you don't have any.
The workaround is to change the ClientSize after this code runs, the OnLoad override or Load event handler is the right place for that. Beware that if you hard-code the form size like this then you should also set the AutoScaleMode property to None. Make sure that this doesn't cause trouble on a machine that has a different DPI setting.

Resources