📄 sreenclientdlg.cpp
字号:
// SreenClientDlg.cpp : implementation file
//
#include "stdafx.h"
#include "SreenClient.h"
#include "SreenClientDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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()
/////////////////////////////////////////////////////////////////////////////
// CSreenClientDlg dialog
CSreenClientDlg::CSreenClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSreenClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSreenClientDlg)
m_ip = _T("");
m_port = 0;
m_usrpwd = _T("");
m_usrname = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSreenClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSreenClientDlg)
DDX_Control(pDX, IDC_SCROLLBAR2, m_hbar);
DDX_Control(pDX, IDC_SCROLLBAR1, m_vbar);
DDX_Text(pDX, IDC_EDIT_IP, m_ip);
DDX_Text(pDX, IDC_EDIT_PORT, m_port);
DDX_Text(pDX, IDC_EDIT_PWD, m_usrpwd);
DDX_Text(pDX, IDC_EDIT_USRNAME, m_usrname);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSreenClientDlg, CDialog)
//{{AFX_MSG_MAP(CSreenClientDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
ON_BN_CLICKED(IDC_BUTTON_GETCAP, OnButtonGetcap)
ON_WM_VSCROLL()
ON_WM_HSCROLL()
ON_EN_CHANGE(IDC_EDIT_PORT, OnChangeEditPort)
ON_BN_CLICKED(IDC_BUTTON_SAVEBMP, OnButtonSavebmp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSreenClientDlg message handlers
BOOL CSreenClientDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// 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
// TODO: Add extra initialization here
pclient=NULL;
m_ip="127.0.0.1";
m_port=3009;
m_usrpwd="123";
m_usrname="aaa";
UpdateData(FALSE);
m_pBIH=NULL;
m_pDibBits=NULL;
m_pDib=NULL;
pklen=sizeof(NETPACKAGE);
lbitsOffset=0;
capflag=0;
bcontrol=0;
initDrawParam();
return TRUE; // return TRUE unless you set the focus to a control
}
void CSreenClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSreenClientDlg::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();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CSreenClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSreenClientDlg::OnButtonConnect()
{
// TODO: Add your control notification handler code here
CString name;
GetDlgItemText(IDC_BUTTON_CONNECT,name);
UpdateData(TRUE);
if(name=="连接")
{
pclient=new CClientSock;
pclient->Create();//创建普通的SOCKET
pklen=sizeof(NETPACKAGE);
pclient->setdlg(this,1,pklen);
int rt=pclient->Connect(m_ip,m_port);//连接服务端
if(rt==1)
{
strcpy(usrinfo.name,m_usrname.GetBuffer(0));
strcpy(usrinfo.password,m_usrpwd.GetBuffer(0));
usrinfo.uport=3100;
pclient->SendCommandData(1,(char*)&usrinfo,sizeof(USERINFO));
}
else
{
AfxMessageBox("连接服务器失败!");
SetDlgItemText(IDC_BUTTON_CONNECT,"连接");
}
}
else
{
pclient->SendCommandData(2,(char*)&usrinfo,sizeof(USERINFO));
pclient->Close();
delete pclient;
pclient=NULL;
if(m_pDib!=NULL)
delete[] m_pDib;
m_pDib=NULL;
SetDlgItemText(IDC_BUTTON_CONNECT,"连接");
}
}
void CSreenClientDlg::myRecvData(CClientSock *pc)
{
CString tstr;
pc->GetPackage((char*)&usrpack);
switch(usrpack.type)
{
case 1://用户登录
memcpy(&usrinfo,usrpack.buf,sizeof(usrinfo));
// tstr.Format("Ack Login:%s,%s,%d",usrinfo.name,usrinfo.password,usrinfo.uport);
// AfxMessageBox(tstr);
tstr=usrinfo.password;
if(tstr=="passwork ok"&m_usrname=="aaa")
{
AfxMessageBox("登录成功!");//密码正确
SetDlgItemText(IDC_BUTTON_CONNECT,"退出连接");
}
else
AfxMessageBox("用户名或密码错误,请重新登录!");////密码错误
break;
case 2://用户退出
memcpy(&usrinfo,usrpack.buf,sizeof(usrinfo));
tstr.Format("OK Logoff",usrinfo.name,usrinfo.password,usrinfo.uport);
AfxMessageBox(tstr);
break;
case 3:
memcpy(&cheader,usrpack.buf,sizeof(cheader));
if(m_pDib==NULL)
{
int m_dwDibSize = cheader.filelength + sizeof( BITMAPINFOHEADER )+1000;
m_pDib = new unsigned char[m_dwDibSize];
m_pDibBits=&m_pDib[sizeof(BITMAPINFOHEADER )];
}
lbitsOffset=0;
break;
case 4:
if(m_pDibBits!=NULL&&usrpack.len==50000)
{
memcpy(&m_pDibBits[lbitsOffset],usrpack.buf,usrpack.len);
lbitsOffset=lbitsOffset+usrpack.len;
}
break;
case 5:
if(m_pDibBits!=NULL)
{
memcpy(&m_pDibBits[lbitsOffset],usrpack.buf,usrpack.len);
lbitsOffset=lbitsOffset+usrpack.len;
}
if(lbitsOffset!=cheader.filelength)//接收错误
{
tstr.Format("Error:%d!=%d",lbitsOffset,cheader.filelength);
AfxMessageBox(tstr);
return;
}
m_pBIH = (BITMAPINFOHEADER*)m_pDib;
m_pBIH->biSize= sizeof(BITMAPINFOHEADER);
m_pBIH->biWidth = cheader.width;
m_pBIH->biHeight= cheader.height;
m_pBIH->biPlanes= 1;
m_pBIH->biBitCount= 24;
m_pBIH->biCompression= BI_RGB;
m_pBIH->biSizeImage = 0;
m_pBIH->biXPelsPerMeter = 0;
m_pBIH->biYPelsPerMeter = 0;
m_pBIH->biClrUsed= 0;
m_pBIH->biClrImportant= 0;
initDrawSreen();
break;
}
}
void CSreenClientDlg::OnButtonGetcap()
{
// TODO: Add your control notification handler code here
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -