DrawScreen();
deletebmpdata;
bmpdata=NULL;
size=0;
}
使DrawScreen()来在窗口中绘制接收到的图形:
voidCScreenDlg:
:
DrawScreen()
{
CDC*dc=m_drawscreen.GetDC();
BITMAPINFOHEADERbih;
bih.biBitCount=bitmap.bmBitsPixel;
bih.biClrImportant=0;//绘制图片相应参数
bih.biClrUsed=0;
bih.biCompression=0;
bih.biHeight=bitmap.bmHeight;
bih.biPlanes=1;
bih.biSize=sizeof(BITMAPINFOHEADER);
bih.biSizeImage=size;
bih.biWidth=bitmap.bmWidth;
bih.biXPelsPerMeter=0;
bih.biYPelsPerMeter=0;
CBitmapbm;
bm.CreateBitmapIndirect(&bitmap);
CDCbmpdc;
bmpdc.CreateCompatibleDC(dc);
SetDIBits(bmpdc.m_hDC,bm,0,bitmap.bmHeight,bmpdata,(BITMAPINFO*)&bih,DIB_RGB_COLORS);
bmpdc.SelectObject(&bm);
CRectrect;
m_drawscreen.GetClientRect(&rect);
dc->StretchBlt(0,0,rect.Width(),rect.Height(),&bmpdc,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);
}
当关闭窗口时会调用OnStopf来做处理:
voidCScreenDlg:
:
OnStop()
{
this->KillTimer(0);//结束计时
clientsocket.ShutDown
(2);
clientsocket.Close();//关闭套接字
}
另外,继承自CSocket的CTCPClentSocket和CTCPServerSocket分别定义了用于接收的OnAccept()用于发送的OnRecieve()用于关闭套接字的OnClose();
OnAccept:
voidCTCPServerSocket:
:
OnAccept(intnErrorCode)
{
CSocket:
:
OnAccept(nErrorCode);
clientsocket=newCTCPClientSocket(this);
this->Accept(*clientsocket);
POSITIONpos=ClientList.GetHeadPosition();
ClientList.InsertBefore(pos,clientsocket);
if(serverevent!
=NULL)
clientsocket->SetClientEvent(serverevent);
if(serverevent!
=NULL)
serverevent->OnAccept(this,nErrorCode);
}
OnRecieve:
voidCTCPClientSocket:
:
OnReceive(intnErrorCode)
{
CSocket:
:
OnReceive(nErrorCode);
if(clientevent!
=NULL)
clientevent->OnReceive(this,nErrorCode);
}
OnClose:
voidCTCPClientSocket:
:
OnClose(intnErrorCode)
{
CSocket:
:
OnClose(nErrorCode);
if(clientevent!
=NULL)
clientevent->OnClose(this,nErrorCode);
POSITIONpos=
serversocket->ClientList.GetHeadPosition();
POSITIONoldpos;
while(pos!
=NULL)
{
oldpos=pos;
CTCPClientSocket*socket=
(CTCPClientSocket*)serversocket->ClientList.GetNext(pos);
if(socket!
=NULL)
serversocket->ClientList.RemoveAt(oldpos);
}
}
课程设计总结:
经过的了一周的学习和实践,加深了对网络程序设计的理解和应用。
理解和掌握了网络套接字的用法,使用规则。
在编程上拓宽了自己的思路,学会了一些灵活的方法,熟练了编程技巧。
这次的课程设计中不仅检验了我所学习的知识,也培养了我如何去把握一件事情,如何去做一件事情,又如何完成一件事情。
在设计过程中,与同学分工设计,和同学们相互探讨,相互学习,相互监督。
对于如何参考文献有了更深的理解。
参考资料:
WINDOWS网络与通信设计王艳平
visualc++范例完全自学手册
附源代码:
Screen工程中:
Resourse.h
//{{NO_DEPENDENCIES}}
//MicrosoftDeveloperStudiogeneratedincludefile.
//UsedbyScreen.rc
//
#defineIDM_ABOUTBOXcreen0x0010
#defineIDD_ABOUTBOX100
#defineIDS_ABOUTBOX101
#defineIDD_SCREEN_DIALOG102
#defineIDR_MAINFRAME128
#defineIDC_BUTTON11000
#defineIDC_BUTTON21001
#defineIDC_DRAWSCREEN1002
//Nextdefaultvaluesfornewobjects
//
#ifdefAPSTUDIO_INVOKED
#ifndefAPSTUDIO_READONLY_SYMBOLS
#define_APS_NEXT_RESOURCE_VALUE131
#define_APS_NEXT_COMMAND_VALUE32771
#define_APS_NEXT_CONTROL_VALUE1003
#define_APS_NEXT_SYMED_VALUE101
#endif
#endif
Screen.h:
//Screen.h:
mainheaderfilefortheSCREENapplication
//
#if!
defined(AFX_SCREEN_H__1141ED47_1E96_4C04_B767_6555BE0E67B4__INCLUDED_)
#defineAFX_SCREEN_H__1141ED47_1E96_4C04_B767_6555BE0E67B4__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
#ifndef__AFXWIN_H__
#errorinclude'stdafx.h'beforeincludingthisfileforPCH
#endif
#include"resource.h"//mainsymbols
/////////////////////////////////////////////////////////////////////////////
//CScreenApp:
//SeeScreen.cppfortheimplementationofthisclass
//
classCScreenApp:
publicCWinApp
{
public:
CScreenApp();
//Overrides
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CScreenApp)
public:
virtualBOOLInitInstance();
//}}AFX_VIRTUAL
//Implementation
//{{AFX_MSG(CScreenApp)
//NOTE-theClassWizardwilladdandremovememberfunctionshere.
//DONOTEDITwhatyouseeintheseblocksofgeneratedcode!
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
#endif//!
defined(AFX_SCREEN_H__1141ED47_1E96_4C04_B767_6555BE0E67B4__INCLUDED_)
ScreenDlg.h:
//ScreenDlg.h:
headerfile
//
#include"TCPServer.h"
#if!
defined(AFX_SCREENDLG_H__9DB60DB4_6CA7_45EC_997B_1FC964D08D10__INCLUDED_)
#defineAFX_SCREENDLG_H__9DB60DB4_6CA7_45EC_997B_1FC964D08D10__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
/////////////////////////////////////////////////////////////////////////////
//CScreenDlgdialog
classCScreenDlg:
publicCDialog
{
//Construction
protected:
boolrun;
BITMAPbitmap;
intsize;
char*bmpdata;
voidGetScreen();
voidDrawScreen();
public:
CTCPClientSocketclientsocket;
CScreenDlg(CWnd*pParent=NULL);//standardconstructor
//DialogData
//{{AFX_DATA(CScreenDlg)
enum{IDD=IDD_SCREEN_DIALOG};
CStaticm_drawscreen;
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CScreenDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
HICONm_hIcon;
//Generatedmessagemapfunctions
//{{AFX_MSG(CScreenDlg)
virtualBOOLOnInitDialog();
afx_msgvoidOnSysCommand(UINTnID,LPARAMlParam);
afx_msgvoidOnPaint();
afx_msgHCURSOROnQueryDragIcon();
afx_msgvoidOnStart();
afx_msgvoidOnSize(UINTnType,intcx,intcy);
afx_msgvoidOnTimer(UINTnIDEvent);
afx_msgvoidOnStop();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
#endif//!
defined(AFX_SCREENDLG_H__9DB60DB4_6CA7_45EC_997B_1FC964D08D10__INCLUDED_)
StdAfx.h:
//stdafx.h:
includefileforstandardsystemincludefiles,
//orprojectspecificincludefilesthatareusedfrequently,but
//arechangedinfrequently
//
#define_WIN32_WINNT0x0500
#if!
defined(AFX_STDAFX_H__19AD48ED_1769_499E_925D_EC594502C8AD__INCLUDED_)
#defineAFX_STDAFX_H__19AD48ED_1769_499E_925D_EC594502C8AD__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
#defineVC_EXTRALEAN//Excluderarely-usedstufffromWindowsheaders
#include//MFCcoreandstandardcomponents
#include//MFCextensions
#include//MFCAutomationclasses
#include//MFCsupportforInternetExplorer4CommonControls
#ifndef_AFX_NO_AFXCMN_SUPPORT
#include