Thread: cout

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267

    cout

    is there a function like "cout" for window applications or a function for writing out numbers?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    depends

    console or message box

    Kuphryn

  3. #3

  4. #4
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    I don't think it will work for numbers.
    You have to convert the numbers into strings ???

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    cout is very universal function.
    It automatically converts any data type into an array of chars.
    Converting numbers to strings is easy, with itoa() and for converting strings to numbers you can use atoi(). And then you can use TextOut() or even MessageBox().

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You could redirect cout:
    Code:
    #include <windows.h>
    #include <windowsx.h>
    #include <iostream>
    #include <streambuf>
    
    class winoutbuf: public std::streambuf
    {
    public:
      winoutbuf(int startX, int y):m_startX(startX),m_X(startX),m_Y(y),m_hwnd(0){}
      void SetHandle(HWND hwnd) { m_hwnd = hwnd; }
    private:
      int m_startX,m_X,m_Y;
      HWND m_hwnd;
    protected:
      virtual int_type overflow(int_type c)
      {
        //single char output
        if (c != EOF)
        {
          HDC hdc = GetDC(m_hwnd);
            char z = c;
            if (z=='\n')
            {
              m_X = m_startX; //reset position
              return c;
            }
            SIZE size;
            if(!GetTextExtentPoint32(hdc,&z,1,&size)) //get text width
              return EOF;
            if (!TextOut(hdc,m_X, m_Y, &z, 1))
              return EOF;
            m_X+=size.cx; //update position based on width of char
          ReleaseDC(m_hwnd,hdc);
        }
        return c;
      }
      virtual std::streamsize xsputn(const char* s, std::streamsize num)
      {
        /*
        Do outputting for more than one char
        */
        return num;
      }
    };
    
    class winostream: public std::ostream
    {
    protected:
      winoutbuf buf;
    public:
      winostream():buf(10,10), std::ostream(&buf) {}
      void SetHandle(HWND hwnd) { buf.SetHandle(hwnd); }
    };
    
    winostream g_out;
    
    LRESULT CALLBACK MainWindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
    
    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreviousInstance,LPSTR lpcmdline,int nCmdShow)
    {
      
    	WNDCLASS wc;
    	MSG msg;
    
    	wc.style = CS_VREDRAW | CS_HREDRAW;
    	wc.lpfnWndProc = MainWindowProc;
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hInstance = hInstance;
    	wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    	wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH) (COLOR_SCROLLBAR + 1);
    	wc.lpszMenuName = 0;
    	wc.lpszClassName = "WinClassName";
      
    	if (!RegisterClass (&wc))
    		return 0;
      
      HWND hwnd;
    
      hwnd = CreateWindow ("WinClassName",
          "Redirect cout",
          WS_OVERLAPPEDWINDOW|WS_VISIBLE,
          20,20,400,400,
          NULL,NULL,hInstance,NULL);
      g_out.SetHandle(hwnd);
      if (!hwnd)
        return 0;
     
      while (GetMessage (&msg, NULL, 0, 0))
    	{
    		TranslateMessage (&msg);
    		DispatchMessage (&msg);
    	}
      return 0;
    }
    
    LRESULT CALLBACK MainWindowProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
      switch (msg)
      {
      case WM_CREATE:
        std::cout.rdbuf(g_out.rdbuf()); //redirect cout to custom stream
      break;
      case WM_MOUSEMOVE:
      {
        int x = GET_X_LPARAM(lParam);
        int y = GET_Y_LPARAM(lParam);
        std::cout<<'('<<x<<','<<y<<')'<<'\n';
      }
      break;
      case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
      break;
      }
      return DefWindowProc(hwnd,msg,wParam,lParam);
    }
    Not a perfect example, but you get the idea
    Last edited by JaWiB; 06-26-2006 at 12:01 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class methods to cout stream
    By shintaro in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2008, 07:27 PM
  2. cout vs std::cout
    By none in forum C++ Programming
    Replies: 10
    Last Post: 07-26-2004, 11:20 PM
  3. changing cout output
    By Yohumbus in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 04:09 AM
  4. Redirecting cout stream
    By Arrow Mk84 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2002, 04:17 PM
  5. printf vs cout
    By RyeDunn in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2002, 04:26 PM