1

我想在我的 C++ 应用程序中实现一个控制台。以 ftp 为例。或者 (IIRC) sql,一旦你连接到服务器。

有人知道实现这个的库吗?理想情况下自动完成等?我对此的搜索只提出了“如何构建 C++ 控制台应用程序”,我确实知道该怎么做。

4

3 回答 3

0

GNU Readline实现了您想要的功能。如果文件名自动完成不是您需要的排序,请使用自定义自动完成例程。

于 2012-09-13T17:17:45.920 回答
0

对于 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 );
}
于 2012-09-13T18:09:38.757 回答
0

如果您还想自动完成,您可以查看linenoise的示例(一种轻量级的 readline 替代方案)。

基本上你必须在一个循环中解析用户输入行。

一个非常基本的 CommadLineInterface 示例:

  1. 在 while 循环中显示提示并读取输入,
  2. parseLine()打电话给类似的东西\n
  3. 将 Line in Tokens 至少拆分Space(然后;)将第一个 String 作为 Command cmd,其余作为args.
  4. 称呼dispatch(cmd, args);
于 2012-09-13T17:18:18.520 回答