想通过dos窗口运行该基于Dialog的程序,但运行时要在dos窗口中输出一些信息,如何做到呢?

解决方案 »

  1.   

    有控制台方面的API……
    可以检查是否有输入输出管道,没有就新建一个控制台来输出,有就直接往输出管道写内容
      

  2.   

    谢谢 能给点相关资料吗?我想向已有的dos控制台上输出,不想自己建控制台。再次感谢!
      

  3.   

    //declare#if !defined(__CONSOLE_H_)
    #define __CONSOLE_H_class CConsole
    {
    public:
    CConsole() 

    hConsole = NULL; 
    };public:
    void SetPosition(POINT pt,int wx,int wy);
    // create the console
    bool   Create(const char* szTitle, bool bNoClose = false);

    // set color for output
    void   Color(WORD wColor = NULL);
    // write output to console
    void   Output(const char* szOutput = NULL, ...);

    // set and get title of console
    void   SetTitle(const char* szTitle);
    char*  GetTitle(); // get HWND and/or HANDLE of console
    HWND   GetHWND();
    HANDLE GetHandle(); // show/hide the console
    void   Show(bool bShow = true);
    // disable the [x] button of the console
    void   DisableClose();
    // clear all output
    void   Clear(); // close the console and delete it
    void   Close();
    static   BOOL   CtrlHandler(DWORD fdwCtrlType); 
    private:
    HANDLE hConsole;
    };
      

  4.   

    //definition
    #include "stdafx.h"
    #include "Console.h"//color
    //FOREGROUND_RED | FOREGROUND_INTENSITY
    // macro for the Clear() function
    #define ERR(bSuccess) { if(!bSuccess) return; }
    // macro to check whether hConsole is valid
    #define CHECK(hHandle) { if(hHandle == NULL) return; };
    static CConsole* G_pConsole = NULL;bool CConsole::Create(const char *szTitle, bool bNoClose)
    {
    // Has console been already created?
    if(hConsole != NULL)
    return false;

    // Allocate a new console for our app
    if(!AllocConsole())
    return false;

    // Create the actual console
    hConsole = CreateFile("CONOUT$", GENERIC_WRITE|GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
    //hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
      if(hConsole == INVALID_HANDLE_VALUE)
    return false;

    if(SetConsoleMode(hConsole, ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT ) == 0)
    return false;

      
    // if true, disable the [x] button of the console
    if(bNoClose)
    DisableClose();

    // set the console title
    if(szTitle != NULL)
    SetConsoleTitle(szTitle);   /*BOOL fSuccess; 
     
      fSuccess = SetConsoleCtrlHandler( 
          (PHANDLER_ROUTINE) CtrlHandler,  // handler function 
          TRUE);                           // add to list 
      if (!fSuccess) 
          return false;   G_pConsole = this;*/
    return true;
    }void CConsole::Color(WORD wColor)
    {
    CHECK(hConsole); // no color specified, reset to defaults (white font on black background)
    if(wColor != NULL)
    SetConsoleTextAttribute(hConsole, wColor );
    // change font and/or background color
    else
    SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // white text on black bg
    }void CConsole::Output(const char* szOutput, ...)
    {
    CHECK(hConsole); DWORD dwWritten;
    char out[256];
    va_list va; // if not parameter set, write a new line
    if(szOutput == NULL) 
    sprintf(out,"\n");
    // process arguments
    else
    {
    va_start(va, szOutput);
    vsprintf(out, szOutput, va);
    va_end(va);
    }
       
    // write to the console
    WriteConsole(hConsole,out,strlen(out),&dwWritten,0);
    }void CConsole::SetTitle(const char *title)
    {
    // self-explanatory
    SetConsoleTitle(title);
    }char* CConsole::GetTitle()
    {
    // get the title of our console and return it
    static char szWindowTitle[256] = "";
    GetConsoleTitle(szWindowTitle,sizeof(szWindowTitle)); return szWindowTitle;
    }
    HWND CConsole::GetHWND()
    {
    if(hConsole == NULL) 
    return NULL; // try to find our console window and return its HWND
    return FindWindow("ConsoleWindowClass",GetTitle());
    }void CConsole::Show(bool bShow)
    {
    CHECK(hConsole); // get out console's HWND and show/hide the console
    HWND hWnd = GetHWND();
    if(hWnd != NULL)
    ShowWindow(hWnd, SW_HIDE ? SW_SHOW : bShow);
    }void CConsole::DisableClose()
    {
    CHECK(hConsole); HWND hWnd = GetHWND();

    // disable the [x] button if we found our console
    if(hWnd != NULL)
    {
    HMENU hMenu = GetSystemMenu(hWnd,0);
    if(hMenu != NULL)
    {
    DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
    DrawMenuBar(hWnd);
    }
    }
    }
    void CConsole::Clear()
    {
    CHECK(hConsole); /***************************************/
    // This code is from one of Microsoft's
    // knowledge base articles, you can find it at 
        // http://support.microsoft.com/default.aspx?scid=KB;EN-US;q99261&
    /***************************************/ COORD coordScreen = { 0, 0 }; BOOL bSuccess;
        DWORD cCharsWritten;
        CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */ 
        DWORD dwConSize;  /* get the number of character cells in the current buffer */     bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
        ERR(bSuccess);
        dwConSize = csbi.dwSize.X * csbi.dwSize.Y;    /* fill the entire screen with blanks */     bSuccess = FillConsoleOutputCharacter( hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten );
        ERR(bSuccess);    /* get the current text attribute */     bSuccess = GetConsoleScreenBufferInfo( hConsole, &csbi );
        ERR(bSuccess);    /* now set the buffer's attributes accordingly */     bSuccess = FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten );
        ERR(bSuccess);    /* put the cursor at (0, 0) */     bSuccess = SetConsoleCursorPosition( hConsole, coordScreen );
        ERR(bSuccess);
    }
    HANDLE CConsole::GetHandle()
    {
    // simply return the handle to our console
    return hConsole;
    }void CConsole::Close()
    {
    // free the console, now it can't be used anymore until we Create() it again
    FreeConsole();
    hConsole = NULL;
    }BOOL CConsole::CtrlHandler(DWORD fdwCtrlType) 
    {
      
      switch (fdwCtrlType) 
        { 
         // Handle the CTRL+C signal. 
         //case CTRL_C_EVENT: 
      
         //     Beep(1000, 1000); 
         //     return TRUE; 
     
          // CTRL+CLOSE: confirm that the user wants to exit. 
          case CTRL_CLOSE_EVENT: 
              //ASSERT(G_pConsole);
            
              G_pConsole->Close();
              return TRUE;      // Pass other signals to the next handler.       case CTRL_BREAK_EVENT:       case CTRL_LOGOFF_EVENT:       case CTRL_SHUTDOWN_EVENT: 
          default: 
                return FALSE;
        } 
      return FALSE;
    }void CConsole::SetPosition(POINT pt, int wx, int wy)
    {
      if(hConsole)
      {
        HWND hwnd = GetHWND();
        SetWindowPos(hwnd,0,pt.x,pt.y,wx,wy,SWP_NOZORDER);
      }
    }
      

  5.   

    //usage
    CConsole m_Console;
    m_Console.Create("Debug output windows",TRUE);
    HWND hwnd = m_Console.GetHWND();
    ::SetForegroundWindow(hwnd);
    m_Console.Color(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    CString strError = "Test String";
    m_Console.Output(strError);
      

  6.   

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/console_reference.asp
      

  7.   

    #include <conio.h>#ifdef _DEBUG
    AllocConsole();
    static int n = 0;
    n++;
    cprintf("%d", n++);
    #endif