1、创建消息结构体:
struct WindowMsg
{
QString msg;
};
2、发送消息函数:
void PostMsg2Window(QString msg)
{
current_date_time=QDateTime::currentDateTime();
QString current_date =current_date_time.toString("yyyy-MM-dd hh:mm:ss");
HWND hWnd = ::FindWindow(NULL, L"海康报警监听服务");
WindowMsg *struWinMsg= new WindowMsg;
struWinMsg->msg=current_date + " " + msg;
::PostMessage(hWnd,(WM_USER+1),(WPARAM)struWinMsg,sizeof(struWinMsg));
}
3、窗体接收消息
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
MSG *msg = static_cast<MSG*>(message);
if (msg->message == WM_USER+1)
{
WindowMsg * struWinMsg=(WindowMsg*)msg->wParam;
ui->plainTextEdit->appendPlainText(struWinMsg->msg);
delete struWinMsg;
}
return false;
}
4、在回调内调用,信息发送给窗口:
void CALLBACK MessageCallback(LONG lCommand, NET_DVR_ALARMER *pAlarmer, char *pAlarmInfo, DWORD dwBufLen, void* pUser)
{
LONG cmd= lCommand;
char* ip_add= pAlarmer->sDeviceIP;
char* d_name= pAlarmer->sDeviceName;
byte* sn= pAlarmer->sSerialNumber;
DWORD l= dwBufLen;
void* p= pUser;
NET_ITS_PLATE_RESULT struPlateResult;
memcpy(&struPlateResult, pAlarmInfo, sizeof(NET_ITS_PLATE_RESULT));
QString str_ip(ip_add);
PostMsg2Window("设备IP地址:"+str_ip);
QString str_name(d_name);
PostMsg2Window("设备名称:"+str_name);
QString str_sn((char*)sn);
PostMsg2Window("设备序列号:"+str_sn);
switch(cmd)
{
case COMM_ITS_PLATE_RESULT://交通抓拍结果(车辆、车牌识别及抓拍图片)上传
{
NET_DVR_PLATE_INFO struPlateInfo=struPlateResult.struPlateInfo;
char* license= struPlateInfo.sLicense;
QString str_license(license);
PostMsg2Window("车牌号码:"+str_license);
}
break;
default:
break;
}
}
