MediaClientDlg.cpp
上传用户:hhs829
上传日期:2022-06-17
资源大小:586k
文件大小:6k
源码类别:

DirextX编程

开发平台:

Visual C++

  1. // MediaClientDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MediaClient.h"
  5. #include "MediaClientDlg.h"
  6. #include "CFilterGraph.h"
  7. #include "CMediaSocketClient.h"
  8. #include "CDataAdmin.h"
  9. #include "CLocalMachine.h"
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CMediaClientDlg dialog
  17. CMediaClientDlg::CMediaClientDlg(CWnd* pParent /*=NULL*/)
  18. : CDialog(CMediaClientDlg::IDD, pParent)
  19. {
  20. //{{AFX_DATA_INIT(CMediaClientDlg)
  21. // NOTE: the ClassWizard will add member initialization here
  22. //}}AFX_DATA_INIT
  23. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  24. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  25. m_pGraph      = NULL;
  26. m_pReceiver   = NULL;
  27. m_bConnected  = false;
  28. m_pDataList   = NULL;
  29. }
  30. void CMediaClientDlg::DoDataExchange(CDataExchange* pDX)
  31. {
  32. CDialog::DoDataExchange(pDX);
  33. //{{AFX_DATA_MAP(CMediaClientDlg)
  34. DDX_Control(pDX, IDC_STATIC_HOSTIP, m_hostIP);
  35. DDX_Control(pDX, IDC_STATIC_HOST, m_host);
  36. DDX_Control(pDX, IDC_STATIC_COUNT, m_Counter);
  37. DDX_Control(pDX, IDC_VIDEOWND, m_videoWnd);
  38. DDX_Control(pDX, IDC_BUTTON_TRANS, m_btnTrans);
  39. DDX_Control(pDX, IDC_IPADDRESS1, m_ipCtrl);
  40. DDX_Control(pDX, IDC_BUTTON_CONNECT, m_btnConnect);
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CMediaClientDlg, CDialog)
  44. //{{AFX_MSG_MAP(CMediaClientDlg)
  45. ON_WM_PAINT()
  46. ON_WM_QUERYDRAGICON()
  47. ON_WM_DESTROY()
  48. ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
  49. ON_BN_CLICKED(IDC_BUTTON_TRANS, OnButtonTrans)
  50. //}}AFX_MSG_MAP
  51. END_MESSAGE_MAP()
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CMediaClientDlg message handlers
  54. BOOL CMediaClientDlg::OnInitDialog()
  55. {
  56. CDialog::OnInitDialog();
  57. // Set the icon for this dialog.  The framework does this automatically
  58. //  when the application's main window is not a dialog
  59. SetIcon(m_hIcon, TRUE); // Set big icon
  60. SetIcon(m_hIcon, FALSE); // Set small icon
  61. // COM initialize...
  62. if (FAILED(CoInitialize(NULL)))
  63.     {
  64. AfxMessageBox("COM初始化失败!");
  65. return FALSE;
  66. }
  67. char  host[255], hostIP[255];
  68. CLocalMachine  myhost;
  69. myhost.GetIPAddress(hostIP, host);
  70. m_host.SetWindowText(host);
  71. m_hostIP.SetWindowText(hostIP);
  72. m_btnConnect.SetWindowText("连 接");
  73. m_btnTrans.EnableWindow(FALSE);
  74. m_ipCtrl.SetAddress(127, 0, 0, 1);
  75. m_pDataList = new CDataAdmin();
  76. // Create a filter graph and set video window
  77. m_pGraph = new CFilterGraph(m_pDataList);
  78. m_pGraph->BuildGraph();
  79. m_pGraph->SetVideoWndOwner(m_videoWnd.GetSafeHwnd());
  80. RECT rect;
  81. m_videoWnd.GetClientRect(&rect);
  82. m_pGraph->SetWindowPosition(0, 0, rect.right - rect.left, rect.bottom - rect.top);
  83. return TRUE;  // return TRUE  unless you set the focus to a control
  84. }
  85. // If you add a minimize button to your dialog, you will need the code below
  86. //  to draw the icon.  For MFC applications using the document/view model,
  87. //  this is automatically done for you by the framework.
  88. void CMediaClientDlg::OnPaint() 
  89. {
  90. if (IsIconic())
  91. {
  92. CPaintDC dc(this); // device context for painting
  93. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  94. // Center icon in client rectangle
  95. int cxIcon = GetSystemMetrics(SM_CXICON);
  96. int cyIcon = GetSystemMetrics(SM_CYICON);
  97. CRect rect;
  98. GetClientRect(&rect);
  99. int x = (rect.Width() - cxIcon + 1) / 2;
  100. int y = (rect.Height() - cyIcon + 1) / 2;
  101. // Draw the icon
  102. dc.DrawIcon(x, y, m_hIcon);
  103. }
  104. else
  105. {
  106. CDialog::OnPaint();
  107. }
  108. }
  109. // The system calls this to obtain the cursor to display while the user drags
  110. //  the minimized window.
  111. HCURSOR CMediaClientDlg::OnQueryDragIcon()
  112. {
  113. return (HCURSOR) m_hIcon;
  114. }
  115. void CMediaClientDlg::OnDestroy() 
  116. {
  117. CDialog::OnDestroy();
  118. // Release buffers
  119. SAFE_DELETE(m_pReceiver)
  120. SAFE_DELETE(m_pGraph)
  121. Sleep(1000);
  122. SAFE_DELETE(m_pDataList)
  123. // Finished with COM
  124. CoUninitialize();
  125. }
  126. void CMediaClientDlg::OnButtonConnect() 
  127. {
  128. if (m_bConnected)
  129. {
  130. CloseSession();
  131. }
  132. else
  133. {
  134. OpenSession();
  135. }
  136. // Update the window text
  137. if (m_bConnected)
  138. {
  139. m_btnConnect.SetWindowText("断 开");
  140. m_btnTrans.EnableWindow(TRUE);
  141. }
  142. else
  143. {
  144. m_btnConnect.SetWindowText("连 接");
  145. m_btnTrans.EnableWindow(FALSE);
  146. }
  147. }
  148. // Request the server to disconnect...
  149. void CMediaClientDlg::CloseSession(void)
  150. {
  151. if (m_pReceiver)
  152. {
  153. char pData[sizeof(MSG_HEADER)];
  154. PMSG_HEADER pMsg = (PMSG_HEADER) pData;
  155. pMsg->nDataSize  = 0;
  156. pMsg->nMsgType   = DISCONNECT_REQUEST;
  157. m_pReceiver->Send(pData, sizeof(MSG_HEADER));
  158. }
  159. }
  160. void CMediaClientDlg::OpenSession(void)
  161. {
  162. // Create a socket to connect to the server
  163. SAFE_DELETE(m_pReceiver)
  164. m_pReceiver = new CMediaSocketClient();
  165. m_pReceiver->SetRecvBuffer(m_pDataList);
  166. m_pReceiver->SetCountWnd(m_Counter.GetSafeHwnd());
  167. // Connect to the server
  168. char  lpszServerAddr[20];
  169. BYTE  nField[4];
  170. m_ipCtrl.GetAddress(nField[0], nField[1], nField[2], nField[3]);
  171. sprintf(lpszServerAddr,"%d.%d.%d.%d",nField[0],nField[1],nField[2],nField[3]);
  172. if (m_pReceiver->Connect(lpszServerAddr, BASE_SOCKET_PORT))
  173. {
  174. m_bConnected = true;
  175. m_pReceiver->StartReceiving();
  176. }
  177. else
  178. {
  179. m_bConnected = false;
  180. AfxMessageBox("远程连接失败!");
  181. }
  182. }
  183. // Request the server to send data...
  184. void CMediaClientDlg::OnButtonTrans() 
  185. {
  186. if (m_pReceiver)
  187. {
  188. char pData[sizeof(MSG_HEADER)];
  189. PMSG_HEADER pMsg = (PMSG_HEADER) pData;
  190. pMsg->nDataSize  = 0;
  191. pMsg->nMsgType   = DATA_REQUEST;
  192. m_pReceiver->Send(pData, sizeof(MSG_HEADER));
  193. }
  194. }