您的位置:首页 > 其它

鼠标的简单远程控制(原创)

2006-01-10 17:10 423 查看
思想:通过构建一个结构包,包里有鼠标的状态,鼠标的位置,在模拟端根据包信息模拟鼠标的动作,
实现远程鼠标的控制.
几个环节:
1:发送端发出的鼠标位置是逻辑坐标.
2:模拟端必须把收到的包里的鼠标位置,转换为屏幕坐标的位置,再用SetCursorPos实现坐标移动.
用到的函数及作用:
ScreenToClient 屏幕坐标转换成逻辑坐标
ClientToScreen 逻辑坐标转换成屏幕坐标
SetCursorPos 设置鼠标的屏幕位置
mouse_event 模拟鼠标的动作
ClipCursor 圈定鼠标的活动范围
GetWindowRec 获得对象大小
SetCapture 捕获鼠标信息
ReleaseCapture 取消捕获鼠标信息
一下代码可以实现远程的鼠标移动,起到抛砖引玉作用,读者可以自行添加其他功能
struct point_pos
{
int event;
CPoint p;
} ;

server:
//变量
SOCKET m_hServerSocket;
sockaddr_in addr,tempaddr;
int len ;
HANDLE handle1,handle2;
//监听函数
void CServerDlg::Onlisten()
{
int iResult;
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
m_hServerSocket=socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
if(m_hServerSocket==INVALID_SOCKET)
{
//错误处理
MessageBox("ff");
}

addr.sin_family=AF_INET;
//addr.sin_addr.S_un.S_addr =inet_addr("127.0.0.1");
addr.sin_addr.S_un.S_addr =INADDR_ANY; // 自己捕获地址
addr.sin_port =htons(8000);
iResult=bind(m_hServerSocket,(sockaddr*)&addr,sizeof(sockaddr));
if (iResult==SOCKET_ERROR)
{
}
int iListen=listen(m_hServerSocket,2);
if (iListen==SOCKET_ERROR)
{
}
else AfxMessageBox("成功");

UpdateData(false);
handle1 = CreateThread(NULL,0,ThreadPro,0,0,NULL);
}

//数据接受函数
DWORD WINAPI CServerDlg::ThreadPro(LPVOID lpParam)
{
CServerDlg * pDlg;
point_pos pos;

pDlg = (CServerDlg*)AfxGetMainWnd();
pDlg->len = sizeof(sockaddr);
pDlg->MessageBox("hahahha");
SOCKET s_d=accept(pDlg->m_hServerSocket ,(sockaddr*)&(pDlg->addr) ,&(pDlg->len) );
pDlg->MessageBox("ok");

if(pDlg->m_hServerSocket==INVALID_SOCKET)
{

}
int err;
int l =sizeof(sockaddr);
while (1)
{

datapocket RecvBag;
err=recv(s_d,(char*)&pos,sizeof(point_pos),0);

// memcpy(&RecvBag,buff,sizeof(datapocket));//将接收到的结构体赋给RecvBag
if (err==SOCKET_ERROR||err==0)
{ break;
}
else
{
pDlg->ClientToScreen(&pos.p);
SetCursorPos(pos.p.x,pos.p.y);

}
}
return 0;
}
client:
//变量
point_pos pos;
SOCKET m_hSocket;

//连接函数
void CClientDlg::OnOK()
{
// UpdateData(true);
// WSADATA wsaData;
SOCKADDR_IN SockAddr;
int iResult;
WORD wVersionRequested;
WSADATA wsaData;
int err;

wVersionRequested = MAKEWORD( 2, 2 );

err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return;
}
m_hSocket=socket(PF_INET,SOCK_STREAM,0);
SockAddr.sin_family=AF_INET; //使用TCP/IP协议
//SockAddr.sin_port=htons(0); //客户端指定的IP地址
SockAddr.sin_port=htons(8000); //客户端指定的IP地址
SockAddr.sin_addr.S_un.S_addr=inet_addr("192.168.0.2");//客户端指定的IP地址
int nConnect=connect(m_hSocket,(LPSOCKADDR)&SockAddr,sizeof(SockAddr)); //请求连接
if (nConnect==SOCKET_ERROR)
MessageBox("连接失败!");
else
MessageBox("连接成功!",NULL,MB_OK);

}
//发送函数
void CClientDlg::OnMouseMove(UINT nFlags, CPoint point)
{

pos.event=MOUSEEVENTF_MOVE;
pos.p=point;
int nResult=send(m_hSocket,(char*)(&pos),sizeof(point_pos),0);

CDialog::OnMouseMove(nFlags, point);
}
此文结束.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: