Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 21st, 2012  |  syntax: C#  |  size: 6.94 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class IrcFormEx : Form
  2.     {
  3.         /* IRCFormEx form extension class
  4.            By: Jason James Newland and Ryan Alexander
  5.            ©2009 - 2011 - KangaSoft Software
  6.            All Rights Reserved
  7.            
  8.            NOTE: This class MUST be inheritted on ALL child forms of the MDI client for it to work
  9.          */
  10.             #region Fields
  11.         [StructLayout(LayoutKind.Sequential)]
  12.         private struct Menuiteminfo
  13.         {
  14.             public int cbSize;
  15.             public int fMask;
  16.             public int fType;
  17.             public int fState;
  18.             public int wID;
  19.             public IntPtr hSubMenu;
  20.             private readonly IntPtr hbmpChecked;
  21.             private readonly IntPtr hbmpUnchecked;
  22.             private readonly IntPtr dwItemData;
  23.             public string dwTypeData;
  24.             public int cch;
  25.             private readonly IntPtr hbmpItem;
  26.         }
  27.  
  28.         protected const int MfUnchecked = 0x0;
  29.         protected const int MfString = 0x0;
  30.         protected const int MfEnabled = 0x0;
  31.         protected const int MfGrayed = 0x1;
  32.         protected const int MfDisabled = 0x2;
  33.         protected const int MfChecked = 0x8;
  34.         protected const int MfByposition = 0x400;
  35.         protected const int MfSeparator = 0x800;
  36.         protected const int MfRemove = 0x1000;
  37.  
  38.         protected const int MiimState = 0x1;
  39.         protected const int MiimId = 0x2;
  40.         protected const int MiimSubmenu = 0x4;
  41.         protected const int MiimType = 0x10;
  42.         protected const int MiimData = 0x20;
  43.         protected const int MiimBitmap = 0x80;
  44.  
  45.         protected const int UmsgDonothide = 0x404;
  46.         protected const int UmsgOkhide = 0x405;
  47.  
  48.         protected const int WmInitmenu = 0x116;
  49.         protected const int WmGetsysmenu = 0x313;
  50.         protected const int WmSyscommand = 0x112;
  51.         protected const int WmMdirestore = 0x223;
  52.         protected const int WmMdinext = 0x224;
  53.         protected const int WmNcactivate = 0x86;
  54.  
  55.         protected const int ScMinimize = 0xf020;
  56.  
  57.             protected bool DoNotDeactivate;
  58.         protected IntPtr SysMenu = IntPtr.Zero;
  59.  
  60.         [DllImport("user32.dll", EntryPoint = "SendMessageA", CharSet = CharSet.Auto)]
  61.         private static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
  62.  
  63.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  64.         private static extern IntPtr GetTopWindow(IntPtr hWnd);
  65.  
  66.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  67.         private static extern bool BringWindowToTop(IntPtr hWnd);
  68.  
  69.         [DllImport("user32.dll")]
  70.         private static extern IntPtr GetSystemMenu(IntPtr windowHandle, int bReset);
  71.  
  72.         [DllImport("user32.dll")]
  73.         private static extern IntPtr CreatePopupMenu();
  74.  
  75.         [DllImport("user32.dll")]
  76.         private static extern bool InsertMenuItem(IntPtr hMenu, int uItem, bool fByPosition, ref Menuiteminfo lpmii);
  77.  
  78.         [DllImport("user32.dll")]
  79.         private static extern bool GetMenuItemInfo(IntPtr hMenu, uint uItem, bool fByPosition, ref Menuiteminfo lpmii);
  80.  
  81.         [DllImport("user32.dll")]
  82.         private static extern bool SetMenuItemInfo(IntPtr hMenu, uint uItem, bool fByPosition, [In] ref Menuiteminfo lpmii);
  83.  
  84.         [DllImport("user32.dll")]
  85.         private static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
  86.  
  87.         [DllImport("user32.dll")]
  88.         private static extern bool SetMenuDefaultItem(IntPtr hMenu, uint uItem, uint fByPos);
  89.  
  90.         [DllImport("user32.dll")]
  91.         private static extern int GetMenuItemCount(IntPtr hMenu);
  92.  
  93.         [DllImport("user32.dll")]
  94.         private static extern bool ModifyMenu(IntPtr hMenu, uint uPosition, uint uFlags, UIntPtr uIdNewItem, string lpNewItem);
  95.  
  96.         [DllImport("user32.dll")]
  97.         private static extern uint GetMenuItemID(IntPtr hMenu, int nPos);
  98.             #endregion
  99.  
  100.         #region System menu events
  101.         public event Action<IrcFormEx> SysMenuInit;                
  102.         public event Action<int> SysMenuItemClick;
  103.         #endregion
  104.  
  105.             #region No De-Activate property
  106.             public bool NoDeactivate
  107.         {
  108.                     get { return DoNotDeactivate; }
  109.                     set { DoNotDeactivate = value; }
  110.             }
  111.             #endregion
  112.  
  113.             #region Show window without activation
  114.             public void ShowWithoutActive()
  115.             {
  116.                 var topWindow = GetTopWindow(Parent.Handle);
  117.                         SendMessage(topWindow, UmsgDonothide, IntPtr.Zero, IntPtr.Zero);
  118.                         Show();
  119.                         SendMessage(topWindow, UmsgOkhide, IntPtr.Zero, IntPtr.Zero);
  120.                         var f = (IrcFormEx)FromHandle(topWindow);
  121.             if (IrcMain.Settings.WinMaximized)
  122.             {
  123.                 f.WindowState = FormWindowState.Maximized;
  124.             }
  125.             if (f != null) { f.MyActivate(); }
  126.             }
  127.             #endregion
  128.  
  129.             #region Restore a window to previous state (from minimize)
  130.             public void Restore()
  131.             {
  132.                 SendMessage(Parent.Handle, WmMdirestore, Handle, IntPtr.Zero);
  133.             }
  134.             #endregion
  135.  
  136.             #region Window Procedure (Subclass)
  137.             protected override void WndProc(ref Message m)
  138.             {
  139.             if (m.Msg == WmInitmenu)
  140.             {
  141.                 /* System menu opening */
  142.                 if (SysMenuInit != null)
  143.                 {
  144.                     SysMenuInit(this);
  145.                 }
  146.             }
  147.                     if (DoNotDeactivate && m.Msg == WmNcactivate && m.WParam == IntPtr.Zero)
  148.             {
  149.                             m.WParam = (IntPtr)1;
  150.                                 BringWindowToTop(m.HWnd);
  151.                                 base.WndProc(ref m);
  152.                                 m.Result = IntPtr.Zero;
  153.                         }
  154.             else switch (m.Msg)
  155.             {
  156.                 case UmsgOkhide:
  157.                     DoNotDeactivate = false;
  158.                     break;
  159.                 case UmsgDonothide:
  160.                     DoNotDeactivate = true;
  161.                     break;
  162.                 default:
  163.                     if (m.Msg == WmSyscommand && m.WParam.ToInt32() == ScMinimize)
  164.                     {
  165.                         /* Detects if a form is minimized and stops the MDI client from restoring down all child windows */
  166.                         SendMessage(Parent.Handle, WmMdinext, IntPtr.Zero, IntPtr.Zero);
  167.                     }
  168.                     else if (m.Msg == WmSyscommand)
  169.                     {
  170.                         /* System menu */
  171.                         var i = m.WParam.ToInt32();
  172.                         if (i >= 0 && i <= 1000)
  173.                         {
  174.                             if (SysMenuItemClick != null) { SysMenuItemClick(m.WParam.ToInt32()); }
  175.                         }
  176.                     }
  177.                     base.WndProc(ref m);
  178.                     break;
  179.             }
  180.             }
  181.             #endregion
  182.  
  183.         #region Re-activate original form
  184.         public void MyActivate()
  185.             {
  186.                 /* This method raises the original forms Activated event if you
  187.                want other controls to be focused when the form regains focus */
  188.                         OnActivated(new EventArgs());
  189.             }
  190.             #endregion
  191. }