WPF windows position - c#

I need help on WPF...
When user move/drag/change the position of the 1st page window (mainwindow.xaml), then user click on "next" button to proceed to 2nd page(process.xaml), the window (process.xaml) is not at the same position as the (mainwindow.xaml) that the user move to earlier on. How can I make it to remember the position of the window throughout? And when user close the window, and run it again, the window will be default appear in the center unless user move the window.
Really need help for this. Thanks.

Maybe it would be easiest to only have one window throughout the whole application, but replace the window's contents? Create your screens as usercontrols instead, then set content on the main window instead of opening a new one..

You might want to make use of the WindowStartupLocation for process.xaml.
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.
Like this:
this.Owner = MainWindow; // reference to mainwindow.xaml
this.Left = Owner.Left;
this.Top = Owner.Top;
this.WindowStartupLocation = WindowStartupLocation.Manual;

Related

uwp app reposition to bottom left of screen

I want to reposition a uwp app everytime it opens, to bottom left just above the taskbar, to mimic start menu like behavior.
I have tried using AppWindow class to create secondary views and resize and reposition them with the build in methods, but the problem is there is no appwindow instance for the main window.
AppWindow = await AppWindow.TryCreateAsync();
AppWindow.RequestMoveRelativeToDisplayRegion(displayRegion, new Point(xpos, ypos));
AppWindow.RequestSize(new Size(wWidth, wHeight));
In case of ApplicationView for the main window only resizing method is available.
var view = ApplicationView.GetForCurrentView();
view.TryResizeView(new Size(wWidth, wHeight));
So technically I need to do calculations based on display region (monitor) and then place the app on bottom left and also resize it to a specific size depending on monitor effective pixels, resizing isnt the problem as their is a method for it, but moving and repositioning the app window to a specific place on the screen is a problem as there is no method for it.
Thanks.
moving and repositioning the app window to a specific place on the screen is a problem as there is no method for it.
Yes, UWP doesn't have the API that could change its position in the system as mentioned here:UWP Window Placement and How to set UWP app position (x & y coordinates) . Although this is an old question, the answer doesn't change. The position of the UWP window can't be controlled from the app level.

Show icon in taskbar for hidden windows

When I set the main window's visibility to hidden, No icon is shown in taskbar, so I have no control over the window to show it again. I want for the application's icon to be visible even when I hide the window, and to show the window when I click it's button in the taskbar. (something like minimize behavior)
How can I achieve that using WPF and .Net 4.0 in C#?
Edit: I mean the icon in taskbar (usually in the left and middle of the horizontal taskbar) not the notifyicon in system tray.
So, based on "comments" section, what you are looking to do is minimize or hide a window but still show some windows or dialogues that the window opens. First if you want to keep your window in the task bar, you should minimize with:
this.WindowState = WindowState.Minimized
That can be called from anywhere within the form. As you mentioned, though, this will close hide any dialogues that have this window set as the parent. The key, then is to not use this window as the parent. Lets say your dialogues inherit from form. You want to use:
newWindow.Show();
I am guessing that you are calling "ShowDialog", which ties the window state to the parent window state. Try this out and hopefully it will help!
Edit
One more note: the same is actually true of MessageBoxes, but the way to control the parent form is with the first parameter of the MessageBox.Show() call. If you pass in a form as the first parameter, that will be the parent, otherwise the parent will not be set.

Preventing automatic offsetting when opening a new window

In WPF, when a new Window is shown, it is offset by maybe 10 pixels horizontally and vertically. Is there an easy way to prevent this, so the window opens directly over the one that triggered it? (They are the same size - consider it similar to an installer's behavior)
You should set the WindowStartupLocation property of all windows to CenterScreen value.
You can set the Left and Top properties of the new window to be the same as the owner window.
You should do this before you call the Show or ShowDialog methods.
Good luck,
M. Moshe

Check if a window is over another window

I am making a webbrowser and I have to make the tabs draggable. So when you pull out a tab, I delete the tabitem from the tabcontrol and put it in another window, which I create on that very moment, and that's the tab, the window, you drag. Now I have to be able to drop the tab back in so I need to check if the window, the window I am dragging, is over another window where it could be dropped.
So I actually need two things:
1) Is the window over another one?
2) If it is over a window, is it the first window underneath the window youre dragging?
Thanks in advance
This should give you a good starting point Drag and Drop Overview

Skip Aero Window Animation

How can I disable the "opening" animation of a window under Aero programatically?
When opening a new Form it "pops in" (fade in + a slight scaling transformation).
I want to stop this animation and show the window instantly.
I already tried to set the Location property of the Form to somewhere offscreen, then calling Show(), and then moving it at its correct location.
But that doesn't help, the animation will continue.
Maybe there is some hidden property I can set?
I don't want to disable open/close/minimize/maximize animations globally!
I just want to skip the "window-open" animation of my window.
I already played around with single and multiple calls ShowWindow(...) directly after Form.Show(). But no matter what parameters I pass, doesn't abort the opening-animation.
I've got it! After some trying around with ShowWindow, BorderStyles I found my exact solution:
Change the initial "FormBorderStyle" property of the form to one of those:
None
FixedToolWindow
SizeableToolWindow
Add a eventhandler to the Forms "Shown" event.
Inside the eventhandler, change the FormBorderStyle to "Sizeable" (or any other).
Now the trick is that "none" and "*toolwindow" borderstyles will suppress the opening/popup animation for that form. Than, as soon as the form is being displayed the borderstyle is changed, giving it the original functionality (Icon in the Control-Bar, Minimize/Maximize Buttons etc...)
Edit: For everyone who might want to try this too, I have to point out that this can will screw with the actual size of the window when done with PInvoke commands.
If you rely on the size of the window being correct, be sure to resize the the window to its intended size after you done this.
This is part of windows visual effects and can be adjusted using the SystemParametersInfo Method.
I found that the animation is only take place when the form is shown for the first time.
So here is the trick:
var form = new Form();
form.Show();
form.Hide();
form.Show();
I tested it only in Windows 8
You can change the style before and after, like this, which will prevent the fade-out animation.
this.FormBorderStyle = FormBorderStyle.Sizable;
this.Show();
// Do whatever
this.FormBorderStyle = FormBorderStyle.Sizable;
this.Show();

Categories