Is Owner Window required in WPF FolderBrowserDialog in 2021? - wpf

When using FolderBrowserDialog in WPF, I've seen a few answers where using the Win32 API is required to make it modal. For example,
Using FolderBrowserDialog in WPF application
How to use a FolderBrowserDialog from a WPF application
Actually, is the Win32 Api REALLY required ? I just tried the FolderBrowserDialog and it is already modal by default; simply calling .ShowDialog() is already modal to the underlying window. (What are the other answers talking about or have the wpf internals changed ?)
EDIT: using Visual Studio 2015 with .NET framework (not core)

Actually, is the Win32 Api REALLY required
Yes. The FolderBrowserDialog is exposed through Win32. You can't write software for Windows without using Win32 in some way or another.
I just tried the FolderBrowserDialog and it is already modal by default; simply calling .ShowDialog() is already modal to the underlying window.
That's because when you call ShowDialog() it passes IntPtr.Zero for the owner hWnd parameter (the same as NULL in C). When you pass NULL then Windows uses the currently active window in the process as the owner - so an owner is still set, it's just determined automatically:
From the documentation for WinForms' ShowDialog() (the overload without the owner parameter. While WinForms and WPF are separate, the principle is the same for both):
When this version is called, the currently active window is made the owner of the dialog box. If you want to specify a specific owner, use the other version of this method.
That said, you should still specify the owner hWnd where possible because you may rearchitect your application and may want a different window to be the owner (e.g. when you want to show a dialog immediately after the non-root parent window closes)

Related

Custom WinForm with MessageBox Icon and Sound

I want to create a modal dialog that has more controls than what a standard .NET MessageBox offers you. I've created my own Windows Form that will be called with ShowDialog() to give the modal behavior. However, I'd like to utilize the graphics that come with MessageBox via MesageBoxIcon. Is this possible? Is it also possible to replicate playing the error/warning windows sounds associated with the message box icons (as they are set in the user's windows settings)?
See System.Drawing.SystemIcons class to display the system icons the MessageBox class uses, such as Question, Information and Warning.
e.Graphics.DrawImage(SystemIcons.Question.ToBitmap(), new Point(0, 0));
For the sounds, see the System.Media.SystemSounds class to play the associated sounds.
System.Media.SystemSounds.Asterisk.Play();
MessageBox is provided by the OS I'm afraid. You can extend it, but it requires a lot of work (see this CodeProject article for a tutorial). Your best bet is probably to start again with a control inheriting from Form as you suggest.
To access the icons, it's as simple as using the System.Drawing.SystemIcons class (documentation for that is here.)

New window in Silverlight out-of-browser application

In a Silverlight application that doesn't run in a browser, is it possible to create a new top-level window? Or at least a child window?
I found some solution using the ChildWindow class, but even though my project is configured for Silverlight 4, that class can not be found.
I have a UserControl (XAML file) that I want to show as a new window. Using a tab control is not really an option unfortunately as the user has to be able to arrange windows to see more than one at once.
Any suggestions?
ChildWindow is part of the SDK, you need to add the System.Windows.Controls.dll to access the ChildWindow type.
I'm not sure you can get the ChildWindow to do what you are expecting. A ChildWindow is designed to present a window in a modal manner. However this modal behaviour is really a function of the ChildWindow template. It is possible to re-template to remove the modal behaviour. However I've never tried to manipulate multiple child windows. You could give it a go, the big question would be what happens if you close Child windows in a different order in which they were created?
In order to use a ChildWindow to present your UserControl it would probably be best for you to derive from ChildWindow, instead of UserControl. You may even find it would ultimately be better for you create base class between your specific Xaml and ChildWindow where you would put code that is common to all your windows.
I might be worth you noting the Silverlight 5 will support multiple windows.

How to have multiple windows in one WPF Application?

I'm relatively new to the WPF world and I'm working on an application where I need to have multiple windows in one application. Like Photoshop when the user clicks on "New" button a new window opens and the old existing window(s) goes in the background. The toolbar and the statusbar, however, stay on top, untouched.
I would also like to add the feature that the user can work with multiple windows if each of them is not maximized.
How do I achieve this?
I apologize if I'm a bit unclear in framing my question.
What you are (probably) talking about is an MDI Application. "Unfortunately" WPF does not support that natively. I use quotes because it is more frowned upon in modern UI development than it was in the past.
Instead, the most common "WPF way" of doing this is to open each New window in a new tab in a tab control (like Visual Studio and most web browsers do).
If you really want to do an MDI application, Google search that, but to warn you, it probably means creating your own Window manager.
An alternative is to open non-modal windows, with their ShowInTaskbar property set to false. This gives you the ability to open as many windows as you want, but you lose the ability to contain them within a centralized workspace app.

Popup Outside the Bounds of the Silverlight Control

I'm simply wondering if it is possible to open one or more non-modal popups or dialogs, which are NOT limited to be displayed and moved within the bounds of the parent Silverlight control. Doesn't seem to be possible, even for out-of-browser installs. I hope I'm wrong :)
While the previous answers are true, strictly speaking it is possible in browser mode to have a real popup. At the moment it can only be done indirectly and with a lot more work than a normal Silverlight popup Window.
Basically you popup a new Silverlight app (or second instance of the current app) in a browser popup window (not a Silverlight popup). Silverlight can create the popup window via calls to Javascript.
The 2 Silverlight instances usually need to communicate data, but there are several solutions to that problem too. Javascript can also talk to Silverlight objects (love that feature) and pass information back or you could simply pass data one-way via URL parameters.
No this is not possible, Silverlight strictly renders to a single window (or if in Windowless mode the specific rectangle provided to it by its host).
As Anthony said this is not possible. I've heard from some of the people involved with Silverlight development that this is an often-requested feature and will most likely be added in an upcoming version. But I'm guessing it would only be available in "out of browser" mode.

Mixing MFC and WPF: Modal Dialogs

I'm adding C# WPF dialogs to an existing C++ MFC app, using a C++/CLI interface layer. I've got things working, except I'm having a problem with modality. For example:
MFC app shows a WPF dialog using ShowDialog. Works as expected.
That WPF dialog shows a MFC dialog using DoModal. The WPF dialog is hidden behind the base C++ app, and is not disabled unless I manually change IsEnabled. Not ideal, but it works.
Now, that MFC dialog is closed. Now for some reason the base MFC app is enabled, when it should still be disabled due to the WPF dialog not having been closed. That's bad, as it now allows the user to do crazy things while the WPF dialog is still open.
I have a feeling that it would work better if I could set parent dialogs correctly. But so far I havent been able to set an MFC dialog's parent as a WPF dialog, or vice versa. And, I don't even know if that'd fix it.
Any ideas?
When opening a CDialog, the trick is to use a WindowsInteropHelper to get the parent WPF dialog's HWND. Then, you can use CWnd::Attach to wrap that HWND in a CWnd class to pass to the CDialog's constructor.
The problem I had was that I already had the CDialog constructed., but not yet displayed. The various versions of SetParent can only be used if your target child window already has a valid handle. I had to write a new function in my CDialog class to set m_wndParent, which is what it uses as the parent when it finally creates the dialog. Then everything works great!
Somehow creating WPF dialogs from MFC dialogs "just works". It's magic.
When showing the WPF dialog, are you using the HwndSource class to wrap the WPF window? If so, you may be able to ::SetParent the WPF window as well as use the HwndSource.Handle property to set the sub-child's parent.

Resources