1

I want to implement a console inside my C++ application. Like ftp for example. Or (IIRC) sql, once you've connected to a Server.

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.

4

3 に答える 3

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

GNU Readlineは、必要な機能を実装しています。ファイル名のオートコンプリートが必要な並べ替えでない場合は、カスタムのオートコンプリートルーチンを使用してください。

于 2012-09-13T17:17:45.920 に答える
0

オートコンプリートも必要な場合は、linenoise (軽量の readline の代替) の例を確認できます。

基本的に、ユーザー入力行をループで解析する必要があります。

非常に基本的な CommadLineInterface の例:

  1. while ループでプロンプトを表示し、入力を読み取ります。
  2. parseLine()のようなものを呼び出す\n
  3. トークンの行を少なくともSpace(その後;) で分割し、最初の文字列をコマンドcmdとして、残りをとして取りargsます。
  4. 電話dispatch(cmd, args);
于 2012-09-13T17:18:18.520 に答える