⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socksever.cpp

📁 屏幕截取的服务端
💻 CPP
字号:
// Project  : WindowSock Server
// Producer : WangQi
// Date     : 2001/04
///////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

//Defien Cons
#define MAX_LOADSTRING  100
#define MAJOR_VERSION     1
#define MINOR_VERSION     2
#define WM_SOCK         280
#define US_MAXSIZE     1024
#define US_FLAG        9999
#define US_TIME		    300

#define US_DIBFLAG        8
#define US_MOUSEMOVE   2000
#define US_MOUSELD     3100
#define US_MOUSELU     3200
#define US_MOUSERD     4100
#define US_MOUSERU     4200

#define US_DESKTOPBIT  5000

#define US_LOCK        6100
#define US_UNLOCK      6200	

// Global Variables:
HINSTANCE hInst;										// current instance
TCHAR     szTitle[MAX_LOADSTRING];			    		// The title bar text
TCHAR     szWindowClass[MAX_LOADSTRING];				// The title bar text
BOOL      CONNECT;										// Flag
BOOL	  MouseEventFlag = true;
BOOL      BitFlag = true;		
HWND      SyshWnd;				
SOCKET    ServerSock;
char      server_address[256] = {0};
struct    sockaddr_in server;

// Funtion Define:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
BOOL                IniSock(HWND hWnd);
BOOL                ConnectSock();
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	Ip(HWND, UINT, WPARAM, LPARAM);
void                SendMousePoint(HWND hWnd);
void                MouseEvent(int MsgM,HWND hWnd);
void                DibEvent(HWND hWnd);
void                SysEvent(int MsgM,HWND hWnd);
void				GetDesktopBit(HWND hWnd);


///////////////////////////////////////////////////////////////////
// Name      : WinMain()
// Parameter : HINSTANCE hInstance,HINSTANCE hPrevInstance,
//             LPSTR lpCmdLine,int nCmdShow
// Return    : INT
///////////////////////////////////////////////////////////////////

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_SOCKSEVER, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SOCKSEVER);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return msg.wParam;
}


///////////////////////////////////////////////////////////////////
// Name      : MyRegisterClass()
// Parameter : HINSTANCE hInstance
// Return    : ATOM
///////////////////////////////////////////////////////////////////

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_SOCKSEVER);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDC_SOCKSEVER;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}


///////////////////////////////////////////////////////////////////
// Name      : InitInstance()
// Parameter : HINSTANCE hInstance, int nCmdShow
// Return    : BOOL
///////////////////////////////////////////////////////////////////

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   SyshWnd = hWnd;
   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}


///////////////////////////////////////////////////////////////////
// Name      : WndProc()
// Parameter : HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam
// Return    : CALLBACK Funtion
///////////////////////////////////////////////////////////////////

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;

	switch (message) 
	{
		case WM_COMMAND:
			wmId    = LOWORD(wParam); 
			wmEvent = HIWORD(wParam); 
			// Parse the menu selections:
			switch (wmId)
			{
				case IDM_CONNECT:
				   DialogBox(hInst, (LPCTSTR)IDD_IP, hWnd, (DLGPROC)Ip);
				   break;
				case IDM_ABOUT:
				   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
				   break;
				case IDM_EXIT:
				   DestroyWindow(hWnd);
				   break;
				case IDM_LOCK:
					//Menu Set
					MENUITEMINFO info;
				    info.cbSize = sizeof (MENUITEMINFO); 
				    info.fMask = MIIM_STATE;             
					GetMenuItemInfo(GetMenu(hWnd),IDM_LOCK,FALSE,&info);
					if (info.fState == MFS_CHECKED)
					{
						CheckMenuItem(GetMenu(hWnd),IDM_LOCK,MF_UNCHECKED);
						if (CONNECT&&MouseEventFlag)
						{
							SysEvent(US_UNLOCK,hWnd);
						}
					}
					else
					{
						CheckMenuItem(GetMenu(hWnd),IDM_LOCK,MF_CHECKED);
						if (CONNECT&&MouseEventFlag)
						{
							SysEvent(US_LOCK,hWnd);
						}
					}
					break;
				default:
				   return DefWindowProc(hWnd, message, wParam, lParam);
			}
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		case WM_MOUSEMOVE:
			if (CONNECT&&MouseEventFlag)
			{
				MouseEvent(US_MOUSEMOVE,hWnd);
			}
			break;
		case WM_LBUTTONDOWN:
			if (CONNECT&&MouseEventFlag)
			{
				MouseEvent(US_MOUSELD,hWnd);
			}
			break;
		case WM_LBUTTONUP:
			if (CONNECT&&MouseEventFlag)
			{
				MouseEvent(US_MOUSELU,hWnd);
			}
			break;
		case WM_RBUTTONDOWN:
			if (CONNECT&&MouseEventFlag)
			{
				MouseEvent(US_MOUSERD,hWnd);
			}
			break;
		case WM_RBUTTONUP:
			if (CONNECT&&MouseEventFlag)
			{
				MouseEvent(US_MOUSERU,hWnd);
			}
			break;
		case WM_TIMER:
			if (CONNECT&&BitFlag)
			{
				//Lock MySelf
				BitFlag = false;
				DibEvent(hWnd);
				BitFlag = true;
			}
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}


///////////////////////////////////////////////////////////////////
// Name      : About()
// Parameter : HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam
// Return    : CALLBACK Funtion
///////////////////////////////////////////////////////////////////

LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog(hDlg, LOWORD(wParam));
				return TRUE;
			}
			break;
	}
    return FALSE;
}

///////////////////////////////////////////////////////////////////
// Name      : Ip()
// Parameter : HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam
// Return    : CALLBACK Funtion
///////////////////////////////////////////////////////////////////

LRESULT CALLBACK Ip(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_INITDIALOG:
				return TRUE;

		case WM_COMMAND:
			switch(wParam)
			{
				case IDC_CANCEL:			//Cancel
					EndDialog(hDlg, LOWORD(wParam));
					return TRUE;
					break;
				case IDC_CONNECT:			//Connect
					GetDlgItemText(hDlg,IDC_IP,server_address,256);
					EndDialog(hDlg, LOWORD(wParam));
					//Ini Sock Start Connect		
  				    if (!IniSock(SyshWnd))
						MessageBox(0,"IniSock err",
							 "err",MB_OK);
					return TRUE;
					break;
			}
	}
    return FALSE;
}

///////////////////////////////////////////////////////////////////
// Name      : IniSock()
// Parameter : HWND hWnd
// Return    : BOOL
///////////////////////////////////////////////////////////////////

BOOL IniSock(HWND hWnd)
{
	//Define Variable
	WORD wVersionrequested;
	WSADATA wsaData;

	wVersionrequested = MAKEWORD(2,0);
	
	//Start Sock
	int err = WSAStartup(wVersionrequested,&wsaData);
	if (err == -1)
	{
			MessageBox(0,"WSAStartup err",
				 "error",MB_OK);
			return FALSE;
	}

	//ALREAD CONNECT
	CONNECT = TRUE;
	SetTimer(hWnd,IDT_TIMER,US_TIME,NULL);
	return TRUE;
}


///////////////////////////////////////////////////////////////////
// Name      : ConnectSock()
// Parameter : HWND hWnd
// Return    : BOOL
///////////////////////////////////////////////////////////////////
BOOL ConnectSock()
{
	//Define Variable
	int msgsock;

	//Ini Sock
	ServerSock = socket(AF_INET,SOCK_STREAM,0);
	if (ServerSock < 0)
	{
			MessageBox(0,"scoker err",
				 "err",MB_OK);
			return FALSE;
	}

	//Connect
	server.sin_family = PF_INET;
	server.sin_port = htons(7016);
	server.sin_addr.s_addr  = inet_addr(server_address);

	msgsock = connect(ServerSock,(struct sockaddr*)&server,sizeof(server));
	if (msgsock!=0)
	{
		return FALSE;
	}
	return TRUE;
}


///////////////////////////////////////////////////////////////////
// Name      : SendMousePoint()
// Parameter : HWND hWnd
// Return    : void
///////////////////////////////////////////////////////////////////

void SendMousePoint(HWND hWnd)
{
	//Define Variable
	POINT point,point1;
	RECT  rect;
	HDC   hdc;
	long  x,y;
	float fx,fy;
	int   ClientWidth,ClientHeight; 
	
	//Get Client Width and Height
	recv(ServerSock,(char*)&ClientWidth,sizeof(ClientWidth)+1,0);
	recv(ServerSock,(char*)&ClientHeight,sizeof(ClientHeight)+1,0);

	//GetMousePoint
	GetCursorPos(&point);
	hdc = GetDC(hWnd);
	GetDCOrgEx(hdc,&point1);
	point.x = point.x - point1.x;
	point.y = point.y - point1.y;

	GetClientRect(hWnd,&rect);
	//GetWindowRect(hWnd,&rect);

	fx = (float)point.x;
	fy = (float)point.y;

	fx = fx * ClientWidth/rect.right;
	fy = fy * ClientHeight/rect.bottom;
	
	point.x = (long)fx;
	point.y = (long)fy;
	
	x = point.x;
	y = point.y;
	
	//SendMousePoint
	send(ServerSock,(char*)&x,sizeof(x)+1,MSG_OOB);
	send(ServerSock,(char*)&y,sizeof(y)+1,MSG_OOB);
}


///////////////////////////////////////////////////////////////////
// Name      : MouseEvent()
// Parameter : LPCSTR Msg
//           : HWND hWnd
// Return    : void
///////////////////////////////////////////////////////////////////

void MouseEvent(int Msg,HWND hWnd)
{
	//Define Variable
	int Flag;

	//CONNECT
	CONNECT = ConnectSock();
	if (!CONNECT)
		MessageBox(0,"connect err",
			"err",MB_OK|MB_APPLMODAL);

	//Accept Flag
	recv(ServerSock,(char*)&Flag,sizeof(Flag)+1,0);
	if (Flag == US_FLAG)
	{
		//Send MouseMove
		send(ServerSock,(char*)&Msg,sizeof(Msg)+1,MSG_OOB);
		SendMousePoint(hWnd);
	}	
	//closesocket(ServerSock);
}


