Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

scrolling away! 1

Status
Not open for further replies.

Bones3

Programmer
Jul 27, 2003
151
0
0
US
[COLOR=e87400]Hi everyone I have a noob question regarding a windows form application I am working on in Visual C++ .net. I want to make a form that has a lot of text and some pictures, and I want the whole thing to be scrollable in case the user makes the window smaller. (kindof like this web-page im MSIE) Do you use the scollbars in the tools? Or is there a property somewhere. Or do I have to edit the .h file?[/color]

-Bones
 
Look at the Scribble sample applicaiton and tutorial on MSDN.

-pete
 
Ok I will examine it for a while.

-Bones
 
I'm still confused. Do you know a place that specifically tutors on how to implament a standard scroll bar in the window? :
-Bones
 
I will only explain about the vertical bar.
The scrollbar does not scroll the window. It is a control that takes care of drawing itself en notify via the WindowProc with a WM_VSCROLL, when an event has happened and what has happened. What is going to be done with the WM_VSCROLL message and its data is up to the programmer so the scrollbar could be used for other things as well.
For bookkeeping a standard scrollbar's state the SCROLLINFO structure is used, together with the GetScrollInfo() and SetScrollInfo() functions.

The nice thing build in is that you can set for a amounth to scroll with SetScrollInfo(), while internally it checks and especially, it corrects the scroll amounth to the amounth that scrolling is allowed. Then, with a GetScrollInfo() call right behind it you retrieve the amounth allowed to scroll. It could be nothing, when already at the bottom of document, or only partially after a page down, when only half a page was left.

Here is how a WM_VSCROLL message can be implemented.
The v structure contains our own scrolling related variables. I have taken the code from a window containing class in which v is contained, but further the code should be correct.

[tt]
case WM_VSCROLL:
si.cbSize = sizeof(SCROLLINFO); //(1)
si.fMask = SIF_ALL;
GetScrollInfo(hWnd, SB_VERT, &si);

v.cyPos = si.nPos; //(2)

switch(LOWORD(wParam)){ //(3)
case SB_TOP: si.nPos = si.nMin; break;
case SB_BOTTOM: si.nPos = si.nMax; break;
case SB_LINEUP: si.nPos -= 1; break;
case SB_LINEDOWN: si.nPos += 1; break;
case SB_PAGEUP: si.nPos -= si.nPage; break;
case SB_PAGEDOWN: si.nPos += si.nPage; break;
case SB_THUMBTRACK: si.nPos = si.nTrackPos; break;
}

si.fMask = SIF_POS; //(4)
SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
GetScrollInfo(hWnd, SB_VERT, &si);

if(si.nPos != v.cyPos){ //(5)
ScrollWindow(hWnd, NC, (si.nPos - v.cyPos) * v.cyUni, NULL, NULL);
v.cyPos = si.nPos;
}
break;
/* COMMENTS:
//A scroll request arrives from the scrollbar
1. Get current scroll info
2. update our own variable with latest data
3. Translate scroll event to amounth to scroll
4. Suggest the scroll and get the corrected data
5. A position change is allowed/made in the bookkeeping;
perform the actual scroll and update our own variable
*/
[/tt]

That's it for the WM_VSCROLL message. Note that if your document or window contains child windows, ScrollWindow() will automatically move them in sync with where they visual appear after the scroll bitblit, unless your window was made with the CS_OWNDC style. If you like/need to control this yourself look at the ScrollDC() function.

When the window its size changes or the document size changes, the SCROLLINFO structure needs updating. Because same things need to be performed I've put the code inside one function that can be called whenever there's a update needed. The function will scroll the window when needed, for example when the document is scrolled till bottom and then the Y-size is increased.
Before calling this function it is important to update our v.vars first with the new scroll settings.

