C ++应用程序中的控制台

时间:2012-09-13 17:10:24

标签: c++

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

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

3 个答案:

答案 0 :(得分:0)

GNU Readline实现了您想要的功能。如果文件名自动完成不是您需要的类型,请使用custom auto-complete例程。

答案 1 :(得分:0)

如果您还想要自动填充功能 查看linenoise(轻量级readline替代)的示例。

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

非常基本的CommadLineInterface示例:

  1. 在while循环中显示提示和读取输入,
  2. parseLine()
  3. 上拨打\n之类的内容
  4. 将代码中的行拆分至少Space(然后;)将第一个字符串作为命令cmd,其余字符串作为args
  5. 致电dispatch(cmd, args);

答案 2 :(得分: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 );
}