日期:2014-05-20  浏览次数:21083 次

C#中如何实现C++中的ON_MESSAGE函数啊?
小弟现在的C#项目中要实现一些硬件上的控制,SDK中提供的C++范例中有使用到ON_MESSAGE函数,我该如何实现啊?我对C++是一窍不通,盼各位赐教!   以下是相关代码和SDK中的说明:

/*==================映射OnMyCommand方法=========================*/
BEGIN_MESSAGE_MAP(CNewclientDlg,   CDialog)
ON_MESSAGE(WM_MYCOMMAND,OnMyCommand)
END_MESSAGE_MAP()

/*==================OnMyCommand方法=========================*/
void   CNewclientDlg::OnMyCommand(WPARAM   wParam,   LPARAM   lParam)
{
}

/*==================调用=========================*/
NET_DVR_SetDVRMessage(WM_MYCOMMAND,this-> m_hWnd);


/*==================SDK中的说明=========================*/
BOOL   NET_DVR_SetDVRMessage(UINT   nMessage,HWND   hWnd)
功能:设置接收硬盘录像机消息的窗口句柄
参数说明:
nMessage:   消息
hWnd:接收消息的窗口句柄
//异常类型
#define   EXCEPTION_EXCHANGE   0x8000   //用户交互时异常
#define   EXCEPTION_AUDIOEXCHANGE   0x8001   //语音对讲异常
#define   EXCEPTION_ALARM   0x8002   //报警异常
#define   EXCEPTION_PREVIEW   0x8003   //网络预览异常
#define   EXCEPTION_SERIAL   0x8004   //透明通道异常
#define   EXCEPTION_RECONNECT   0x8005   //预览时重连
注:消息处理函数如下:
void   OnMyCommand(WPARAM   wParam,   LPARAM   lParam)
{
char   tempbuf[256];
DWORD   dwType   =   wParam;   //异常类型
LONG   lIndex   =   lParam;   //出现异常的相应类型的句柄
ZeroMemory(tempbuf,256);
switch(dwType)
{
case   EXCEPTION_AUDIOEXCHANGE:   //语音对讲时网络异常
sprintf(tempbuf, "语音对讲时网络异常!!! ");
TRACE( "%s ",tempbuf);
//TODO:   关闭语音对讲
break;
case   EXCEPTION_ALARM:   //报警上传时网络异常
sprintf(tempbuf, "报警上传时网络异常!!! ");
TRACE( "%s ",tempbuf);
//TODO:   关闭报警上传
break;
case   EXCEPTION_PREVIEW:   //网络预览时异常
sprintf(tempbuf, "网络预览时网络异常!!! ");
TRACE( "%s ",tempbuf);
//TODO:   关闭网络预览
break;
case   EXCEPTION_SERIAL:   //透明通道传输时异常
sprintf(tempbuf, "透明通道传输时网络异常!!! ");
TRACE( "%s ",tempbuf);
//TODO:   关闭透明通道
break;
case   EXCEPTION_RECONNECT:   //预览时重连
break;
default:
break;
}
}

------解决方案--------------------
//不知道NetDVR是啥,我直接用SendMessage
//参考如下代码

private const int WM_USER = 0x0400;
private const int WM_MYCOMMAND = WM_USER + 10;

protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_MYCOMMAND:
OnMyCommand(m.WParam, m.LParam);
break;
}
base.WndProc(ref m);
}
public void OnMyCommand(IntPtr wParam, IntPtr lParam)
{
MessageBox.Show( "hello ");
}
[DllImport( "User32.DLL ")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int iParam);
private void button3_Click(object sender, EventArgs e)
{
SendMessage(Handle, WM_MYCOMMAND, 0, 0);
}