Resize window (in center screen) wpf - c#

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

Related

Size New Window to Content and Center Relative to Main Window

I have a button that opens a New Window.
I want to fit the New Window to the dynamic content inside.
Then position it Center, relative to Main Window. So it follows the Main Window's location.
Not WindowStartupLocation.CenterScreen.
This is what I'm using. It doesn't quite work right.
XAML
The New Window is originally set to 900x500, but is overridden by SizeToContent.
<Window x:Class="MyProgram.NewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="New Window"
Width="900"
Height="500">
C#
This Button is on Main Window.
I use SizeToContent.Width.
newWindow.Width does not use the SizeToContent's width, but instead detects the XAML width 900.
This causes the New Window to always be off center.
I tried setting the XAML width to Auto or 1, it still goes off center a different direction.
I tried using double width = Convert.ToInt32(SizeToContent.Width); but it says the width is 1.
I ran newWindow.UpdateLayout() but it didn't work. https://stackoverflow.com/a/2149676/6806643
public static NewWindow newWindow;
private Boolean IsNewWindowOpened = false;
private void btnNeWindow_Click(object sender, RoutedEventArgs e)
{
// Check if Window is already open
if (IsNewWindowOpened) return;
newWindow = new NewWindow(this);
// Only allow 1 Window instance
newWindow.ContentRendered += delegate { IsNewWindowOpened = true; };
newWindow.Closed += delegate { IsNewWindowOpened = false; };
// Keep Window on Top
newWindow.Owner = Window.GetWindow(this);
// Fit Window to Content
newWindow.SizeToContent = SizeToContent.Width;
// Update Layout
newWindow.UpdateLayout()
// Detect which screen we're on
var allScreens = System.Windows.Forms.Screen.AllScreens.ToList();
var thisScreen = allScreens.SingleOrDefault(s => this.Left >= s.WorkingArea.Left && this.Left < s.WorkingArea.Right);
if (thisScreen == null) thisScreen = allScreens.First();
// Position Relative to MainWindow
newWindow.Left = Math.Max((this.Left + (this.Width - newWindow.Width) / 2), thisScreen.WorkingArea.Left);
newWindow.Top = Math.Max((this.Top + (this.Height - newWindow.Height) / 2), thisScreen.WorkingArea.Top);
// Open Window
newWindow.Show();
}
Examples
Off Center
Correctly Centered
Here is what I've done. Let me know if you have any improvements.
Main Window
Open New Window Button
public static NewWindow newWindow;
private Boolean IsNewWindowOpened = false;
private void btnNeWindow_Click(object sender, RoutedEventArgs e)
{
if (IsPreviewWindowOpened) return;
// Open Preview Window
previewWindow = new Preview(this);
// Only allow 1 Window instance
previewWindow.ContentRendered += delegate { IsPreviewWindowOpened = true; };
previewWindow.Closed += delegate { IsPreviewWindowOpened = false; };
// Keep Window on Top
previewWindow.Owner = Window.GetWindow(this);
// Size to Content
previewWindow.SizeToContent = SizeToContent.Width;
// Open Window
previewWindow.Show();
}
New Window
XAML
Window_Loaded
<Window x:Class="MyProgram.NewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="New Window"
Width="900"
Height="500"
Loaded="Window_Loaded">
C#
Size Window to fit dynamic content.
Position Center, relative to Main Window.
Also nudges window back on screen if going off.
private void Window_Loaded(object sender, EventArgs e)
{
// Detect which screen we're on
var allScreens = System.Windows.Forms.Screen.AllScreens.ToList();
var thisScreen = allScreens.SingleOrDefault(s => this.Left >= s.WorkingArea.Left && this.Left < s.WorkingArea.Right);
if (thisScreen == null) thisScreen = allScreens.First();
// Position Relative to MainWindow
this.Left = Math.Max((mainwindow.Left + (mainwindow.Width - this.Width) / 2), thisScreen.WorkingArea.Left);
this.Top = Math.Max((mainwindow.Top + (mainwindow.Height - this.Height) / 2), thisScreen.WorkingArea.Top);
}

Position of notification window at the bottom right corner

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();

WPF maximize main window with center for application

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();

create inkcanvas class dynamically in wpf with tool tip display

I want to create a multiple inkcanvases with tooltip displayes.so i want to create ink canvas class dynamically.whenever i create instance of inkcanvas class ,a new ink canvas with tool display have to be created in WPF window.
class1 mycanvas1 = new class1(" aa");
class1 mycanvas2 = new class1("bb")
The letter in the string is the text of tool tip display.can u tell me the way .
I created a ink canvas usercontrol with tooltip empty text.but i unable to call this wpf user control in the above way.
InkCanvas Customized class with ToolTip:
[DebuggerDisplay("[{Scene}]Strokes:{Strokes.Count}, Children:{Children.Count}")]
public class InkCanvas_SandeepJadhav : InkCanvas
{
public InkCanvas_SandeepJadhav(string toolTip)
{
ToolTip = toolTip;
}
}
Inkcanvas class created dynamically.
public partial class MainWindow : Window
{
public InkCanvas_SandeepJadhav currCanvas = null;
double width = 0, height = 0, toolWindowHeight = 0;
public MainWindow()
{
InitializeComponent();
this.WindowState = System.Windows.WindowState.Maximized;
width = System.Windows.SystemParameters.WorkArea.Width;
height = System.Windows.SystemParameters.WorkArea.Height;
currCanvas = new InkCanvas_SandeepJadhav("Sandy");
currCanvas.Width = width;
currCanvas.Height = height - 150;
currCanvas.Background = (System.Windows.Media.Brush)new SolidColorBrush(Colors.Lime);
toolWindowHeight = (height / 10);
currCanvas.Margin = new Thickness(0, 0, 0, toolWindowHeight);
myGrid.Children.Add(currCanvas);
}
}
XAML Code
<Window x:Class="WpfMultiInkCanvas.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="myGrid"></Grid>
</Window>
here my code
namespace strokecollectio
{
class mycan : InkCanvas
{
public mycan()
{
this.Width = 300;
this.Height = 200;
}
}
}

Setting position of the windows alert

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;

Categories

Resources