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

书籍源码

开发平台:

Visual C++

  1. // BroadcastSvrDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "BroadcastSvr.h"
  5. #include "BroadcastSvrDlg.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. // CBroadcastSvrDlg dialog
  51. CBroadcastSvrDlg::CBroadcastSvrDlg(CWnd* pParent /*=NULL*/)
  52. : CDialog(CBroadcastSvrDlg::IDD, pParent)
  53. {
  54. //{{AFX_DATA_INIT(CBroadcastSvrDlg)
  55. m_szSend = _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 CBroadcastSvrDlg::DoDataExchange(CDataExchange* pDX)
  61. {
  62. CDialog::DoDataExchange(pDX);
  63. //{{AFX_DATA_MAP(CBroadcastSvrDlg)
  64. DDX_Text(pDX, IDC_EDIT1, m_szSend);
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CBroadcastSvrDlg, CDialog)
  68. //{{AFX_MSG_MAP(CBroadcastSvrDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. ON_WM_TIMER()
  73. //}}AFX_MSG_MAP
  74. END_MESSAGE_MAP()
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CBroadcastSvrDlg message handlers
  77. BOOL CBroadcastSvrDlg::OnInitDialog()
  78. {
  79. CDialog::OnInitDialog();
  80. // Add "About..." menu item to system menu.
  81. // IDM_ABOUTBOX must be in the system command range.
  82. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  83. ASSERT(IDM_ABOUTBOX < 0xF000);
  84. CMenu* pSysMenu = GetSystemMenu(FALSE);
  85. if (pSysMenu != NULL)
  86. {
  87. CString strAboutMenu;
  88. strAboutMenu.LoadString(IDS_ABOUTBOX);
  89. if (!strAboutMenu.IsEmpty())
  90. {
  91. pSysMenu->AppendMenu(MF_SEPARATOR);
  92. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  93. }
  94. }
  95. // Set the icon for this dialog.  The framework does this automatically
  96. //  when the application's main window is not a dialog
  97. SetIcon(m_hIcon, TRUE); // Set big icon
  98. SetIcon(m_hIcon, FALSE); // Set small icon
  99. // TODO: Add extra initialization here
  100. //创建本地套接口
  101. m_sockSend.Create(6800,SOCK_DGRAM,NULL);
  102. //绑定本地套接口
  103. m_sockSend.Bind(6800,"127.0.0.1");
  104. //创建一个定时器定时广播
  105. SetTimer(1,3000,NULL);
  106. return TRUE;  // return TRUE  unless you set the focus to a control
  107. }
  108. void CBroadcastSvrDlg::OnSysCommand(UINT nID, LPARAM lParam)
  109. {
  110. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  111. {
  112. CAboutDlg dlgAbout;
  113. dlgAbout.DoModal();
  114. }
  115. else
  116. {
  117. CDialog::OnSysCommand(nID, lParam);
  118. }
  119. }
  120. // If you add a minimize button to your dialog, you will need the code below
  121. //  to draw the icon.  For MFC applications using the document/view model,
  122. //  this is automatically done for you by the framework.
  123. void CBroadcastSvrDlg::OnPaint() 
  124. {
  125. if (IsIconic())
  126. {
  127. CPaintDC dc(this); // device context for painting
  128. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  129. // Center icon in client rectangle
  130. int cxIcon = GetSystemMetrics(SM_CXICON);
  131. int cyIcon = GetSystemMetrics(SM_CYICON);
  132. CRect rect;
  133. GetClientRect(&rect);
  134. int x = (rect.Width() - cxIcon + 1) / 2;
  135. int y = (rect.Height() - cyIcon + 1) / 2;
  136. // Draw the icon
  137. dc.DrawIcon(x, y, m_hIcon);
  138. }
  139. else
  140. {
  141. CDialog::OnPaint();
  142. }
  143. }
  144. // The system calls this to obtain the cursor to display while the user drags
  145. //  the minimized window.
  146. HCURSOR CBroadcastSvrDlg::OnQueryDragIcon()
  147. {
  148. return (HCURSOR) m_hIcon;
  149. }
  150. void CBroadcastSvrDlg::OnTimer(UINT nIDEvent) 
  151. {
  152. static iIndex=0;
  153. char szSend[20];
  154. sprintf(szSend,"%010d",iIndex++);
  155. //发送UDP数据
  156. int iSend= m_sockSend.SendTo(szSend,10,6801,"127.0.0.1",0);
  157. TRACE("sent %d byten",iSend);
  158. m_szSend=szSend;
  159. UpdateData(FALSE);
  160. CDialog::OnTimer(nIDEvent);
  161. }