I was wondering if it's possible to get the Window object for the current form in C#?
For example:
Window window = theCurrentlyOpenedForm
Not very clear what you mean by Window object.
To reference the current form, you use the keyword "this".
If you mean the hWnd value you use for pinvoke, then you would use the this.Handle property.
Related
I have an application that has the start up window for the login, if the login is correct, it open the main window of the application.
When the login is correct, I open the new window in this way (I am using MVVM pattern):
PrincipalViewModel miPrincipalViewModel = new PrincipalViewModel();
PrincipalView miPrincipalView = new PrincipalView();
miPrincipalView.DataContext = miPrincipalViewModel;
App.Current.MainWindow.DataContext = null;
App.Current.MainWindow.Close();
miPrincipalView.Show();
In this case I don't set the new main window. Another option is this code:
PrincipalViewModel miPrincipalViewModel = new PrincipalViewModel();
PrincipalView miPrincipalView = new PrincipalView();
miPrincipalView.DataContext = miPrincipalViewModel;
App.Current.MainWindow.DataContext = null;
App.Current.MainWindow.Close();
App.Current.MainWindow = miPrincipalView;
miPrincipalView.Show();
In this second case, I set the main window property to the main window of the application, because the login window is closed.
But I don't see any difference in the behavior in both cases, so I am wondering what is really MainWindow and the use and meaning that is has.
Thank you.
In the first case you no longer have an actual MainWindow but that shouldn't be an issue unless you rely on the Application.Current.MainWindow property to return a valid reference to a window somewhere in your application or if you have set the Application.ShutdownMode property to OnMainWindowClose.
The default value is OnLastWindowClose which means that your application will shut down as expected when you close the new window anyway.
As the documentation on MSDN says "the lifetime of some applications may not be dependent on when the main window or last window is closed, or may not be dependent on windows at all.".
Here is part about what is App.Current.MainWindow from MSDN
MainWindow is automatically set with a reference to the first Window
object to be instantiated in the AppDomain. You can specify a
different main window by setting MainWindow assigning another Windows
object to the MainWindow property. If the ShutdownMode property of the
Application object is set to OnMainWindowClose, closing the main
window causes the application to shut down. It is possible to set the
MainWindow property from XAML, if an application's main window is not
the window that is produced by setting the StartupUri property in
XAML. The two limitations of the XAML approach are: You can specify
either a XAML-only Window or a XAML-only NavigationWindow as the main
window. You must set the Visibility property of the window you
specify, otherwise it won't be shown. The reference to the first
Window object to be instantiated is also added as the first item to
the Windows collection. If MainWindow is subsequently set with a
reference to a different Window, the position of the item with the
reference to the main window will change, while the order of items in
Windows remains the same. Consequently, always use MainWindow to refer
to the main window instead of the first item in Windows.
I think that you just need to set App.Current.ShutdownMode to ShutdownMode.OnExplicitShutdown before App.Current.MainWindow.Close(); and call App.Current.Shutdown when you will want to close the application.
MSDN Application.ShutdownMode
How to use WPF window as a messagebox? here is how I was able to get messagebox. Now I want it to return back certain value in the userControl. Any help?
as #SLaks says, use the DialogReslult... if that is not enough and you are using an MVVM model, then you could use your data model: set the DataContext of the child window to your data model instance then you can bind the contorls in your child window to any data member on your model--typically you would set the DataContext to the DataContext of the parent window...
protected popMyWindow()
{
MyChildWindow cw = new MyChildWindow();
cw.DataContext = this.DataContext();
// show the window...
}
Set the window's DialogResult in the window itself before closing.
The value you set will be returned by ShowDialog()
If you want to return more than a bool?, create a wrapper method that calls ShowDialog() and returns whatever you want.
The WPF solution for these problems is the 'Page Function'.
PageFunction is a new term defined in WPF. It enables the user to navigate to a specific page and perform a task, then navigate back to the caller page with the result. It behaves just like Modal Dialogbox with the difference that PageFunction won’t be displayed as s pop-up, instead it is displayed in the same page as the caller.
Source: http://blogs.msdn.com/b/marthami/archive/2007/10/02/how-to-use-pagefunction-to-create-dialog-behavior-in-wpf.aspx
It differs from the pattern of wrapping the ShowDialog in that the page is navigated to, and more importantly, it is already strongly typed within the WPF plumbing and does not require you to develop a new class to do the same thing.
There is an explanatory StackOverflow thread here...
WPF - PageFunctions. Why are they needed?
In WPF we have Window.ShowDialog() which allows showing a modal dialog box.
In WinForms there is similar functionality but it also has an overload Form.ShowDialog(IWin32Window) which allows an IWin32Window owner to be passed in. That way the new dialog is not modal, and always maintains a z-order directly above its owner.
How would I get this same functionality using WPF?
Use the Owner property on a Window.
To expand on #Jonathan.Peppers's answer:
Say you had a Window you named FooWindow, and in BarWindow.cs you wanted to create and execute an instance. You can create a modal version of FooWindow as simple as this:
new FooWindow(){ Owner = this }.ShowDialog();
That would assume you didn't need a reference to you instance, obviously, but you get the idea?
I want to know if it is possible to acquire the desktop's Form. I have tried to get the hWnd from the desktop and use Form.FromHandle to get the form. But it always returns null. So I assume this is not possible; if it is possible can someone show me an example code.
Here is the code that did not work below:
hWnd = GetDesktopWindow();
desktop = Form.FromHandle(hWnd) as Form;
System.Diagnostics.Debugger.Break();
P.S. Can someone also explain what I did wrong here.
You can't do this because the desktop window isn't a Form. FromHandle() tries to find the managed Control (in this case a Form) that corresponds to the given window handle. Since no such Control exists, it returns null.
Im using the FindWindow method from user32.dll to find a window and get the handle, but is it possible to get the form control from the handle? and the use it like an ordinary form? Example:
int myhwnd = FindWindow(null, "MyWindow");
form myform = SomeMagic.GetFormFromHandle(myhwnd);
myform.Visible = false;
or do I have to continue to use the methods in user32.dll to send a message to the window?
If it's a managed window (you've created it with System.Windows.Forms, and it has inherited from System.Windows.Forms.Control) you can get a reference to the Control object with
Control.FromHandle(myIntPtr);
Then you just get the parent of the control until you get the form.
If not, you can't get a Control object, what you can do though is to is to create a NativeWindow, and assign the IntPtr handle to the object with the AssignHandle. That will at least give you some access to the WndProc, and similar, but that's it.
Have you tried Control.FromHandle()? Forms are (inherit from) Controls. If you hit a nested control, you'll have to search up through its parents until you hit your Form.
This assumes there actually is a Form somewhere, and you've just used the user32 methods to locate its HWND.
It's very difficult to wrap a Form class around Win32 window handle. There is no full fledged implementation provided by Microsoft. So, you have to use Native functions only to communicate with a given handle.
If the window belongs to your application you can use Control.FromHandle Method. Otherwise you will have to continue using win api. For example to hide the window you need to call ShowWindow Function.