Horizontally and Vertically Scrolling Text


This program builds upon the previous program and adds the feature of horizontal (as well as vertical) scrolling of the text. The output of the program is shown below (see also the full code listing).

Both vertical and horizontal scroll bars are available to facilitate scrolling lines of text into view. The window procedure contains several new static variables and messages pertaining to scrolling the text horizontally and vertically. The static variables of the window procedure are shown below.

void* __stdcall Client(void* window,
                       unsigned Identity,
                       void* parameter1,
                       void* parameter2)
{
 Handle Window(window);
 Handle Parameter1(parameter1);
 Handle Parameter2(parameter2);

 enum {Column1=30, Column2=40};

 static int WidthOfCharacter,
            WidthOfCapitals,
            HeightOfCharacter,
            WidthOfClient,
            HeightOfClient,
            MaximumWidth,
            PositionOfVerticalScroll,
            MaximumOfVerticalScroll,
            PositionOfHorizontalScroll,
            MaximumOfHorizontalScroll;

  int IncrementOfVerticalScroll,
      IncrementOfHorizontalScroll;

  ....
}

The variables that have been added are shown in the table below.

WidthOfClient The current width of the client area.
MaximumWidth The maximum width occupied by the text.
PositionOfHorizontalScroll The current position of the horizontal scroll bar.
MaximumOfHorizontalScroll The current range of the horizontal scroll bar.

Setting of the range and initial position of the scroll bars now occurs during the processing of the message Message::Size - which is shown below.

   case (unsigned)Message::Size:
     WidthOfClient = Parameter2.LowPart;
     HeightOfClient = Parameter2.HighPart;

     MaximumOfVerticalScroll = maximum(0,(int)Lines+2-HeightOfClient/HeightOfCharacter);
     PositionOfVerticalScroll = minimum(PositionOfVerticalScroll,MaximumOfVerticalScroll);

     Win::SetScrollRange(Window,(int)ScrollbarIdentity::Vertical,0,MaximumOfVerticalScroll,false);
     Win::SetScrollPosition(Window,(int)ScrollbarIdentity::Vertical,PositionOfVerticalScroll,true);

     MaximumOfHorizontalScroll = maximum(0,2+(MaximumWidth-WidthOfClient)/WidthOfCharacter);
     PositionOfHorizontalScroll = minimum(PositionOfHorizontalScroll,MaximumOfHorizontalScroll);

     Win::SetScrollRange(Window,(int)ScrollbarIdentity::Horizontal,0,MaximumOfHorizontalScroll,false);
     Win::SetScrollPosition(Window,(int)ScrollbarIdentity::Horizontal,PositionOfHorizontalScroll,true);
     break;

The range of the vertical scroll bar is set to be the number of lines plus 2, minus the number of lines that fit the current client area (rounded up to zero if necessary). The range of the horizontal scroll bar is 2 plus the maximum width of the text minus the width of the client divided by the width of a character.

Applications should emulate this final example rather than the previous two. The processing that occurs during Message::Size correctly performs scrolling calculations. Whilst the code is fairly voluminous, it provides a template for scrolling text in all sorts of applications. For example, editors and word processors require this type of logic. Most of the samples of subsequent chapters are a lot simpler.