本文最后更新于 1089 天前,其中的信息可能已经有所发展或是发生改变。
前言
众所周知,Dev-C++ 是一款过时的 IDE,也是一款在 OI 选手中使用最多的 IDE。由于种种原因,短时间内无法抛弃它(NOIP/CSP 用的是它、像它一样即开即用的轻量 IDE 几乎没有、CP-Editor 还等待进一步的完善 、机房渣机连 VSCode 都带不动)。
(Dev 项目似乎被接手了,有了 6.0 版,但 BUG 一堆)
熟悉了 VSC 的内置终端、Windows Terminal 优美的字体渲染和 Fluent Design,再来看默认的 CMD 就觉得十分丑陋,于是就有了这篇文章,让 Dev-C++ 执行编译的程序时套一层 Windows Terminal 的壳,顺便给结果加上内存大小显示。
这篇文章十分水,就当是水文章了。
增加内存显示
Dev-C++ 调用其目录下一个名为 ConsolePauser.exe 的程序来套壳执行编译好的程序,顺便还可以输出一下运行时间。因为它是开源[1]的,所以直接在 ConsolePauser 的源代码[2]基础上改更快。直接加上内存统计的代码即可。注意编译参数要带上 -lpsapi
。
ConsolePauserPlus.cpp
// Execute & Pause
// Runs a program, then keeps the console window open after it finishes
#include <string>
using std::string;
#include <stdio.h>
#include <windows.h>
#include <Psapi.h>
#define MAX_COMMAND_LENGTH 32768
#define MAX_ERROR_LENGTH 2048
LONGLONG GetClockTick() {
LARGE_INTEGER dummy;
QueryPerformanceCounter(&dummy);
return dummy.QuadPart;
}
LONGLONG GetClockFrequency() {
LARGE_INTEGER dummy;
QueryPerformanceFrequency(&dummy);
return dummy.QuadPart;
}
void PauseExit(int exitcode) {
system("pause");
exit(exitcode);
}
string GetErrorMessage() {
string result(MAX_ERROR_LENGTH,0);
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),&result[0],result.size(),NULL);
// Clear newlines at end of string
for(int i = result.length()-1;i >= 0;i--) {
if(isspace(result[i])) {
result[i] = 0;
} else {
break;
}
}
return result;
}
string GetCommand(int argc,char** argv) {
string result;
for(int i = 1;i < argc;i++) {
// Quote the arguments in case they contain spaces
// Could use additional quoting code around the argument
if(string(argv[i]).find(" ")!=string::npos) {
result += string("\"") + string(argv[i]) + string("\"");
} else {
result += string(argv[i]);
}
// Add a space except for the last argument
if(i != (argc-1)) {
result += string(" ");
}
}
if(result.length() > MAX_COMMAND_LENGTH) {
printf("\n--------------------------------");
printf("\nError: Length of command line string is over %d characters\n",MAX_COMMAND_LENGTH);
PauseExit(EXIT_FAILURE);
}
return result;
}
double usedMemory;
int usedMemoryUnit = 0;
char usedMemoryUnitsName[5][5] = {"B", "KB", "MB", "GB", "TB"};
DWORD ExecuteCommand(string& command) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si,0,sizeof(si));
si.cb = sizeof(si);
memset(&pi,0,sizeof(pi));
if(!CreateProcess(NULL, (LPSTR)command.c_str(), NULL, NULL, false, 0, NULL, NULL, &si, &pi)) {
printf("\n--------------------------------");
printf("\nFailed to execute \"%s\":",command.c_str());
printf("\nError %lu: %s\n",GetLastError(),GetErrorMessage().c_str());
PauseExit(EXIT_FAILURE);
}
WaitForSingleObject(pi.hProcess, INFINITE); // Wait for it to finish
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(pi.hProcess, &pmc, sizeof(pmc));
usedMemory = pmc.PeakPagefileUsage;
while (usedMemory >= 1024){
usedMemory /= 1024.0;
usedMemoryUnit++;
}
DWORD result = 0;
GetExitCodeProcess(pi.hProcess, &result);
return result;
}
int main(int argc, char** argv) {
// First make sure we aren't going to read nonexistent arrays
if(argc < 2) {
printf("\n--------------------------------");
printf("\nUsage: ConsolePauser.exe <filename> <parameters>\n");
PauseExit(EXIT_SUCCESS);
}
// Make us look like the paused program
SetConsoleTitle(argv[1]);
// Then build the to-run application command
string command = GetCommand(argc,argv);
// Save starting timestamp
LONGLONG starttime = GetClockTick();
// Then execute said command
DWORD returnvalue = ExecuteCommand(command);
// Get ending timestamp
LONGLONG endtime = GetClockTick();
double seconds = (endtime - starttime) / (double)GetClockFrequency();
// Done? Print return value of executed program
printf("\n--------------------------------");
printf("\nProcess exited with return value %lu , Time: %.4g s , Memory: %.2lf %s\n",returnvalue,seconds,usedMemory,usedMemoryUnitsName[usedMemoryUnit]);
PauseExit(EXIT_SUCCESS);
}
在 Windows Terminal 中运行
只要在原来的命令行前面调用一下 wt
即可。编译时加入 -mwindows
参数来隐藏一闪而过的 Console。
ConsolePauser.cpp
#include <string>
using std::string;
#include <stdio.h>
#include <windows.h>
#define MAX_COMMAND_LENGTH 32768
#define MAX_ERROR_LENGTH 2048
string GetCommand(int argc,char** argv) {
string result;
for(int i = 1;i < argc;i++) {
if(string(argv[i]).find(" ")!=string::npos) {
result += string("\"") + string(argv[i]) + string("\"");
} else {
result += string(argv[i]);
}
if(i != (argc-1)) {
result += string(" ");
}
}
if(result.length() > MAX_COMMAND_LENGTH) {
printf("\n--------------------------------");
printf("\nError: Length of command line string is over %d characters\n",MAX_COMMAND_LENGTH);
}
return result;
}
DWORD ExecuteCommand(string& command) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si,0,sizeof(si));
si.cb = sizeof(si);
memset(&pi,0,sizeof(pi));
if(!CreateProcess(NULL, (LPSTR)command.c_str(), NULL, NULL, false, 0, NULL, NULL, &si, &pi)) {
exit(0);
}
WaitForSingleObject(pi.hProcess, INFINITE);
DWORD result = 0;
GetExitCodeProcess(pi.hProcess, &result);
return result;
}
int main(int argc, char** argv) {
if(argc < 2) {
printf("\n--------------------------------");
printf("\nUsage: ConsolePauser.exe <filename> <parameters>\n");
}
string command = GetCommand(argc,argv);
command = "wt ConsolePauserPlus.exe " + command;
ExecuteCommand(command);
}
替换
编译以上两个程序,替换到 Dev-C++ 目录下,然后将 Dev 的目录加入环境变量,当下一次按下 F10 的时候,看到的就是漂亮的 Fluent Design 界面了。
wa
不错不错
实际上。。。
Redpanda c++(小熊猫) 也很不错
支持自动补全、调试、题目库、实时检查等功能
也不用新建项目,直接运行文件