public class IrcFormEx : Form
{
/* IRCFormEx form extension class
By: Jason James Newland and Ryan Alexander
©2009 - 2011 - KangaSoft Software
All Rights Reserved
NOTE: This class MUST be inheritted on ALL child forms of the MDI client for it to work
*/
#region Fields
[StructLayout(LayoutKind.Sequential)]
private struct Menuiteminfo
{
public int cbSize;
public int fMask;
public int fType;
public int fState;
public int wID;
public IntPtr hSubMenu;
private readonly IntPtr hbmpChecked;
private readonly IntPtr hbmpUnchecked;
private readonly IntPtr dwItemData;
public string dwTypeData;
public int cch;
private readonly IntPtr hbmpItem;
}
protected const int MfUnchecked = 0x0;
protected const int MfString = 0x0;
protected const int MfEnabled = 0x0;
protected const int MfGrayed = 0x1;
protected const int MfDisabled = 0x2;
protected const int MfChecked = 0x8;
protected const int MfByposition = 0x400;
protected const int MfSeparator = 0x800;
protected const int MfRemove = 0x1000;
protected const int MiimState = 0x1;
protected const int MiimId = 0x2;
protected const int MiimSubmenu = 0x4;
protected const int MiimType = 0x10;
protected const int MiimData = 0x20;
protected const int MiimBitmap = 0x80;
protected const int UmsgDonothide = 0x404;
protected const int UmsgOkhide = 0x405;
protected const int WmInitmenu = 0x116;
protected const int WmGetsysmenu = 0x313;
protected const int WmSyscommand = 0x112;
protected const int WmMdirestore = 0x223;
protected const int WmMdinext = 0x224;
protected const int WmNcactivate = 0x86;
protected const int ScMinimize = 0xf020;
protected bool DoNotDeactivate;
protected IntPtr SysMenu = IntPtr.Zero;
[DllImport("user32.dll", EntryPoint = "SendMessageA", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr GetTopWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool BringWindowToTop(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr windowHandle, int bReset);
[DllImport("user32.dll")]
private static extern IntPtr CreatePopupMenu();
[DllImport("user32.dll")]
private static extern bool InsertMenuItem(IntPtr hMenu, int uItem, bool fByPosition, ref Menuiteminfo lpmii);
[DllImport("user32.dll")]
private static extern bool GetMenuItemInfo(IntPtr hMenu, uint uItem, bool fByPosition, ref Menuiteminfo lpmii);
[DllImport("user32.dll")]
private static extern bool SetMenuItemInfo(IntPtr hMenu, uint uItem, bool fByPosition, [In] ref Menuiteminfo lpmii);
[DllImport("user32.dll")]
private static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
[DllImport("user32.dll")]
private static extern bool SetMenuDefaultItem(IntPtr hMenu, uint uItem, uint fByPos);
[DllImport("user32.dll")]
private static extern int GetMenuItemCount(IntPtr hMenu);
[DllImport("user32.dll")]
private static extern bool ModifyMenu(IntPtr hMenu, uint uPosition, uint uFlags, UIntPtr uIdNewItem, string lpNewItem);
[DllImport("user32.dll")]
private static extern uint GetMenuItemID(IntPtr hMenu, int nPos);
#endregion
#region System menu events
public event Action<IrcFormEx> SysMenuInit;
public event Action<int> SysMenuItemClick;
#endregion
#region No De-Activate property
public bool NoDeactivate
{
get { return DoNotDeactivate; }
set { DoNotDeactivate = value; }
}
#endregion
#region Show window without activation
public void ShowWithoutActive()
{
var topWindow = GetTopWindow(Parent.Handle);
SendMessage(topWindow, UmsgDonothide, IntPtr.Zero, IntPtr.Zero);
Show();
SendMessage(topWindow, UmsgOkhide, IntPtr.Zero, IntPtr.Zero);
var f = (IrcFormEx)FromHandle(topWindow);
if (IrcMain.Settings.WinMaximized)
{
f.WindowState = FormWindowState.Maximized;
}
if (f != null) { f.MyActivate(); }
}
#endregion
#region Restore a window to previous state (from minimize)
public void Restore()
{
SendMessage(Parent.Handle, WmMdirestore, Handle, IntPtr.Zero);
}
#endregion
#region Window Procedure (Subclass)
protected override void WndProc(ref Message m)
{
if (m.Msg == WmInitmenu)
{
/* System menu opening */
if (SysMenuInit != null)
{
SysMenuInit(this);
}
}
if (DoNotDeactivate && m.Msg == WmNcactivate && m.WParam == IntPtr.Zero)
{
m.WParam = (IntPtr)1;
BringWindowToTop(m.HWnd);
base.WndProc(ref m);
m.Result = IntPtr.Zero;
}
else switch (m.Msg)
{
case UmsgOkhide:
DoNotDeactivate = false;
break;
case UmsgDonothide:
DoNotDeactivate = true;
break;
default:
if (m.Msg == WmSyscommand && m.WParam.ToInt32() == ScMinimize)
{
/* Detects if a form is minimized and stops the MDI client from restoring down all child windows */
SendMessage(Parent.Handle, WmMdinext, IntPtr.Zero, IntPtr.Zero);
}
else if (m.Msg == WmSyscommand)
{
/* System menu */
var i = m.WParam.ToInt32();
if (i >= 0 && i <= 1000)
{
if (SysMenuItemClick != null) { SysMenuItemClick(m.WParam.ToInt32()); }
}
}
base.WndProc(ref m);
break;
}
}
#endregion
#region Re-activate original form
public void MyActivate()
{
/* This method raises the original forms Activated event if you
want other controls to be focused when the form regains focus */
OnActivated(new EventArgs());
}
#endregion
}