鼠标的简单远程控制(原创)
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);
}
此文结束.
实现远程鼠标的控制.
几个环节:
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);
}
此文结束.
相关文章推荐
- 简单远程控制(仅传递鼠标和键盘消息)的实现
- 简单远程控制(仅传递鼠标和键盘消息)的实现
- 使用anyremote进行远程鼠标控制
- Unity简单利用屏幕坐标转换实现鼠标控制物体移动
- [Unity3D]Unity3D游戏开发之史上最简单的鼠标点击控制人物走动实现
- 20171012学习笔记Selenium 2 第四章WebDriver API 4.2控制浏览器 4.3简单元素操作4.4鼠标事件4.5键盘事件
- 用鼠标键盘来控制你的Android手机——同屏显示简单教程
- C#利用钩子控制鼠标【月儿原创】
- Unity3D游戏开发之史上最简单的鼠标点击控制人物走动实现
- java 鼠标简单控制和浮点数在内存中的表示方式
- 自己动手编写简单远程控制(c版)(转载)
- 转贴: QUARTUS 实现远程控制的简单方法
- 简单智能远程控制服务器
- 【Unity3D自学记录】简单的鼠标点击控制人物走动
- 用鼠标滚轮控制窗口上下滚动(原创)
- 分享 原创的一个模拟鼠标键盘操作的 设置简单 示例可以模拟向QQ群,以及QQ群内成员发送信息。
- 方便简单的远程控制:putty和WinSCP
- VB.Net远程控制编程实例[鼠标钩子、 键盘钩子]
- 远程连接(win7 + win2003) ,多桌面, 一个键盘一个鼠标控制两台电脑