www.pudn.com > 截屏.rar > MyServerDlg.cpp
// MyServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MyServer.h"
#include "MyServerDlg.h"
#pragma comment (lib,"Ws2_32.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define CMD_SHOW 3
#define CMD_CAPTURE 4
#define CMD_GET 7
#define CMD_MSG 5
#define CMD_LOCK 9
#define CMD_UNLOCK 10
#define SERVER_PORT 10007
struct InfoData{
char Info[4];
DWORD dwSize;
WORD nCommand;
WORD nReserved;
char Msg[200];
};
struct InfoData *pInfo,SendInfo;
HANDLE hDib;
LPBYTE pImagePoint;
UINT MY_SOCK_SIZE= 1024;
DWORD WINAPI ThreadProc(CWnd* pWnd);
HANDLE hThread;
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CMyServerDlg dialog
CMyServerDlg::CMyServerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyServerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyServerDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMyServerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyServerDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyServerDlg, CDialog)
//{{AFX_MSG_MAP(CMyServerDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_Exit, OnExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyServerDlg message handlers
BOOL CMyServerDlg::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
WORD WINSOCK_VERSION=0x0101;
WSADATA wsaData;
if(WSAStartup(WINSOCK_VERSION,&wsaData))
AfxMessageBox("Cound not load Windows Socket DLL.");
else{
SendInfo.Info[0]='Y';
SendInfo.Info[1]='e';
SendInfo.Info[2]='s';
SendInfo.Info[3]='?';
DWORD thread2ID;
hThread=::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadProc,(LPVOID)this,0,&thread2ID);
}
ShowWindow(SW_MINIMIZE );
return TRUE; // return TRUE unless you set the focus to a control
}
void CMyServerDlg::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 CMyServerDlg::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 CMyServerDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMyServerDlg::OnExit()
{
// TODO: Add your control notification handler code here
TerminateThread( hThread, -1 );
WSACleanup();
EndDialog(1);
}
HANDLE DDBtoDIB( CBitmap& bitmap, DWORD dwCompression, CPalette* ppal )
{
BITMAP bm;
BITMAPINFOHEADER bi;
LPBITMAPINFOHEADER lpbi;
DWORD dwLen;
HANDLE hDib;
HANDLE handle;
HDC hdc;
HPALETTE hPal;
ASSERT( bitmap.GetSafeHandle() );
// the function has no arg for bitfields
if( dwCompression == BI_BITFIELDS )
return NULL;
// if a palette has not been supplied use defaul palette
hPal = (HPALETTE) ppal->GetSafeHandle();
if (hPal==NULL)
hPal = (HPALETTE) GetStockObject(DEFAULT_PALETTE );
// get bitmap information
bitmap.GetObject(sizeof(bm),(LPSTR)&bm);
// initialize the bitmapinfoheader
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = bm.bmPlanes * bm.bmBitsPixel;
bi.biCompression = dwCompression;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// compute the size of the infoheader and the color table
int ncolors = (1 << bi.biBitCount); if( ncolors> 256 )
ncolors = 0;
dwLen = bi.biSize + ncolors * sizeof(RGBQUAD);
// we need a device context to get the dib from
hdc = GetDC(NULL);
hPal = SelectPalette(hdc,hPal,FALSE);
RealizePalette(hdc);
// allocate enough memory to hold bitmapinfoheader and color table
hDib = GlobalAlloc(GMEM_FIXED,dwLen);
if (!hDib){
SelectPalette(hdc,hPal,FALSE);
ReleaseDC(NULL,hdc);
return NULL;
}
lpbi = (LPBITMAPINFOHEADER)hDib;
*lpbi = bi;//////看清楚
// call getdibits with a NULL lpbits param, so the device driver
// will calculate the bisizeimage field
GetDIBits(hdc, (HBITMAP)bitmap.GetSafeHandle(), 0L, (DWORD)bi.biHeight,
(LPBYTE)NULL, (LPBITMAPINFO)lpbi, (DWORD)DIB_RGB_COLORS );
bi = *lpbi;//看清楚
// if the driver did not fill in the bisizeimage field, then compute it
// each scan line of the image is aligned on a dword (32bit) boundary
if (bi.biSizeImage == 0){
bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
* bi.biHeight;
// if a compression scheme is used the result may infact be larger
// increase the size to account for this.
if (dwCompression != BI_RGB)
bi.biSizeImage = (bi.biSizeImage * 3) / 2;
}
// realloc the buffer so that it can hold all the bits
dwLen += bi.biSizeImage;
if (handle = GlobalReAlloc(hDib, dwLen, GMEM_MOVEABLE))
hDib = handle;
else{
GlobalFree(hDib);
// reselect the original palette
SelectPalette(hdc,hPal,FALSE);
ReleaseDC(NULL,hdc);
return NULL;
}
// get the bitmap bits
lpbi = (LPBITMAPINFOHEADER)hDib;
// finally get the dib
BOOL bgotbits = GetDIBits( hdc, (HBITMAP)bitmap.GetSafeHandle(),
0L, // start scan line
(DWORD)bi.biHeight, // # of scan lines
(LPBYTE)lpbi // address for bitmap bits
+ (bi.biSize + ncolors * sizeof(RGBQUAD)),
(LPBITMAPINFO)lpbi, // address of bitmapinfo
(DWORD)DIB_RGB_COLORS); // use rgb for color table
if( !bgotbits )
{
GlobalFree(hDib);
SelectPalette(hdc,hPal,FALSE);
ReleaseDC(NULL,hdc);
return NULL;
}
SelectPalette(hdc,hPal,FALSE);
ReleaseDC(NULL,hdc);
return hDib;
}
HANDLE Capture(CWnd *pWnd)
{
HANDLE hDib;
CBitmap bitmap;
CWindowDC dc(pWnd);
CDC memdc;
CRect rect;
WORD nWid,nHei;
memdc.CreateCompatibleDC(&dc);
pWnd->GetWindowRect(rect);
nWid=rect.Width();
nHei=rect.Height();
bitmap.CreateCompatibleBitmap(&dc, nWid,nHei);
CBitmap* poldbitmap = memdc.SelectObject(&bitmap);
memdc.StretchBlt(0, 0, nWid,nHei, &dc, 0, 0, rect.Width(),rect.Height(),SRCCOPY);
// create logical palette if device support a palette
CPalette pal;
if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
{
UINT nsize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * 256);
LOGPALETTE *plp = (LOGPALETTE *) new BYTE[nsize];
plp->palVersion = 0x300;
plp->palNumEntries = GetSystemPaletteEntries( dc, 0, 255, plp->palPalEntry );
// create the palette
pal.CreatePalette( plp );
delete[] plp;
}
memdc.SelectObject(poldbitmap);
// convert the bitmap to a dib
hDib = DDBtoDIB( bitmap, BI_RGB, &pal );
return hDib;
}
DWORD WINAPI ThreadProc(CWnd* pWnd)
{
int len;
char *buf;
buf=new char[MY_SOCK_SIZE];
SOCKET serverSocket;
SOCKADDR_IN serverSocketAddr,clientSocketAddr;
memset(&serverSocketAddr,0,sizeof(SOCKADDR_IN));
memset(&clientSocketAddr,0,sizeof(SOCKADDR_IN));
serverSocketAddr.sin_family=AF_INET;
serverSocketAddr.sin_addr.s_addr=htonl(INADDR_ANY);
serverSocketAddr.sin_port=htons(SERVER_PORT);
//当在服务器端,因为先执行接收操作,
//不用初始化clientSocketAddr,在执行接受后,自动填充
/* clientSocketAddr.sin_family=AF_INET;
clientSocketAddr.sin_addr.s_addr=htonl(INADDR_ANY);
clientSocketAddr.sin_port=htons(1087);*/
int addrinSize=sizeof(SOCKADDR_IN);
if((serverSocket=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP))!=INVALID_SOCKET)
{
if(bind(serverSocket,(SOCKADDR*)&serverSocketAddr,sizeof(SOCKADDR_IN))!=SOCKET_ERROR)
{
BOOL fBroadCastEnable=1;
if(SOCKET_ERROR ==setsockopt(serverSocket,SOL_SOCKET,SO_BROADCAST ,(char FAR * )&fBroadCastEnable,sizeof(BOOL)))AfxMessageBox("Can't enable broadcast");
while(1){
len=recvfrom ( serverSocket,buf,MY_SOCK_SIZE, 0, (SOCKADDR*)&clientSocketAddr, &addrinSize );
///AfxMessageBox(inet_ntoa(clientSocketAddr.sin_addr));// 此语句用来测试能否自动填充clientSocketAddr结构,经过测试,是可以的
pInfo=(struct InfoData*)buf;
if(len>0&&pInfo->Info[0]=='Y'&&pInfo->Info[3]=='?')
switch(pInfo->nCommand)
{
case CMD_LOCK:
pWnd->ShowWindow(SW_RESTORE );
pWnd->GetDlgItem(IDC_MSG1)->SetWindowText(_T("此计算机已经被锁定,请找网络管理人员解锁!"));
EnableWindow (GetDesktopWindow (),FALSE);//设置桌面窗口无效,用户将不能进行任何操作,只能Ctrl+Alt+Del重启动
break;
case CMD_UNLOCK:
pWnd->GetDlgItem(IDC_MSG1)->SetWindowText(_T("解锁"));
pWnd->ShowWindow(SW_MINIMIZE );
EnableWindow (GetDesktopWindow (),TRUE);//设置桌面窗口使能,用户可以操作计算机
break;
case CMD_MSG:
pWnd->ShowWindow(SW_RESTORE );
pWnd->GetDlgItem(IDC_MSG1)->SetWindowText(_T(pInfo->Msg));
break;
case CMD_CAPTURE :
buf=new char[MY_SOCK_SIZE];
hDib=Capture(pWnd->GetDesktopWindow ());
if(hDib){
SendInfo.nCommand=CMD_SHOW;
SendInfo.dwSize=GlobalSize(hDib);
//发送应答信号
len=sendto ( serverSocket,(char*)&SendInfo,sizeof(SendInfo), 0, (SOCKADDR*)&clientSocketAddr, addrinSize );
len=recvfrom ( serverSocket,buf,MY_SOCK_SIZE, 0, (SOCKADDR*)&clientSocketAddr, &addrinSize );
pInfo=(struct InfoData*)buf;
pImagePoint=(LPBYTE)hDib;
for(WORD i=0;ibiBitCount); if( ncolors> 256 )
ncolors = 0;
//用于显示
CRect rc;
GetClientRect(rc);
unsigned char *p;
p=(LPBYTE)GlobalLock( hDib);
//下面代码:性能一般的显示
::StretchDIBits(GetDC()->GetSafeHdc (),
0,0,rc.Width(),rc.Height(),
0,0,((LPBITMAPINFOHEADER)p)->biWidth,((LPBITMAPINFOHEADER)p)->biHeight,
(LPBYTE)p // address for bitmap bits
+ (sizeof(BITMAPINFOHEADER) + ncolors * sizeof(RGBQUAD)),
(LPBITMAPINFO)p,
DIB_RGB_COLORS,SRCCOPY);
GlobalUnlock(hDib);
GlobalFree(hDib);
}
}*/