假设两个同样的应用程序,运行在相同的操作系统上,要实现远程控制,可以使用传递鼠标和键盘的消息给对方,对方收到后解析出鼠标和键盘消息如何执行即可。
下面是几处关键程序:
一是处理收到消息,下面应该放在套接字接收或者串口接收中:(小心下面的右键单击、双击几处没实现!)
// // 解析从客户端发送过来的消息并发送到本机的消息队列 // void DispatchWMMessage(char *szString, void *p) { CTestDlg *pDlg = (CTestDlg *)p; //鼠标消息 struct {char *szWMMouseMsg;} WMMouseMsg[] = {"WM_MM","WM_LBD","WM_LBU","WM_LBK", "WM_MBD","WM_MBU","WM_MBK", "WM_RBD","WM_RBU","WM_RBK"}; // 键盘消息 struct {char *szWMKeyBdMsg;} WMKeyBdMsg[] = {"WM_KD","WM_KU"}; // 通用消息:色彩模式,网格数和压缩消息 struct {char *szMsg;} Msg[] = {"WM_COMP","WM_GRID","WM_CMOD"}; int nWMMouseMsg; int nWMKeyBdMsg; int nMsg; struct CommandList CommandStart; struct CommandList *pCommandNode; struct CommandDS Command; char *pDest; int iLoc,nChar; int iLoop,iParms; char szString2[2049]; // 分别得到鼠标,键盘,通用消息的数目 nWMMouseMsg = (int)(sizeof(WMMouseMsg)/sizeof(WMMouseMsg[0])); nWMKeyBdMsg = (int)(sizeof(WMKeyBdMsg)/sizeof(WMKeyBdMsg[0])); nMsg = (int)(sizeof(Msg)/sizeof(Msg[0])); // 初始化command链表 CommandStart.pNext = NULL; pCommandNode = &CommandStart; // 分析command命令,截获命令的参数 iParms = 0; while (pDest = strchr(szString,';')) { iLoc = pDest - szString; nChar = iLoc; memset(Command.szElement,'\0',sizeof(Command.szElement)); strncpy(Command.szElement,szString,nChar); // 发送到命令栈中 pCommandNode = Add_Command(pCommandNode,Command); memset(szString2,'\0',sizeof(szString2)); strcpy(szString2,&szString[iLoc + 1]); strcpy(szString,szString2); iParms++; if (iParms == 5) // 每条命令5个参数 break; } // 处理命令 pCommandNode = CommandStart.pNext; if (pCommandNode) { // 鼠标消息 UINT keyFlags; int iMessage; int fWMMouseMsg; DWORD dwX,dwY; // 键盘消息 int fWMKeyBdMsg; UINT vk; int fDown; int cRepeat; UINT flags; // 判断是否有鼠标消息 fWMMouseMsg = FALSE; for (iLoop = 0;iLoop < nWMMouseMsg;iLoop++) { if (strcmp(pCommandNode->Command.szElement,WMMouseMsg[iLoop].szWMMouseMsg) == 0) { // 设置鼠标消息的标志 fWMMouseMsg = TRUE; // 具体的鼠标消息 if (strcmp(WMMouseMsg[iLoop].szWMMouseMsg,"WM_MM\0") == 0) iMessage = 1; else if (strcmp(WMMouseMsg[iLoop].szWMMouseMsg,"WM_LBD\0") == 0) iMessage = 2; else if (strcmp(WMMouseMsg[iLoop].szWMMouseMsg,"WM_LBU\0") == 0) iMessage = 3; else if (strcmp(WMMouseMsg[iLoop].szWMMouseMsg,"WM_LBK\0") == 0) iMessage = 4; else if (strcmp(WMMouseMsg[iLoop].szWMMouseMsg,"WM_MBD\0") == 0) iMessage = 5; else if (strcmp(WMMouseMsg[iLoop].szWMMouseMsg,"WM_MBU\0") == 0) iMessage = 6; else if (strcmp(WMMouseMsg[iLoop].szWMMouseMsg,"WM_MBK\0") == 0) iMessage = 7; else if (strcmp(WMMouseMsg[iLoop].szWMMouseMsg,"WM_RBD\0") == 0) iMessage = 8; else if (strcmp(WMMouseMsg[iLoop].szWMMouseMsg,"WM_RBU\0") == 0) iMessage = 9; else if (strcmp(WMMouseMsg[iLoop].szWMMouseMsg,"WM_RBK\0") == 0) iMessage = 10; // 移动到参数栈的下一个节点,x坐标 pCommandNode = pCommandNode->pNext; dwX = (DWORD)atof(pCommandNode->Command.szElement); // 移动到参数栈的下一个节点,y坐标 pCommandNode = pCommandNode->pNext; dwY = (DWORD)atof(pCommandNode->Command.szElement); // 移动到参数栈的下一个节点,辅助键 pCommandNode = pCommandNode->pNext; keyFlags = atoi(pCommandNode->Command.szElement); // 退出循环 break; } } // 如果有鼠标消息则对鼠标消息进行处理 if (fWMMouseMsg) { // 处理鼠标消息 if (iMessage == 1) //光标移动 { //mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE,0,0,0,0); SetCursorPos(dwX,dwY); } else if (iMessage == 2) //左键按下 { mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,0,0,0,0); } else if (iMessage == 3) //左键抬起 { mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,0,0,0,0); } //以下鼠标的消息还未处理!!! else if (iMessage == 4) { mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,0,0,0,0); mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP,0,0,0,0); } else if (iMessage == 5) { mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MIDDLEDOWN,0,0,0,0); } else if (iMessage == 6) { mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MIDDLEUP,0,0,0,0); } else if (iMessage == 7) { mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MIDDLEDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MIDDLEUP,0,0,0,0); mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MIDDLEDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MIDDLEUP,0,0,0,0); } else if (iMessage == 8) { mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN,0,0,0,0); } else if (iMessage == 9) { mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP,0,0,0,0); } else if (iMessage == 10) { mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP,0,0,0,0); mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTDOWN,0,0,0,0); mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_RIGHTUP,0,0,0,0); } } else { // 没有鼠标消息则判断是否有键盘消息 fWMKeyBdMsg = FALSE; for (iLoop = 0;iLoop < nWMKeyBdMsg;iLoop++) { if (strcmp(pCommandNode->Command.szElement,WMKeyBdMsg[iLoop].szWMKeyBdMsg) == 0) { // 设置键盘消息标志 fWMKeyBdMsg = TRUE; if (strcmp(WMKeyBdMsg[iLoop].szWMKeyBdMsg,"WM_KD\0") == 0) iMessage = 1;//按下键 else if (strcmp(WMKeyBdMsg[iLoop].szWMKeyBdMsg,"WM_KU\0") == 0) iMessage = 2;//松开键 // 移动到参数链表的下一个节点,Virtural 键码 pCommandNode = pCommandNode->pNext; vk = atoi(pCommandNode->Command.szElement); //移动到参数链表的下一个节点,按下键标志 pCommandNode = pCommandNode->pNext; fDown = atoi(pCommandNode->Command.szElement); // 移动到参数链表的下一个节点,按键重复数 pCommandNode = pCommandNode->pNext; cRepeat = atoi(pCommandNode->Command.szElement); // 移动到参数链表的下一个节点,标志位 pCommandNode = pCommandNode->pNext; flags = atoi(pCommandNode->Command.szElement); break; } } // 如果有键盘消息,则处理键盘消息 if (fWMKeyBdMsg) { if (iMessage == 1) //模拟按键消息 { keybd_event((BYTE)vk,(BYTE)vk,0,0); } else if (iMessage == 2) //模拟松开键的消息 { keybd_event((BYTE)vk,(BYTE)vk,KEYEVENTF_KEYUP,0); } } else // 通用消息 { for (iLoop = 0;iLoop < nMsg;iLoop++) { if (strcmp(pCommandNode->Command.szElement,Msg[iLoop].szMsg) == 0) { if (strcmp(Msg[iLoop].szMsg,"WM_COMP\0") == 0) { // 移动到参数链表的下一个节点,压缩级数 pCommandNode = pCommandNode->pNext; //iCompressionLevel = atoi(pCommandNode->Command.szElement); } else if (strcmp(Msg[iLoop].szMsg,"WM_GRID\0") == 0) { // 移动到参数链表的下一个节点,x网格数 pCommandNode = pCommandNode->pNext; //nGridX = atoi(pCommandNode->Command.szElement); // 移动到参数链表的下一个节点,y网格数 pCommandNode = pCommandNode->pNext; //nGridY = atoi(pCommandNode->Command.szElement); // 清除当前的显示设置 //ClearDisplay(hServerWnd); // 初始化新的显示设置 //InitDisplay(hServerWnd); } else if (strcmp(Msg[iLoop].szMsg,"WM_CMOD\0") == 0) { // 移动到参数链表的下一个节点,每个屏幕点的字节数 pCommandNode = pCommandNode->pNext; //bmBitsPixel = atoi(pCommandNode->Command.szElement); //ClearDisplay(hServerWnd); //InitDisplay(hServerWnd); } } } } } } // 清除命令队列 Clear_Command(&CommandStart); } // 添加一个元素到命令栈中 struct CommandList *Add_Command(struct CommandList *pNode,struct CommandDS Command) { if (pNode->pNext = (struct CommandList *)malloc(sizeof(struct CommandList))) { pNode = pNode->pNext; strcpy(pNode->Command.szElement,Command.szElement); pNode->pNext = NULL; return pNode; } return NULL; } //完全清除命令栈元素 void Clear_Command(struct CommandList *pStart) { struct CommandList *pPrev; struct CommandList *pNode; while (pNode = pStart->pNext) { pPrev = pStart; pPrev->pNext = pNode->pNext; free(pNode); } }
BOOL CTestDlg::PreTranslateMessage(MSG* pMsg) { if (pMsg->message == WM_LBUTTONDOWN)//左键按下 { if(m_bSend) { CPoint pt; GetCursorPos(&pt); memset(szMsg,'\0',sizeof(szMsg)); sprintf(szMsg,"WM_LBD;%d;%d;%d;0;\0",pt.x, pt.y, keyFlags); LanSend(szMsg, strlen(szMsg)); } } else if (pMsg->message == WM_LBUTTONUP)//左键抬起 { if(m_bSend) { CPoint pt; GetCursorPos(&pt); memset(szMsg,'\0',sizeof(szMsg)); sprintf(szMsg,"WM_LBU;%d;%d;%d;0;\0",pt.x, pt.y, keyFlags); LanSend(szMsg, strlen(szMsg)); } } else if (pMsg->message == WM_MOUSEMOVE) //光标移动 { if(m_bSend) { POINT pt; ::GetCursorPos(&pt); memset(szMsg,'\0',sizeof(szMsg)); //sprintf(szMsg,"WM_MM;%d;%d;%d;0;\0", GET_X_LPARAM(pMsg->lParam), GET_Y_LPARAM(pMsg->lParam), 0); sprintf(szMsg,"WM_MM;%d;%d;%d;0;\0", pt.x, pt.y, 0); LanSend(szMsg, strlen(szMsg)); return FALSE; } } else if (pMsg->message == WM_KEYDOWN) //键盘按下 { if(m_bSend) { memset(szMsg,'\0',sizeof(szMsg)); sprintf(szMsg,"WM_KD;%d;%d;%d;%d;\0",pMsg->wParam, 0, 0, 0); LanSend(szMsg, strlen(szMsg)); } } else if (pMsg->message == WM_KEYUP) //键盘抬起 { if(m_bSend) { memset(szMsg,'\0',sizeof(szMsg)); sprintf(szMsg,"WM_KU;%d;%d;%d;%d;\0",pMsg->wParam, 0, 0, 0); LanSend(szMsg, strlen(szMsg)); } } return CDialog::PreTranslateMessage(pMsg); }