#include #include #include #include <windows.h>

int main() { Sleep(2000); POINT pt, ptScreen; GetCursorPos(&ptScreen); pt = ptScreen; HWND hParent, hWnd; hParent = WindowFromPoint(ptScreen); ScreenToClient(hParent, &pt); hWnd = ChildWindowFromPoint(hParent, pt); printf("%d\n", hWnd); char text[10240], buffer[20240]; SendMessage(hWnd, WM_GETTEXT, 10240, (LPARAM)text);

FILE *fp;
int line_num = 0; // 文件行数
int line_len = 0; // 文件每行的长度
char buf[20240] = {0}; // 行数据缓存

fp = fopen("text.txt", "r");
if (NULL == fp) {
    printf("open failed.\n");
    return -1;
}

while(fgets(buf, 20240, fp)) {
    line_num++;
    line_len = strlen(buf);
    // 排除换行符
    if ('\n' == buf[line_len - 1]) {
        buf[line_len - 1] = '\0';
        line_len--;
        if (0 == line_len) {
            //空行 
            continue;
        }
    }
    // windos文本排除回车符
    if ('\r' == buf[line_len - 1]) {
        buf[line_len - 1] = '\0';
        line_len--;
        if (0 == line_len) {
            //空行 
            continue;
        }
    }
    // showtext 
    sprintf(buffer, "%s\r\n%s", buf, text);
    SendMessage(hWnd, WM_SETTEXT, NULL, (LPARAM)buffer);
    // waitF7Down 
    while(!(GetAsyncKeyState(VK_F7) && 0x8000)) {
        if (GetAsyncKeyState(VK_F6) && 0x8000) {
            // TODO实现返回上一行 
            fseek(fp, -2 * (line_len + 2), SEEK_CUR); // 返回上一行的位置
            break;
        }
    }
    while(GetAsyncKeyState(VK_F7) && 0x8000);
}

if (0 == feof) {
    // 未读到文件末尾
    printf("fgets error\n");
    return -1;
}
fclose(fp);
return 0;

}

C++ Windows程序:使用F7/F6键控制文本显示

原文地址: https://www.cveoy.top/t/topic/qklN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录