Chinaunix首页 | 论坛 | 博客
  • 博客访问: 180752
  • 博文数量: 12
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-04 15:43
文章分类

全部博文(12)

文章存档

2011年(1)

2009年(1)

2008年(10)

我的朋友

分类: C/C++

2008-04-17 10:27:17

修改IDR_MAINFRAME的菜单设置如下:

   资源标识

       标题

       命令处理函数

ID_FILE_CONNET

创建服务器

CMyQQClietnDoc::OnFileConnet()

主要实现函数 

void CMyQQClientDoc::OnFileConnet()

{

      // TODO: 连接服务器

     CSetDlg m_Setdlg;

      if(m_Setdlg.DoModal()!=IDOK)

           return;

      mPort=m_Setdlg.m_port;

      strAddr=m_Setdlg.m_address;

name=m_Setdlg.m_name;

      //关闭上次有效连接

      if(m_socket!=NULL)

      {

           m_socket->Close();

           delete m_socket;

      }

            //创建一个新的套接字

      m_socket=new CChatSocket(this);

      if(!m_socket->Create())

      {

           AfxMessageBox("创建SOCKET失败!");

           delete m_socket;

           m_socket=NULL;

           return;

      }

      //连接服务器

      if(!m_socket->Connect(strAddr,mPort))

      {

           AfxMessageBox("连接服务器失败!");

           delete m_socket;

           m_socket=NULL;

           return;

      }

    AfxMessageBox("连接服务器成功!");

 CString temp=m_Setdlg.m_name;

      temp+=":上线了!";

      m_socket->Send(temp,strlen(temp));

      POSITION  pos=GetFirstViewPosition(   );  

    CMyQQClientView *  eView=(CMyQQClientView *)GetNextView(pos) ;

      CMyQQClientView *  cView=(CMyQQClientView *)GetNextView(pos) ;  

      CEdit&edit=cView->GetEditCtrl();

      edit.SetWindowText(name+":");

}

 

登陆框类CsetDlg实现

class CSetDlg : public CDialog

{

// Construction

public:

      CSetDlg(CWnd* pParent = NULL);   // standard constructor

// Dialog Data

      //{{AFX_DATA(CSetDlg)

      enum { IDD = IDD_DIALOG1 };

      CString m_address;

      int       m_port;

      CString m_name;

      //}}AFX_DATA

// Overrides

      // ClassWizard generated virtual function overrides

      //{{AFX_VIRTUAL(CSetDlg)

      protected:

      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

      //}}AFX_VIRTUAL

// Implementation

protected:

      // Generated message map functions

      //{{AFX_MSG(CSetDlg)

           // NOTE: the ClassWizard will add member functions here

      //}}AFX_MSG

      DECLARE_MESSAGE_MAP()

};

 

 

 

void CMyQQClientDoc::ReceiveMessage()

{

      //确认套接字可用

      ASSERT(m_socket);

      char buffer[100];

      int len=m_socket->Receive(buffer,99);

      //检查信息是否有效

      if(len<1)

      {

           AfxMessageBox("接收信息为空!");

           return;

      }

      buffer[len]=0;

      ShowMessage(buffer);

}

 

void CMyQQClientDoc::ShowMessage(LPSTR msg)

{

      //依次获取对应的视图

      POSITION pos=GetFirstViewPosition();

      while(pos!=NULL)

      {

           CEditView*view=(CEditView*)GetNextView(pos);

            if(view->IsKindOf(RUNTIME_CLASS(CMyQQClientView)))

                 continue;

         CEdit& edit=view->GetEditCtrl();

         char buffer[100];

         wsprintf(buffer,"%s\r\n", msg);

         int len=edit.GetWindowTextLength();

         edit.SetSel(len,len);

         edit.ReplaceSel(buffer);

         }

}

void CMyQQClientDoc::SendMessage(LPSTR msg)

{

   if(!m_socket->Send(msg,strlen(msg)))

      {

           AfxMessageBox("发送消息失败!");

           return;

      }

}

响应键盘回车键发送信息

void CMyQQClientView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)

{

      // TODO: Add your message handler code here and/or call default

      //如果用户按下回车则发送数据

   if(nChar==VK_RETURN)

     {

           CMyQQClientDoc* pDoc=GetDocument();

           ASSERT_VALID(pDoc);

           CEdit&edit=GetEditCtrl();

           char buffer[100];

           memset(buffer,0,sizeof(buffer));

           int index=edit.GetLineCount()-1;

           edit.GetLine(index,buffer,99);

           pDoc->SendMessage(buffer);

           edit.SetWindowText(pDoc->name+":");

           }

           CEditView::OnChar(nChar,nRepCnt,nFlags);

}

 

阅读(1034) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~