[英]Console in C++ application
I want to implement a console inside my C++ application. 我想在我的C ++应用程序中实现一个控制台。 Like ftp for example.
例如ftp。 Or (IIRC) sql, once you've connected to a Server.
连接到服务器后,也可以使用(IIRC)sql。
Does anybody know a library which implements this? 有人知道实现此功能的库吗? Ideally with auto-completion and such?
理想情况下具有自动完成功能? My searches for this only come up with "how to build a C++ console application", which I do know how to do.
我对此的搜索仅涉及“如何构建C ++控制台应用程序”,我确实知道该怎么做。
GNU Readline implements the features you want. GNU Readline实现了所需的功能。 If filename auto-completion is not the sort you need, use a custom auto-complete routine.
如果文件名自动完成不是您需要的排序,请使用自定义自动完成例程。
If you want also autocomplete you could check the example of linenoise (a lightweight readline alternative). 如果还需要自动完成功能,则可以查看线噪声示例(一种轻量级的readline替代方案)。
Basically you have to parse the Userinput Line in a Loop. 基本上,您必须在循环中解析Userinput行。
Example for a very basic CommadLineInterface : 一个非常基本的CommadLineInterface的示例:
parseLine()
on \\n
\\n
上调用类似parseLine()
类的东西 Space
(then ;
) take the first String as Command cmd
and the rest as args
. Space
(然后;
),将第一个String作为Command cmd
,其余作为args
。 dispatch(cmd, args);
dispatch(cmd, args);
For Windows: Use AllocConsole()
to attach a text console to your GUI app and freopen( "CON", "w", stdout ) ;
对于Windows:使用
AllocConsole()
将文本控制台附加到您的GUI应用程序,然后freopen( "CON", "w", stdout ) ;
to redirect and printf()
to output text. 重定向和
printf()
输出文本。
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681944(v=vs.85).aspx http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms681944(v=vs.85).aspx
Sample code: 样例代码:
#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 );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.