I have created a win app, which uses secondary monitor to show images.
I have used the following code to detect and set location of secondary monitor(which is an extended monitor of primary monitor).
public void secondarydisplay()
{
FrmSecondaryDisplay secdis = new FrmSecondaryDisplay();
Screen[] screens = Screen.AllScreens;
secdis.MyBase = this;
this.MySecScreen = secdis;
secdis.Show();
setFormLocation(secdis, screens[1]);
}
private void setFormLocation(Form form, Screen screen)
{
Rectangle bounds = screen.Bounds;
form.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);
form.StartPosition = FormStartPosition.Manual;
}
Problem is i am getting this thin 6mm white line on the left most corner of the secondary display. which is nothing but the extension of primary display. how do i make it disapper? on moving the cursor to secondary monitor and click on the screen of secondary monitor this white line disappears. and on moving back the cursor to primary monitor and click on it makes the white line appear on secondary monitor. kindly help me how can i resolve this issue. it looks ugly in secondary mopnitor.
Always prepared the window properties before Show()
public void Secondarydisplay()
{
if (Screen.AllScreens.Count() == 1)
{
return; // No second display
}
var secdis = new FrmSecondaryDisplay();
// Actually you shall use secdis.Show(this) to build the relation of the forms.
// When "this" closes, the second display form closes.
secdis.MyBase = this;
this.MySecScreen = secdis;
// Setup Windows Position before Show()
secdis.StartPosition = FormStartPosition.Manual;
secdis.Location = Screen.AllScreens[1].WorkingArea.Location;
secdis.TopMost = true;
secdis.FormBorderStyle = FormBorderStyle.None;
secdis.WindowState = FormWindowState.Maximized;
secdis.Show(this); // See comment above
}
Related
I want to develop a windows form application where the destination system is having screen resolution of 1080x1920. but my system is not accepting that resolution . even i cant set my winform size to 1080x1920.
can anybody help me out to solve this problem
This works for me. This is the Load event for the form (you can generate this by double clicking the form in the designer). In this event we check the current dimensions of the screen containing the form. Then we set the form size to match. We also move the form's position to 0, 0 so it doesn't clip off the screen.
//the name of this function will be different for you.
//generate this function by double clicking the form in designer.
//the code inside the function is what you're interested in.
private void MainForm_Load(object sender, EventArgs e)
{
Rectangle screenSize = Screen.GetBounds(this); //find out the current screen size
this.Top = 0; //move the form to top
this.Left = 0; //move the form to left
this.Width = screenSize.Width; //set form width to screen width
this.Height = screenSize.Height; //set form height to screen height
}
EDIT: Also, why not just maximize?
this.WindowState = FormWindowState.Maximized;
I am creating a C# form application. I wanted to show the initial form on a second screen. I want to open the other form pages on the first screen. There is a resolution difference between two screens. How can I show the initial form on the second screen and the other five forms on the main screen?
In matter of window positioning there are no "two screens" but just one "working area".
Meaning if you have two FullHD screens next to each other you have a working area of 3840x1080 (minus some for taskbar and such).
If you then place a window at Left = 200 and Top = 100 it will be placed 200 pixels to the left side of the left screen and if you place it at Left = 2120 and Top = 100 it will be placed at the same position on the right screen.
And for all that to work you need to use StartPosition = FormStartPosition.Manual.
I've used this method, hope it helps you:
public void MaximizeToMonitor(Form frm, int monitorIndex)
{
try
{
Screen screen = Screen.AllScreens[monitorIndex];
if(screen != null)
{
frm.WindowState = FormWindowState.Normal;
var workingArea = screen.WorkingArea;
frm.Left = workingArea.Left + 10;
frm.Top = workingArea.Top + 10;
frm.Width = workingArea.Width + 10;
frm.Height = workingArea.Height + 10;
frm.WindowState = FormWindowState.Maximized;
}
}
catch (Exception ex)
{
MessageBox.Show($"Monitor does not exists. {Environment.NewLine}{ex.Message}");
}
}
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();
}
I have a problem with multimonitor system and maximizing my borderless WPF C# .NET 4.0 window. One monitor is 1680x1050 (primary) and the second is 1920x1080 (secondary). When I'm maximizing my window on the primary screen - no problems, even when I switch the order of those two. But every time I'm trying to maximize it on the secondary screen it's being cut off to the size of primary monitor. I see that the window size is given appropriate but this doesn't work.
Getting the size of monitor:
private System.Windows.Forms.Screen GetCurrentScreen()
{
System.Drawing.Point centerPoint = new System.Drawing.Point((int)(Left + Width / 2), (int)(Top + Height / 2));
foreach (System.Windows.Forms.Screen s in System.Windows.Forms.Screen.AllScreens)
{
if (s.Bounds.Contains(centerPoint))
{
return s;
}
}
return null;
}
Maximizing:
private void Maximize
{
if (this.WindowState == WindowState.Normal)
{
var scr = GetCurrentScreen();
//this.MaxHeight = scr.WorkingArea.Height;
//this.MaxWidth = scr.WorkingArea.Width;
if (scr != null)
{
if (scr.Primary)
{
this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
}
else
{
this.MaxHeight = double.PositiveInfinity; //even ridiculous values don't work
this.MaxWidth = double.PositiveInfinity;
this.Height = scr.WorkingArea.Height; // correct values of 2nd screen
this.Width = scr.WorkingArea.Width;
}
}
else
{
this.MaxHeight = SystemParameters.MaximizedPrimaryScreenHeight;
this.MaxWidth = SystemParameters.MaximizedPrimaryScreenWidth;
}
this.WindowState = WindowState.Maximized;
}
What I get is:
http://imgur.com/ZYzVV9Q,yf7lSfY#1
What I want:
http://imgur.com/ZYzVV9Q,yf7lSfY#0
Thanks
This worked for me last time I had to maximize a window on my secondary screen:
Screen sTwo = Screen.AllScreens[1];
this.Top = sTwo.Bounds.Top;
this.Left = sTwo.Bounds.Left;
this.Width = sTwo.Bounds.Width;
this.Height = sTwo.Bounds.Height;
Make sure you add the reference
using System.Windows.Forms;
And set
AllowsTransparency="True"
in your *.xaml
There is a possibility that some of your code is causing this issue as I cannot replicate it on my two screens... they are both the same size, but if I change the resolution on one, the application still maximises correctly. If you start a new WPF Application and just add
WindowState = "Maximized"
to the Window declaration, do you still have the same problem?
Try to override or correct ArrangeOverride method:
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.arrangeoverride.aspx
When overridden in a derived class, positions child elements and
determines a size for a FrameworkElement derived class
Symptoms are similar.
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.