How to implement such a scroll bar?

This is a discussion on How to implement such a scroll bar? within the Windows Programming forums, part of the Platform Specific Boards category; Hi, there I want to create a scroll bar that can scroll the content in the main window using pure ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    How to implement such a scroll bar?

    Hi, there
    I want to create a scroll bar that can scroll the content in the main window using pure win32 api. The difficulty is that I don't know how to calculate the accurate length of the whole content (blending of English and Chinese, so surely they have different width), and how many lines the whole content will be broken down into to fit the CURRENT window(window is to be resized from time to time).

    Any clue on this will be greatly appreciated.
    Thank you in advance.
    Last edited by kevintse; 04-09-2009 at 09:12 PM.

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    The way I write scrollable windows is to call GetTextMetrics in the WM_CREATE routine to get the character width and height that is based on the systems font which normally does not change during a Windows session. Also, I track the size of the client area in the WM_SIZE routine where LOWORD(lParam) is the X value and HIWORD(lParam) is the Y value. This is basis for aligning text using TextOut to display the data in the client area.

    If possible, how about posting some of your code so that we can see what you have so far.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Thanks for your reply, Bob.
    Well, I already tried the way you said above before I raised this thread, I was not using fixed width font, while GetTextMetrics can only obtain average width of the characters, so each line would have different widths (even so when the program is supposed to display text written in languages becides English).

    I have figured out that I can call DrawText(hdc, buf, -1, &rect, DT_WORDBREAK) to let the API do word breaks for me (buf is of type TCHAR*), but I don't know how to calculate the number of lines, which is required to implement a scroll bar.

    The code is rather simple, it reads a text file and displays the text, now the difficulty comes to the "scrolling", how do I know the number of lines?
    Last edited by kevintse; 04-12-2009 at 08:58 AM.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Someone guided me to using GetTextExtentPoint32, which computes the width and height of a string of text. so now, I know the width of the whole content, and the number of lines would be "widthOfString / widthOfClientArea", but now how do I draw each line?

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    See the well-known sample from Petzold which does exatly that, posted many times on usenet..

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    The code is rather simple, it reads a text file and displays the text, now the difficulty comes to the "scrolling", how do I know the number of lines?
    Can you upload a representative sample of the text file? Thus, possible solutions can be tested prior to posting those solutions.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Quote Originally Posted by Alex31 View Post
    See the well-known sample from Petzold which does exatly that, posted many times on usenet..
    I was reading the book by Petzold, and there was indeed an example showing how to create scroll bars and how to scroll text. but the text he was using was not read from a text file, it was just an array whose number of lines was known before he run the program, and he didn't deal with word break cause the program he was demonstrating was displaying something like a table, or a spread sheet.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    Quote Originally Posted by BobS0327 View Post
    Can you upload a representative sample of the text file? Thus, possible solutions can be tested prior to posting those solutions.
    The text file is a long article. basically, I want to make a book reader which reads a text file (a book or an article) and displays the content of the text file in the window, the program has to deal with word break, scrolling, that's all.
    Last edited by kevintse; 04-12-2009 at 07:28 PM.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by kevintse View Post
    I was reading the book by Petzold, and there was indeed an example showing how to create scroll bars and how to scroll text. but the text he was using was not read from a text file, it was just an array whose number of lines was known before he run the program, and he didn't deal with word break cause the program he was demonstrating was displaying something like a table, or a spread sheet.
    You can use the Sysmets3 example with a dynamic struct array instead of the static struct that he is using in the example. For instance, the following code can be used with the example. This code reads in all the lines from an ASCII text file. You would do all your preliminary processing such as word breaks etc. when loading the file into the struct array. I've already implemented the following in sysmets3 to verify that it works.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    #include <stdlib.h> 
    
    struct node {
    	TCHAR * szSentence ;
    };
    
    struct node **sysmetrics = NULL;
    ULONG NUMLINES = 0;
    
    void AddSentenceToStruct( char *pSentence)
    {
    	sysmetrics = (struct node **)realloc(sysmetrics, (NUMLINES + 1) * sizeof(struct node *));
    	sysmetrics[NUMLINES] = (struct node *)malloc(sizeof(struct node));
    	sysmetrics[NUMLINES]->szSentence = strdup(pSentence);
    	NUMLINES++;
    }
    
    int main(int argc, char **argv)
    {
    	int iIndex = 0; 
    	FILE *hFile;
    	char szBuffer[MAX_PATH+1];
    
    	hFile = fopen ( argv[1], "rb" );
    	if ( hFile == NULL ) {
    		printf("Unable to open the file\n");
    		return -1;
    	}
    	while( fgets(szBuffer, MAX_PATH, hFile) != NULL)
    	{
    		fgets(szBuffer,MAX_PATH,hFile);
    		AddSentenceToStruct(szBuffer);
    	}
    	fclose (hFile);
    	for(iIndex = 0; iIndex < NUMLINES; iIndex++) {
    		printf("[%d]->str: %s", iIndex, sysmetrics[iIndex]->szSentence);
    	}
    	/* free all sysmetrics elements */
    	for(iIndex = 0; iIndex < NUMLINES; iIndex++) {
    		free(sysmetrics[iIndex]->szSentence);
    		free(sysmetrics[iIndex]);
    	}
    	free(sysmetrics);
    	return 0;
    }

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    This approach just fixes my problem, all I need is dynamic memory allocation.
    Now I can compute how many characters will fit one line, and I can put the character string for each line in the struct...
    Thank you, Bob, Thank you for your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 12:32 PM
  2. Bitmap with a scroll bar
    By SuperNewbie in forum Windows Programming
    Replies: 1
    Last Post: 10-29-2003, 10:36 PM
  3. no horizontal scroll bar?
    By scott27349 in forum C++ Programming
    Replies: 0
    Last Post: 03-16-2002, 09:41 PM
  4. Problem with Scroll Bar
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 11-05-2001, 04:34 AM
  5. Problem with Scroll Bar
    By Garfield in forum Windows Programming
    Replies: 3
    Last Post: 11-01-2001, 01:23 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21