Solved

Scroll Bar Buttons

Posted on 1999-01-21
10
221 Views
Last Modified: 2010-05-03
I'm creating an activex control which mimics the scroll bar buttons in the infamous outlook control.  I have the visual interface and basic properties down.  Just a couple of questions though...is there a simple api that can return the mouse button pressed?  And how can I simulate the behavior of scroll bar button when it is pressed and held down(i.e., continuous scroll in a given direction)?  The second question is the important one.

thanks, showme
0
Comment
Question by:showme
  • 4
  • 4
  • 2
10 Comments
 
LVL 1

Accepted Solution

by:
james_bligh earned 150 total points
ID: 1469949
Hi Showme,

I assume you are trying to respond to mouse clicks using the standard click event (if this is not the case, my apologies).

If this is the case then use the MouseDown event instead.  The MouseDown event interface gives some very useful info such as the button that was pressed and the (x,y) coordinates of the mouse at the time the button was pressed.

To do scrolling have the MouseDown event turn a boolean flag variable to true and capture the x and y coords in other global variables.

Next have the MouseMove event update the display every time the mouse moves over your control but only if the flag variable is set to true.  You can use the x and y coords you set when the button was pressed to calculate where the scrollbar should be moved to.

Lastly have the MouseUp event set the variable to false to stop the scrolling.

Hope this helps.

Rgds,

James
0
 
LVL 1

Expert Comment

by:Johnn
ID: 1469950
I'm just about done my ScrollWindow control...if you'd like a beta drop me a line.  All you do is drop this control on a container(form, picturebox, frame?, usercontrol) and the form adds V, H, or both scroll bars and scrolls the window in response to the users actions.

jnickle@shaw.wave.ca
0
 

Author Comment

by:showme
ID: 1469951
james_bligh,
I think I know what you mean but do you think you could provide some code to illustrate your answer.  Also, how can I specify the rate at which the VALUE property increases/decreases?  The way that I implemented your idea proved to approach the extremes too quickly?

thanks, showme
0
 
LVL 1

Expert Comment

by:james_bligh
ID: 1469952
Showme,

Here's some sample code that creates a control that demonstrates what I was talking about earlier.  The reason that you were approaching the extremes too quickly could have been because you were not scaling the coords given by MouseMove to the actual min and max of your scrollbar (but that's just a guess :).

Note: I didn't realise it at the time of my first response but you may actually be able to do without the MouseDown and MouseUp events (and the mblnScrolling flag) altogether because the MouseMove event actually gives the button state as well.  It depends on what functionality you are trying to implement.

[Start Code]
Option Explicit
Private mintValue As Integer
Private mintMax As Integer
Private mintMin As Integer
Private mblnScrolling As Boolean

Private Sub UserControl_Initialize()
    mintValue = 0
    mintMax = 100
    mintMin = 0
    mblnScrolling = False
End Sub

Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton Then
        mblnScrolling = True
    End If
End Sub

Private Sub UserControl_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If mblnScrolling = True Then
        mintValue = (X / Width) * (mintMax - mintMin)
        If mintValue < mintMin Then
            mintValue = mintMin
        ElseIf mintValue > mintMax Then
            mintValue = mintMax
        End If
        UserControl_Paint
    End If
End Sub

Private Sub UserControl_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton Then
        mblnScrolling = False
    End If
End Sub

Private Sub UserControl_Paint()
    Dim sngX As Single
    ' Blank the control
    Line (0, 0)-(Width, Height), RGB(255, 255, 255), BF
   
    ' Draw the marker line
    sngX = (mintValue / (mintMax - mintMin)) * Width
    Line (sngX, 0)-(sngX, Height), RGB(0, 0, 255)
End Sub
[End Code]

Hope this helps,

James
0
 
LVL 1

Expert Comment

by:Johnn
ID: 1469953
I think you guys would be saving yourselves alot of valuable time if you looked into my vbScrollWindow component.

http://members.home.net/wwwebsoft/vbScroll/index.htm

I understand you probably want to "know" how to do it, but in todays demanding market it's just sometimes better to use something that's already there instead of reinventing the wheel.

PS.  to correct a couple things in my earlier comment, frames can have a scroll bar but they don't seem to get scroll messages.  As for usercontrols, they don't provide a container property so it took some serious hacking but I finally got it work on usercontrols.  This new v2.0 will be available shortly.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:showme
ID: 1469954
james_bligh,
Thank you for the code and I apologize for drawing this out any longer than necessary.  However, I may not have clear stated my needs.  You see, I have created a usercontrol with two buttons at each end to represent the scroll up and scroll down buttons.  There is nothing inbetween these two buttons.  The only way to change to value of the scroll bar control should result from either pressing the up button or down button and nothing else.  Therefore, I need a way to handle a case when the user presses, say, the down arrow button and holds down the mouse button.  The control should respond by internally increase the Value property, at a reasonable pace, until the Max is reached.  I hope that this clears up the problem, if not please let me know and I'll try to clarify it again.

thanks, showme
0
 
LVL 1

Expert Comment

by:james_bligh
ID: 1469955
Ahhhh

Now I understand :)

In that case use a timer control.

1) Use the MouseDown event to set a variable indicating which button was pushed - up or down then start the timer control (by setting enabled to true).

2) Have the Timer event for the timer control update the value after a suitable delay (set the interval property on the timer control to adjust this delay).  If the variable says go up increase the value if it says down go down.

3) Use the MouseUp event to disable the timer control (by setting enabled to false).

Phew...

Hope this works better for you Showme.

Rgds,

James



0
 

Author Comment

by:showme
ID: 1469956
james_bligh,
Regardless of how you answer this I'll award you the points.  I was hoping there might be another way to accomplish this task without the use of any other controls.  My own control is not made up of any constituent controls in an attempt to keep it's size small (buttons created via DrawFrameControl api).  I realize a timer control probably won't add that much but would appreciate any alternatives.

thanks, showme
0
 
LVL 1

Expert Comment

by:james_bligh
ID: 1469957
Showme,

There is an alternative:

1) Get rid of the timer control.

2) In the MouseDown event keep the variable to indicate up or down but instead of enabling the timer run a Subroutine to do the movement.

3) That routine should do a while loop while the variable is true.  That loop should update the value, call DoEvents and then call the Win32 API sleep for a number of milliseconds.

4) The MouseDown event should just set the scrolling variable bat to false.

The following code gives a very simple example of this with a project with a single form with a textbox called text1 that has a number in it.

[Start Code]
Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private mblnGo As Boolean

Private Sub Form_Load()
    mblnGo = False
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    mblnGo = True
    RunGo
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    mblnGo = False
End Sub

Private Sub RunGo()
    While mblnGo
        Text1.Text = Str(Val(Text1.Text) + 1)
        Sleep 100
        DoEvents
    Wend
End Sub
[End Code]

Rgds,

James
0
 

Author Comment

by:showme
ID: 1469958
james_bligh,
Fantastic!  I greatly appreciate all your help.

regards, showme
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now