[tt]
void MyUpdateScrollInfo()
{
si.cbSize = sizeof(SCROLLINFO); //(1)
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;

si.nMin = 0;
si.nMax = (v.cyDoc / v.cyUni) - 1; //(2)
si.nPage = v.cyWin / v.cyUni;
si.nPos = v.cyPos;

SetScrollInfo(hwnd, SB_VERT, &si, TRUE); //(3)
GetScrollInfo(hwnd, SB_VERT, &si);

if(si.nPos != v.cyPos){ //(4)
ScrollWindow(hWnd, NC, (si.nPos - v.cyPos) * v.cyUni, NULL, NULL);
v.cyPos = si.nPos;
}
}
/* COMMENTS:
1. Prepare the structure and announce what needs update
2. Set the possible changes document-, scrollunit- and window size,
and current scroll position.
3. Set the info and receive the checked and corrected data
4. scroll if needed and update v.var
*/
[/tt]

That's it for updating the scroll info. Call the function from the following code situations...

[tt]
case WM_SIZE:
v.cxWin = LOWORD(lParam);
v.cyWin = HIWORD(lParam);

zUpdateScrollInfo();
break;

case WM_MOVE:
v.oxWin = LOWORD(lParam);
v.oyWin = HIWORD(lParam);
break;

void SetDocumentSize(long cx, long cy)
{
v.cxDoc = max(1, cx); //(1)
v.cyDoc = max(1, cy);

MyUpdateScrollInfo();
}

void SetScrollUnits(long cx, long cy)
{
v.cxUni = max(1, cx);
v.cyUni = max(1, cy);

MyUpdateScrollInfo();
}

void SetWindowDim(long ox, long oy, long cx, long cy) //(2)
{
MoveWindow(hwnd, ox, oy, max(1, cx), max(1, cy), FALSE);
}
/*COMMENTS:
1. Minimum 1, to prevent divide by zero errors
2. If difference coords, will result in WM_SIZE and/or WM_MOVE message
*/

[/tt]

Few! got that? =)
Goodluck!



 
I believe I now understand what a scrollbar is and does, thank you. But I don't understand why your example code won't compile for me. First of all, this is supposed to go in the form class that will use the scroll bar (i.e. form1.h) right? The compile time errors I am getting start with some synax errors for the break, case, if, switch, hWnd, missing ';' and then it goes on to some missing storage class type specifiers and then some more stuff.
Do I need to include some other namespace?

Also I am confused about where some of the variables in your example came from, but I understand that could be a tedious question to answer.

It's ok, call me a self-taught noob. :)

-Bones
 
It is not fast copy and paste code, you'd have to do something yourself. It teaches the principles of how a scrollbar works and what needs to be handled. The essence of the code will work, but you have to read it, interpret it right, and then make your own constructs in which everything gets wired properly.

This is code taken from my own SCROLLER class that provides a scrollable surface when instanciated. All function calls are actually methods, and the missing vars like 'hwnd' and 'v 'are contained in the class. struct 'v' has members
oxWin, oyWin, cxWin, cyWin, cxDoc, cyDoc, cxPos, cyPos, cxUni and cyUni. So, its not code for MFC.




 
Ok, umm.... what do I do first. Should I make a situation in the form class that calls MyUpdateScrollInfo(), then make MyUpdateScrollInfo in a .h file perhaps? Then I can put a WM_VSCROLL section in the same .h file. (as long as the form class file has it included)

-Bones
 
If you're a beginner with C++, I suggest you go to your local bookstores programming section and just sit there and read some books on C++, MFC, .NET, etc. The Win32 API and/or MFC can't be picked up without some work. A basic understanding of MFC and/or API conventions is absolutely necessary.

If you are mostly interested in windows forms, check out C#. It uses the .NET framework, and with that its quite simple. Visual Basic is even easier.

Fixing the errors can sometimes be quicker than you think. A single missing semicolon can cause dozens of chain-reaction errors. So examine the first one, see if you can resolve it. Make sure to list the errors by filename. If you can't resolve the first one for a file, then move to another file. .H include files can extend errors beyond just themselves and on into other files as well.
 
Are there any specific titles you would recommend? Here is my situation:

1. I am familiar with C++ console programming.
2. I want to build programs in Windows with Visual C++ .net 2003
3. I want to know how to make a standard scroll bar.
4. I want to make windows forms.
5. I want to do 3d stuff someday (maybe that will have to be a whole new book?)
6. I want a book to keep that is step by step and will take me through most of what I want to know and more.


-Bones
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top