My winform can't change the size. The wince device is 480*764. I debugged and got a new size:
But the size still unchanged. What happened?
Try putting your sizing code into the constructor not the Load event. Don't think that FormBorderStyle needs to be sizable (is this even supported in the .NET Compact Framework which I assume you are using?). Our forms have a style of FixedDialog. If you need to centre the form you can use this helper function which you call immediately after setting the size:
public static void SetFormPosition(Form frmChild)
{
// Set the form position on the Windows CE panel
if (frmChild != null)
{
// Get the size of the screen (should be 480x768)
Rectangle rctScreen = Screen.PrimaryScreen.WorkingArea;
// Now calculate the position of the form
int nPosX = ((rctScreen.Width - frmChild.Width) / 2);
int nPosY = ((rctScreen.Height - frmChild.Height) / 2);
// Set the position
frmChild.Location = new Point(nPosX, nPosY);
}
}
Related
I'm trying to display an overlay during an auto login HTTP call.
I've found this code, which seems outdated somehow, but found nothing more recent.
Anyway, the Overlay is showing but not covering the whole screen as expected.
The calling code is this:
AppDelegate.FinishedLaunching
var avc = new AutoLoginViewController();
var navController = new UINavigationController(avc);
AutoLoginViewController.ViewDidLoad
var bounds = UIScreen.MainScreen.Bounds;
// show the loading overlay on the UI thread using the correct orientation sizing
loadPop = new LoadingOverlay(bounds, NSBundle.MainBundle.GetLocalizedString("connecting"));
View.Add(loadPop);
But the result is the following:
If I set a breakpoint in the LoadingOverlay constructor, I can see that the screen bounds (iPhone 6) are fine:
{{X=0,Y=0,Width=375,Height=667}}
public class LoadingOverlay : UIView
{
public LoadingOverlay(CGRect frame, string text) : base(frame)
{
// configurable bits
BackgroundColor = UIColor.Black;
Alpha = 0.75f;
AutoresizingMask = UIViewAutoresizing.FlexibleDimensions;
...
}
}
Clearly the UIView width is incorrect.
Because we're in 2020, maybe there is another way.
Any help appreciated.
EDIT: The app breaks on iPhone 8 iOS 13.3 simulator, So I can't say if this is tied to a particular screen size (1x in my case).
Cause :
It seems that you didn't set the LaunchImage , So whether on a simulator or real device , the value of bounds is a static value .
Solution:
The easiest way is set the size of overlay as bounds of View .
var bounds = View.Bounds;
Or you could set all size of LaunchImage of different screen .
In the below version of App.xaml.cs, written using Visual Studio 2019, associated with my Windows C# UWP solution / project called Example_Application, class App's constructor successfully resizes the blue app window that appears when starting the app. My question: Assuming a resolution scale of 1, just to make things easier, how do I change 1920 and 1080 to the two numbers comprising my Windows-10 display resolution?
namespace Example_Application
{
sealed partial class App : Windows.UI.Xaml.Application
{
public App()
{
Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize = new Windows.Foundation.Size(1920, 1080);
Windows.UI.ViewManagement.ApplicationView.PreferredLaunchWindowingMode = Windows.UI.ViewManagement.ApplicationViewWindowingMode.PreferredLaunchViewSize;
}
protected override void OnLaunched(Windows.ApplicationModel.Activation.LaunchActivatedEventArgs e)
{
Windows.UI.Xaml.Controls.Frame rootFrame = new Windows.UI.Xaml.Controls.Frame();
Windows.UI.Xaml.Window.Current.Content = rootFrame;
rootFrame.Navigate(typeof(MainPage), e.Arguments);
Windows.UI.Xaml.Window.Current.Activate();
}
}
}
Things I've tried:
Changing "PreferredLaunchViewSize" to "Maximized" does not maximize my blue window on my monitor. Changing "PreferredLaunchViewSize" to "FullScreen" does make my application take up the full screen, but this is not what I want because I want to be able to see my application title bar and my Windows-10 taskbar.
I can write Windows.Foundation.Rect visibleBounds = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().VisibleBounds; only at the very end of OnLaunched, and bounds' Width and Height properties return present application width and height, not Windows-10 display resolution.
I can write uint screenWidthInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels; only at the very end of OnLaunched, and screenWidthInRawPixels is the present application width, not Windows-10 display width.
To get the screen resolution in UWP apps, you could try to use DisplayInformation.ScreenHeightInRawPixels Property and DisplayInformation.ScreenWidthInRawPixels Property.
Like the following code:
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
........
Window.Current.Activate();
}
//screen resolution
string heightsize = DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels.ToString();
string widthsize = DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels.ToString();
Size mysize = new Size(Convert.ToDouble(widthsize), Convert.ToDouble(heightsize));
ApplicationView.PreferredLaunchViewSize = mysize;
ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize;
}
My resolution is 1920*1080. In my test, it could get my screen resolution correctly as 1920*1080.
Ultimately, I choose to maximize my UWP App (written in C#) in my workspace, that area of my screen above my Windows taskbar. I would like to provide minimal instructions for creating a maximized app that you know is working.
I created a new default Blank App (Universal Windows) using C# in Visual Studio Community 2019, called "Draw Bounding Boxes". I included spaces here so that I could access "Draw Bounding Boxes" with spaces from my Start Menu.
I replaced the contents of "App.xaml.cs" with the following code block.
namespace Draw_Bounding_Boxes
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed partial class App : Windows.UI.Xaml.Application
{
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="e">Details about the launch request and process.</param>
protected override void OnLaunched(Windows.ApplicationModel.Activation.LaunchActivatedEventArgs e)
{
// Resize app.
uint screenWidthInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenWidthInRawPixels;
uint screenHeightInRawPixels = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().ScreenHeightInRawPixels;
double rawPixelsPerViewPixel = Windows.Graphics.Display.DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
double screenWidthInViewPixels = System.Convert.ToDouble(screenWidthInRawPixels) / rawPixelsPerViewPixel;
double screenHeightInViewPixels = System.Convert.ToDouble(screenHeightInRawPixels) / rawPixelsPerViewPixel;
// If offsetToScreenWidthInViewPixels is less than 15,
// on first load app will be of default size, and on second load app will be full screen.
// A loaded image will have height equal to full screen height minus app title bar height minus app toolbar height minus 5 view pixels of padding.
// Part of a loaded image with aspect ratio less than one will be behind Windows taskbar.
// This is all very complicated and undesirable.
// If offsetToScreenHeightInViewPixels is less than 40,
// on first load app will be of default size, and on second load app will be full screen.
// A loaded image will have height equal to full screen height minus app title bar height minus app toolbar height minus 5 view pixels of padding.
// Part of a loaded image with aspect ratio less than one will be behind Windows taskbar.
// This is all very complicated and undesirable.
// If offsetToScreenWidthInViewPixels is greater than or equal to 15 and offsetToScreenHeightInViewPixels is greater than or equal to 40,
// on first load app will be of PreferredLaunchViewSize, and a loaded image with aspect ratio less than one will have height exactly equal to height of app minus app title bar height minus app toolbar height.
// If PreferredLaunchViewSize.Height is only screenHeightInViewPixels - offsetToScreenHeightInViewPixels,
// part of app and a loaded image with aspect ratio less than one will be behind taskbar.
// If taskbarHeight is taken off of screenHeightInViewPixels - offsetToScreenHeightInViewPixels,
// bottom of app and coincident bottom of loaded image will be slightly above taskbar.
// I consider this ideal.
double offsetToScreenWidthInViewPixels = 15;
double offsetToScreenHeightInViewPixels = 40;
double taskbarHeight = 40;
Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize = new Windows.Foundation.Size(screenWidthInViewPixels - offsetToScreenWidthInViewPixels, screenHeightInViewPixels - offsetToScreenHeightInViewPixels - taskbarHeight);
Windows.UI.ViewManagement.ApplicationView.PreferredLaunchWindowingMode = Windows.UI.ViewManagement.ApplicationViewWindowingMode.PreferredLaunchViewSize;
// Set the app window to a new Frame.
Windows.UI.Xaml.Controls.Frame rootFrame = new Windows.UI.Xaml.Controls.Frame();
Windows.UI.Xaml.Window.Current.Content = rootFrame;
// Navigate the frame to the initial default page.
rootFrame.Navigate(typeof(MainPage), e.Arguments);
// Attempts to activate the application window by bringing it to the foreground and setting the input focus to it.
Windows.UI.Xaml.Window.Current.Activate();
} // protected override void OnLaunched
} // sealed partial class App
} // namespace Draw_Bounding_Boxes
I added property x:Name="page" to the <Page> tag in "MainPage.xaml".
I removed the <Grid> </Grid> environment from MainPage.xaml.
I replaced the contents of "MainPage.xaml.cs" with the following code block.
// Create namespace Draw_Bounding_Boxes to contain all classes associated with our app.
namespace Draw_Bounding_Boxes
{
// Create class MainPage that inherits fields and methods from Windows.UI.Xaml.Controls.Page and
// is used to declare and define user-interface elements and functionality.
public sealed partial class MainPage : Windows.UI.Xaml.Controls.Page
{
// Create constructor public MainPage.
public MainPage()
{
// Necessary to instantiate this Page, add a stackPanel to this Page, et cetera.
this.InitializeComponent();
// Find width of app in view pixels and height between bottom of app and bottom of title bar in view pixels.
double widthOfAppInViewPixels = Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize.Width;
double heightBetweenBottomOfAppAndBottomOfTitleBarInViewPixels = Windows.UI.ViewManagement.ApplicationView.PreferredLaunchViewSize.Height;
// Create a stackPanel.
Windows.UI.Xaml.Controls.StackPanel stackPanel = new Windows.UI.Xaml.Controls.StackPanel();
// Create a toolbar with width equal to the width of the app, height equal to 50 view pixels, and background color of light blue that has one row and four columns.
Windows.UI.Xaml.Controls.Grid toolbar = new Windows.UI.Xaml.Controls.Grid();
toolbar.Width = widthOfAppInViewPixels;
toolbar.Height = 50;
toolbar.Background = new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.AliceBlue);
Windows.UI.Xaml.Controls.RowDefinition row = new Windows.UI.Xaml.Controls.RowDefinition();
toolbar.RowDefinitions.Add(row);
Windows.UI.Xaml.Controls.ColumnDefinition column = new Windows.UI.Xaml.Controls.ColumnDefinition();
column.Width = new Windows.UI.Xaml.GridLength(widthOfAppInViewPixels);
toolbar.ColumnDefinitions.Add(column);
stackPanel.Children.Add(toolbar);
page.Content = stackPanel;
}
}
}
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();
}
We have made a terms of use application that fills the user's entire screen, and centres the text in the middle of the screen.
We have achieved these by setting the Location to Screen.PrimaryScreen.Bounds.X + 10 and then the width to Width = Screen.PrimaryScreen.Bounds.Width - 10, - this gives effectively 10px padding and otherwise perfectly central text.
The problem however, is when users change the 'Make it easier to read what's on your screen' option to 125%, the Bounds.Width value is incorrect, and the text goes way off the screen.
Interestingly if you set it to 'Larger - 150%' it calculates the screen width just fine.
I've tried using Screen.PrimaryScreen.WorkingArea.Width too, but that just gives the same (incorrect) result.
Is there anyway of resolving this please? Thank you
(edit) Here is the full code which generates the element:
protected override void OnLoad(System.EventArgs e) {
this._webBrowser = new WebBrowser {
IsWebBrowserContextMenuEnabled = false,
AllowWebBrowserDrop = false,
Location = new Point(Screen.PrimaryScreen.Bounds.X + 10, Screen.PrimaryScreen.Bounds.Y + 150),
Width = Screen.PrimaryScreen.Bounds.Width - 20,
ScrollBarsEnabled = true,
DocumentText = "<html><body><div>" + agreement.Terms + "</div></body></html>"
};
}
Sight unseen (always post a code snippet), this happened because you put the code in the wrong place. You are setting the size in the constructor of your Form class. Then, since the form was originally designed at 96 DPI, your form gets auto-scaled to accommodate the larger DPI setting. Which grows the form by 125%, now making it too big.
You must set the size after the form is rescaled. The Load event handler is the best opportunity, in fact one of the few reasons to actually need Load. At that point, the scaling is already applied but the window not yet visible. Therefore the best place to override Size and Location properties. Fix:
protected override void OnLoad(EventArgs e) {
var scr = Screen.FromPoint(this.Location);
this.Bounds = new Rectangle(
scr.WorkingArea.Left + 10, scr.WorkingArea.Top,
scr.WorkingArea.Width - 20, scr.WorkingArea.Bottom);
base.OnLoad(e);
}
After edit, you should not use screen coordinates to set the control's Location and Size property. A child control's location is relative from it's Parent's client area and must fit inside it. Fix:
this._webBrowser = new WebBrowser {
Location = new Point(10, 10),
Width = this.ClientSize.Width - 20,
Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Top,
// etc...
};
Setting the Anchor ensures it resizes along with the parent, probably what you want. This code belongs in the constructor, not OnLoad() since now you do want the browser to get rescaled along with the form. If you had used the designer then you'd automatically would have gotten the correct code.
I'm storing the last position for each window.
Next time the user opens the window, the last position is restored.
If the user changes his screen (from dual screen to one screen) or just to a smaller resolution, the form is nowhere to be seen...
How can I detect this? I don't like to store user settings, depending on his environment.
Thanks in advance
Let's start from scratch, you want two settings to store the state of the window. Let's call them Location (type Point, default = 0,0) and Size (type Size, default = 0, 0). You want to save them when the window is resized, avoiding storing state if the window is minimized:
protected override void OnResizeEnd(EventArgs e) {
if (this.WindowState != FormWindowState.Minimized) {
Properties.Settings.Default.Location = this.Location;
Properties.Settings.Default.Size = this.Size;
Properties.Settings.Default.Save();
}
base.OnResizeEnd(e);
}
Restore the state in the form's OnLoad method. You'll want to use Screen.FromPoint() to find the screen bounds back. Add extra code to ensure the window doesn't get too large and locates properly when the screen has disappeared:
protected override void OnLoad(EventArgs e) {
if (Properties.Settings.Default.Size != Size.Empty) {
Screen scr = Screen.FromPoint(Properties.Settings.Default.Location);
int width = Math.Min(Properties.Settings.Default.Size.Width, scr.WorkingArea.Width);
int height = Math.Min(Properties.Settings.Default.Size.Height, scr.WorkingArea.Height);
this.Size = new Size(width, height);
if (scr.WorkingArea.Contains(Properties.Settings.Default.Location))
this.Location = Properties.Settings.Default.Location;
else this.Location = new Point(scr.Bounds.Left + (scr.Bounds.Width - width) / 2,
scr.Bounds.Top + (scr.Bounds.Height - height) / 2);
}
base.OnLoad(e);
}
Use the Bounds property from System.Windows.Forms.Screen.PrimaryScreen to see what the bounds of the screen is, compare that to the position/size of your form and compensate where needed.
To get at the bounds of other screens, use the Screen.AllScreens property on the PrimaryScreen property to gain access to other Screen objects representing multiple screens.
For example, this may be as simple as checking that the Location you want to change to is on an available screen:
foreach (var screen in Screen.AllScreens)
{
if (screen.Bounds.Contains(this.Location))
{
return; // on a screen, so don't update location
}
}
// not found on a screen, so assume screen was removed and move to the primary screen
this.Location = Screen.PrimaryScreen.Bounds.Location;
You can, of course, make this more complicated by deciding which screen contains more of the form than any other (based on Bounds) and make a determination that way; but, without more details about exactly what you want, I can't suggest specifics.