MFC中使用SDK个人经验总结.docx
MFC中使用SDK个人经验总结dlgdata.cpp Line 40 的问题 首先打开C->PROGRAM FILE(X86)-> Microsoft Visual Studio 11.0->VC->atlmfc->src->mfc->dlgdata.cpp打开,并在LINE40添加断点,调试找出nIDC的值,然后解决问题! 如果在resource.h中找不到对应ID,又或者更改过resource.h,出现异常,应删除debug文件夹,重新生成解决方案! 重新生成! 1. 项目属性中“C/C+”->“常规”->“附加包含目录” 添加SDK包中的头文件; 2. 项目属性中“链接器”->“常规”->“附加库目录” 把对应的文件夹位置添加进去; 3. 项目属性中“链接器”->"输入"->“附加依赖项” 添加相应的lib文件名。 4.在C*Dlg.h中添加头文件#include "GeneralDef.h" 5.在C*App的初始化程序InitInstance中添加SDK的初始化程序NET_DVR_Init; 6. 添加成员变量:播放句柄变量LONG m_lPlayHandle; 播放句柄用于判断是否播放正常以及作为NET_DVR_StopRealPlay等函数的句柄参数。 添加成员变量 DWORD m_fault_code; BOOL m_bIsLogin; BOOL m_bIsPlaying; STRU_DEVICE_INFO m_struDeviceInfo; 并在构造函数中对其初始化 m_lPlayHandle = -1; m_fault_code = 0; m_bIsLogin = FALSE; m_bIsPlaying = FALSE; 7.在C*Dlg.cpp中添加全局变量 char DeviceIp13 = "192.168.1.11" char UserName6 = "admin" char Password10 = "xde123456" WORD DeviceIpPort = 8000; 8. 单按钮启动 if(!m_bIsLogin) LONG lLoginID = NET_DVR_Login_V30(DeviceIp , DeviceIpPort, UserName, Password, NET_DVR_DEVICEINFO_V30 DeviceInfoTmp; memset(&DeviceInfoTmp,0,sizeof(NET_DVR_DEVICEINFO_V30); &DeviceInfoTmp); if(!m_bIsPlaying) m_lPlayHandle = NET_DVR_RealPlay_V40(m_struDeviceInfo.lLoginID, &ClientInfo, NULL, /预览播放成功返回非负数,失败返回-1 if(m_lPlayHandle >= 0) else CString strTemp1; strTemp1.LoadStringW(IDS_STRING_0_PLAY_STATUS); m_bIsPlaying = TRUE; CString strTemp; strTemp.LoadStringW(IDS_STRING_0_PLAY_BUTTON_STATUS2); GetDlgItem(IDC_0_PLAY)->SetWindowText(strTemp); this->SetTimer(Timer_Camera_Connect_Detection, 1000, NULL); NET_DVR_PREVIEWINFO ClientInfo = 0; ClientInfo.lChannel = 1; /选择预览的窗口句柄 ClientInfo.hPlayWnd = GetDlgItem(IDC_0_STATIC_CAMERA)->m_hWnd; if(lLoginID = -1) m_bIsLogin = TRUE; m_struDeviceInfo.lLoginID = lLoginID; m_struDeviceInfo.iDeviceChanNum = DeviceInfoTmp.byChanNum; m_struDeviceInfo.iIPChanNum = DeviceInfoTmp.byIPChanNum; m_struDeviceInfo.iStartChan = DeviceInfoTmp.byStartChan; m_struDeviceInfo.iIPStartChan = DeviceInfoTmp.byStartDChan; CString strTemp; strTemp.LoadStringW(IDS_STRING_0_LOGIN_STATUS); MessageBox(strTemp); return; NULL); else m_bIsPlaying = FALSE; CString strTemp; strTemp.LoadStringW(IDS_STRING_0_PLAY_BUTTON_STATUS1); GetDlgItem(IDC_0_PLAY)->SetWindowText(strTemp); NET_DVR_StopRealPlay(m_lPlayHandle); m_lPlayHandle = -1; GetDlgItem(IDC_0_STATIC_CAMERA)->Invalidate; CString strTemp4; strTemp4 = strTemp1 + L", " + strTemp3 + strTemp2; MessageBox(strTemp4); return; CString strTemp3; strTemp3.LoadStringW(IDS_STRING_0_PLAY_FAULT_CODE); CString strTemp2; m_fault_code = NET_DVR_GetLastError; strTemp2.Format(L"%d", m_fault_code); 云台控制 1.添加一个基于CButton的CPTZButton类, 2.在头文件中添加以下声明 #if !defined(AFX_PTZBUTTON_H_B3E99438_BBCE_4862_845F_D3AB668824A6_INCLUDED_) #define AFX_PTZBUTTON_H_B3E99438_BBCE_4862_845F_D3AB668824A6_INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif / _MSC_VER > 1000 并在头文件的结尾加上#endif 3.添加成员变量 int m_iSubBtnIndex; /功能按钮的索引号,添加按钮类时自动+1,用于对应相应功能 添加成员函数 void PTZControlAll(LONG lRealHandle,DWORD dwPTZCommand,DWORD dwStop ,int Speed); /用于实现各按钮的功能 4.添加全局变量int g_iPtzBtnIndex = 0; 在构造函数中对m_iSubBtnIndex进行初始化m_iSubBtnIndex = g_iPtzBtnIndex+; 5.通过类向导,添加消息处理函数OnLButtonDown和OnLButtonUp; 6.在PTZButton.cpp中添加*app的头文件,并添加声明 #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE = _FILE_; #endif 7.在按钮所在画面的类中添加两个函数,用于获得预览窗口的句柄以及云台移动的速度。 LONG CFrameCameraControl:GetPlayHandle int CFrameCameraControl:GetPTZSpeed return m_lPlayHandle; return 2; 8. 获得按钮所在画面的句柄,用来调用前面的两个函数。 在APP的.h和.cpp文件中添加全局变量CFrameCameraControl *g_pFrame_Camera_Control = NULL; 然后在dlg.cpp中创建画面后,把按钮所在画面的句柄赋值给g_pFrame_Camera_Control。 9. void CPTZButton:OnLButtonDown(UINT nFlags, CPoint point) / TODO: 在此添加消息处理程序代码和/或调用默认值 LONG lPlayHandle = g_pFrame_Camera_Control->GetPlayHandle; int iPTZSpeed = g_pFrame_Camera_Control->GetPTZSpeed; switch(m_iSubBtnIndex) case 0: /云台上 PTZControlAll(lPlayHandle,TILT_UP,0,iPTZSpeed); break; /云台下 PTZControlAll(lPlayHandle,TILT_DOWN,0,iPTZSpeed); break; /云台左 PTZControlAll(lPlayHandle,PAN_LEFT,0,iPTZSpeed); break; /云台右 PTZControlAll(lPlayHandle,PAN_RIGHT,0,iPTZSpeed); break; PTZControlAll(lPlayHandle,ZOOM_IN,0,iPTZSpeed); break; /调焦右 PTZControlAll(lPlayHandle,ZOOM_OUT,0,iPTZSpeed); break; /聚焦左 PTZControlAll(lPlayHandle,FOCUS_NEAR,0,iPTZSpeed); break; /聚焦右 PTZControlAll(lPlayHandle,FOCUS_FAR,0,iPTZSpeed); break; /光圈左 PTZControlAll(lPlayHandle,IRIS_OPEN,0,iPTZSpeed); break; /光圈右 PTZControlAll(lPlayHandle,IRIS_CLOSE,0,iPTZSpeed); break; case 1: case 2: case 3: case 4: /调焦左 case 5: case 6: case 7: case 8: case 9: case 10: /左上 PTZControlAll(lPlayHandle,UP_LEFT,0,iPTZSpeed); break; PTZControlAll(lPlayHandle,UP_RIGHT,0,iPTZSpeed); break; PTZControlAll(lPlayHandle,DOWN_LEFT,0,iPTZSpeed); break; PTZControlAll(lPlayHandle,DOWN_RIGHT,0,iPTZSpeed); break; case 11: /右上 case 12: /左下 case 13: /右下 default: break; CButton:OnLButtonDown(nFlags, point); void CPTZButton:OnLButtonUp(UINT nFlags, CPoint point) / TODO: 在此添加消息处理程序代码和/或调用默认值 LONG lPlayHandle = g_pFrame_Camera_Control->GetPlayHandle; int iPTZSpeed = g_pFrame_Camera_Control->GetPTZSpeed; switch(m_iSubBtnIndex) case 0: /云台上 PTZControlAll(lPlayHandle,TILT_UP,1,iPTZSpeed); break; /云台下 PTZControlAll(lPlayHandle,TILT_DOWN,1,iPTZSpeed); break; /云台左 PTZControlAll(lPlayHandle,PAN_LEFT,1,iPTZSpeed); break; /云台右 PTZControlAll(lPlayHandle,PAN_RIGHT,1,iPTZSpeed); break; PTZControlAll(lPlayHandle,ZOOM_IN,1,iPTZSpeed); break; case 1: case 2: case 3: case 4: /调焦左 / / / case 5: /调焦右 PTZControlAll(lPlayHandle,ZOOM_OUT,1,iPTZSpeed); break; /聚焦左 PTZControlAll(lPlayHandle,FOCUS_NEAR,1,iPTZSpeed); break; /聚焦右 PTZControlAll(lPlayHandle,FOCUS_FAR,1,iPTZSpeed); break; /光圈左 PTZControlAll(lPlayHandle,IRIS_OPEN,1,iPTZSpeed); break; /光圈右 PTZControlAll(lPlayHandle,IRIS_CLOSE,1,iPTZSpeed); break; PTZControlAll(lPlayHandle,UP_LEFT,1,iPTZSpeed); break; PTZControlAll(lPlayHandle,UP_RIGHT,1,iPTZSpeed); break; PTZControlAll(lPlayHandle,DOWN_LEFT,1,iPTZSpeed); break; PTZControlAll(lPlayHandle,DOWN_RIGHT,1,iPTZSpeed); break; PTZControlAll(lPlayHandle,PAN_AUTO,1,iPTZSpeed); break; break; case 6: case 7: case 8: case 9: case 10: /左上 case 11: /右上 case 12: /左下 case 13: /右下 case 14: /自动 default: CButton:OnLButtonUp(nFlags, point); void CPTZButton:PTZControlAll(LONG lRealHandle,DWORD dwPTZCommand,DWORD dwStop ,int Speed) if(lRealHandle>=0) BOOL ret = FALSE; if(Speed>=1) else ret = NET_DVR_PTZControl(lRealHandle,dwPTZCommand,dwStop); if(!ret) AfxMessageBox(L"云台控制失败!"); return; ret = NET_DVR_PTZControlWithSpeed(lRealHandle,dwPTZCommand,dwStop,Speed); if(!ret) AfxMessageBox(L"云台控制失败!"); return; 10 按钮所在画面中添加按钮成员变量,变量类型为CPTZButton,注意,这里按钮添加的顺序应安装CPTZButton类中switch的顺序添加。