title: How to create C# Event to handle MFC Windows message from PostMessage()
body: <p>I have a managed C++ DLL using WINSOCK.  On receive it sends a custom message to a CWnd * via PostMessage().  This works fine when called from unmanaged C++.  The target CWnd * is registered with the C++ class after construction using this code:  </p>
  
  <pre><code>// Registers a window (CWnd *) to receive a message when a valid
  // incoming data packet is received on this UdpRetrySocket. 
  void CUdpRetrySocket::RegOnReceive(CWnd *i_pOnReceiveWnd, UINT i_RecvMsgId = WM_USER_RECV_DATA_AVAIL)
  {
      m_pOnReceiveWnd = i_pOnReceiveWnd;
      m_RecvMsgId = i_RecvMsgId;
  }
  </code></pre>
  
  <p>Here's the code that Posts the message to CWnd *</p>
  
  <pre><code>// If there is a pending incoming packet and there is a window
  // registered for receive notification then post a message to it.
  if (m_bInPktPending &amp;&amp; m_pOnReceiveWnd != NULL)
      m_pOnReceiveWnd-&gt;PostMessage(m_RecvMsgId,
                                   (WPARAM)m_RecvSocket.LocalSockAddrIn().Port(),
                                   (LPARAM)this
                                  );
  </code></pre>
  
  <p>I'm now using this CUdpRetrySocket class from a C# Windows Forms application. 
  Questions:</p>
  
  <ol>
  <li><p>From C# Forms class how do I obtain a CWnd * to register with my C++ CUdpRetrySocket class</p>
  
  <p><a href="http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/bdfb2783-4111-4ebf-b9ed-218200c09d03" rel="nofollow">FOUND ANSWER #1 HERE</a></p></li>
  </ol>
  
  <blockquote>
  <pre><code>// C++ Register Window Method
  void RegOnReceive(System::IntPtr i_Hwnd)
     { m_pOnReceiveWnd = CWnd::FromHandle((HWND)i_Hwnd.ToPointer()); }
  
  // C# Caller of Register Window Method
  class MyForm : Form
  {
      . . .
      cmdDev.RegOnReceive(Handle);
  </code></pre>
  </blockquote>
  
  <ol>
  <li><p>How do I create an event in my C# window to capture this custom MFC style message?</p></li>
  <li><p>I need the C# app to process packets even when window is minimized.  Will the C# Forms Window still receive these messages if it is minimized?</p></li>
  <li><p>Is there a better way to do this?</p></li>
  </ol>