C++加载位图跟SOCKET通信的编写

  //将屏幕信息转换为位图结构
  CDC *pDeskDC=GetDesktopWindow()->GetDC();//获取桌面的额画图对象
  int width=;
  int height=;
  CDC memDC;//定义一个内存画布
  memDC.CreateCompatibleDC(pDeskDC);//创建一个兼容的画布
  CBitmap bmp;
  bmp.CreateCompatibleBitmap(pDeskDC,width,height);//创建兼容位图
  memDC.SelectObject(&bmp);//选中位图对象
  memDC.BitBlt(,,width,height,pDeskDC,,,SRCCOPY);//将屏幕信息绘制到位图中
  。。。。
  char* pData=new char[bitmap,bmWidthBytes* bitmap,bmHeight];//分配内存,存储位图的实际数据
  //获取位图的实际数据
  ::GetDIBits(memDC.m_hDC,bmp,,bitmap.bmHeight,pData,pBInfo,DIB_RGB_COLORS);
  // 客户端SOCKET通信
  #include "Winsock2.h"
  #pragma comment(lib,"Ws2_32.lib");
  // 连接库文件
  WSDATA data;
  WSAStartup(,&data);//初始化套接字
  hostent* phost=gethostbyname("");//获取本机信息
  char* localIP=inet_ntoa(*(struct in_addr*)*phost->h_addr_list);//获取IP地址
  sockaddr_in addr;
  //定义一个套接字地址
  addr.sin_addr.S_un.S_addr=inet_addr(localIP);//设置IP地址
  addr.sin_port=htons();//设置端口号
  m_Socket=socket(AF_INET,SOCK_DGRAM,);//创建UDP套接字
  if(m_Socket==INVALID_SOCKET)
  {
  MessageBox("套接字创建失败");
  //如果套接字创建失败,弹出对话框
  }
  char*len="";
  if(setsockopt(m_Socket,SQL_SOCKET,SO_SNDBUF,len,)!=)
  {
  MessageBox("设置失败");
  }
  if(bind(m_Socket,(sockeaddr*)&addr,sizeof(addr))==SOCKET_ERROR)//绑定套接字
  {
  MessageBox("套接字绑定失败");
  }
  char* len="";
  if(setsockopt(m_Socket,SOL_SOCKET,SO_SNDBUF,len,)!=)
  {//设置套接字发送缓冲区大小
  MessageBox("设置失败");
  }
  if(bind(m_Socket,(sockaddr*)&addr,sizeof(addr)==SOCKET_ERROR)//绑定套接字
  {MessageBox("套接字绑定失败");
  }
  //在创建成功后可以调用sendto方法发送数据。
  sendto(m_Socket,pPackage,packsize,,(sockaddr*)&addr,sizeof(addr));
  ////////////////////////////////////////////////////////////////////////////////////////////
  //主窗体设计
  hostent* phost=gethostbyname("");
  char* localIP=inet_ntoa(*(struct in_addr*)*phost->h_addr_list);//获取IP地址
  sockaddr_in addr;
  addr.sin_family=AF_INET;
  addr.sin_addr.S_addr=inet_addr(localIP);
  addr.sin_port=htons();
  m_Socket=socket(AF_INET,SOCK_DGRAM,);
  if(bind(m_Socket,(sockaddr*)&addr,sizeof(addr)==SOCKET_ERROR)//绑定套接字
  {
  MessageBox("套接字绑定失败");
  }
  /////////////////////////////////////////////
  sockaddr_in addr;//定义网络地址
  addr.sin_family=AF_INET;//设置网络地址类型
  addr.sin_port=htons(m_ClientPort);//设置网络端口号
  addr.sin_addr.S_un.S_addr=inet_addr(m_ClientIP.GetBuffer());//设置IP
  int size=sizeof(addr);//获取地址大小
  char* pData="Confirm";//定义发送的信息
  sendto(m_Socket,pData,,,(socketaddr*)&addr,size);//发送数据
  atoi()//将编辑文本转换为整数。
  CFont newFont,*oldFont;
  //声明新字体对象,和旧字体对象。
  gameFrame::gameFrame()
  {
  Rect rect;
  Create(NULL,"绘图窗口");
  //建立窗口
  CClientDC dc(this);//将DC的数据映射到窗口
  int width=dc.GetDeviceCaps(HORZRES);//取得屏幕区的宽度
  int height=dc.GetDeviceCaps(VERTRES);//取得屏幕区的高度
  GetWindowRect(&rect);//取得窗口区矩形的大小
  width=(width-(rect.right-rect.left))/;
  height=(height-(rect.bottom-rect.top))/;
  MoveWindow(width,//移动窗口位置
  height,
  (rect.right-rect.left),
  (rect.bottom-rect.top),
  true);
  //在这里建立Cfont类对象newFont
  newFont.CreateFont(,,,,,
  TRUE,TRUE,FALSE,
  DEFAULT_CHARSET,
  OUT_CHARCTER_PRECIS,
  CLIP_DEFAULT_PRECIS,
  DEFAULT_QUALITY,
  FIXED_PITCH|FF_MODERN,
  "楷体");
  }
  //////////////////////////////////////
  LoadImage()//加载位图
  CPaintDC dc(this);//建立响应WM_PAINT消息的DC
  CDC *mdc=new CDC;
  //声明内存dc
  CBitmap * bitmap=new CBitmap;//声明CBitmap位图对象
  mdc->CreateCompatibleDC(&dc);//建立于dc兼容的DC
  bitmap->m_hObject=(HBITMAP)::LoadImage(NULL,"/bground.bmp",
  IMAGE_BITMAP,,,LR_LOADFROMFILE);//加载位图
  mdc->SelectObject(bitmap);//将位图对象指定到mdc中
  //镂空原理的和运算与或运算。
  OnPaint()
  {
  CPaintDC dc(this);
  mdc->SelectObject(bgbmp);//取得背景图
  //贴上背景图
  dc.BitBlt(,,rect,right,rect.bottom,mdc,,,SRCCOPY);
  mdc->SelectObject(bitmap);//取得原位图
  //将蒙版与背景图做AND运算
  dc.BitBlt(,,,,mdc,,,SRCAND);
  //将要镂空的图与背景图做OR运算
  dc.BitBlt(,,,,mdc,,,SRCPAINT);
  }
  //服务器端SOCKET
  BOOL CServerDlg::OnInitDialog()
  {
  //获取本机IP
  hostent* phost=gethostbyname("");
  char* localIP=inet_ntoa(*(struct in_addr *)*phost->h_addr_list);
  sockaddr_in addr;
  addr.sin_family=AF_INET;
  addr.sin_addr.S_un.S_addr=inet_addr(localIP);
  addr.sin_port=htons();
  //创建套接字
  m_Socket=socket(AF_INET,SOCK_DGRAM,);
  if(m_Socket==INVALID_SOCKET);
  {
  MessageBox("套接字创建失败");
  }
  //绑定套接字
  if(bind(m_Socket,(sockaddr*)&addr,sizeof(addr))==SOCKET_ERROR)
  {
  MessageBox("套接字绑定失败");
  }
  m_BmpData=new char[**];
  memset(m_BmpData,,**);
  m_TempData=new char[**];
  memset(m_TempData,,**);
  m_Header=m_BmpData;
  WSAAsyncSelect(m_Socket,m_hWnd,CM_RECEIVEDM,FD_READ);
  m_ShowBmp=FALSE;
  m_BmpSize=;
  m_RecSize=;
  m_ClientPort=;
  CServerApp* pApp=(CServerApp*)AfxGetApp();
  CString sql="select * from tb_ClientInfo";
  pAp->m_DataManage.m_pRecord->CusorLocation=adUseServer;
  pApp->m_DataManage.m_pRecord->Open((_bstr_t)sql,pApp->m_DataManage.m_pConnect.GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
  m_ClientIP="";
  if(pApp->m_DataManage.m_pRecord->RecordCount>)
  {
  m_ClientIP=(char*)(_bstr_t)pApp->m_DataManage.m_pRecord->GetFields()->GetItem(0L)->Value;
  }
  pApp->m_DtaManage.m_pRecord->Close();
  m_LeftTopX=;
  m_LeftTopY=;
  m_BK.SetWindowPos(NULL,,,,,SWP_NOMOVE);
  return TRUE;
  }
  void CServerDlg::OnReceived()
  {
  //接收数据
  char* buffer=new char[MAX_BUFF];
  sockaddr_in addr;
  int factsize=sizeof(sockaddr);
  int ret=recvfrom(m_Socket,buffer,MAX_BUFF,,(sockaddr*)&addr,&factsize);
  if(ret!=-)
  {CString recvIP=inet_ntoa(addr.sin_addr);
  if(recvIP!=m_ClinetIP)
  { delete []buffer;
  return;
  }
  m_ClientPort=ntohs(addr.sin_port);
  //记录接收的数据报大小
  m_RecvSize+=ret;
  //读取序号
  WORD orderID=*(WORD)&buffer[];
  WORD endID=*(WORD)&buffer[];
  //读取位图的大小
  int bmpsize=*(int*)&buffer[ret-];
  //获取屏幕的X轴坐标
  m_LeftTopX=*(WORD*)&buffer[ret-];
  //获取屏幕的Y轴坐标
  m_LeftTopY=*(WORD*)&buffer[ret-];
  //读取结束标记
  sockaddr_in addr;
  addr.sin_family=AF_INET;
  addr.sin_port=htons(m_ClientPort);
  addr.sin_addr.S_un.S_addr=inet_addr(m_ClientIP.GetBuffer());
  int size=sizeof(addr);
  //发送确认信息
  char* pData="Confirm";
  sendto(m_Socket,pData,,,(sockaddr*)&addr,size);
  if(orderID==)
  {m_BmpSize=bmpsize;
  m_BmpData=m_Header;
  }else
  m_BmpSize+=bmpsize;
  memcpy(m_BmpData,&buffer[],bmpsize);
  m_BmpData=m_Header;
  }else
  m_BmpSize+=bmpsize;
  memcpy(m_BmpData,&buffer[],bmpsize);
  m_BmpData+=bmpsize;
  m_ShowBmp=FALSE;
  if(endID==)
  { m_ShowBmp=TRUE;
  m_BmpData=m_Header;
  memcpy(m_TempData,m_Header,*);
  ShowImage();
  m_RecSize=;
  }
  }
  delete [] buffer;
  }
  void CServerDlg::OnButtonDown(UINT nID)
  {
  if(!m_ClinetIP.IsEmpty())
  {
  WORD x=m_LeftTopX;
  WORD y=m_LeftTopY;
  char buffer[];
  sockaddr_in addr;
  addr.sin_family=AF_INET;
  addr.,sin_port=htons(m_ClinetPort);
  addr.sin_addr.S_un.S_addr=inet_addr(m_ClientIP.GetBuffer());
  int size=sizeof(addr);
  switch(nID)
  {
  case IDC_BTNDOWN:
  {
  y=m_LeftTopY+;
  *(WORD*)&buffer[]=x;
  *(WORD*)&buffer[]=y;
  int ret=sendto(m_Socket,&buffer[],,,(sockaddr*)&addr,size);
  break;
  }
  case IDC_BTUP:
  {y=m_LeftTopY-;
  *(WORD*)&buffer[]=x;
  *(WORD*)&buffer[]=y;
  int ret=sendto(m_Socket,&buffer[],,,(sockaddr*)&addr,size);
  break;
  }
  case IDC_BTLEFT:
  {
  x=m_LeftTopX-;
  *(WORD*)&buffer[]=x;
  *(WORD*)&buffer[]=y;
  int ret=sendto(m_Socket,&buffer[],,,(sockaddr*)&addr,size);
  break;
  }
  case IDC_BTRIGHT:
  {
  x=m_LeftTopX+;
  *(WORD*)&buffer[]=x;
  *(WORD*)&buffer[]=y;
  int ret=sendto(m_SOcket,&buffer[],,,(sockaddr*)&addr,size);
  break;
  }
  case IDC_BTRESTORE:
  {
  *(WORD*)&buffer[]=;
  *(WORD*)&buffer[]=;
  int ret=sendto(m_Socket,&buffer[],,,(sockaddr*)&addr,size);
  break;
  }
  }
  }
  }

---------------------------------

http://www.educity.cn/wenda/285572.html

上一篇:HtmlCleaner CleanerProperties 参数配置(转自macken博客,链接:http://macken.iteye.com/blog/1579809)


下一篇:03-maven学习-eclipse中创建maven项目