The source code for the old version of Paint.Net is available in openpdn Fork of Paint.NET 3.36.7
I tried to extract their methods from this source code into the most concise working example I could put together:
Eject Class:
internal static class Win32 { public const int WM_ACTIVATE = 0x006; public const int WM_ACTIVATEAPP = 0x01C; public const int WM_NCACTIVATE = 0x086; [DllImport("user32.dll", SetLastError = false)] internal static extern IntPtr SendMessageW(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] internal extern static bool PostMessageW(IntPtr handle, uint msg, IntPtr wParam, IntPtr lParam); }
Basic form:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool ignoreNcActivate = false; protected override void WndProc(ref Message m) { base.WndProc(ref m); switch (m.Msg) { case Win32.WM_NCACTIVATE: if (m.WParam == IntPtr.Zero) { if (ignoreNcActivate) { ignoreNcActivate = false; } else { Win32.SendMessageW(this.Handle, Win32.WM_NCACTIVATE, new IntPtr(1), IntPtr.Zero); } } break; case Win32.WM_ACTIVATEAPP: if (m.WParam == IntPtr.Zero) { Win32.PostMessageW(this.Handle, Win32.WM_NCACTIVATE, IntPtr.Zero, IntPtr.Zero); foreach (Form2 f in this.OwnedForms.OfType<Form2>()) { f.ForceActiveBar = false; Win32.PostMessageW(f.Handle, Win32.WM_NCACTIVATE, IntPtr.Zero, IntPtr.Zero); } ignoreNcActivate = true; } else if (m.WParam == new IntPtr(1)) { Win32.SendMessageW(this.Handle, Win32.WM_NCACTIVATE, new IntPtr(1), IntPtr.Zero); foreach (Form2 f in this.OwnedForms.OfType<Form2>()) { f.ForceActiveBar = true; Win32.SendMessageW(f.Handle, Win32.WM_NCACTIVATE, new IntPtr(1), IntPtr.Zero); } } break; } } protected override void OnShown(EventArgs e) { base.OnShown(e); Form2 f = new Form2(); f.Show(this); } }
Always active Form2 (if the application is not active):
public partial class Form2 : Form { internal bool ForceActiveBar { get; set; } public Form2() { InitializeComponent(); this.ShowInTaskbar = false; this.ForceActiveBar = true; } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == Win32.WM_NCACTIVATE) { if (this.ForceActiveBar && m.WParam == IntPtr.Zero) { Win32.SendMessageW(this.Handle, Win32.WM_NCACTIVATE, new IntPtr(1), IntPtr.Zero); } } } }
There is no need to set TopMost to true for Form2, since it must belong to the main form when it is displayed. In addition, Form2 is not a child form of MDI.