WPF MessageBox not waiting for result [WPF NotifyIcon] - c#

I am using WPF NotifyIcon to create a System Tray service. When I show a messagebox, it shows up for half a second and then disappears immediately without waiting for input.
This kind of situation has happened before, and the usual advice is to use an overload which accepts a Window parameter. However, being a System Tray service, there is no window to use as a parent, and null is not accepted in its place.
Is there any way to make the MessageBox wait for user input short of creating a custom MessageBox window myself?

You don't need to create a proxy window for this. Just add MessageBoxOptions.DefaultDesktopOnly to your message box and it will fire on your desktop without disappearing.
Example
MessageBox.Show("My Message", "Title", MessageBoxButton.OK,
MessageBoxImage.Information, MessageBoxResult.OK,
MessageBoxOptions.DefaultDesktopOnly);

According to the answer here, a workaround is to actually open an invisible window and use that as the parent of the MessageBox:
Window window = new Window()
{
Visibility = Visibility.Hidden,
// Just hiding the window is not sufficient, as it still temporarily pops up the first time. Therefore, make it transparent.
AllowsTransparency = true,
Background = System.Windows.Media.Brushes.Transparent,
WindowStyle = WindowStyle.None,
ShowInTaskbar = false
};
window.Show();
...then open the MessageBox with the appropriate parameter:
MessageBox.Show(window, "Titie", "Text");
...and don't forget to close the window when you're done (possibly on application exit):
window.close();
I tried this and it works well. It's undesirable to have to open an extra window, but it's better than making your own messagebox window just for the sake of making this work.

Related

how to make wpfapp always on top and not blocked by taskbar

I used a wpf and set Topmost=true, this works for other applications but I want to put the form in taskbar, when the task bar get focus, the form is blocked.
I want to know if there is any method to make the form always on top?
enter image description here
enter image description here
Another question is that I want to add some buttons on taskbar so that I can click them easily, but I only found trayIcon which is limited in functions.
YourCustomWindow cw = new YourCustomWindow ();
cw.Owner = Application.Current.MainWindow;
cw.ShowInTaskbar = false;
cw.ShowDialog() ;

How to keep window visible at all times, but not force it to be on top

I'm creating a "desktop gadget" of sorts, I've disabled manual minimizing of the window, but now there is another problem: the system can still hide the window if the user presses Windows+D, for example.
When hidden that way, no usual minimize/resize/visibility events are fired.
I want to do something almost like TopMost, but without forcing the window order.
Maybe it's possible to install a global shortcut event using win32 API, and briefly set TopMost to true, but that sounds very hackish.
I found one solution, but it does not seem to work on Windows 10: Keeping window visible through "Show Desktop"/Win+D
The other common option, which would be writing an actual desktop gadget, is not possible on Windows 10, given their deprecation.
Are there any other methods to keep a window visible (but not on top of the screen) at all moments?
This function is working for me:
BOOL FixShowDesktop(HWND hWnd)
{
HWND hWndTmp = FindWindowEx(NULL, NULL, L"Progman", NULL);
if (hWndTmp)
{
hWndTmp = FindWindowEx(hWndTmp, NULL, L"SHELLDLL_DefView", NULL);
if (hWndTmp)
{
SetWindowLongPtr(hWnd, -8, (LONG_PTR)hWndTmp);
return TRUE;
}
}
return FALSE;
}
Note, this code is a bit better then from Keeping window visible through "Show Desktop"/Win+D because the window can be overflowed by other windows (like any other window). Using SetParent places window under all other windows.

WPF - Bring Window to Front

I have a WPF-Window which I don't close. Instead I do Hide() and Show() it. Now, when I doubleclick in my MainWindow in a Grid on a Record, which will Trigger to Show() the Window, the Window will always be shown behind the MainWindow.
I have tried the fallowing, but with no success:
view.Show();
view.Activate();
view.Topmost = true;
view.Topmost = false;
view.Focus();
Is there another way which I can use to bring the window absolut to the front? I cannot set the MainWindow as the Owner.
Window.Activate is the way to go (If you dont want to set to owner). If this does not work (as you describe), there is an error at another location.
Maybe your MainWindow has TopMost set to true ? Or you have a deferred call that focuses your main window or a control within?
Calling ShowDialog() as proposed in another answer is not an option unless you want that the whole app is blocked until the modal opened window is closed.
There is an error in the Win32-Api that influences also the window-management if WPF, but the description of your problem does not sound like this.
Additionally here a hack, but I hope that you don't need it:
Dispatcher.BeginInvoke(new Action(delegate {
view.Activate();
}), System.Windows.Threading.DispatcherPriority.ContextIdle, null);
I went through a similar issue and found a solution using a combination of the other answers. Once the window is hidden I put it in the foreground with the following code :
view.WindowState = WindowState.Normal;
view.Activate();
Note : If the window was maximised before hiding, that code will make it come back as maximised
myWindow.WindowState = WindowState.Normal;
That worked for me.
I had the same problem - I was showing a window with Owner set to NULL from a MouseDoubleClick event. I realised (eventually) that I needed to set:
e.Handled = true
before my event code completed. The following Microsoft document outlines that you may want to mark an event as handled when it responds in a "significant and relatively complete way":
http://msdn.microsoft.com/en-us/library/ms747183.aspx
This is subjective, but in my case it was preventing the window I just opened from being visible to the user.

How do I change the MessageBox location?

