How to compute maximum width/height of the element? - c#

Let's say you have WPF window with one element that changes it size as the window resizes, say DockPanel with two elements. You maximize the window, and the second element gets maximized as well. So not I could read its height/width and those would be maximum values (this is good enough for my purpose, so I don't have to worry if the taskbar takes some space or similar issues).
OK, but let's get back to the freshly started window -- I would like to compute the max width/height of the given element without actually resizing the window, just pure computation. Is it doable? How?

If I understand you correctly you want to know the maximum size the window could be. That would be the size of the screen then. Screensize is an available value:
System.Windows.SystemParameters.PrimaryScreenWidth
System.Windows.SystemParameters.PrimaryScreenHeight
Perhaps you could do something like this:
Get size of window
Get size of element
calculate offset (windowSize - elementSize)
Get Screen size
calculate maximum size (screenSize - offset)

You can create a test window which will appear as maximised, get its width and height and then close it.
This can be done every time you need to calculate values or at the start of the application, assuming you will check whether screen resolution has not changed.
private void GetMaximumWidthAndHeight(FrameworkElement element)
{
var testWindow = new Window();
testWindow.Visibility = Visibility.Collapsed;
testWindow.ShowInTaskbar = false;
testWindow.WindowState = WindowState.Maximized;
testWindow.Show();
var maximizedWindowHeight = testWindow.ActualHeight;
var maximizedWindowWidth = testWindow.ActualWidth;
testWindow.Close();
var maxToCurrentHeightRatio = maximizedWindowHeight / this.ActualHeight;
var maxToCurrentWidthRatio = maximizedWindowWidth / this.ActualWidth;
var maximumElementHeight = element.ActualHeight * maxToCurrentHeightRatio;
var maximumElementWidth = element.ActualWidth * maxToCurrentWidthRatio;
// Do something with the values
}

Related

How to set minimum window size in Compact overlay mode of UWP app

There is a button in my UWP app which enters app into picture-in-picture mode. The window size in PIP mopde can be changed between 150 x 150 and 500 x 500 and also I can set the window size while entering into compact overlay mode. But I want to limit the minimum window size of app in PIP mode. How can I restrict the app resizing to default 150x150 min size.
var applicationView = ApplicationView.GetForCurrentView();
var viewModePreference = ViewModePreferences.CreateDefault(ApplicationViewMode.CompactOverlay);
viewModePreference.CustomSize = new Size(width, height);
applicationView.SetPreferredMinSize(new Size(360, 400));
await applicationView.TryEnterViewModeAsync(ApplicationViewMode.CompactOverlay, viewModePreference);
SetPreferredMinSize() is not solving the purpose. Please help me with the solution. Thankyou
Please check SetPreferredMinSize documentation,
The smallest allowed minimum size is 192 x 48 effective pixels. The largest allowed minimum size is 500 x 500 effective pixels. If you set a value outside of these bounds, it is coerced to be within the allowed bounds. (To learn about effective pixels, see Responsive design 101 for .)
So, please edit your largest width 150 to 192.
private void Button_Click(object sender, RoutedEventArgs e)
{
var size = new Size(192,150);
ApplicationView.GetForCurrentView().TryResizeView(size);
}
Update
if you want to limit the users to resize the window in overlay mode. please listen SizeChanged event and resize pip view when user change the window size.
private void CoreWindow_SizeChanged(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.WindowSizeChangedEventArgs args)
{
var applicationView = ApplicationView.GetForCurrentView();
if(applicationView.ViewMode == ApplicationViewMode.CompactOverlay)
{
var size = new Size(200, 200);
ApplicationView.GetForCurrentView().TryResizeView(size);
}
}

Show secondary window center of main window in UWP

