📄 scrollerctrl.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<P align="center">
<img src="ScrollerCtrl/Scroller.jpeg" width="584" height="408" alt="Scroller"><br>
<i>CScrollerCtrl test app, running under Windows XP.</i></P>
<p>
<a href="#Motivation">Motivation</a><br>
<a href="#Description">Description</a><br>
<a href="#Features">Features</a><br>
<a href="#Use">Use</a><br>
<a href="#Demo">The Demo</a><br>
<a href="#CScrollerTestDlg::OnInitDialog">CScrollerTestDlg::OnInitDialog()</a><br>
<a href="#CTipsDialog::OnInitDialog">CTipsDialog::OnInitDialog()</a><br>
<a href="#CTipsDialog::SwitchTip">CTipsDialog::SwitchTip()</a><br>
<a href="#Implementation">Implementation</a><br>
<a href="#Compatibility">Compatibility</a><br>
</p>
<h2><a name="Motivation">Motivation:</a></h2>
<P align="justify">Well... i was bored the other day, and not wanting to do
anything necessary, decided to write a little autoscroller. It
turned out quite nicely, so i'm posting it here in the off chance it might be
useful to someone else.
</P>
<h2><a name="Description">Description:</a></h2>
<P align="justify">An <STRONG>autoscroller </STRONG>is a control that displays
text, scrolling it automatically at a pre-determined speed. They are
generally used for displaying credits in about dialogs, or other such trivial
tasks; by design they are not friendly enough to be used for displaying
important things, since users will be frustrated if they read faster or slower
than the text is scrolled. My scroller has an optional scrollbar though,
just to make it easier for such users should it ever be used to display text
they will actually want to read.</P>
<h2><a name="Features">Features:</a></h2>
<UL>
<LI>
<DIV align="justify">Smooth scrolling, with configurable speed.</DIV>
<LI>
<DIV align="justify">Optional manual scrolling, via scrollbar, mouse wheel, or
keyboard.</DIV>
<LI>
<DIV align="justify">Configurable delay before and after scrolling is complete and
after manual scrolling.</DIV>
<LI>
<DIV align="justify">Optional bitmap logo header: useful for displaying company
logo on about dialog type things.</DIV>
<LI>
<DIV align="justify">Optional bitmap background pattern, either tiled or centered.</DIV>
<LI>
<DIV align="justify">Configurable font, configurable background and text colors.</DIV>
</LI>
</UL>
<h2><a name="Use">Use:</a></h2>
<P align="justify">The public API for CScrollerCtrl consists of the following
methods:</P>
<PRE>// create the window; remove WS_VSCROLL to avoid showing scrollbar, remove WS_TABSTOP to disable keyboard scrolling.
BOOL Create(const RECT& rect, CWnd* pParentWnd, UINT uStyle = WS_CHILD|WS_VISIBLE|WS_VSCROLL|WS_TABSTOP|WS_GROUP, UINT nID = 0);
// activate/deactivate wrapping mode:
void SetWrapping(BOOL bWrap);
// Sets the color used for the background (if no pattern is set) or margins (if pattern is set and not tiled)
void SetBgColor(COLORREF clrBg);
// Sets the color used for text
void SetFgColor(COLORREF clrBg);
// Sets the font; size is in points, see LOGFONT documentation for weight constants
void SetFont(const CString& strName, int nSize, int nWeight);
// Sets the text to be displayed
void SetText(const CString& strText);
// Sets the bitmap to be displayed above the text
CBitmap* SetLogo(CBitmap* pbmpLogo);
// Sets the background pattern
CBitmap* SetPattern(CBitmap* pbmpPattern, BOOL bTile);
// Sets the time between frames (autoscrolling speed) (will revert to default if less than 0) (milliseconds)
void SetScrollDelay(int nScrollDelay);
// Sets the delay when autoscrolling pauses (will disable pausing if set less than scroll delay) (milliseconds)
void SetScrollPause(int nScrollPause); </PRE>
<P>Calling <code>Create()</code> gives the control its initial size and position,
as well as the styles in effect. To disable the scroll bar, remove the
WS_VSCROLL style; to disable all manual scrolling, remove both WS_VSCROLL
and WS_TABSTOP.</P>
<P>All other methods can be called either before or after the control is created;
if the control has been created, they will take effect immediately, otherwise
they will be in effect when <code>Create()</code> is called. All
attributes have defaults, so you only need to set the ones you need. Most
are self-explanitory, but a couple of things should be known:</P>
<UL>
<LI>
If a background pattern is set, text will be drawn with a shadow to aid in
visibility. The shadow color is calculated from the foreground and
background colors. The shadow offset is calculated at 1/10th the font
size.
<LI>
<code>SetPattern()</code>
takes a boolean flag as its second parameter; if false, it will center the
bitmap instead of tiling it.
<LI>
<code>SetWrapping()</code> changes the wrapping mode. This is on by
default, meaning that as the end of the text (or logo if no text) scrolls up
the screen, it is immediately followed by the beginning of the logo (or text if
no logo set). If <code>SetWrapping()</code> is called with false as the
parameter, content is scrolled completely off the screen before it is scrolled
back on again. See the contrast between the two dialogs in the demo; it's
a lot simpler than it sounds.</LI></UL>
<h2><a name="Demo">The Demo:</a></h2>
<P>The demo consists of a main dialog and child dialog. The main dialog
displays the introduction above, along with a logo bitmap, in an auto-scrolling
window with a scrollbar. The child dialog illistrates changing the text
on the fly by presenting a scrolling tips window.</P>
<P>The demo is mostly autogenerated, so you can safely ignore most of the
code. The important bits are in <code>CScrollerTestDlg::OnInitDialog()</code>,
<code>CTipsDialog::OnInitDialog()</code>, and <code>CTipsDialog::SwitchTip()</code>.</P>
<h3><a name="CScrollerTestDlg::OnInitDialog">CScrollerTestDlg::OnInitDialog():</a></h3>
<P>Here i begin by initializing the scroller with one of two sets of background
patterns and colors:</P>
<PRE> if ( ::GetTickCount()%2 ) // lazy man's rand()
{
m_scroller.SetPattern(CBitmap::FromHandle((HBITMAP)::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDB_BACKGROUND), IMAGE_BITMAP, 0,0, LR_SHARED)), FALSE);
// this background should be centered over a white background
m_scroller.SetBgColor(RGB(255,255,255));
m_scroller.SetFgColor(RGB(0,127,0));
}
else
{
m_scroller.SetPattern(CBitmap::FromHandle((HBITMAP)::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDB_BACKGROUND2), IMAGE_BITMAP, 0,0, LR_SHARED)), TRUE);
m_scroller.SetFgColor(RGB(255,255,225));
}</PRE>
<P>Note that in the first case, the bitmap is centered, while in the second it is
tiled.</P>
<P>Next, i set the logo bitmap and text:</P>
<pre> // logo bitmap
m_scroller.SetLogo(CBitmap::FromHandle((HBITMAP)::LoadImage(AfxGetResourceHandle(), MAKEINTRESOURCE(IDB_SCROLLER), IMAGE_BITMAP, 0,0, LR_SHARED)));
// text
m_scroller.SetText("Welcome!\n\n\n Please take a moment to familiarize yourself with the new design:\n\n\t稟bove you will see three tabs; these can be used to select either the Print, the Export, or the Preview page, depending on what operation you wish to perform.\n\n\t稟vailable on all pages, the area near the bottom of the window allows you to specify what items you wish to produce reports for, and what reports you wish to produce for these items. Below, options may be available for the selected report, depending on the type of report.\n\n\t稵he Print page allows selection of a printer, number of copies, and collation. These settings also apply to the Preview pane.\n\n\t稵he Export page allows selection of output destination and format.\n\n\t稵he Preview page allows you to see what the reports will look like before you print them. Any configuration changes made while this page is active will be previewed immediately, so it can be somewhat slower than the other pages. It's often a good idea to set report options while on the Print or Export page, and then switch to the Preview page to view the result.\n\n\n\n Thank you for using TOPSS!");</pre>
<P>Finally, i create the scroller to fill the entire client are:</P>
<pre> CRect rect;
GetClientRect(&rect);
m_scroller.Create(rect, this);</pre>
<P>It's probably worth noting here that this dialog is resizeable, and that the
scroller is resized in <code>OnSize()</code> to always fill the client
area. I set the WS_CLIPCHILDREN style for the dialog to prevent flashing
during resizing.</P>
<P>After creating the scroller, i create and show the tips dialog:</P>
<pre> // create tips dialog, and show at top-left of screen
m_dlgTips.Create(CTipsDialog::IDD, this);
m_dlgTips.SetWindowPos(NULL,0, 0, 0,0, SWP_NOZORDER|SWP_NOSIZE|SWP_NOACTIVATE|SWP_SHOWWINDOW);</pre>
<h3><a name="CTipsDialog::OnInitDialog">CTipsDialog::OnInitDialog():</a></h3>
<P>Here i begin by initalizing the scroller with a custom font, a message, and
turning off wrapping. I'll explain the reason for turning off wrapping
shortly:</P>
<Pre> m_scroller.SetFont("Microsoft Sans Serif", 10, FW_SEMIBOLD);
m_scroller.SetText("\tHello!");
m_scroller.SetWrapping(FALSE);</Pre>
<P>The messages displayed in this scroller aren't long enough that they need to be
readable while scrolling, so i let them scroll as fast as possible, pausing
when the tip is fully shown.</P>
<Pre> // short messages, read while paused, not scrolling
// so scroll quickly and pause for a 6 secs.
m_scroller.SetScrollDelay(0);
m_scroller.SetScrollPause(6000);</Pre>
<P>Finally do the creation. Note that i give the control an ID (1) and omit
the WS_VSCROLL and WS_TABSTOP styles; i don't want the user to be able to
scroll this manually.</P>
<Pre> // short messages, read while paused, not scrolling
// so scroll quickly and pause for a 6 secs.
m_scroller.SetScrollDelay(0);
m_scroller.SetScrollPause(6000);</Pre>
<P>Ok, now an explanation for turning off wrapping mode. The purpose of this
dialog is to display tips, one after another, and to do this by changing the
text at appropriate times. If wrapping is turned on, the text will never
be completely off screen, and switching will not look good. Now that
that's clear, on to how the text is actually switched...</P>
<h3><a name="CTipsDialog::SwitchTip">CTipsDialog::SwitchTip():</a></h3>
<P>CScrollerCtrl is an output only control; the only user input it accepts is
manual scrolling, and this it handles internally. However, it does send
one command message to its parent when the content has scrolled off the screen
completely. There is a constant defined
(CScrollerCtrl::SC_SCROLL_COMPLETE) to identify this command message, but since
it is the only command message sent by this control, it is not really necessary
to check this. <code>SwitchTip()</code> handles the command message when
it is recieved, and changes the text. The new text then scrolls on
screen, and life goes on.</P>
<h2><a name="Implementation">Implementation:</a></h2>
<P>This is a fairly simple control... Output is double-buffered to ensure
smooth updates. Two timers are used: the first is active for the life of
the window, ticking off at the scroll rate. The second is used to clear
the paused state when autoscrolling is paused for whatever reason; it is killed
as soon as it is triggered. All drawing is contained in one of three
methods:</P>
<UL>
<LI>
<code>FillBackground()</code> clears the back buffer when the drawing of a new
frame begins. If you wish to have a fancier background (animated, etc.)
this would be the place to do it.</LI>
<LI>
<code>DrawLogo()</code> draws the logo bitmap, if one is set. It takes a
parameter specifying the offset to draw at, and another specifying whether to
actually draw, or just calculate the size necessary to draw.</LI>
<LI>
<code>DrawBodyText()</code> draws the actual text. It also
takes parameters specifying the offset to draw at, and whether to draw or
just calculate the size necessary.</LI></UL>
<P>These methods are all virtual, so if you create a derived class, you can
override any or all of them to do something interesting. (display rich
text, etc.)</P>
<h2><a name="Compatibility">Compatibility:</a></h2>
<P>I've tested this on Windows XP, 2000, and 98.</P>
<P>It will compile with both Visual Studio 6 and Visual Studio.NET</P>
<P> </P>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -