How to set the change size in Trackbar due to scrolling wheel on mouse in C#, Winform

dominicwong
dominicwong used Ask the Experts™
on
Hi experts

Could someone please tell me how to control the change size in trackbar due to scrolling wheel.
I have 'SmallChange' default to '1'. But that works only with keyboard.

Now when I scroll up the mouse wheel, the trackbar value increments by '3', and vice verse for scroll down.

Thanks in advance
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
That's usually a function of the mouse DRIVER itself, and not controlled by the application.  For instance, here is my mouse wheel settings window, which is also set to "3 lines":
Mouse Wheel Settings

Author

Commented:
Thanks for your prompt response.

What puzzles me is that another pc windows application written in Borland C works fine without any mouse driver setting change.
Even though the mouse driver setting of "Scrolling Size" is '3 Lines', the trackbar value still increments/decrements by value of '1' when scrolling up/down the mouse wheel.

Is it because the Borland C application has some setting so that it can be done at the application level rather than the mouse driver level?

If Borland C application can do it, can .NET application do it too?
DavidPresident
Top Expert 2010

Commented:
The change size (not surprisingly, the proper unit of measurement for the mouse change is called a 'Mikey' .. haven't had the opportunity to ever write that in EE before ) ... is actually a function of the hardware.  The specs don't address or  require that it be changeable, so you are asking about something that is vendor/product specific.

So don't assume it is even changeable on all hardware.  You need to contact the manufacturer to see if their hardware and/or driver has a programmable mechanism to do what you want, but good luck getting a response unless you tell them you'll be buying thousands of them.

Author

Commented:
Thanks dlethe.
Actually, in the same PC, a Windows application written in Borland C responds to mouse wheel movement in unit of one 'Mikey'.
But my Windows application written in C#, .NET responds in unit of 3 'Mikey'. I could change it down to one 'Mikey' by modifying the mouse driver setting as per the other Expert's advice.
So it is not a matter of hardware in my situation.

My question is why Borland C can do it while my C# .NET can only do it via the mouse driver.
DavidPresident
Top Expert 2010

Commented:
Borland C dates me, I remember working on a mouse driver in early 80s with borland C, but have forgotten almost all of what had to be done, other then that these drivers were all compiled with Borland C rather then the early MSFT C compilers.  

I had to compile to static executables rather then produce dynamically linked code.  Perhaps that is the problem.  Also C#.NET for drivers??  I can't imagine using that compiler.  Go old-school.  Create a MSDOS virtual machine and use Borland C.  

Hey, if it works with BorlandC, then use Borland C.  C#.NET is "probably" too bloated to even work if you disabled optimization and went to static small memory runtime.  I don't know, but I certainly wouldn't use that compiler for such things.
Most Valuable Expert 2012
Top Expert 2008

Commented:
What are the values for TrackBar.LargeChange and TrackBar.SmallChange?

Author

Commented:
In the TrackBar setting, I am using the default values:
SmallChange is 1
LargeChange is 5.
Most Valuable Expert 2012
Top Expert 2008
Commented:
I tried an experiment, with a custom TrackBar class, that overrides the WndProc, and detects WM_MOUSEWHEEL message.  The mouse wheel action is translated into the same action as SmallChange.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class TrackBarEx : TrackBar
    {

        [Flags]
        private enum VirtualKeyState
        {
            None,
            ControlKey = 0x0008,    // The CTRL key is down.
            LeftButton = 0x0001,    // The left mouse button is down.
            MiddleButton = 0x0010,    // The middle mouse button is down.
            RightButton = 0x0002,    // The right mouse button is down.
            ShiftKey = 0x0004,      // The SHIFT key is down.
            XButton1 = 0x0020,   // The first X button is down.
            XButton2 = 0x0040,   // The second X button is down.
        }

        protected override void WndProc(ref Message m)
        {
            const int WM_MOUSEWHEEL = 0x020A;

            if (m.Msg == WM_MOUSEWHEEL)
            {
                int wParam = m.WParam.ToInt32();

                // Get the mouse wheel delta from the high word (upper 16 bits)
                short delta = this.HiWord(wParam);
                // Get the virtual key state from the low word (lower 16 bits)
                VirtualKeyState keyState = (VirtualKeyState)this.LoWord(wParam);

                // Increment the value by the small change amount
                int newValue = this.Value - (Math.Sign(delta) * this.SmallChange);

                // Limit the value to be between Minimium and Maximum.
                this.Value = Math.Max(this.Minimum, Math.Min(newValue, this.Maximum));

                // Block the mouse wheel event
                return;
            }

            base.WndProc(ref m);
        }

        private short LoWord(int value)
        {
            return (short)new Point(value).X;
        }

        private short HiWord(int value)
        {
            return (short)new Point(value).Y;
        }

    }
}

Open in new window

Author

Commented:
Thanks TheLearnedOne for going out of your way to try it out for me. I appreciate that. Thank you.

Thanks also to dlethe.

Author

Commented:
Thanks TheLearnedOne.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial