⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clientdlg.cpp

📁 本书通过实例介绍了嵌入式编程的方法与技巧书中例子具有实用性
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "stdafx.h"
#include "talkclient.h"
#include "talkclientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CClientDlg::CClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CClientDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_strUserName = _T("");
m_strServer = _T("");
m_bVoiceTalk = FALSE;
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bConnect = FALSE;
m_bTalking = FALSE;
memset(m_lpPeerName, 0, 16);
m_bVoiceMsg = FALSE;
m_uiBufLen = VT_MAX;
m_iWaveFormat = VT_WAVE1M08;
}
void CClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CClientDlg)
DDX_Control(pDX, IDC_BTNCONNECT, m_btnConnect);
DDX_Control(pDX, IDC_CHKVOICE, m_chkVoice);
DDX_Control(pDX, IDC_EDITNAME, m_editUserName);
DDX_Control(pDX, IDC_EDITSERVER, m_editServer);
DDX_Control(pDX, IDC_COMBPROT, m_ComBoProts);
DDX_Control(pDX, IDC_COMBCHATTER, m_ComBoTalker);
DDX_Control(pDX, IDC_SENDMETER, m_progctrlSend);
DDX_Control(pDX, IDC_RECVMETER, m_progctrlRecv);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CClientDlg, CDialog)
//{{AFX_MSG_MAP(CClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(VTM_DATAREADY, OnVTDataReady)
ON_MESSAGE(VTM_READYFORWRITE, OnVTReadyForWrite)
ON_MESSAGE(MM_WIM_DATA, OnRecordEvent)
ON_MESSAGE(MM_WOM_DONE, OnPlayEvent)
ON_EN_CHANGE(IDC_EDITNAME, OnChangeEditname)
ON_EN_CHANGE(IDC_EDITSERVER, OnChangeEditserver)
ON_CBN_SELCHANGE(IDC_COMBPROT, OnSelchangeCombprot)
ON_CBN_SELCHANGE(IDC_COMBCHATTER, OnSelchangeCombchatter)
ON_BN_CLICKED(IDC_BTNCONNECT, OnBtnconnect)
ON_BN_CLICKED(IDC_BTNDISCONNECT, OnBtndisconnect)
ON_BN_CLICKED(IDC_CHKVOICE, OnChkvoice)
ON_WM_CLOSE()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog.  The framework does this automatically
//  when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE);			// Set big icon
SetIcon(m_hIcon, FALSE);		// Set small icon
// Add extra initialization here
m_editUserName.SetWindowText("");
m_editServer.SetWindowText("131.200.2.246"); // Server IP address
m_ComBoTalker.ResetContent();
m_ComBoProts.ResetContent();
m_editUserName.LimitText(15);
m_progctrlSend.SetRange(0, 1);
m_progctrlRecv.SetRange(0, 1);
m_progctrlSend.SetPos(0);
m_progctrlRecv.SetPos(0);
WORD      wVersionRequested;
WSADATA   wsaData;
char      outtext[128];
int       iLoop;
wVersionRequested = MAKEWORD(1,1);
if (WSAStartup(wVersionRequested, &wsaData) != 0)
{
   return FALSE;
}
// Check compatible network protocols
if(!InitProtocols())
return FALSE;
// Add compatible protocol to protocol combo-box
for(iLoop = 0; iLoop < m_iNumWorkProts; iLoop++)
{
 wsprintf(outtext, "%2d: %s", iLoop, 
         m_lpProtBuf[m_iWorkProts[iLoop]].lpProtocol);
 m_ComBoProts.InsertString(-1, outtext); 
}
// Set the controls status
DoConnection(FALSE);
m_bVoiceMsg = FALSE;
m_vtPlay.SetHwnd(GetSafeHwnd());
m_vtPlay.SetVTWaveFormat(m_iWaveFormat);
m_vtPlay.AllocMemory();
m_vtRecord.SetHwnd(GetSafeHwnd());
m_vtRecord.SetVTWaveFormat(m_iWaveFormat);
m_vtRecord.AllocMemory();
m_ComBoProts.SetCurSel(0);
// return TRUE  unless you set the focus to a control
SetTimer(1, 3000, NULL);
return TRUE;  
}
void CClientDlg::OnClose() 
{
// Add your message handler code here and/or call default
// Clean the combo-boxes
m_ComBoTalker.ResetContent();
m_ComBoProts.ResetContent();
// Close the socket
closesocket(m_MySock.m_hSock);
WSACleanup();
// Close the wave I/O device and
if(m_bVoiceMsg)
   m_vtPlay.StopPlay();
if(m_bVoiceTalk)
m_vtRecord.StopRecord();
m_vtPlay.CleanMemory(); 
m_vtRecord.CleanMemory();
CDialog::OnClose();
}
void CClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CClientDlg::OnPaint() 
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
BOOL CClientDlg::InitProtocols(void)
{
int      numStrucs;
int      index;
unsigned long  iProtBufSize = 0;
EnumProtocols(NULL, NULL, &iProtBufSize);
if((m_lpProtBuf = (LPPROTOCOL_INFO)VirtualAlloc(NULL,
                                                 iProtBufSize,
                                                 MEM_COMMIT,
                                                 PAGE_READWRITE)) == NULL)
 {
     AfxMessageBox("Failed to allocate memory for m_lpProtBuf!");
     return FALSE;
 }
if((numStrucs = EnumProtocols(NULL, m_lpProtBuf, &iProtBufSize)) == 
               SOCKET_ERROR) 
 {
   AfxMessageBox("Failed to numerate protocol!");
   return FALSE;
}
m_iNumWorkProts = 0;
for(index = 0; index < numStrucs; index++)
{
  if((m_lpProtBuf[index].dwServiceFlags & XP_CONNECTIONLESS) == 0)
  {
     m_iWorkProts[m_iNumWorkProts++] = index; 
  }
}
return TRUE;
}
BOOL CClientDlg::InitSockets(int iProtIndex)
{
int numStrucs;
GUID guidNW = SVCID_NETWARE(VT_NETID);    // Generate GUID for Novel prot
GUID guidDNS = SVCID_TCP(VT_DNSID);       // Generate GUID for DNS
char strServName[128];
char outtext[128];
// Check the protocol type
switch(m_lpProtBuf[iProtIndex].iAddressFamily)
{
case AF_IPX:
     //  SAP/Bindery Name Space
    m_dwCSABufsize = sizeof(m_CSABuf);
    numStrucs = GetAddressByName(0, &guidNW, "VOICE_TALK_SERVER_IPX", 
                                  NULL, RES_FIND_MULTIPLE, NULL, m_CSABuf,
                                  &m_dwCSABufsize, NULL, NULL);
    if((numStrucs == SOCKET_ERROR) || (numStrucs == 0))
    {
       AfxMessageBox("Server not found.  Try a different protocol");
       return FALSE;
    }
  break;
default:
  return FALSE;
}
m_MySock.m_iSockType = m_lpProtBuf[iProtIndex].iSocketType;
m_MySock.m_iProtocol = m_lpProtBuf[iProtIndex].iProtocol; 
if((m_MySock.m_hSock =  
      socket(m_lpProtBuf[iProtIndex].iAddressFamily, 
      m_lpProtBuf[iProtIndex].iSocketType,
      m_lpProtBuf[iProtIndex].iProtocol)) == INVALID_SOCKET)
{
   wsprintf(outtext, "Failure: socket() returned %u", WSAGetLastError());
   AfxMessageBox(outtext);
   return FALSE;
}
if(connect(m_MySock.m_hSock,m_CSABuf[0].RemoteAddr.lpSockaddr,
          m_CSABuf[0].RemoteAddr.iSockaddrLength) == SOCKET_ERROR)
{
  wsprintf(outtext, "Failure: connect() returned %u", WSAGetLastError());
  AfxMessageBox(outtext);
  return FALSE;
}
if(WSAAsyncSelect(m_MySock.m_hSock, GetSafeHwnd(), VTM_READYFORWRITE, 
                  FD_WRITE) == SOCKET_ERROR)
{
  wsprintf(outtext, "Failure: WSAAsyncSelect() returned %u", 
           SAGetLastError());
  AfxMessageBox(outtext);
  return FALSE;
}
return TRUE;
}
void CClientDlg::DoConnection(BOOL bTrue)
{
  m_bConnect = bTrue;
  if(bTrue)
  {
    m_ComBoTalker.ResetContent();
    m_ComBoTalker.InsertString(0, "No one to talk");
    if(WSAAsyncSelect(m_MySock.m_hSock, GetSafeHwnd(),VTM_DATAREADY, 
                       FD_READ | FD_CLOSE) == SOCKET_ERROR)
    {
     AfxMessageBox("Failure: WSAAsyncSelect() to get online users!");
     return;
    }
}
else 
{
 closesocket(m_MySock.m_hSock);
 m_ComBoTalker.ResetContent();
 m_ComBoTalker.InsertString(0, "No one to talk");
 m_chkVoice.SetCheck(0);
 m_bVoiceMsg = FALSE;
 m_progctrlSend.SetPos(0);
 m_progctrlRecv.SetPos(0);
}
m_btnConnect.EnableWindow(!bTrue);
m_editUserName.EnableWindow(!bTrue);
m_editServer.EnableWindow(!bTrue);
m_ComBoProts.EnableWindow(!bTrue);
if(!bTrue)
{
 EnableTalking(FALSE);
  // Close wave I/O devices
 if(m_bVoiceMsg)
 m_vtPlay.StopPlay();
 if(m_bVoiceTalk)
   m_vtRecord.StopRecord();
}
}
void CClientDlg::EnableTalking(BOOL bTrue)
{
char sShowText[128];
int  iIndex;
 m_bTalking = bTrue;
 m_chkVoice.EnableWindow(bTrue);
 m_progctrlSend.EnableWindow(bTrue);
 m_progctrlRecv.EnableWindow(bTrue);
 if(bTrue)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -