BroadcastClientDlg.cpp
上传用户:qzzxgm
上传日期:2009-12-14
资源大小:1882k
文件大小:5k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // BroadcastClientDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "BroadcastClient.h"
  5. #include "BroadcastClientDlg.h"
  6. #include "MySocket.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CAboutDlg dialog used for App About
  14. class CAboutDlg : public CDialog
  15. {
  16. public:
  17. CAboutDlg();
  18. // Dialog Data
  19. //{{AFX_DATA(CAboutDlg)
  20. enum { IDD = IDD_ABOUTBOX };
  21. //}}AFX_DATA
  22. // ClassWizard generated virtual function overrides
  23. //{{AFX_VIRTUAL(CAboutDlg)
  24. protected:
  25. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  26. //}}AFX_VIRTUAL
  27. // Implementation
  28. protected:
  29. //{{AFX_MSG(CAboutDlg)
  30. //}}AFX_MSG
  31. DECLARE_MESSAGE_MAP()
  32. };
  33. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  34. {
  35. //{{AFX_DATA_INIT(CAboutDlg)
  36. //}}AFX_DATA_INIT
  37. }
  38. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  39. {
  40. CDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CAboutDlg)
  42. //}}AFX_DATA_MAP
  43. }
  44. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  45. //{{AFX_MSG_MAP(CAboutDlg)
  46. // No message handlers
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CBroadcastClientDlg dialog
  51. CBroadcastClientDlg::CBroadcastClientDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CBroadcastClientDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CBroadcastClientDlg)
  55. m_szRecv = _T("");
  56. //}}AFX_DATA_INIT
  57. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  58. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  59. }
  60. void CBroadcastClientDlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62. CDialog::DoDataExchange(pDX);
  63. //{{AFX_DATA_MAP(CBroadcastClientDlg)
  64. DDX_Text(pDX, IDC_EDIT1, m_szRecv);
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CBroadcastClientDlg, CDialog)
  68. //{{AFX_MSG_MAP(CBroadcastClientDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  73. ON_WM_TIMER()
  74. //}}AFX_MSG_MAP
  75. END_MESSAGE_MAP()
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CBroadcastClientDlg message handlers
  78. BOOL CBroadcastClientDlg::OnInitDialog()
  79. {
  80. CDialog::OnInitDialog();
  81. // Add "About..." menu item to system menu.
  82. // IDM_ABOUTBOX must be in the system command range.
  83. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  84. ASSERT(IDM_ABOUTBOX < 0xF000);
  85. CMenu* pSysMenu = GetSystemMenu(FALSE);
  86. if (pSysMenu != NULL)
  87. {
  88. CString strAboutMenu;
  89. strAboutMenu.LoadString(IDS_ABOUTBOX);
  90. if (!strAboutMenu.IsEmpty())
  91. {
  92. pSysMenu->AppendMenu(MF_SEPARATOR);
  93. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  94. }
  95. }
  96. // Set the icon for this dialog.  The framework does this automatically
  97. //  when the application's main window is not a dialog
  98. SetIcon(m_hIcon, TRUE); // Set big icon
  99. SetIcon(m_hIcon, FALSE); // Set small icon
  100. // TODO: Add extra initialization here
  101. //创建本地套接口
  102. m_sockRecv.Create(6801,SOCK_DGRAM,"127.0.0.1");
  103. //绑定本地套接口
  104. m_sockRecv.Bind(6801,"127.0.0.1");
  105. //创建一个定时器定时读取
  106. SetTimer(1,3000,NULL);
  107. return TRUE;  // return TRUE  unless you set the focus to a control
  108. }
  109. void CBroadcastClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
  110. {
  111. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  112. {
  113. CAboutDlg dlgAbout;
  114. dlgAbout.DoModal();
  115. }
  116. else
  117. {
  118. CDialog::OnSysCommand(nID, lParam);
  119. }
  120. }
  121. // If you add a minimize button to your dialog, you will need the code below
  122. //  to draw the icon.  For MFC applications using the document/view model,
  123. //  this is automatically done for you by the framework.
  124. void CBroadcastClientDlg::OnPaint() 
  125. {
  126. if (IsIconic())
  127. {
  128. CPaintDC dc(this); // device context for painting
  129. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  130. // Center icon in client rectangle
  131. int cxIcon = GetSystemMetrics(SM_CXICON);
  132. int cyIcon = GetSystemMetrics(SM_CYICON);
  133. CRect rect;
  134. GetClientRect(&rect);
  135. int x = (rect.Width() - cxIcon + 1) / 2;
  136. int y = (rect.Height() - cyIcon + 1) / 2;
  137. // Draw the icon
  138. dc.DrawIcon(x, y, m_hIcon);
  139. }
  140. else
  141. {
  142. CDialog::OnPaint();
  143. }
  144. }
  145. // The system calls this to obtain the cursor to display while the user drags
  146. //  the minimized window.
  147. HCURSOR CBroadcastClientDlg::OnQueryDragIcon()
  148. {
  149. return (HCURSOR) m_hIcon;
  150. }
  151. void CBroadcastClientDlg::OnButton1() 
  152. {
  153. }
  154. void CBroadcastClientDlg::OnTimer(UINT nIDEvent) 
  155. {
  156. char szRecv[20];
  157. CString szIP("127.0.0.1");
  158. UINT uPort=6800;
  159. //接收UDP数据
  160. int iRecv =m_sockRecv.ReceiveFrom(szRecv,10,szIP,uPort,0);
  161. TRACE("received %d byten",iRecv);
  162. szRecv[iRecv]='';
  163. m_szRecv=szRecv;
  164. UpdateData(FALSE);
  165. CDialog::OnTimer(nIDEvent);
  166. }