I need to change the message box location. I don't want it to be in the center of the page.
MessageBox.Show("Hello");
Normally, you can't change startup location of standard message box.
Solutions for your question:
Create your own custom message box.
There is example of creation on
CodeProject.
Complicated way using Windows Hook
Procedure (WinAPI) (KB180936).
You will need to create a new form that inherits from the MessageBox form. That is the only way to access the position properties.
There is a way to change the location, but its way too complicated for such a small task.
If you really need to change its location, you could display it, then use GetForegroundWindow to get a window handle, then MoveWindow to your desired location.
But, as I already mensioned, this is way too complicated. Just create your own form with a label on it an a "OK" button. Set the button as the default window button, and then, in Form1 do MyWndName.ShowDialog();
What you can do is to create a new window, set the property AllowsTransparency to true and set the Background to Transparent. In that window you can put a TextBlock, or a label and also add Yes/No Buttons. Set the location of this window using Canvs.SetTop(Window,TopPosition) and Canvas.SetLeft(Window,LeftPosition). next, call the window with the method Show() or ShowDialog().
Since I already use AutoIt for several other tasks in my project so I just create another thread to move the Message box
using System.Threading;
using AutoIt;
//Namespace, class, function stuffs
//New thread BEFORE create message box - safety measure
Thread autoItThread = new Thread(delegate ()
{
AutoItX.WinWait("New Message box");
AutoItX.WinMove("New Message box", "This box will be moved", 400, 300);
});
autoItThread.Start();
MessageBox.Show("This box will be moved", "New Message box");
Please note
The coordinate 400,300 is absolute. 0,0 will be top left corner.
This is screen-dependent. If you want to be exact, other code to determine location is needed
This task is to change the absolute position of the Message box rather than move it.
How to get/install AutoIt is not addressed here. Please look for instruction on that if you need to.

WPF owner window on top of child window

Is it possible for Owner window in WPF be on top of Child window when you click on it while Owner window is below Child window?
here is example how I call child window:
Window2 window = new Window2();
window.Owner = this;
window.Show();
Parent/Owner window will always be under child window.
To get the behavior you want, you don't want to set the Owner on either window.
You, of course will have to handle the logic yourself when closing either of the windows to close your imaginary "child" window.
There may be some other logic you'll have to implement related to minimizing, maximizing, etc.
Many of the answers on this page involve nulling-out the Window.Owner property for some or all of the (non-MainWindow) windows in your System.Windows.Application. While this is a simple and easy fix that does indeed, in isolation, fix the issue of Window overlap, unfortunately it also inhibits a lot of useful application-wide functionality that WPF seems otherwise eager to provide in the areas of:
Application acti­vation/deactivation (via mouse-click, desktop Alt-Tab switching, etc...),
correct observance of the Application.​ShutdownMode property, and generally,
orderly cleanup, resource disposal, and exit of your Application upon shutdown.
It is possible fix the Window overlap issue while still preserving these system-wide WPF features by instead designating a special invisible window instance as your Application.MainWindow.
Modify your application so that the first Window it creates--which is the Window that gets assigned to Application.MainWindow--is a special dummy Window that is then made invisible by setting its Visibility to Visibility.Hidden or calling Window.Hide(). This will not hide its owned windows. Then, ensure that your desired "main" window containing your true content, plus all the other windows, owned by this invisible window.
Once hidden, the dummy Window will not show in the Windows 10 taskbar. You can set the Window.ShowInTaskbar property on whichever of the visible windows you deem appropriate to simulate apparent special designation, as required.
This approach allows any of the visible windows in your Application to be on top of the others, while still preserving WPF features and behaviors for system-wide app activation. For example, when the Application is activated (by clicking on any one of the windows, or via Alt-tab), all of the application's windows are together brought above any other desktop app windows, while still preserving the most recent "in-app" Z-order. WPF shutdown functionality is also preserved, including correct observation of the Application.ShutdownMode logic in accordance with the invisible MainWindow (or all the others) being closed.
I ran into a similar situation. I solved this by simply removing the owner after showing the window.
Window2 window = new Window2();
window.Owner = this;
window.Show();
window.Owner = null;
Edit:
Someone replied to this, and while looking at it, I decided I wanted to make an extension method.
public static void ShowAsIfChildOf(this Window childWindow, Window parentWindow)
{
childWindow.Owner = parentWindow;
childWindow.Show();
childWindow.Owner = null;
}
Please note that the solution mentioned by Licht
Window2 window = new Window2();
window.Owner = this;
window.Show();
window.Owner = null;
seems to me to be the same as
Window2 window = new Window2();
window.Show();
i.e., as if no ownership relationship has been set, i.e., when you close the owner window the owned windows do not close, etc., and may not be a solution when one would wish to have all of the ownership relationship features except "An owner window can never cover an owned window."
Once this relationship is established, the following behaviours are exhibited:
If an owner window is minimized, all its owned windows are minimized as well.
If an owned window is minimized, its owner is not minimized.
If an owner window is maximized, both the owner window and its owned windows are restored.
An owner window can never cover an owned window.
Owned windows that were not opened using ShowDialog are not modal. The user can still interact with the owner window.
If you close an owner window, its owned windows are also closed.
The best solution here looks to me to be the one with hidden main dummy window described by Glenn Slayden, at the link below, though it would be nice if there was a simpler one.
https://stackoverflow.com/a/66110288/19683309
Don't set Owner for child window, then in MainWindow handle OnClosing event like this:
private void MainWindow_OnClosing(object? sender, CancelEventArgs e)
{
foreach (var window in Application.Current.Windows)
{
if (window is Window appWindow)
{
if(appWindow.Equals(Application.Current.MainWindow))
continue;
appWindow.Close();
}
}
}

Categories