MahApps ActualWidth when maximized too high - c#

I'm having some problem with getting window size of MahApps window.
It's working perfectly when not maximized, but if I maximize it, it's too high;
I have code behind like this:
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
UpRect1.Width = ActualWidth;
}
And in ActualWidth property is proper value when windows IS NOT maximized, but if I maximize window i get higher value than my resolution is (I have 1366 screen width, it returns 1382).
Is it some kind of bug, or it's standard Windows crazy behaviour?
P.S.
Same stuff with height
P.S 2
I have also checked with Width property, but it's not updated on resize

That's the normal behaviour of Windows and has nothing to do with MahApps.Metro. See Raymond Chen's "Why are the dimensions of a maximized window larger than the monitor?":
The extra eight pixels that hang off the screen are the window borders. When you maximize a window, the window manager arranges things so that the client area of the window fills the width of your work area, and it plus the caption bar fills the height of the work area. After all, you want to see as much of your document as possible; there's no need to show you the window borders that aren't doing you any good. (It leaves the caption bar on the screen for obvious reasons.)

Related

Popup dimensions on screen

I thought I understood the WPF content scaling system but ran into an issue that's mysterious to me. I have a very simple popup that is supposed to be used as a loupe that opens on top of an image when the user clicks the image. The XAML is this:
<Popup Name="LoupePopup" AllowsTransparency="True" IsOpen="False" StaysOpen="True">
<Border BorderThickness="2" BorderBrush="Azure" />
</Popup>
In code behind, I then tie the popup's placement to an image that is already showing when the popup is opened. I set the loupe size to half the size of the image with this code:
LoupePopup.PlacementTarget = FullImg;
LoupePopup.Placement = PlacementMode.Relative;
LoupePopup.Width = FullImg.ActualWidth / 2;
LoupePopup.Height = FullImg.ActualHeight / 2;
Further code then moves the loupe along with MouseMove, but that does not really relate to the issue here: I'd expect the loupe to be exactly half the width/height of the image, but it is not - it is quite a bit larger (by about 18%). I verified the actual image size and the actual size of the window on which it is displayed, as well as the mouse coordinates. Those dimensions/point coordinates all made perfect sense, but why would the popup not use the same width/height scaling as the other elements here?
For clarification: The image covers 80% or so of the screen, so this is not just an issue of a few pixels. The window is a child window created in code, and the popup is defined in the XAML of the main window, but both windows are set to full screen size.
Appreciate your thoughts!
Maybe it is so because border thickness is not taking into account when calculating LoupePopup size
or default margin and padding of popup or image
I'm not sure, but I think you should have a look at the Stretch property of the image. If you have a width and a height defined on the image the ActualWidth/-Height are influenced by the Stretch property. Setting it to Fill makes the ActualWidth/-Height equal to the defined Width and Height.
After trying more options I had to realize that the child-versus main window aspect caused the issue. The image was showing in a child window; the popup was defined in the main window. After moving the popup XAML to the child window, the popup dimensions were correct.
Not sure I understand why (both windows have the full screen dimensions and should use the same scaling, but at least this solves my issue. Thanks for trying to help!

Winform Main Control Too Large

I am developing a Winform application which should occupy the entire screen, the target device having 1920x1080 resolution.
The main control window is defined as follows:
MaximumSize 1920, 1080
Size 1920, 1080
MinimumSize 1918, 1078
StartPosition CenterScreen
To fill the screen I have added the following to the _Load method:
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.Activate();
When I run the application the Control Window begins at the top left, as it should, but the rightmost 5th and the bottom 5th (approximately) of the form is off the screen. The controls that should appear in the cut-off regions are at appropriate locations (e.g. a label at Location 1712, 551, which should be within the boundaries of a 1920x1080 screen).
Can anyone explain? If I knew why this is happening, I'm sure the solution would be obvious.
I have added the following to the _Load method
Using the Load event handler is the problem. You are changing the layout metrics of the window after it was created. A window is maximized by giving it a negative location and a size that's larger than the screen by twice the border width. So that the border isn't visible.
But your code changes FormBorderStyle and now that location and size is not correct anymore. Since the window no longer has a border. So the window is too large and part of the client area is off the screen.
You must move the FormBorderStyle assignment into the constructor of the form so the window is created with the correct border. Setting the property with the designer is simplest.
It is possible your Windows installation is actually scaling the form. This can be done using the DPI settings (a cut-off of 1/5th sounds like a DPI setting of 125%), or by Windows 10 when it is self-adjusting.
The best thing to do is not rely on screen measurements to be correct. Instead, try to use as much panels that are capable of auto-sizing, like TableLayoutPanel and others. In that way you won't need to keep calculating, instead the framework will do that for you.

How can i get the Width and Height of self-adaptived form in C#

My Main Form is also self-adaptived and its "windowState" is "Maximized".
I set the width of it 1022px in design Properties.
When running the application in fullscreen, the form.width is still 1022 , but not the screen resolution.
how can i get right value?
If the Window is maximized you can use Screen.WorkingArea to get the true dimensions.

Is there a way to leave the resolution of a C# Windows Form Application fixed?

I want to fix the resolution of my main form, I can set the number of pixels using
this.Height = 480;
this.Width = 640;
But I want some way to make it constant, so that even if the frame is enlarged, the number of pixels will not change, but their size.
By setting the option AutoSizeMode to GrowAndShrink I can prevent re-sizing of the form by dragging the edges of the form, but when the maximize button is pressed, the form becomes big and adds new pixels. I want to allow the re-sizing of the form and putting it in maximized or fullscreen mode without changing the number of pixels in the form, or in other words, enlarging the size if the current pixels, instead of adding new pixels.
Is there a way too do it?
Pixels are fixed units. You probably would like WPF since its a vector based system.

Determine size of SizeToContent WPF Window before its rendered

I have a window in my WPF application that is displayed on occasion. When it is shown it is faded in with an annimation, and when closed it is faded out. Nothing fancy, just a storyboard that modifies the opacity. Actually the window is never really closed, the opacity is just faded out to 0 where it remains until its to be displayed again.
This window is an informative window and doesn't always show the same content. It is sized to content (Width and Height) and works well in that regard. The user chooses the basic area of the screen for it to be displayed (TopLeft, TopRight, Center, BottomLeft, BottomRight).
Before the window is faded in the content is updated. Because the window is sized to content it increases or descreases in size. The width and height can change.
When positioning the window, lets say, in the bottom right corner, I simply take the WorkingArea of the screen (width and height) and then minus the width/height of the window to get the Top and Left position that I need.
The logic works, but the trouble I am having is the Window's Height and Width is not returning the size it is after the content was updated, but is returning the size it was the last time it was displayed. I am assuming this is because it hasn't yet been rendered with the new content.
This causes me grief becuase if the Window is larger than it was the last time it obviously extends off the screen.
I tried positioning the window in the OnContentRendered event, but this only fires once when the Window is created, not after the content has been updated, when the opacity is set to 0.
Does anyone have any idea how I might get an accurate width and height of this window before it is faded in?
Any help would be appreciated!!
I found the answer to my own question. After updating the content a simple call to the window's UpdateLayout() method forces an update. Calls to the width and height then return accurate values.

Categories

Resources