Java Swing Windows "always on top" in WPF app - c#

for some reasons, we have an application built with WPF 4, but in some cases we have to show a windows built using java swing. The problem is, you can set some Window as parent of some dialog or other windows, but always in the same technology.
But when we launch another window that is not built using WPF is very easy to that window to be hidden behind WPF app, and is not easy way to put in front WPF.
Is there any way to put that Java windows "on top" of WPF app?

Window#toFront() and Window#setAlwaysOnTop() can help.
If you need to set window always on top, use setAlwaysOnTop() method.
If you want window to get focus, toFront() or setAutoRequestFocus() may help.
Try this:
import javax.swing.JFrame;
public class NewClass1 {
public static void main(String[] args) {
JFrame frame = new JFrame("On Top");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 100);
frame.setVisible(true);
frame.toFront();
}
}

See Window methods:
toFront()
setAlwaysOnTop(boolean)

Related

WPF, MVVM, and effective navigation / control flow

I am currently building an application based on a real world scenario, to help me learn and understand WPF and MVVM. To that end I have read and worked through Karl Shifflett's "In The Box" VSIX, and I was able to adapt most of the concepts to the application that I am working on.
While I think MVVM is a powerful design pattern, it does (seemingly) make things that were once trivial (e.g. displaying messages, navigation, interacting with multiple window), not so trivial or straightforward. Now onto the crux of my problem / confusion.
The WPF application that I am working on is a Windows based application, and I am working from a set of basic requirements:
A basic login screen
After a successful login, close the login screen and open the actual application
Simulate a typical program workflow (opening "child" windows via button clicks, displaying modal windows, etc.)
Preform data validation / error handling
Log out
I am used to working with MDI Applications on a windows platform where interactions on a parent form cause child forms to open; I understand that MDI is not something that WPF supports and I am fine with approaching development from a different perspective. My UI would still work in a similar manner to a MDI application though: I have my application layout, and as I interact with that layout my application will respond by opening windows, displaying messages, and so on. It isn't clear to me (via MVVM) how to interact with multiple windows, or how well MVVM would scale to a large application with many windows / views.
I am not opposed to using something like Prism, but I haven't found a good article on how Prism approaches my particular problem very well. Any help, advice, feedback, or otherwise is greatly appreciated!
Have you tried looking at nRoute Framework?
A link can be found here
There are actually some good tuturials about prism
Link 1
Link 2 (Part II of Link1)
Link 3
For a more straight forrward application (not very complex and modular), you can always create a aplication, with a main window that manages child usercontrols (login window, menu window, other windows ...)
For example, create a window a contentpresenter in it, and in codebehind:
public partial class ShellWindow: Window
{
public enum PagesTypes { Login, Home }
PagesTypes currentOpenedPage;
LoginUserControl login;
HomeUserControl home;
public WindowController()
{
InitializeComponent();
login = new LoginUserControl ();
login.GoToPage += new LoginUserControl.ChangePageHandler(GoToPage);
GoToPage(PagesTypes.Login);
}
public void GoToPage(PagesTypes page)
{
switch (page)
{
case PagesTypes.Login:
//Close last opened usercontrol,
....
//open new usercontrol
login = new LoginUserControl();
contentpresenter.content = login;
break;
//other pages cases
....
}
currentOpenedPage = page;
}
}
And in for example the login usercontrol:
public partial class LoginUserControl : UserControl
{
internal delegate void ChangePageHandler(ShellWindow.PagesTypes toPage);
internal event ChangePageHandler GoToPage;
public LoginUserControl()
{...}
//Methods for login
.....
internal void LoginOK()
{
if(this.GoToPage != null)
GoToPage(ShellWindow.PagesTypes.Home);
}
}
You can build a good dynamic using this method changing usercontrols, simulating diferent windows.
Hope this gives you some ideas.
MVVMing your child windows actually can be kind of easy, especially if you decide that a tabbed interface is OK. Your outer window's view model simply has a collection of ChildWindowViewModel. You create a new tab just by creating the new view model, asking the outer window to add it to it's collection, and WPF's DataTemplate awesomeness will take care of the proper display. You'll have to do some fiddling to get tab 'close' operations working the way you want. It's kind of a pain but doable.
If you really want to do MDI, there's nothing built into WPF for it (I think Microsoft has decided that it is a bad UI pattern now?), but there may be 3rd party controls out there for it. Any good one will still mirror this solution where their MDI container control will bind to your list of child window view models.

How do I open a window as a modal dialog in MonoMac using C#?

