PeekMessage

User-Defined Types C#:

[StructLayout(LayoutKind.Sequential)]
  public struct NativeMessage 
  {
      public IntPtr handle;
      public uint msg;
      public IntPtr wParam;
      public IntPtr lParam;
      public uint time;
      public System.Drawing.Point p;
  }

User-Defined Types VB.NET:

Public Structure NativeMessage
    Public handle As IntPtr
    Public msg As UInteger
    Public wParam As IntPtr
    Public lParam As IntPtr
    Public time As UInteger
    Public p As System.Drawing.Point
    End Structure

C# Signature:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PeekMessage(out NativeMessage lpMsg, HandleRef hWnd, uint wMsgFilterMin,
   uint wMsgFilterMax, uint wRemoveMsg);

VB.NET Signature:

ByVal filterMin As UInteger, ByVal filterMax As UInteger, ByVal flags As UInteger) _
    As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

Tips & Tricks 2:

NativeMessage msg = new NativeMessage();
GCHandle handle = GCHandle.Alloc(msg);
bool foundMessage = PeekMessage(ref msg, hWnd, 0, 0, 0);
handle.Free();

Sample Code:

NativeMessage message = new NativeMessage();
PeekMessage(
    out message, 
    new HandleRef(myWindow,myWindow.hWnd),
    0,
    0,
    PM_REMOVE);

C# Signature:

[SuppressUnmanagedCodeSecurity]
  [return: MarshalAs(UnmanagedType.Bool)]
  [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  public static extern bool PeekMessage(out NativeMessage message, IntPtr handle, uint filterMin, uint filterMax, uint flags);

Sample Code:

NativeMessage msg;
  bool foundMessage = PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);

Última actualización