2012-09-13 231 views
1

我想在我的C++應用程序中實現控制檯。例如,像ftp。或者(IIRC)sql,一旦你連接到服務器。C++應用程序中的控制檯

有沒有人知道一個實現它的庫?理想的自動完成和這樣的?我的搜索只是提出了「如何構建一個C++控制檯應用程序」,我知道該怎麼做。

+0

爲什麼另起爐竈時,你可以創建一個C++控制檯應用程序,提供相同的功能? – J2N

+0

這被稱爲終端模擬器。不知道爲什麼你需要,但... – 2012-09-13 17:14:45

+0

[readline庫](http://cnswww.cns.cwru.edu/php/chet/readline/rltop.html)可能有助於某些部分。 –

回答

0

如果你還想自動完成,你可以 檢查linenoise(一個輕量級的readline替代)的例子。

基本上你必須解析循環中的用戶輸入線。

實施例爲一個非常基本的CommadLineInterface:

  1. 顯示提示和讀取在while循環輸入,
  2. 呼叫類似parseLine()\n
  3. 在令牌由至少Space(然後分割線;)以第一個字符串作爲命令cmd,其餘爲args
  4. 呼叫dispatch(cmd, args);
+0

這看起來很有前途,謝謝! –

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); 
}