如何创建C#的事件处理和PostMessage MFC的Windows消息() [英] How to create C# Event to handle MFC Windows message from PostMessage()

查看:353
本文介绍了如何创建C#的事件处理和PostMessage MFC的Windows消息()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个托管C ++ DLL用winsock。在接收它通过PostMessage的发送自定义消息到一个CWnd *()。从非托管C ++调用时能正常工作。 *与C ++类施工后使用此代码注册目标的CWnd:

  //注册一个窗口(CWnd的*)来当此UdpRetrySocket接收到有效的
//传入数据包接收的消息。
无效CUdpRetrySocket :: RegOnReceive(的CWnd * i_pOnReceiveWnd,UINT i_RecvMsgId = WM_USER_RECV_DATA_AVAIL)
{
m_pOnReceiveWnd = i_pOnReceiveWnd;
m_RecvMsgId = i_RecvMsgId;
}

下面是代码的帖子消息给的CWnd *

  //如果有一个处于等待进入的数据包,有一个窗口
//用于接收通知,然后发布一条消息给它注册。
如果(m_bInPktPending&放大器;&放大器;!m_pOnReceiveWnd = NULL)
m_pOnReceiveWnd-方式> PostMessage的(m_RecvMsgId,
(WPARAM)m_RecvSocket.LocalSockAddrIn()端口(),
( LPARAM)这个
);

现在我使用这个CUdpRetrySocket类与C#Windows窗体应用程序。
的问题:




  1. 从C#窗体类我如何获得一个CWnd *与我的C ++ CUdpRetrySocket类注册



    FOUND答案#1这里





  // C ++注册窗口方法
无效RegOnReceive(系统:: IntPtr的i_Hwnd)
{m_pOnReceiveWnd =的CWnd :: FromHandle((HWND)i_Hwnd.ToPointer()); }

//注册窗口方法
类MyForm的C#的来电:表
{
。 。 。
cmdDev.RegOnReceive(手柄);





  1. 我如何在我的C#窗口中创建一个事件捕捉到了这个定制的MFC风格的消息?


  2. 我需要的C#应用​​程序的最小化窗口时,即使处理数据包。如果是最小化将C#窗体窗口仍然收到这些消息?


  3. 有没有更好的方式来做到这一点?



解决方案

找到答案如何创建事件处理程序的这里结果
你只需重写Form.WndProc()虚方法,测试的具体的自定义消息ID并传递到处理的基类所有其他人。

 保护覆盖无效的WndProc(参考消息System.Windows.Forms.Message )
{
如果(message.Msg == MY_CUSTOM_WINDOW_MSG_ID)
{
//的过程事件HERE
}
base.WndProc(参考消息);
}



测试显示答案的最后一个问题是肯定的。发送消息和窗体最小化时,即使处理。


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:

// 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;
}

Here's the code that Posts the message to CWnd *

// 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 && m_pOnReceiveWnd != NULL)
    m_pOnReceiveWnd->PostMessage(m_RecvMsgId,
                                 (WPARAM)m_RecvSocket.LocalSockAddrIn().Port(),
                                 (LPARAM)this
                                );

I'm now using this CUdpRetrySocket class from a C# Windows Forms application. Questions:

  1. From C# Forms class how do I obtain a CWnd * to register with my C++ CUdpRetrySocket class

    FOUND ANSWER #1 HERE

// 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);

  1. How do I create an event in my C# window to capture this custom MFC style message?

  2. 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?

  3. Is there a better way to do this?

解决方案

Found answer to how to create event handler HERE
You simply override the Form.WndProc() virtual method, test for the specific custom message ID and pass all others on to the base class for processing.

protected override void WndProc(ref System.Windows.Forms.Message message)
{
    if (message.Msg == MY_CUSTOM_WINDOW_MSG_ID)
    {
        // PROCESS EVENT HERE
    }            
    base.WndProc(ref message);
}

Testing shows that the answer to the last question is YES. The message is sent and processed even when Form is minimized.

这篇关于如何创建C#的事件处理和PostMessage MFC的Windows消息()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