Changing the start up location of a WPF window - wpf

I'd like to have a WPF window open in the top right part of the screen.
Right now I can achieve that by opening the window and then moving it (via movewindow in user32.dll). However, this approach means the window opens in it's default location, fully loads, and then moves to the top right.
How could I do I change it so that I could specify the window's initial position and size?

Just set WindowStartupLocation, Height, Width, Left, and Top in xaml:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="500" Width="500"
WindowStartupLocation="Manual"
Left="0" Top="0">
</Window>

I like to use WindowStartupLocation="CenterOwner" (MSDN docs for it)
The caller needs to specify itself as owner for this to work though, such as:
new MyWindow() { Owner = this }.ShowDialog();
Then just define the window height and width, e.g:
<Window ...
Height="400" Width="600"
WindowStartupLocation="CenterOwner"
>
...

For people who like me wanted to set the position of the window to the current mouse position, you can do it like this:
myWindow.WindowStartupLocation = WindowStartupLocation.Manual;
myWindow.Left = PointToScreen(Mouse.GetPosition(null)).X;
myWindow.Top = PointToScreen(Mouse.GetPosition(null)).Y;

There is a property for Window, called "WindowStartupLocation"
You can find that in properties window. Simply just select Window in constructor, then go to properties list. Search for "Startup" or smth similar and you can find that property. Change it to "CenterScreen" and it will make the deal.
NOTE! Make sure, that you did not select grid instead of window! Otherwise you`ll fail.
Or you just can done it via XAML editing as some guys wrote before.

This is what worked for me (with a different placement on screen):
<Window x:Class="BtnConfig.MainWindow"
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"
xmlns:local="clr-namespace:BtnConfig"
mc:Ignorable="d"
Title="MainWindow" Height="142.802" Width="448.089"
Top="288" Left="0">
</Window>
Notice it does not contain:
WindowStartupLocation="Manual"

In my case I want my "Find" tool box to appear top-right corner of the RichTextBox control.
It is done with this code:
// Assumes variable: RichTextBox m_Box, Window m_FindWordDialog
m_FindWordDialog.Show( ); // Let dialog show itself first
// Note: I had to use ActualWidth because m_Box.Width somehow is (NaN) in my case
// Not tested in different DPI.
Point locationFromScreen = m_Box.PointToScreen( new Point(m_Box.ActualWidth, 0) );
m_FindWordDialog.Left = locationFromScreen.X - m_FindWordDialog.Width;
m_FindWordDialog.Top = locationFromScreen.Y;
My search window XAML has following properties.
WindowStartupLocation="Manual" Left="0" Top="0"
It will briefly appear on its initial location and jump a bit, but I can live with it. Actually any value is fine, WindowStartupLocation="CenterOwner" also work, it just jump from center of parent. Or as dirty workaround, you could use manual mode and let it start in faraway position to not let user see jumping.
To make it appear relative on desktop position, acquire the screen workspace location, you can check: How to set the location of WPF window to the bottom right corner of desktop?

Related

How to show alignment line between two windows

I have windows inside a dock panel which are resizable.
Is there a way to show an alignment line between controls, while dragging or resizing?
What you need to do is to create a new window wihthout decoration like this:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="640" Height="480"
WindowStyle="None"
AllowsTransparency="True"
ResizeMode="CanResizeWithGrip">
<!-- Content -->
</Window>
Then you calculate the placement of your separator line and draw that line in this window. That is the only way I know to draw stuff "between" windows (regardless of platform, the same is true for any language/toolkit like Qt5, Java Spring etc).

Strange strip at the top of Windows 10 (wpf)

I have this xaml on a project that uses Caliburn micro :
<Window x:Class="Myproject.MainWindowView"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:cal="http://www.caliburnproject.org"
mc:Ignorable="d" d:DesignHeight="800" d:DesignWidth="1024" WindowStyle="None" Background="Black" >
</Window>
but when I run the application, I have a white line at the top of window :
How can I remove the line at the top?
I need a window that has no title bar, but should be resizable.
Well for the sake of easy points I suppose, the window chrome is built into the style templates and still inherited when you define WindowStyle="None" but still allow re-sizing to allow a hit spot for the manipulation event to occur. So like described in another answer you can take control of the base template and edit it to your requirements while still retaining the ability for the user to have point to invoke the re-sizing ability but with the frame thickness set to 0.
Hope this helps, cheers!

How to disable resizing of user control in WPF

I have Usercontrol.I want to disable its resizing.
The usercontrol is:
<UserControl x:Class="DocumentUpload"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView"
xmlns:telerikGrid1="clr-namespace:Telerik.Windows.Controls.GridView;assembly=Telerik.Windows.Controls.GridView"
xmlns:telerikInp="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input"
xmlns:telerikNav="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation"
xmlns:telerikData="clr-namespace:Telerik.Windows.Data;assembly=Telerik.Windows.Data"
xmlns:telerik="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Height="auto" Width="auto" MaxWidth="520">
I got to know that there is property called
ResizeMode="NoResize"
.But it is not available in UserControl.Any suugestion?
You have Width and Height set to Auto, so I guess you want to allow the control to take as much space as needed but not more.
Also, UserControl is not resizing by itself, but depends upon the layout that it's part of.
So, the quickest way to fix your issue would be to set HorizontalAlignment="Left" and VerticalAlignment="Top". But you should consider the whole layout of your application and how the UC is affected by-/affects on other components of the UI.
Then the Parent property of your UserControl is holding the Window instance. Most of times, it will be NavigationWindow. Try the below code in loaded event of your UserControl and it will work.
((NavigationWindow)this.Parent).ResizeMode = ResizeMode.NoResize

What is the WPF equivalent of Windows Forms' `StartPosition` property?

On Windows Forms, you have the StartPosition property which allows you to specify the position on the screen your form will be placed when it launches.
Does WPF have an equivalent and if so what is it?
Thanks
I think you are looking for WindowsStartupLocation:
Setting WindowStartupLocation to Manual causes a window to be positioned according to its Left and Top property values. If either the Left or Top properties aren't specified, their values are determined by Windows.
<Window x:Class="WpfAppl.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowName"
Height="500" Width="500"
WindowStartupLocation="Manual"
Left="0" Top="0">
</Window>

How do I set the size of a WPF window based on a desired client area size?

I know that in XAML/WPF, I can set the Window size using the Width and Height properties, but suppose I want to set the window size such that the client area (minus the Windows borders/decorations) will be a certain fixed size, what is the easiest way to do that?
Like, suppose I want the client area of the window to be 640x480, ala this:
<Window x:Class="SomeProject.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Background="Black">
<Canvas Width="640" Height="480"/>
</Window>
How can I ensure the inital Window size is such that it has a 640x480 client size that fits the child canvas perfectly? I'm only really interested in how to easily set this up for startup time, not so worried about how to deal with resize events and such.
Thanks.
Add the following attribute to the window
SizeToContent="WidthAndHeight"

Resources