I started using multiple windows in UWP and need to display secondary windows center of screen or at least center of parent window.
I found nothing relevant about how to specify where to show additional windows on the screen, other than Window.Current.Bounds property.
Here is the simplified version of the method that I am using to create additional windows. The method signature is: CreateFrameWindow(Size size, Type pageType, object parameter)
CoreApplicationView newWindow = CoreApplication.CreateNewView();
ApplicationView newView = null;
bool result = await newWindow.Dispatcher.TryRunAsync(CoreDispatcherPriority.Normal, () =>
{
Frame frame = new Frame();
frame.Navigate(pageType, parameter);
Window.Current.Content = frame;
Window.Current.Activate();
newView = ApplicationView.GetForCurrentView();
});
result = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newView.Id);
newView.TryResizeView(size);
The TryResizeView works fine as long as the secondary window has enough space to resize based on its current location on the screen. I want to enable resize up to the maximum available size (size of window when it is maximized) and place it to the center of the screen. If this is not possible, placing to the center of the parent or main window is acceptable.
Show secondary window center of main window in UWP
CoreApplicationView does not provide api to set the view position manually. For your requirement please try to use AppWindow to archive this feature. And AppWindow has RequestMoveRelativeToDisplayRegion method that position the window in the specified display region at the specified offset. For more please refer official code sample scenario 5
Update
If you want to make your new window display in the center, you need know your windows size before, and calculate X Y value for RequestMoveRelativeToDisplayRegion method.
X = (1920-W)/2 //1920 is Horizontal Resolution W is the new window's width
Y = (1080-H)/2 //1080 is Vertical Resolution H is the new window's height
For get current display resolution please refer this case link
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
var scaleFactor = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
var size = new Size(bounds.Width*scaleFactor, bounds.Height*scaleFactor);
For AppWindow I'm using...
//Set custom window size
Windows.UI.WindowManagement.Preview.WindowManagementPreview.SetPreferredMinSize(appWindow, new Size(500, 500));
appWindow.RequestSize(new Size(500, 500));
DisplayRegion displayRegion = ApplicationView.GetForCurrentView().GetDisplayRegions()[0];
double displayRegionWidth = displayRegion.WorkAreaSize.Width;
double displayRegionHeight = displayRegion.WorkAreaSize.Height;
int horizontalOffset = (int)(displayRegionWidth - 520); //New window is 500 width + 20 to accomodate for padding
int verticalOffset = (int)(displayRegionHeight - 500); //New window is 500 height
appWindow.RequestMoveRelativeToDisplayRegion(displayRegion, new Point(horizontalOffset / 2 , verticalOffset / 2));

How to ensure UWP app is always full screen on launch?

