我想在我的C++应用程序中实现控制台。例如,像ftp。或者(IIRC)sql,一旦你连接到服务器。C++应用程序中的控制台
有没有人知道一个实现它的库?理想的自动完成和这样的?我的搜索只是提出了“如何构建一个C++控制台应用程序”,我知道该怎么做。
我想在我的C++应用程序中实现控制台。例如,像ftp。或者(IIRC)sql,一旦你连接到服务器。C++应用程序中的控制台
有没有人知道一个实现它的库?理想的自动完成和这样的?我的搜索只是提出了“如何构建一个C++控制台应用程序”,我知道该怎么做。
GNU Readline实现您想要的功能。如果文件名自动完成不是您需要的排序,请使用例程custom auto-complete。
如果你还想自动完成,你可以 检查linenoise(一个轻量级的readline替代)的例子。
基本上你必须解析循环中的用户输入线。
实施例为一个非常基本的CommadLineInterface:
parseLine()
上\n
Space
(然后分割线;
)以第一个字符串作为命令cmd
,其余为args
。dispatch(cmd, args);
这看起来很有前途,谢谢! –
对于Windows:使用AllocConsole()
到文本控制台连接到您的GUI应用程序和freopen("CON", "w", stdout) ;
重定向和printf()
输出文本。
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681944(v=vs.85).aspx
示例代码:
#include <stdio.h>
#include <stdlib.h>
// Function prototypes.
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow);
// In a C++ Windows app, the starting point is WinMain().
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
// these next few lines create and attach a console
// to this process. note that each process is only allowed one console.
AllocConsole() ;
freopen("CON", "w", stdout) ;
printf("HELLO!!! I AM THE CONSOLE!\n") ;
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof(WNDCLASSEX) ;
wc.cbClsExtra = 0; // ignore for now
wc.cbWndExtra = 0; // ignore for now
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = TEXT(" ");
wc.lpszMenuName = 0;
wc.style = CS_HREDRAW | CS_VREDRAW; // Redraw the window
RegisterClassEx(&wc);
HWND hwnd = CreateWindowEx(0, TEXT(" "), TEXT("window's title!"), WS_OVERLAPPEDWINDOW, 10, 10, 200, 200, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam; // return from WinMain
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch(message)
{
case WM_CREATE:
// upon creation, let the speaker beep at 50Hz, for 10ms.
Beep(50, 10);
printf("HELLO!!! I AM THE CONSOLE!\n") ;
return 0;
break;
case WM_PAINT:
{
// we would place our Windows painting code here.
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
// draw a circle and a 2 squares
Ellipse(hdc, 20, 20, 160, 160);
Rectangle(hdc, 50, 50, 90, 90);
Rectangle(hdc, 100, 50, 140, 90);
printf("HELLO!!! I AM THE CONSOLE!\n") ;
EndPaint(hwnd, &ps);
}
return 0;
break;
case WM_LBUTTONDOWN:
printf("STOP POKING MEEE!!!\n") ;
break;
case WM_DESTROY:
PostQuitMessage(0) ;
return 0;
break;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
为什么另起炉灶时,你可以创建一个C++控制台应用程序,提供相同的功能? – J2N
这被称为终端模拟器。不知道为什么你需要,但... – 2012-09-13 17:14:45
[readline库](http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html)可能有助于某些部分。 –