I recently found out that by default MessageBoxes were not the top most form when displayed by default and I was wondering if anyone knew any circumstances when you wouldn't want the messagebox to be shown on top of other windows?
I found the issue when I started to show splash screens whilst loading an application, and it looked like my program was still running but there was a MessageBox behind the splash screen that was waiting for input.. The splash screen was shown on a different thread to the thread that called the messagebox so I imagine this is why it didn't appear above the splash; but this still doesn't explain why MessageBox doesn't have the MB_TOPMOST flag by default?
Edit
To better clarify:
in the end I had to do something similar to this in the end to make a messagebox, code isn't exactly correct as wrote from memory)
[DllImport("User32.dll")]
private int extern MessageBox(windowhandle, message, caption, flag);
public static void MessageBox(windowhandle, string message, string caption)
{
MessageBox(windowhandle, message,caption, MB_TOPMOST);
}
The proposed solutions work if you can get a handle or reference to the window the dialog is supposed to appear on top of. However, this may not always be possible or easy to achieve:
the window is a splash screen and should not tightly coupled with your business logic
the window is created by another class or library than the current one
the window is out of your control, i.e. from a third party (native) library
In such scenarios, you could use the Win232 MessageBox API from User32.dll, but a simpler, managed solution is also available:
MessageBox.Show(new Form { TopMost = true }, "Hello, I'm on top!");
The code new Form { TopMost = true } will create a hidden form with the MB_TOPMOST property, which is inherited by the messagebox dialog window. As a result, it will appear on top of all your other windows. Using new Form() inline has no side-effects, has no visual appearance and it will be destroyed normally via the garbage collector.
Note: if you are not inside a form already, don't forget the namespace, this is System.Windows.Forms.MessageBox, not System.Windows.MessageBox! (thanks, user1).
To show the MessageBox on top-most of all for the application
Code
//Should be MessageBox.Show() below
MessageBox.Show(this, "My top most message");
Reason for not being MB_TOPMOST by default
If MB_TOPMOST will be default then the MessageBox will show up in a 'system modal' mode and it will be exactly on top on that form and side effects are that the 'system modal' mode will cause the MessageBox to Block the windows until the message is dismissed normally it will be 'application modal' mode.
Reference links
MSDN forum - How to display a MessageBox as topmost window
SO - C# MessageBox To Front When App is Minimized To Tray
When showing MessageBox provide its owner as the first argument. For example when invoking from a Form instance call:
MessageBox.Show(this, "Message");
Provide a reference to the window owning it as the first argument.
Message boxes (and modal forms in general) do not appear on top of all windows of your application. They only appear on top of their owner. If you want your message-box (or other modal forms) be on top of your splash screen, set their owner to the splash form instance.
I try to paste a more complete code piece, it's definitely working
[CLSCompliant(false)]
[DllImport("user32.dll", EntryPoint = "MessageBox")]
public static extern int MessageBoxUser32(int hWnd, String text, String caption, uint type);
const uint MB_TOPMOST = 0x00040000;
const uint MB_OK = 0x00000000;
const uint MB_OKCANCEL = 0x00000001;
const uint MB_ABORTRETRYIGNORE = 0x00000002;
const uint MB_YESNOCANCEL = 0x00000003;
const uint MB_YESNO = 0x00000004;
const uint MB_RETRYCANCEL = 0x00000005;
public static void ShowMessageBox(string message, bool topMost = true
, string title = null, MessageBoxButtons buttons = MessageBoxButtons.OK)
{
if(topMost)
{
uint mbv = MB_TOPMOST;
if (buttons == MessageBoxButtons.OK)
mbv |= MB_OK;
if (buttons == MessageBoxButtons.OKCancel)
mbv |= MB_OKCANCEL;
if (buttons == MessageBoxButtons.AbortRetryIgnore)
mbv |= MB_ABORTRETRYIGNORE;
if (buttons == MessageBoxButtons.YesNoCancel)
mbv |= MB_YESNOCANCEL;
if (buttons == MessageBoxButtons.YesNo)
mbv |= MB_YESNO;
if (buttons == MessageBoxButtons.RetryCancel)
mbv |= MB_RETRYCANCEL;
MessageBoxUser32(0, message, title == null ? string.Empty : title, MB_TOPMOST);
}
else
{
MessageBox.Show(message, title == null ? string.Empty : title, buttons);
}
}
The answer given above is obviously correct minus the fact that it needs to call System.iDisposable.Dispose on the object new Form.
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
MessageBoxIcon icon = MessageBoxIcon.Error;
string message = Resources.ResourceManager.GetString("MESSAGE");
string caption = Resources.ResourceManager.GetString("TITLE");
DialogResult result;
Form form;
using (form = new Form())
{
form.TopMost = true;
result = MessageBox.Show(form, caption, message, buttons, icon);
}
if (result == DialogResult.Yes)
{
// do something with the result
}
For more:
Top-Most-MessageBox Examples
Related
I'm annoyed that the I'm promised a fixed window that the user can't resize, but then of course they're allowed to double click the title bar to maximize this 'unresizable' window. How can I turn this off? Can I do it with winforms code, or must I go down to Win32?
Thanks!
You could set the MaximizeBox property of the form to false
You can disable the double-click message on a title bar in general (or change the default behavior which is maximizing the window). it works on any FormBorderStyle:
private const int WM_NCLBUTTONDBLCLK = 0x00A3; //double click on a title bar a.k.a. non-client area of the form
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCLBUTTONDBLCLK)
{
m.Result = IntPtr.Zero;
return;
}
base.WndProc(ref m);
}
MSDN Source
Cheers!
///
/// This is we are overriding base WIN32 window procedure to prevent the form from being moved by the mouse as well as resized by the mouse double click.
///
///
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
const int WM_NCLBUTTONDBLCLK = 0x00A3; //double click on a title bar a.k.a. non-client area of the form
switch (m.Msg)
{
case WM_SYSCOMMAND: //preventing the form from being moved by the mouse.
int command = m.WParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
return;
break;
}
if(m.Msg== WM_NCLBUTTONDBLCLK) //preventing the form being resized by the mouse double click on the title bar.
{
m.Result = IntPtr.Zero;
return;
}
base.WndProc(ref m);
}
I know I'm late to the party, May help someone who is searching for the same.
private const int WM_NCLBUTTONDBLCLK = 0x00A3; //double click on a title bar a.k.a. non-client area of the form
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case WM_NCLBUTTONDBLCLK: //preventing the form being resized by the mouse double click on the title bar.
handled = true;
break;
default:
break;
}
return IntPtr.Zero;
}
I just checked it in VB.Net. Below code worked for me.
Private Const Win_FormTitleDoubleClick As Integer = 163
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = Win_FormTitleDoubleClick Then
m.Result = IntPtr.Zero
Return
End If
MyBase.WndProc(m)
End Sub
Note: 163 is the event code
I approached the problem slightly differently. First I removed the minimize and maximize options from the control box via the MaximizeBox and MinimizeBox properties as you'd expect.
Then I added the following OnResizeEnd() event and attached it to the Form's ResizeEnd event handler:
/// <summary>Locks the form to fill the screen that it's placed on and remain in that state as long as it's open.</summary>
private void GuiMain_ResizeEnd( object sender, EventArgs e )
{
this.Location = Screen.WorkingArea.Location;
this.Size = Screen.WorkingArea.Size;
this.MaximizedBounds = Screen.WorkingArea;
this.MinimumSize = Screen.WorkingArea.Size;
this.WindowState = FormWindowState.Normal;
}
This solution necessarily relies on the existence of the following accessor which you can copy, or you can simply replace each instance of Screen. in the above with Screen.FromHandle( this.Handle ).
protected Screen Screen => Screen.FromHandle( this.Handle );
Obviously, if ironically, this actually keeps the form in the FormWindowState.Normal state, but it mimics the effect of maximization, and resets this fullscreen state after any attempt to change it.
Interestingly, due to the use of the Screen.FromHandle() settings (as opposed to hard-coded ones), you can actually drag the form from one display to another, whereupon it immediately "snaps" to fill THAT screen instead. Something I found quite handy, but which may require additional code to correct if you don't want that functionality in your application.
I have a program which runs in the background, and when something happens a message box appears, I want to have it so clicking yes will switch to a specified program.
I just can't find what ClassName and CaptionName work with it, I need it to work with the game World of Warcraft.
The Window Title is "Wold of Warcraft" on Task manager its called "World of Warcraft Retail", when I inspect its' properties it says "Wow-64" Properties, on properties it says product name is "World of Warcraft" so I've tried every combination of these and nothing works. The code works if I put:
BringToFront("Notepad", "Untitled - Notepad");
So it works, I just don't know what I need for it to apply to WoW.
My code is:
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
private void BringToFront(string className, string CaptionName)
{
SetForegroundWindow(FindWindow(className, CaptionName));
}
private void Alert()
{
string title = "WoW Queue Alert: Message";
string message = "The Queue is ready to accept!";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
DialogResult result;
result = MessageBox.Show(new Form() { TopMost = true }, message, title, buttons, MessageBoxIcon.Information);
if (result == System.Windows.Forms.DialogResult.Yes)
{
BringToFront("World of Warcraft Retail", "World of Warcraft");
}
}
I really don't see anything special about WoW, and going by how the notepad example works the correct code should be:
BringToFront("World of Warcraft Retail", "World of Warcraft");
Being a fullscreen program should affect it, and I can't see there being anyway Blizzard has implemented something to stop this function.
Edit: I just set the ClassName to null and works, as caption name is just the window title. No idea what the ClassName is I tried everything I could find.
You should use some window inspection tool to inspect the window of World of Warcraft to get its Class Name and Window Name. A suggestion would be Spy++ if you installed Visual Studio (it comes along with it). It can help you detect the window's Class Name and Window Name, and Windows Messages sent to that window easily.
For example, the picture below shows all windows in current environment. Highlighted entry is a PowerPoint instance, which has Caption = "Microsoft PowerPoint" and ClassName = "PPTFrameClass".
Another approach will be writing a "always-inactivate" winform and call GetForegroundWindow from that form to get the window handle of the window you want.
To create an "always-inactivate" winform, simply override its CreateParams property:
protected override CreateParams CreateParams
{
get
{
CreateParams p = base.CreateParams;
p.ExStyle |= 0x8000000; // WS_EX_NOACTIVATE - requires Win 2000 or higher
return p;
}
}
By overriding this, the form won't get focus even on a mouse click. Therefore, you could create a button to trigger GetForegroundWindow(). Signature of the function in C# looks like below:
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
which returns the window handle of current fore ground window.
I have been struggling to hide another application from the taskbar from my application.
I have been using the SetWindowLong function in order to set/remove WS_EX_APPWINDOW on the extended style.
I have tried both setting and removing the property individually as well as taking the current WindowLong, and removing/adding it to that one, like so:
SetWindowLong(pMainWindow, GWL_EXSTYLE, GetWindowLong(pMainWindow) & WS_EX_APPWINDOW);
And tried removing it like so:
SetWindowLong(pMainWindow, GWL_EXSTYLE, GetWindowLong(pMainWindow) & ~WS_EX_APPWINDOW);
Also tried both those methods without first getting the window long. Here is my entire code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
[DllImport("User32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("User32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_HIDE = 0x00;
private const int SW_SHOW = 0x05;
private const int WS_EX_APPWINDOW = 0x40000;
private const int GWL_EXSTYLE = -0x14;
private void HideWindowFromTaskbar(IntPtr pMainWindow)
{
SetWindowLong(pMainWindow, GWL_EXSTYLE, ~WS_EX_APPWINDOW);
ShowWindow(pMainWindow, SW_HIDE);
ShowWindow(pMainWindow, SW_SHOW);
}
private void ButtonHide_Click(object sender, RoutedEventArgs e)
{
HideWindowFromTaskbar(Process.GetProcessesByName("notepad")[0].MainWindowHandle);
}
}
I have noticed changes in Spy++ looking at the properties. I had a bunch of different results, like WS_EX_APPWINDOW being added, but also randomly have other attributes disappearing, etc.
When looking at the messages, I also saw that it DID get messages like STYLE_CHANGED.
The rules for determining which windows have buttons on the taskbar are documented on MSDN. Raymond Chen gives the following summary of these rules:
There are some basic rules on which windows go into the taskbar. In
short:
If the WS_EX_APPWINDOW extended style is set, then it will show (when visible).
If the window is a top-level unowned window, then it will show (when visible).
Otherwise it doesn't show.
The fact that you are trying to modify a window in another app severely hampers you. You are removing the WS_EX_APPWINDOW extended style. This is not enough because the window in question will be a top-level unowned window (see bullet point 2). You cannot change the owner of a window once it has been created and since the window is controlled by another process you are pretty much stuck.
The only option remaining is to remove the WS_EX_APPWINDOW extended style and replace it with WS_EX_TOOLWINDOW. This will indeed get the window off the taskbar but it will change the appearance of the window:
The window is intended to be used as a floating toolbar. A tool window
has a title bar that is shorter than a normal title bar, and the
window title is drawn using a smaller font. A tool window does not
appear in the taskbar or in the dialog that appears when the user
presses ALT+TAB. If a tool window has a system menu, its icon is not
displayed on the title bar. However, you can display the system menu
by right-clicking or by typing ALT+SPACE.
Here is how I do this.
1. Create a window (hwndOwner) with WS_POPUP style and WS_EX_TOOLWINDOW
2. Call SetWindowLong(hwnd, GWL_HWNDPARENT, hwndOwner)
3. Call SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | ~WS_EX_APPWINDOW)
This will remove the target window from the taskbar and alt tab. This will work on every OS since WS2000 at least.
Now, David Heffernan pointed to the MSDN documentation which says you can't do this. Well, I don't know why it says that but its inaccurate. You can do this and INFACT the .NET Framework does this, just use .NET Reflector to examine the code for System.Windows.Forms.Form.Owner property - it uses SetWindowLong to transfer ownership, as often as you would like!
And for more evidence of incorrect MSDN documentation, look no further than the docs for the Owner property, it says "A top-level window cannot have an owner." it should be the exact opposite of this, ONLY top-level windows can have an owner!
private const int SW_HIDE = 0x00;
private const int SW_SHOW = 0x05;
private const int WS_EX_APPWINDOW = 0x40000;
private const int GWL_EXSTYLE = -0x14;
private const int WS_EX_TOOLWINDOW = 0x0080;
private static void HideAppinTaskBar()
{
var Handle = FindWindowByCaption(IntPtr.Zero, "Untitled - Notepad");
ShowWindow(Handle, SW_HIDE);
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) | WS_EX_TOOLWINDOW);
ShowWindow(Handle, SW_SHOW);
}
This works for me - tested with notepad. WinXp 32 Bit machine.
This works in WinUI3 with PInvoke.User32 nuget package on .NET 6.0:
Adapted from Alex's answer.
private void HideAppFromTaskBar(MainWindow window)
{
//Get the Window's HWND
var windowNative = window.As<IWindowNative>();
var windowHandle = windowNative.WindowHandle;
User32.ShowWindow(windowHandle, User32.WindowShowStyle.SW_HIDE);
var flags = (User32.SetWindowLongFlags)User32.GetWindowLong(windowHandle, User32.WindowLongIndexFlags.GWL_EXSTYLE);
User32.SetWindowLong(windowHandle, User32.WindowLongIndexFlags.GWL_EXSTYLE, flags | User32.SetWindowLongFlags.WS_EX_TOOLWINDOW);
User32.ShowWindow(windowHandle, User32.WindowShowStyle.SW_SHOW);
}
My application needs to display a popup near the cursor position of any active application when certain conditions are met (known by my app).
So I would like to display the form (without stealing the focus from the active application). I tried using ShowWindow with the SW_SHOWNOACTIVATE param but, this way, the my form is displayed under the currently active form.
How can I force my form to be displayed on top of any form active on the screen without stealing the input focus?
Thanks.
What you want is to use the TopMost property of the form you want to stay on top.
You need to add a bit of plumbing to the pop-up form so you can override the WM_ACTIVATE message:
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_ACTIVATE)
{
if (((int)m.WParam & 0xFFFF) != WA_INACTIVE)
{
if (m.LParam != IntPtr.Zero)
{
SetActiveWindow(m.LParam);
}
else
{
// Could not find sender, just in-activate it.
SetActiveWindow(IntPtr.Zero);
}
}
}
base.WndProc(ref m);
}
Make sure you add the following to your pop-up form as well:
[DllImport("user32.dll")]
private extern static IntPtr SetActiveWindow(IntPtr handle);
private const int WM_ACTIVATE = 6;
private const int WA_INACTIVE = 0;
You can use the pop-up form as you would any other, by calling Show() on it. You can make it the top-most window through the TopMost property, as with other forms.
My application is intended to work almost entirely through a Windows 7 taskbar item with the use of thumbnails and jump lists. I know I can easily create a Form and simply hide it, but this seems like overkill. Plus, I'd like to toy around with NativeWindow as much as possible, because I've never used it before.
Essentially, I have a class called RootWindow that derives from NativeWindow that will handle hotkeys and hopefully everything else. I don't need a visible window at all, but simply something to process window messages and provide a taskbar item that I can attach thumbnails and jump lists to.
Is there some kind of special CreateParams option I need to pass to CreateHandle? Or am I out of luck?
EDIT: Well, it was easier than I thought it would be, although it's not exactly what I want. Once I passed the NativeWindow's handle to the ShowWindow API, the taskbar item appeared. However, it also shows a window in the upper left corner of the screen. Is there some way to get rid of that window while still showing the taskbar item?
public class RootWindow : NativeWindow {
public const int SW_SHOWNOACTIVATE = 4;
[DllImport("User32.dll")]
private static extern int ShowWindow(IntPtr hWnd, short cmdShow);
public RootWindow() {
CreateHandle(new CreateParams());
ShowWindow(this.Handle, SW_SHOWNOACTIVATE);
}
}
The trick was to set the window's style to WS_POPUP.
const int WS_POPUP = unchecked((int)0x80000000);
const int SW_SHOWNOACTIVATE = 4;
CreateHandle(new CreateParams() {
Style = WS_POPUP
});
ShowWindow(Handle, SW_SHOWNOACTIVATE);
I also disabled Aero Peek for the window, since it's just for background work.
const int DWMNCRP_ENABLED = 2;
const int DWMWA_DISALLOW_PEEK = 11;
int policy = DWMNCRP_ENABLED;
DwmSetWindowAttribute(Handle, DWMWA_DISALLOW_PEEK, ref policy, sizeof(int));