Is there a way (either C# or XAML) I can maximize a UWP app window even after I resized and closed it previously on desktop?
I have tried with ApplicationViewWindowingMode.FullScreen but this makes the app go entire full screen and covers the Windows Taskbar too.
You can use another value PreferredLaunchViewSize from ApplicationViewWindowingMode and then set ApplicationView.PreferredLaunchViewSize but the key is to find out what the size is going to be.
Theoretically, you could use a really big number and window would just extend to the max it could be. However, it's probably safer to just calculate the screen dimensions in effective pixels.
So if you just call the following method before InitializeComponent(); on your main Page, it should maximize the window on startup.
private static void MaximizeWindowOnLoad()
{
// Get how big the window can be in epx.
var bounds = ApplicationView.GetForCurrentView().VisibleBounds;
ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
}
Note the app somehow remembers these settings even after you uninstalled it. If you ever want to change back to the default behavior (app starts up with the previous window size), simply call ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto; once and remove all the code.
Update
Looks like in the latest Windows 10 build, ApplicationView.GetForCurrentView().VisibleBounds no longer returns the full window size in effective pixels anymore. So we now need a new way to calculate it.
Turns out it's quite straightforward since the DisplayInformation class also gives us the screen resolution as well as the scale factor.
The following is the updated code -
public MainPage()
{
MaximizeWindowOnLoad();
InitializeComponent();
void MaximizeWindowOnLoad()
{
var view = DisplayInformation.GetForCurrentView();
// Get the screen resolution (APIs available from 14393 onward).
var resolution = new Size(view.ScreenWidthInRawPixels, view.ScreenHeightInRawPixels);
// Calculate the screen size in effective pixels.
// Note the height of the Windows Taskbar is ignored here since the app will only be given the maxium available size.
var scale = view.ResolutionScale == ResolutionScale.Invalid ? 1 : view.RawPixelsPerViewPixel;
var bounds = new Size(resolution.Width / scale, resolution.Height / scale);
ApplicationView.PreferredLaunchViewSize = new Size(bounds.Width, bounds.Height);
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
}
}
If you want to MAXIMISE your app on launch you can use the following:
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Maximized;
But be sure to put it into the Loaded Event for your Page or it will not work!
I've too few points to comment directly. None of the above resized to a maximized view for me (or the below single-line ApplicationViewWindowingMode.Maximized method), but I have used some of the answers to come up with something that worked for me. It is still very clunky however. The screen size given in 'DisplayInformation' is too big to allow the page to be resized directly to it. Trying to do it didn't work and I had to take 60 off height and width to get it to return 'true', therefore I have the following bit of nonsense which worked, maybe it will help someone else find a better answer. It goes in the page/window loaded event. Nothing else needs to be added elsewhere.
private void Page_Loaded(object sender, RoutedEventArgs e)
{
var view = ApplicationView.GetForCurrentView();
var displayInfo = DisplayInformation.GetForCurrentView();
double x = ActualWidth;
double y = ActualHeight;
bool answer = true;
// Get the screen resolution (APIs available from 14393 onward).
var resolution = new Size(displayInfo.ScreenWidthInRawPixels-60, displayInfo.ScreenHeightInRawPixels-60);
answer = view.TryResizeView(resolution); //This should return true if the resize is successful
if (answer)
{
x = displayInfo.ScreenWidthInRawPixels - 60;
y = displayInfo.ScreenHeightInRawPixels - 60;
}
answer = true;
while (answer == true)
{
x++;
answer = view.TryResizeView(new Size { Width = x, Height = y });
}
x = x - 1;
answer = true;
while (answer == true)
{
y++;
answer = view.TryResizeView(new Size { Width = x, Height = y });
}
Adding the following line to the OnLaunched event under App.xaml.cs did it for me.
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.FullScreen;
NOTE: Make sure to add it before the following line
Window.Current.Activate();
If you like to go fullscreen at the runtime use the following line.
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
I have this one liner that works as I expected Justins code to, but for some reason, when using Justins answer, my window would not be maximized... But then I changed something that did make it maximized but I lost all my fluent design such as Acrylic and RevealHighlite...
So I came up with this one liner which keeps all of my fluent design principles happy:
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
Something to note:
I did try Justins answer, and I am using his method of MaximizeWindowOnLoad() which I have called straight after the initializeComponent();
Full overview:
public class()
{
this.InitializeComponent();
MaximizeWindowOnLoad();
}
private static void MaximizeWindowOnLoad()
{
ApplicationView.GetForCurrentView().TryEnterFullScreenMode();
}

How do I force my window to re-size?

I have an auto-sized application user control in WPF (Height="Auto" Width="Auto"). When a bunch of elements inside it are resized, the windows stays the same rendered size. How do I force it to resize? I have tried this line Application.Current.MainWindow.SizeToContent = SizeToContent.WidthAndHeight; in the function that resizes the components inside the window. The compiler gives me an error that there is no object associated with this call. I have tried this line MainWindowBorder.Height = Double.NaN; to trick it to resize. No luck either.
Use SizeToContent in XAML
SizeToContent="WidthAndHeight"
https://msdn.microsoft.com/en-us/library/system.windows.window.sizetocontent%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
If all the inside controls can be resized but the only one thing to resize is Window, How about this simple approach? Hope this helps..
int mycurrentscreenwidth = 1366;
int mycurrentwindowwidth = 1366;
var screen = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
var screenwidth = screen.Width;
if (Convert.ToInt32(screenwidth) < 1366)
{
double calculate_scalefactor= Convert.ToInt32(screenwidth) / (double)mycurrentscreenwidth;
double newwidth_tobescaled = mycurrentwindowwidth * calculate_scalefactor;
this.Width = newwidth_tobescaled;
}
To obtain the window of your current user control, see:
Access parent window from User Control
Then use:
Window yourParentWindow = Window.GetWindow(userControl);
yourParentWindow.SizeToContent = SizeToContent.WidthAndHeight;

What is the best way to show a WPF window at the mouse location (to the top left of the mouse)?

I have found that this works PART of the time by inheriting the Windows Forms mouse point and subtracting out the height and width of my window to set the left and top (since my window's size is fixed):
MyWindowObjectThatInheritsWindow window = new MyWindowObjectThatInheritsWindow();
System.Windows.Point mouseLocation = GetMousePositionWindowsForms();
window.Left = mouseLocation.X - 300;
window.Top = mouseLocation.Y - 240;
window.Show();
Edit: Here is the code for getting the mouse position...
public System.Windows.Point GetMousePositionWindowsForms()
{
System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
return new System.Windows.Point(point.X, point.Y);
}
Note that this works by making the bottom right edge of the window touch the top left of your mouse cursor. But this breaks for different screen resolutions, or maybe multiple monitors with different resolutiosn? I haven't fully narrowed it down yet, but I just tried this same code on another PC, and it seems to spawn the window not to the top left of the mouse cursor, but to the bottom left of it, and a good distance past it...
I should probably add that my window sizes to content, width and height, so I can't just use the ActualWidth and ActualHeight properties since they're not available. Perhaps the issue is in getting that sizing right? Is there any way to do that? I know for sure the 300 and 240 is correct according to my main PC with two monitors running 1920x1080 resolutions, as I have calculated the widths and heights of all the objects in my window which I have explicitly sized. Edit: Just tried explicitly setting the height and width to 240/300, to ensure that the window is no longer sized to content, and I still have this issue when subtracting out the actual height and width!
Any ideas?
In the end, this did the trick:
protected override void OnContentRendered(EventArgs e)
{
base.OnContentRendered(e);
MoveBottomRightEdgeOfWindowToMousePosition();
}
private void MoveBottomRightEdgeOfWindowToMousePosition()
{
var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
var mouse = transform.Transform(GetMousePosition());
Left = mouse.X - ActualWidth;
Top = mouse.Y - ActualHeight;
}
public System.Windows.Point GetMousePosition()
{
System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
return new System.Windows.Point(point.X, point.Y);
}
Can you not use something like this?:
Point mousePositionInApp = Mouse.GetPosition(Application.Current.MainWindow);
Point mousePositionInScreenCoordinates =
Application.Current.MainWindow.PointToScreen(mousePositionInApp);
I haven't been able to test it, but I think it should work.
UPDATE >>>
You don't have to use the Application.Current.MainWindow as the parameter in these methods... it should still work if you have access to a Button or another UIElement in a handler:
Point mousePositionInApp = Mouse.GetPosition(openButton);
Point mousePositionInScreenCoordinates = openButton.PointToScreen(mousePositionInApp);
Again, I haven't been able to test this, but if that fails as well, then you can find one more method in the How do I get the current mouse screen coordinates in WPF? post.
You can also do this by slightly modifying your initial example and positioning the window before showing it.
MyWindowObjectThatInheritsWindow window = new MyWindowObjectThatInheritsWindow();
var helper = new WindowInteropHelper(window);
var hwndSource = HwndSource.FromHwnd(helper.EnsureHandle());
var transformFromDevice = hwndSource.CompositionTarget.TransformFromDevice;
System.Windows.Point wpfMouseLocation = transformFromDevice.Transform(GetMousePositionWindowsForms());
window.Left = wpfMouseLocation.X - 300;
window.Top = wpfMouseLocation.Y - 240;
window.Show();

Categories

Resources