can we make any control(like edit box) as the standard output device so that the result of the process can b directly shown in that particular edit box without any interaction ..............i think it is possible when we run the procees using createProces() and pass appropriate arguements.......but i am not able to complete it

Recommended Answers

All 7 Replies

You can write one GUI program that contains the edit box and reads the command-line arguments. Then write another program that calls CreateProcess() to launch the GUI program. But why would you want to do that because it is a huge waste of computer resources and time ????

the simplest way would be to use a streambuf that outputs to the control
the skeleton would be:

#include <streambuf>

class textbox_streambuf : public std::streambuf
{
   // ...
  protected:
    virtual int overflow( int c = traits_type::eof() )
    {
      if ( c != traits_type::eof() )
      {
        char cc = traits_type::to_char_type(c);
        // do whatever is required send cc to text box 
        // or control you want to use.
        // if( successfull )
          return traits_type::not_eof( c ) ;
        // else
          return traits_type::eof() ;
       }
       return traits_type::not_eof( c ) ;
    }
    // ...
  private:
    // ...
} ;

int main()
{
  textbox_streambuf tbsbuf( /* ... */ ) ;
  std::streambuf* old = std::cout.rdbuf( &tbsbuf ) ;
  // ...
  // before tbsbuf is destroyed:
  std::cout.rdbuf( old ) ;
}

vijay: how would you connect that streambuf to a GUI edit control ? I don't understand the relevance of your code.

something like this perhaps (it's still only a skeleton and it is not the most efficient):

class edit_control_streambuf : public std::streambuf
{
  public:
    explicit edit_control_streambuf( HWND hedit ) 
          : edit_control(hedit) {}
   // ...
  protected:
    virtual int overflow( int c = traits_type::eof() )
    {
      if ( c != traits_type::eof() )
      {
        char cc = traits_type::to_char_type(c);
        int sz = GetWindowTextLength( edit_control ) ;
        std::vector<char> buffer[sz+1] ;
        GetWindowText( edit_control, &buffer.front(), 
                 buffer.size() ) ;
        buffer.back() = cc ;
        buffer.push_back(0) ;
        SetWindowText( edit_control, &buffer.front() ) ;
      }
      return traits_type::not_eof( c ) ;
     }
     // ...
  private:
    HWND edit_control ;
} ;

Hi,

How about just forgetting the Get/SetWindowxxx functions and use something like ...
SendMessage(edit_control, WM_CHAR, achar, 0);

> How about just forgetting the Get/SetWindowxxx functions and use something like ...
> SendMessage(edit_control, WM_CHAR, achar, 0); yes, when it works it would be a lot more efficient. issues involved include current insertion position (caret) in the control, could text in the control have been selected, is the control read-only etc.

here is a working version which tees the output to en both an edit control and stdout. it's still a toy. real life code would manage an internal buffer (and also handle sync()), would put a cap on the window text size, would be polymorphic on the char_type and traits_type and would have a bunch of typedefs to make usage easy.

#include <iostream>
#include <streambuf>
#include <vector>
#include <cstdio>
#define NO_STRICT
#include <windows.h>

struct my_buff : public std::streambuf
{
    explicit my_buff( HWND w ) : window(w) {}

    protected : 
      virtual int_type overflow( int_type c = traits_type::eof() )
      {
          if( c != traits_type::eof() ) 
          {
              char cc = traits_type::to_char_type(c) ;
              size_t sz = GetWindowTextLength( window ) ;
              std::vector<char> buffer(sz+1) ;
              GetWindowText( window, &buffer.front(), 
                       buffer.size() ) ;
              if( cc == '\n' ) 
              { 
                buffer.back() = '\r' ; 
                buffer.push_back('\n') ; 
              }  
              else buffer.back() = cc ;
              buffer.push_back(0) ;
              if( !SetWindowText( window, &buffer.front() ) ||
                   ( std::putchar(cc) == EOF ) ) 
                      return traits_type::eof() ;
	   }
	  return traits_type::not_eof( c ) ;
	}
  private : HWND window ;
} ;

HWND edit = 0 ;

LRESULT CALLBACK WindowProc(  HWND hw,
                            UINT msg,  WPARAM wp,  LPARAM lp ) 
{
  switch(msg)
  {
    case WM_CREATE :
           edit = CreateWindow( "edit", "",
                    WS_CHILD|ES_MULTILINE|ES_READONLY|WS_VISIBLE, 
                    0, 0, 0, 0, hw, 0, GetModuleHandle(0), 0 ) ;
           SetTimer( hw, 0,  5000, 0 ) ;
           break ;
    case WM_SIZE :
           MoveWindow( edit, 0, 0, LOWORD(lp), HIWORD(lp), TRUE ) ;
           break ;    
    case WM_TIMER :
    {
           static int elapsed = 5 ;
           std::cout << "elapsed: " << elapsed << " seconds\n" ;
           elapsed += 5 ;
           SetTimer( hw, 0,  5000, 0 ) ;
    }
           break ;
    case WM_DESTROY :
           std::cout << "window is being destroyed\n" ;
           PostQuitMessage(0) ;
           break ;
    default :
           return DefWindowProc(hw,msg,wp,lp) ;
  }
  return 0 ;
}

int main()
{
  WNDCLASS wc  =   { 0, WindowProc, 0, 0, GetModuleHandle(0), 
          LoadIcon( 0, IDI_APPLICATION ), 
          LoadCursor( 0, IDC_ARROW ), 
          HBRUSH( COLOR_WINDOW+1 ), 0, "myclass" } ;
  RegisterClass( &wc ) ;
  HWND hw = CreateWindow( wc.lpszClassName, "Output", 
                          WS_OVERLAPPEDWINDOW, 0, 0, 300, 500,
                          0, 0, GetModuleHandle(0), 0 ) ;
  ShowWindow( hw, SW_NORMAL ) ;

  my_buff buff(edit) ;
  std::streambuf* old = std::cout.rdbuf( &buff ) ;
  std::cout << "*** output from cout ***\n"
                  << "------------------------------------\n" ;
  std::cout << "window created HWND: " << hw << '\n' ;

  MSG msg ;
  while( GetMessage( &msg, 0, 0, 0 ) )
  {
    static int n = 0 ;
    if( ( ++n % 50 ) == 0  ) 
         std::cout << n << " messages recd\n" ;
    DispatchMessage( &msg ) ;
  }

  std::cout.rdbuf( old ) ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.