要实现在代码中加上上一行(F6)的功能,可以使用GetAsyncKeyState函数来检测键盘按键状态。以下是修改后的代码:

#include <cstdio>
#include <cstring>
#include <cwchar>
#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) {
				fseek(fp, -(line_len + 1), SEEK_CUR); // 返回上一行
				break;
			}
		}
		while(GetAsyncKeyState(VK_F7) && 0x8000);
	}

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

在原代码中加入了一个嵌套的while循环来检测F6键的按下状态。当检测到F6键按下时,使用fseek(fp, -(line_len + 1), SEEK_CUR);将文件指针返回到上一行的开头。


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

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