I am developing a Cocoa application with MonoMac (C#).
I have a class MyWindowController that inherits MonoMac.AppKit.NSWindowController, and open a new instance of this window like this:
MyWindowController mwc = new MyWindowController();
mwc.Window.MakeKeyAndOrderFront(this);
But how do I open it as a modal dialog? It is imperative that nothing else in my application is executed while the dialog is open, so I cannot use a window sheet (which only blocks the current window). And I can't find anything else that seems to do what I want on my controller. On Windows, I would have done this simply by calling:
mwc.ShowDialog();
So what I want is the MonoMac equivalent of ShowDialog(), I believe.
I spent almost three hours trying to figure this out before posting the question, but of course I found the solution right after asking.
It looks like I need to use the NSApplication object:
NSApplication.SharedApplication.RunModalForWindow(ewc.Window);
I had success with the answer above, but then had trouble dismissing the modal in the case that the 'close' button was pressed. The solution was to add the code
[Export ("windowWillClose:")]
public void WindowWillClose(NSNotification notification)
{
Console.WriteLine("windowWillClose:");
NSApplication.SharedApplication.StopModal ();
}
to the window controller, then set the window controller to be the window's delegate by, in Interface Builder, right-click the window and drag a line from "delegate" to the "file's owner" block.

Set Wpf parent to a MDIform

I have a win form application with a MDI Form.
for some reason i used a WPF Window in my application.
so i want to ask how can i set WPF window parent to my MDI Form?
The following code should give you the ability to set the owner of the wpf dialog to your win form.
public static void SetOwner(System.Windows.Forms.Form owner, System.Windows.Window wpfWindow)
{
WindowInteropHelper helper = new WindowInteropHelper(wpfWindow);
helper.Owner = owner.Handle;
}
There is an open-source MDI implementation for WPF that you might want to look at. It may be a good bit of work and re-structuring of your code, but if you absolutely must have MDI, then that may be the best way to go forward with this - MDI for WPF.

How to have user prompt dialogs in Windows 8 metro apps?

I would like to get some user inputs for my app like Name, DOB, etc from a modal window
for this I need a dialog to be displayed which would contain the textboxes and other controls.
Normally in WinForms/WPF I would create a class inherited from the Form/Window class and use the Show/ShowDialog method to present the form to the user
How do I achieve this behavior in Windows 8 metro apps using XAML/C# ?
I have looked at the MessageDialog class under the Windows.UI.Popups namespace
but its for showing only message like the classic MessageBox.
I have looked at another CoreWindowFlyout class and also not sure if that can be used for the behavior I am expecting.
You can simply create a custom UserControl with all the required inputs (such as TextBoxes) and then show it using Popup class.
for example:
Popup myPopup = new Popup();
myPopup.Child = new CustomUserControl();
myPopup.IsOpen = true;
Following on from the spot on answer from MBZ, have you tried looking at "Windows 8 app samples - C#, VB.NET, C++, JavaScript" particularly the "XAML Popup sample".

WPF OpenFileDialog with Win32 parent window; window is not regaining focus when closed

I have a c++ windows app calling into a c++/cli library, which is calling into a managed library. The managed library is calling OpenFileDialog.Show with a WPF window parent which is owned by the Win32 window. I haven't found a better way to do this, and all the resources I've read here and searching google and social.msdn recommend doing what I'm doing.
The dialog opens just fine, but when I hit the cancel button, for instance, the app loses focus completely. I'm not sure why it's happening, but I can't seem to make it stop. I've tried a number of different things to no avail.
If I just launch the OpenFileDialog without creating a WPF Window, I don't see the problem.
If I don't set the owner of the WPF Window, I don't see the problem. If I call OpenFileDialog.Show and don't pass the parent, but still create the WPF Window and set its owner, I still see the problem.
I am able to hack it to set the parent app window to foreground after it loses focus, but I would like to not have to.
I have uploaded a small example solution for my scenario that illustrates the problem:
http://dl.dropbox.com/u/26054523/MixedExample.zip
Any help would be appreciated.
Have you tried inverting the hosting scenario? Right now it sounds like you're going unmanaged->bridge->managed->WPF->Winforms. Maybe you could go ...managed->WinForms->WPF using ElementHost http://msdn.microsoft.com/en-us/library/ms742215.aspx
In that way, the WPF window would just be a child control of the WinForms app and that might work out better for focus switches. WinForms controls are not really meant to work directly with WPF apps so well, two different UI threading setups are being used as you've noted.
I know that this is an old post but I think that this is a common problem and I have a good answer. If you have a Win32 window parent window called ParentWindow and a WPF child window called WPFChild you can do this:
using System.Windows.Interop;
void OpenWindow()
{
WPFChildWindow WPFChild = new WPFChildWindow();
WindowInteropHelper helper = new WindowInteropHelper(WPFChild)
{
Owner = new NativeWindowWrapper(ParentWindow.Hwnd).Handle
};
bool? ret = _stepsForm.ShowDialog();
}
This will cause the child window to remain on top of the parent and function as a dialog. Keep in mind that the WPF window does not return a DialogResult but rather a nullable bool.
NativeWindow wrapper is a simple class that takes casts an int as an IntPtr. It's actually from a .net Excel ref edit project located here: How to code a .NET RefEdit ControlT

Categories

Resources