How to get the number of lines to scroll with each mouse wheel notch

11 02 2012

 

The mouse wheel can be rotated towards or away from the user. How to find the amount of scrolling that should take place when the mouse wheel is moved.

We have two options to retrieve this information.

1) Use Win32 API SystemParametersInfo – This function is used to retrieve or set different system parameters.
For our purpose we can use SPI_GETWHEELSCROLLLINES as the 1st parameter of this function and the 3rd parameter is the the output which must point to a UINT variable that receives the number of lines.

2) We can read the same information from the system registery.
HKEY_CURRENT_USER\Control Panel\Desktop\WheelScrollLines

 

 

  

UINT uCachedScrollLines;
::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &uCachedScrollLines, 0);
UINT GetMouseScrollLines()
{
    int nScrollLines = 3;
    HKEY hKey;

    if (RegOpenKeyEx(HKEY_CURRENT_USER,  _T("Control Panel\\Desktop"),
                     0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
    {
        TCHAR szData[128];
        DWORD dwKeyDataType;
        DWORD dwDataBufSize = sizeof(szData);

        if (RegQueryValueEx(hKey, _T("WheelScrollLines"), NULL, &dwKeyDataType,
                           (LPBYTE) &szData, &dwDataBufSize) == ERROR_SUCCESS)
        {
            nScrollLines = _tcstoul(szData, NULL, 10);
        }

        RegCloseKey(hKey);
    }

    return nScrollLines;
}

 

SPI_SETWHEELSCROLLLINES can be used to sets the number of lines to scroll when the vertical mouse wheel is moved.