How to listen to Windows broadcast messages in .NET?

I have Object(i.e. not a form) that wants to listen to broadcast messages from Windows, for example:

What is the .NET mechanism for installing a window without WinForm that can listen to broadcast messages?

i.e. Have a class WindowsListener?

Bonus Chat

In earlier times, in other development environments, the infrastructure provided : AllocateHwnd

HWND listener = AllocateHWnd(ListenerWindowProc);

where ListenerWindowProcwas my window procedure method:

private void ListenerWindowProc(ref Message msg)
{
    switch (msg.msg)
    {
       case WM_SETTINGCHANGE: 
       {
          ...
       }
       break;
       case WM_POWERBROADCAST:
       {
          ...
       }
       break;
       case WM_THEMECHANGED: 
       {
          ...
       }
       break;
       ...
   }
   DefWindowProc(msg);
}

Secret sauce is a function AllocateHwnd:

Pseudo Code:

public static HWND AllocateHWnd(WndMethod method)
{
   HWND result;
   WNDCLASS tempClass;
   Boolean classRegistered;

   UtilWindowClass.hInstance := HInstance;
   Boolean classRegistered = GetClassInfo(HInstance, UtilWindowClass.lpszClassName, tempClass);
   if (!classRegistered || (tempClass.lpfnWndProc != @DefWindowProc)
   {
      if classRegistered
      {
         UnregisterClass(utilWindowClass.lpszClassName, HInstance);
         RegisterClass(utilWindowClass);
      }
      result = CreateWindowEx(WS_EX_TOOLWINDOW, UtilWindowClass.lpszClassName,
            '', WS_POPUP, 0, 0, 0, 0, 0, 0, HInstance, null);
      if (!Method != null)
         SetWindowLong(result, GWL_WNDPROC, UInt32(MakeObjectInstance(Method)));
   }
}

With much more secret code related to UtilWindowClass.

And when you are done, you are DeallocateHwnd :

DeallocateHwnd(listenerWindow);

- .NET, WM_SETTINGCHANGE .NET. , Reflector WM_SETTINGCHANGE; , , 0x001A.

:

. . , , , . Windows ( )

:

public class FrobbingGrobber: IDisposable
{
    private IntPtr hwnd = IntPtr.Zero;

    public FrobbingGrobber
    {
       _hwnd = AllocateHwnd(listenerWindowProc);
    }

    protected virtual void listenerWindowProc(ref Message msg)
    {
       switch (msg.msg)
       {
          case WM_DwmColorizationColorChanged, WM_DwmCompositionChanged: 
          {
              UpdateColorizationColor();
          }
          break;
       }
       DefWindowProc(msg);
    }

    public void UpdateColorizationColor()
    {
        NativeMethods.DwmGetColorizationColorParameters_UndocumentedExport137(ref _color);
    }

    public void Dispose()
    {
        Dispose(true);
    }

    protected void Dispose(Boolean safeToDisposeManagedObjects)
    {
       if (_hwnd != 0)
       {
          DeallocateHwnd(_hwnd);      
          _hwnd = 0;
       }

       if (safeToDisposeManagedObjects)
          GC.SuppressFinalize(this);
    }

    public ~FrobbingGrobber
    {
       //If the user forgot to call Dispose i could (correctly) leak a handle, 
       //or i could fix their mistake for them
       Dispose(false);
    }
+5
1

, : . , . WM_POWERBROADCAST Microsoft.Win32.SystemEvents.PowerModeChanged . WM_SETTINGCHANGED Microsoft.Win32.SystemEvents.UserPreferenceChanged.

, , , , , WndProc , .

NativeWindow , . , Form, CreateWindowEx . WS_VISIBLE, , .

.NET Framework . , TimerNativeWindow , System.Windows.Forms.Timer. Reflector, . , , , , , , . SystemEvents ( ) .

, (HWND_MESSAGE), . TimerNativeWindow, , , , !

+7

All Articles