Paste Search Dynamic
Recent pastes
WinEventProcDelegate
  1. using system;
  2. using system.Collections.Generic;
  3. using system.ComponentModel;
  4. using system.Data;
  5. using system.Drawing;
  6. using system.Linq;
  7. using system.Runtime.InteropServices;
  8. using system.Text;
  9. using system.Threading.Tasks;
  10. using system.Windows.Forms;
  11.  
  12. namespace CSharp_SetWinEventHook
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         // See WinUser.h for more values
  17.         public const uint EVENT_OBJECT_CREATE = 0x8000;
  18.         public const uint EVENT_OBJECT_STATECHANGE = 0x800A;
  19.         public const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
  20.         public const uint EVENT_OBJECT_FOCUS = 0x8005;
  21.         public const uint EVENT_OBJECT_LOCATIONCHANGE =0x800B;
  22.         public const uint EVENT_OBJECT_NAMECHANGE = 0x800C;  // hwnd + ID + idChild is item w/ name change
  23.  
  24.         public const uint EVENT_MIN = 0x00000001;
  25.         public const uint EVENT_MAX = 0x7FFFFFFF;
  26.  
  27.         public const uint WINEVENT_OUTOFCONTEXT = 0x0000; // Events are ASYNC
  28.  
  29.         public const int OBJID_WINDOW = (0x00000000);
  30.         public const int OBJID_SYSMENU = unchecked((int)0xFFFFFFFF);
  31.         public const int OBJID_TITLEBAR = unchecked((int)0xFFFFFFFE);
  32.         public const int OBJID_MENU = unchecked((int)0xFFFFFFFD);
  33.         public const int OBJID_CLIENT = unchecked((int)0xFFFFFFFC);
  34.         public const int OBJID_VSCROLL = unchecked((int)0xFFFFFFFB);
  35.         public const int OBJID_HSCROLL = unchecked((int)0xFFFFFFFA);
  36.         public const int OBJID_SIZEGRIP = unchecked((int)0xFFFFFFF9);
  37.         public const int OBJID_CARET = unchecked((int)0xFFFFFFF8);
  38.         public const int OBJID_CURSOR = unchecked((int)0xFFFFFFF7);
  39.         public const int OBJID_ALERT = unchecked((int)0xFFFFFFF6);
  40.         public const int OBJID_SOUND = unchecked((int)0xFFFFFFF5);
  41.         public const int OBJID_QUERYCLASSNAMEIDX = unchecked((int)0xFFFFFFF4);
  42.         public const int OBJID_NATIVEOM = unchecked((int)0xFFFFFFF0);
  43.  
  44.         [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  45.         static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventProcDelegate pfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
  46.  
  47.         [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  48.         public static extern bool UnhookWinEvent(IntPtr hWinEventHook);
  49.  
  50.         delegate void WinEventProcDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
  51.  
  52.         [DllImport("User32.dll", SetLastError = true)]
  53.         public static extern bool IsZoomed(IntPtr hWnd);
  54.  
  55.         [DllImport("User32.dll", SetLastError = true)]
  56.         public static extern bool IsIconic(IntPtr hWnd);
  57.  
  58.         [DllImport("User32.dll", SetLastError = true)]
  59.         public static extern bool IsWindowVisible(IntPtr hWnd);
  60.  
  61.         [DllImport("User32.dll", SetLastError = true)]
  62.         public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
  63.  
  64.         [StructLayout(LayoutKind.Sequential)]
  65.         public struct WINDOWPLACEMENT
  66.         {
  67.             public uint length;
  68.             public uint flags;
  69.             public uint showCmd;
  70.             public system.Drawing.Point ptMinPosition;
  71.             public system.Drawing.Point ptMaxPosition;
  72.             public RECT rcNormalPosition;
  73.         }
  74.  
  75.         [StructLayout(LayoutKind.Sequential)]
  76.         public struct RECT
  77.         {
  78.             public int left;
  79.             public int top;
  80.             public int right;
  81.             public int bottom;
  82.             public RECT(int Left, int Top, int Right, int Bottom)
  83.             {
  84.                 left = Left;
  85.                 top = Top;
  86.                 right = Right;
  87.                 bottom = Bottom;
  88.             }
  89.         }
  90.  
  91.         public const int SW_HIDE = 0;
  92.         public const int SW_SHOWNORMAL = 1;
  93.         public const int SW_NORMAL = 1;
  94.         public const int SW_SHOWMINIMIZED = 2;
  95.         public const int SW_SHOWMAXIMIZED = 3;
  96.         public const int SW_MAXIMIZE = 3;
  97.         public const int SW_SHOWNOACTIVATE = 4;
  98.         public const int SW_SHOW = 5;
  99.         public const int SW_MINIMIZE = 6;
  100.         public const int SW_SHOWMINNOACTIVE = 7;
  101.         public const int SW_SHOWNA = 8;
  102.         public const int SW_RESTORE = 9;
  103.         public const int SW_SHOWDEFAULT = 10;
  104.         public const int SW_FORCEMINIMIZE = 11;
  105.         public const int SW_MAX = 11;
  106.    
  107.         WinEventProcDelegate procDelegate;
  108.         IntPtr hEventHook;
  109.  
  110.         private system.Windows.Forms.ListBox listBox1;
  111.         public Form1()
  112.         {
  113.             InitializeComponent();        
  114.  
  115.             this.listBox1 = new system.Windows.Forms.ListBox();
  116.             this.listBox1.FormattingEnabled = true;
  117.             this.listBox1.Location = new system.Drawing.Point(10, 10);
  118.             this.listBox1.Name = "listBox1";
  119.             this.listBox1.Size = new system.Drawing.Size(380, 200);
  120.             this.Controls.Add(this.listBox1);
  121.  
  122.             this.ClientSize = new system.Drawing.Size(400, 280);
  123.             CenterToScreen();
  124.         }
  125.            
  126.         int nCpt = 0;
  127.         void WinEventProc(IntPtr hWinEventHook, uint eventConst, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
  128.         {
  129.             if (idObject == OBJID_WINDOW)
  130.             {
  131.                // string sMessage = string.Format("[{0}] hWnd = {1:X} - eventConst = {2:X}", nCpt, hWnd.ToInt32(), eventConst);
  132.                // Console.WriteLine(sMessage);
  133.                 listBox1.Items.Insert(0, string.Format("[{0}] hWnd = {1:X} - eventConst = {2:X}", nCpt, hWnd.ToInt32(), eventConst));
  134.                 nCpt++;
  135.                 if (eventConst == EVENT_OBJECT_LOCATIONCHANGE)
  136.                 {
  137.                     //if (IsZoomed(hWnd))
  138.                     //{                      
  139.                     //    Console.Beep(1000, 10);                      
  140.                     //}
  141.                     //if (IsIconic(hWnd))
  142.                     //{
  143.                     //    Console.Beep(3000, 10);
  144.                     //}
  145.                     WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
  146.                     GetWindowPlacement(hWnd, ref wp);
  147.                     if (wp.showCmd == SW_SHOWMAXIMIZED)
  148.                         Console.Beep(1000, 10);
  149.                     else if (wp.showCmd == SW_SHOWMINIMIZED)
  150.                         Console.Beep(3000, 10);
  151.                     //else if (wp.showCmd == SW_RESTORE)
  152.                     //    Console.Beep(5000, 10);  
  153.                 }
  154.             }
  155.         }
  156.  
  157.         private void Form1_Load(object sender, EventArgs e)
  158.         {
  159.             procDelegate = new WinEventProcDelegate(WinEventProc);  
  160.             hEventHook = SetWinEventHook(EVENT_MIN, EVENT_MAX, IntPtr.Zero, procDelegate, 0, 0, WINEVENT_OUTOFCONTEXT);
  161.         }
  162.     }
  163. }
Parsed in 0.030 seconds