如何让自己编写的软件支持Del键?
  • 板块灌水区
  • 楼主FOF_FOF_QBI
  • 当前回复12
  • 已保存回复12
  • 发布时间2024/7/15 11:41
  • 上次更新2024/7/15 11:46:40
查看原帖
如何让自己编写的软件支持Del键?
738332
FOF_FOF_QBI楼主2024/7/15 11:41

还有这份代码里打开文件的功能有问题

#include<bits/stdc++.h>
#include <conio.h>
#include <windows.h>
using namespace std; 
string QFIDE_ver="1.01"; 
void gotoxy(int x, int y) {
	COORD pos = { x, y };
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
}

void displayMenu() {
	string QF_C_title="title QF  IDE:"+QFIDE_ver;
	system(QF_C_title.c_str()); 
	std::cout << "1. Open File\n";
	std::cout << "2. Edit File\n";
	std::cout << "3. Save File\n";
	std::cout << "4. Exit\n";
	std::cout << "Choose an option: ";
}

void openFile(std::string& content, const std::string& filename) {
	std::ifstream file(filename);
	if (file.is_open()) {
		content.assign((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
		file.close();
		std::cout << "File opened successfully.\n";
	} else {
		std::cout << "Failed to open file.\n";
	}
}

void displayStatusBar(const std::string& content, int x, int y) {
	int lines = 1;
	for (char c : content) {
		if (c == '\n') lines++;
	}
	int columns = x + 1;
	int length = content.length();
	gotoxy(0, 24); // Assuming the console height is 25 lines
	std::cout << "Length: " << length << " | Line: " << y + 1 << " | Column: " << columns << " ";
	gotoxy(x, y);
}

void saveFile(const std::string& content, const std::string& filename) {
	std::ofstream file(filename);
	if (file.is_open()) {
		file << content;
		file.close();
		std::cout << "File saved successfully.\n";
	} else {
		std::cout << "Failed to save file.\n";
	}
}
void editFile(std::string& content, const std::string& filename) {
	system("cls");
	system("title Editing mode (use arrow keys to navigate, 'ESC' to exit)");
	std::string line;
	int x = 0, y = 0;
	gotoxy(x, y);
	while (true) {
		displayStatusBar(content, x, y);
		int ch = _getch();
		if (ch == 224) { // Arrow keys
			ch = _getch();
			switch (ch) {
				case 72: // Up arrow
				if (y > 0) y--;
				break;
				case 80: // Down arrow
				y++;
				break;
				case 75: // Left arrow
				if (x > 0) x--;
				break;
				case 77: // Right arrow
				x++;
				break;
			}
		} else if (ch == 27) { // ESC key
			break;
		} else if (ch == 8) { // Backspace key
			if (!content.empty() && x > 0) {
				content.pop_back();
				x--;
				gotoxy(x, y);
				std::cout << ' ';
				gotoxy(x, y);
			}
		} else if(ch==0x2E){
			if(!content.empty()&&x>0){
				content.pop_back(); 
				x=x;
				gotoxy(x,y);
				std::cout<<' ';
				gotoxy(x,y); 
			}
		} 
		else if (ch == 13) { // Enter key
			content += '\n';
			y++;
			x = 0;
			gotoxy(x, y);
		} else if (ch == 19) { // Ctrl + S
			saveFile(content, filename);
		} else {
			content += ch;
			std::cout << (char)ch;
			x++;
		}
		gotoxy(x, y);
	}
}



int main() {
	std::string content;
	std::string filename;
	int choice;
	while (true) {
		system("cls");
		displayMenu();
		std::cin >> choice;
		std::cin.ignore();
		
		switch (choice) {
		case 1:
			std::cout << "Enter filename to open: ";
			std::getline(std::cin, filename);
			openFile(content, filename);
			break;
		case 2:
			editFile(content, filename);
			break;
		case 3:
			std::cout << "Enter filename to save: ";
			std::getline(std::cin, filename);
			saveFile(content, filename);
			break;
		case 4:
			return 0;
		default:
			std::cout << "Invalid choice. Please try again.\n";
		}
	}
	
	return 0;
}
	
2024/7/15 11:41
加载中...