Home   Index   About
Ultimate Pack


Custom Search
Example of Scrolling Text

The following example shows how to have your application scroll text in response to input from the horizontal and vertical scroll bars.

HDC hdc;

PAINTSTRUCT ps;

TEXTMETRIC tm;

SCROLLINFO si;

/* These variables are required to display text. */

static int xClient; /* width of client area */

static int yClient; /* height of client area */

static int xClientMax; /* maximum width of client area */

static int xChar; /* horizontal scrolling unit */

static int yChar; /* vertical scrolling unit */

static int xUpper; /* average width of uppercase letters */

static int xPos; /* current horizontal scrolling position */

static int yPos; /* current vertical scrolling position */

static int xMax; /* maximum horiz. scrolling position */

static int yMax; /* maximum vert. scrolling position */

int xInc; /* horizontal scrolling increment */

int yInc; /* vertical scrolling increment */

int i; /* loop counter */

int x, y; /* horiz. and vert. printing coords */

int FirstLine; /* first line in the invalidated area */

int LastLine; /* last line in the invalidated area */

/* Create an array of lines to display. */

#define LINES 27

static char *abc[] = { "anteater", "bear", "cougar", "dingo",

"elephant", "frog", "gazelle", "hyena", "iguana", "jackal",

"kangaroo", "llama", "moose", "newt", "octopus", "penguin",

"quail", "rat", "squid", "tortoise", "urus", "vole",

"walrus", "xylophone", "yak", "zebra",

"This line contains many words, but no character. Go figure." };

switch (uMsg) {

case WM_CREATE :

/* Get the handle of the client area's device context. */

hdc = GetDC (hwnd);

/* Extract font dimensions from the text metrics. */

GetTextMetrics (hdc, &tm);

xChar = tm.tmAveCharWidth;

xUpper = (tm.tmPitchAndFamily & 1 ? 3 : 2) * xChar/2;

yChar = tm.tmHeight + tm.tmExternalLeading;

/* Free the device context. */

ReleaseDC (hwnd, hdc);

/*

* Set an arbitrary maximum width for client area.

* (xClientMax is the sum of the widths of 48 average

* lowercase letters and 12 uppercase letters.)

*/

xClientMax = 48 * xChar + 12 * xUpper;

return 0;

case WM_SIZE:

/* Retrieve the dimensions of the client area. */

yClient = HIWORD (lParam);

xClient = LOWORD (lParam);

/*

* Determine the maximum vertical scrolling position.

* The two is added for extra space below the lines

* of text.

*/

yMax = max (0, LINES + 2 - yClient/yChar);

/*

* Make sure the current vertical scrolling position

* does not exceed the maximum.

*/

yPos = min (yPos, yMax);

/*

* Adjust the vertical scrolling range and scroll box

* position to reflect the new yMax and yPos values.

*/

si.cbSize = sizeof(si);

si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;

si.nMin = 0;

si.nMax = yMax;

si.nPage = yClient / yChar;

si.nPos = yPos;

SetScrollInfo(hwnd, SB_VERT, &si, TRUE);

/*

* Determine the maximum horizontal scrolling position.

* The two is added for extra space to the right of the

* lines of text.

*/

xMax = max (0, 2 + (xClientMax - xClient)/xChar);

/*

* Make sure the current horizontal scrolling position

* does not exceed the maximum.

*/

xPos = min (xPos, xMax);

/*

* Adjust the horizontal scrolling range and scroll box

* position to reflect the new xMax and xPos values.

*/

si.cbSize = sizeof(si);

si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;

si.nMin = 0;

si.nMax = xMax;

si.nPage = xClient / xChar;

si.nPos = xPos;

SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);

return 0;

case WM_PAINT:

/* Prepare the window for painting. */

hdc = BeginPaint(hwnd, &ps);

/*

* Use the current vertical scrolling position and

* coordinates of the invalid rectangle to determine

* the range of new lines that should be drawn in the

* client area.

*/

FirstLine = max (0, yPos + ps.rcPaint.top/yChar - 1);

LastLine = min (LINES, yPos + ps.rcPaint.bottom/yChar);

/* Display these lines. */

for (i = FirstLine;i < LastLine;i++) {

x = xChar * (1 - xPos);

y = yChar * (1 - yPos + i);

TextOut (hdc, x, y, abc[i], lstrlen(abc[i]));

}

/* Indicate that painting is finished. */

EndPaint(hwnd, &ps);

break;

case WM_HSCROLL:

switch(LOWORD (wParam)) {

/* User clicked shaft left of the scroll box. */

case SB_PAGEUP:

xInc = -8;

break;

/* User clicked shaft right of the scroll box. */

case SB_PAGEDOWN:

xInc = 8;

break;

/* User clicked the left arrow. */

case SB_LINEUP:

xInc = -1;

break;

/* User clicked the right arrow. */

case SB_LINEDOWN:

xInc = 1;

break;

/* User dragged the scroll box. */

case SB_THUMBTRACK:

xInc = HIWORD(wParam) - xPos;

break;

default:

xInc = 0;

}

/*

* If applying the horizontal scrolling increment does not

* take the scrolling position out of the scrolling range,

* increment the scrolling position, adjust the position

* of the scroll box, and update the window.

*/

if (xInc = max (-xPos, min (xInc, xMax - xPos))) {

xPos += xInc;

ScrollWindowEx (hwnd, -xChar * xInc, 0,

(CONST RECT *) NULL, (CONST RECT *) NULL,

(HRGN) NULL, (LPRECT) NULL, SW_INVALIDATE);

si.cbSize = sizeof(si);

si.fMask = SIF_POS;

si.nPos = xPos;

SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);

UpdateWindow (hwnd);

}

return 0;

case WM_VSCROLL:

switch(LOWORD (wParam)) {

/* User clicked the shaft above the scroll box. */

case SB_PAGEUP:

yInc = min(-1, -yClient / yChar);

break;

/* User clicked the shaft below the scroll box. */

case SB_PAGEDOWN:

yInc = max(1, yClient / yChar);

break;

/* User clicked the top arrow. */

case SB_LINEUP:

yInc = -1;

break;

/* User clicked the bottom arrow. */

case SB_LINEDOWN:

yInc = 1;

break;

/* User dragged the scroll box. */

case SB_THUMBTRACK:

yInc = HIWORD(wParam) - yPos;

break;

default:

yInc = 0;

}

/*

* If applying the vertical scrolling increment does not

* take the scrolling position out of the scrolling range,

* increment the scrolling position, adjust the position

* of the scroll box, and update the window. UpdateWindow

* sends the WM_PAINT message.

*/

if (yInc = max(-yPos, min(yInc, yMax - yPos))) {

yPos += yInc;

ScrollWindow(hwnd, 0, -yChar * yInc,

(CONST RECT *) NULL, (CONST RECT *) NULL,

(HRGN) NULL, (LPRECT) NULL, SW_INVALIDATE);

si.cbSize = sizeof(si);

si.fMask = SIF_POS;

si.nPos = YPos;

SetScrollInfo(hwnd, SB_VERT, &si, TRUE);

UpdateWindow (hwnd);

}

return 0;


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun