Thread: How can I make a scroll bar in c++?

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    1

    How can I make a scrolling text in c++?

    How can I make a scrolling text in c++?

    If someone know some code that can read an external file and make it scroll on the program window, lease help me!!!

    Last edited by androide; 11-14-2003 at 10:50 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >How can I make a scroll bar in c++?
    With a windowing API.

    >If someone know some code that can read an external file and make it scroll on the program window, lease help me!!!
    Is this on a textual command line or a GUI based program?
    Last edited by Prelude; 11-14-2003 at 10:51 AM.
    My best code is written with the delete key.

  3. #3
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    A short question deserves a short answer.
    Look here.

    Since you did not specify what API, I assume you're using the Windows API.

    read an external file and make it scroll on the program window
    For the reading I would use <iostream>. I don't know what kind of window "program window" is, but I would just use SetWindowText() on a multiline edit control.
    Last edited by Dante Shamest; 11-14-2003 at 10:53 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I was bored, so here's a way to scroll with a text interface:
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <stdlib.h>
    #include <conio.h>
    
    using namespace std;
    
    vector<string> map_file ( ifstream& in );
    
    int main()
    {
      ifstream in ( "input.txt" );
    
      if ( !in.is_open() )
        return EXIT_FAILURE;
      vector<string> file = map_file ( in );
      vector<string>::size_type scroll = 0;
    
      cout<<"How to scroll: Up = j, Down = k, Quit = q"<<endl;
      for ( ; ; ) {
        cout<< file[scroll] <<endl;
        switch ( getch() ) {
        case 'j':
          if ( scroll > 0 )
            scroll--;
          break;
        case 'k':
          if ( scroll < file.size() - 1 )
            scroll++;
          break;
        case 'q':
          return EXIT_SUCCESS;
        }
      }
    }
    
    vector<string> map_file ( ifstream& in )
    {
      vector<string> file;
      string line;
    
      while ( getline ( in, line ) )
        file.push_back ( line );
    
      return file;
    }
    "But Prelude, it only scrolls one line at a time!" Duh, of course it does, but adding the functionality for more than one line at a time is simple enough to be left as an exercise.
    My best code is written with the delete key.

  5. #5
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Interesting way of implementing scrolling, Prelude.

  6. #6
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    two questions about this.....
    a) what does the last function do?
    it returns a vector of strings and takes a reference to an ifstream object is all i know.

    b) : vector<string>::size_type scroll = 0;
    what is that? is size_type an enum or what?
    Ph33r the sphericalCUBE

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >a) what does the last function do?
    It reads the contents of a file, line by line, into a vector of strings. It's a convenience function so that you don't have to do it in main.

    >what is that? is size_type an enum or what?
    size_type is a type (usually a typedef) defined by std::vector to represent a value that holds a valid index. It is equivalent to size_t in function, except restricted to vectors.
    My best code is written with the delete key.

  8. #8
    Gronkulator of Surds littleweseth's Avatar
    Join Date
    Oct 2003
    Posts
    68
    so size_type is used to prevent overrun?

    vector<string>::size_type scroll = 0;
    and this is (im guessing here.....) a static member variable?
    Ph33r the sphericalCUBE

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with edit box and vert scroll
    By AndewWood in forum Windows Programming
    Replies: 11
    Last Post: 01-08-2008, 02:00 AM
  2. Wm-hscroll
    By bigdan43 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2005, 09:59 AM
  3. make Child Dialog not Popup?
    By Zeusbwr in forum Windows Programming
    Replies: 5
    Last Post: 04-08-2005, 02:42 PM
  4. Someone guide me here..
    By xeddiex in forum Windows Programming
    Replies: 3
    Last Post: 04-04-2005, 12:12 AM
  5. How to make scroll bar for MFC?
    By Kristian25 in forum Windows Programming
    Replies: 4
    Last Post: 03-05-2003, 07:15 AM