C++ Windows API 实现文本框内容逐行显示并支持返回上一行
#include
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}; // 行数据缓存
char prevText[10240] = {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) {
// 返回上一行
SendMessage(hWnd, WM_SETTEXT, NULL, (LPARAM)prevText);
while(GetAsyncKeyState(VK_F6) && 0x8000);
break;
}
}
while(GetAsyncKeyState(VK_F7) && 0x8000);
// 保存当前行的文本内容到prevText中
strcpy(prevText, text);
}
if (0 == feof) {
// 未读到文件末尾
printf("fgets error\n");
return -1;
}
fclose(fp);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/qklL 著作权归作者所有。请勿转载和采集!