///////////////////////////////////////////////////////////////////
// Name      : DibEvent()
// Parameter : HWND hWnd
// Return    : void
///////////////////////////////////////////////////////////////////

void DibEvent(HWND hWnd)
{
	//Define Variable
	int  Flag,Msg;
	int  BitWidth,BitHeight;
	RECT rect;

	//CONNECT
	CONNECT = ConnectSock();
	if (!CONNECT)
		MessageBox(0,"connect err",
			"err",MB_OK|MB_APPLMODAL);

	//Get Windown Height and Weidth
	GetWindowRect(hWnd,&rect);
	BitWidth  = rect.right  - rect.left;
	BitHeight = rect.bottom - rect.top;
	
	//Accept Flag
	recv(ServerSock,(char*)&Flag,sizeof(Flag)+1,0);
	if (Flag == US_FLAG)
	{
		MouseEventFlag = false;

		//Send Msg
		Msg = US_DESKTOPBIT;
		send(ServerSock,(char*)&Msg,sizeof(Msg)+1,MSG_OOB);

		//Send Bit Height and Weidth
		send(ServerSock,(char*)&BitWidth,sizeof(BitWidth)+1,MSG_OOB);
		send(ServerSock,(char*)&BitHeight,sizeof(BitHeight)+1,MSG_OOB);
		
		//Recv Bit
		GetDesktopBit(hWnd);
		MouseEventFlag = true;
	}

	//Release Data
	closesocket(ServerSock);
}


///////////////////////////////////////////////////////////////////
// Name      : SysEvent()
// Parameter : LPCSTR Msg
//           : HWND hWnd
// Return    : void
///////////////////////////////////////////////////////////////////

void SysEvent(int Msg,HWND hWnd)
{
	//Define Variable
	int Flag;

	//CONNECT
	CONNECT = ConnectSock();
	if (!CONNECT)
		MessageBox(0,"connect err",
			"err",MB_OK|MB_APPLMODAL);

	//Accept Flag
	recv(ServerSock,(char*)&Flag,sizeof(Flag)+1,0);
	if (Flag == US_FLAG)
	{
		//Send MouseMove
		send(ServerSock,(char*)&Msg,sizeof(Msg)+1,MSG_OOB);
	}	
	//closesocket(ServerSock);
}


///////////////////////////////////////////////////////////////////
// Name      : GetDesktopBit()
// Parameter : HWND hwnd
// Return    : void
///////////////////////////////////////////////////////////////////

void GetDesktopBit(HWND hWnd)
{
	//Define Variable
	char *buf;
	buf=new char[US_MAXSIZE];
	int len;
	int Flag;
	DWORD bitSize;
	HDC hdc;
	HANDLE hDib;
	RECT rect;
	BYTE *p,*p2;
	
	Flag = US_DIBFLAG;

	//Get Bit Size
	recv(ServerSock,(char*)&bitSize,sizeof(bitSize)+1,0);
	send(ServerSock,(char*)&Flag,sizeof(Flag)+1,MSG_OOB);

	//allocates Memory
	hDib = GlobalAlloc(GMEM_MOVEABLE,bitSize);

	//Lock to Byte
	p = (LPBYTE)GlobalLock(hDib);
	p2 = p;
	
	//Get Dib Data
	for(WORD i=0;i<bitSize/US_MAXSIZE;i++)
	{
		len = recv(ServerSock,buf,US_MAXSIZE,0);
		CopyMemory(p2,buf,US_MAXSIZE);
		p2 = p2 + US_MAXSIZE;
     	send(ServerSock,(char*)&Flag,sizeof(Flag)+1,MSG_OOB);
	}
	if (bitSize%US_MAXSIZE)
	{
		len = recv(ServerSock,buf,bitSize%US_MAXSIZE,0);
		CopyMemory(p2,buf,len);
		p2 = p2 + bitSize%US_MAXSIZE;
	    send(ServerSock,(char*)&Flag,sizeof(Flag)+1,MSG_OOB);
	}
	p2 = p2 - bitSize;

	// Get Dc
	hdc = GetDC(hWnd);

	GetClientRect(hWnd,&rect);

	// Define Colors
	int color = (1<<((LPBITMAPINFOHEADER)p2)->biBitCount);
	if(color>256)
		color = 0;

	// Draw Bit to Window Dc
	StretchDIBits(hdc, 0, 0, rect.right,rect.bottom,0,0,
		((LPBITMAPINFOHEADER)p)->biWidth, 
		((LPBITMAPINFOHEADER)p)->biHeight,
		(LPBYTE)p+(sizeof(BITMAPINFOHEADER)+color*sizeof(RGBQUAD)),
		(LPBITMAPINFO)p,DIB_RGB_COLORS, SRCCOPY);

	//Release 
	GlobalFree(hDib);
	ReleaseDC(hWnd,hdc);
	delete buf;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -