i have WPF application and i want to maximize main window. i tried the below code but the issue is that the window is maximized but not centered. i want the window to be maximized as i maximize window with mouse click. my code is:
mainWindow.Height = SystemParameters.MaximizedPrimaryScreenHeight;
mainWindow.Width = SystemParameters.MaximizedPrimaryScreenWidth;
Set the WindowState property instead of Width and Height:
mainWindow.WindowState = WindowState.Maximized;
This should work:
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.WindowState = WindowState.Maximized;
but if not, try this approach:
private void CenterWindow()
{
double screenWidth = System.Windows.SystemParameters.PrimaryScreenWidth;
double screenHeight = System.Windows.SystemParameters.PrimaryScreenHeight;
double windowWidth = this.Width;
double windowHeight = this.Height;
this.Left = (screenWidth / 2) - (windowWidth / 2);
this.Top = (screenHeight / 2) - (windowHeight / 2);
}
and then maximize it:
window.WindowState = WindowState.Maximized;
Further more, when working with 2 ore more displays you may want to create a Window and show it on the display on which the cursor is active. In order to accomplish this you will need the following trick:
Window window = new Window();
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.SourceInitialized += (s, a) => window.WindowState = WindowState.Maximized;
window.Show();
Related
I need help with Setting dialog window position relative to User control.
I want to show my window in the middle user control when a window starts.
How can I find the left and tom location of my User Control?
I use this code in my app but does not work correctly in WPF.
Thank you for help.
private void PossitionWindow(object sender, RoutedEventArgs e)
{
Window wind = new Window();
var location = this.PointToScreen(new Point(0, 0));
wind.Left = location.X;
wind.Top = location.Y - wind.Height;
location.X = wind.Top + (wind.Height - this.ActualHeight) / 2;
location.Y = wind.Left + (wind.Width - this.ActualWidth) / 2;
}
Here is a small example.
// Get absolute location on screen of upper left corner of the UserControl
Point locationFromScreen = userControl1.PointToScreen(new Point(0, 0));
// Transform screen point to WPF device independent point
PresentationSource source = PresentationSource.FromVisual(this);
Point targetPoints = source.CompositionTarget.TransformFromDevice.Transform(locationFromScreen);
// Get Focus
Point focus = new Point();
focus.X = targetPoints.X + (userControl1.Width / 2.0);
focus.Y = targetPoints.Y + (userControl1.Height / 2.0);
// Set coordinates
Window window = new Window();
window.Width = 300;
window.Height = 300;
window.WindowStartupLocation = WindowStartupLocation.Manual;
window.Top = focus.Y - (window.Width / 2.0);
window.Left = focus.X - (window.Height / 2.0);
window.ShowDialog();
Preview
I have the following code. I'm trying to make the window open on the right side of the primary screen, midway down the side of the screen. It's not moving the starting location of the window at all.
int screenWidth = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
int screenHeight = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
cd.Top = (screenHeight / 2) - (cd.Height / 2);
cd.Left = screenWidth - (cd.Width + 4);
You should place your code in Load event of the window, also this code is a bit more readeable, and works as you wanted to, I checked it.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
this.Left = desktopWorkingArea.Right - this.Width;
this.Top = desktopWorkingArea.Top + this.Height;
}
I've got a problem with positioning a window. The window is not my main window. I want to position the window to the bottom right corner of my working area above a taskbar.
I have following code:
public partial class NotificationWindow : Window
{
public NotificationWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Rect desktopWorkingArea = SystemParameters.WorkArea;
Left = desktopWorkingArea.Right - Width;
Top = desktopWorkingArea.Bottom - Height;
}
}
And unwanted result of that code:
I want to have the notification window a little higher above the taskbar.
I think my code should work but it does not.
Thanks for advice.
I used this with satisfactory result.
double width = 375;
double height = 275;
Window w = new Window();
w.Width = width;
w.Height = height;
w.Left = SystemParameters.FullPrimaryScreenWidth - width;
w.Top = SystemParameters.FullPrimaryScreenHeight - height;
w.ShowDialog();
Here I report an example of my alert:
As you can see from the picture I can not bring my alert window above the taskbar.
This is the code that I set the poszione of the window:
public new void Show()
{
this.Left = Application.Current.MainWindow.Left +
Application.Current.MainWindow.ActualWidth - this.Width;
this.Top = Application.Current.MainWindow.Top +
Application.Current.MainWindow.ActualHeight - this.Height;
this.Topmost = true;
Application.Current.MainWindow.Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.ApplicationIdle,
(System.Threading.ThreadStart)delegate()
{
base.Show();
});
}
You can use some useful info in SystemParameters to locate the window correctly. Specifically you need to get the SystemParameters.WorkArea.Bottom. The code should be like this:
this.Left = SystemParameters.WorkArea.Right - this.ActualWidth;
this.Top = SystemParameters.WorkArea.Bottom - this.ActualHeight;
i need open in all work area
this.Width = SystemParameters.WorkArea.Width;
this.Height = SystemParameters.WorkArea.Height;
but, window not center location in the screen, how i can location my window in center screen?
Use WindowState
In XAML
<Window WindowState="Maximised">
// ...
</Window>
In Code-behind
MyWindow mw = new MyWindow();
mw.WindowState = WindowState.Maximised;
write like this .
WindowStartupLocation="CenterScreen">
Try this WindowStartupLocation
UPDATED
If I use...
public MainWindow() {
InitializeComponent();
this.Width = SystemParameters.WorkArea.Width;
this.Height = SystemParameters.WorkArea.Height;
this.Top = 0;
this.Left = 0;
}
Without WindowStartupLocation, the window is centered and fills the screen