Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27333

VS 2010 Scrolling Hotkey No Focus?

$
0
0
Below is simple code which I've put together for the purpose of being able to read
richtextbox text and scroll it w/out having to loose focus on an already focused application.

So I came up with this code:

Code:


Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.Marshal

Public Class FAQ
    Dim Setting As New My.MySettings()

    <DllImport("User32.dll")> _
    Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _
    ByVal id As Integer, ByVal fsModifiers As Integer, _
    ByVal vk As Integer) As Integer
    End Function

    <DllImport("User32.dll")> _
    Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, _
    ByVal id As Integer) As Integer
    End Function

    Private Declare Function GetScrollPos Lib "user32.dll" ( _
        ByVal hWnd As IntPtr, _
        ByVal nBar As Integer) As Integer

    Private Declare Function SetScrollPos Lib "user32.dll" ( _
        ByVal hWnd As IntPtr, _
        ByVal nBar As Integer, _
        ByVal nPos As Integer, _
        ByVal bRedraw As Boolean) As Integer

    Private Declare Function PostMessage Lib "user32.dll" ( _
        ByVal hwnd As IntPtr, _
        ByVal wMsg As Integer, _
        ByVal wParam As Integer, _
        ByVal lParam As Integer) As Boolean

    Public Const MOD_ALT As Integer = &H1
    Public Const WM_HOTKEY As Integer = &H312

    Const SBS_HORZ = 0
    Const SBS_VERT = 1
    Const WM_VSCROLL = &H115
    Const WM_HSCROLL = &H114
    Const SB_THUMBPOSITION = 4

    Private Sub FAQ_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            RegisterHotKey(Me.Handle, 100, MOD_ALT, Keys.Up)
            RegisterHotKey(Me.Handle, 200, MOD_ALT, Keys.Down)
        Catch
        End Try
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_HOTKEY Then
            Dim id As IntPtr = m.WParam
            Select Case (id.ToString)
                Case "100"
                    Try
                        ' will add when below code works
                    Catch
                    End Try
                Case "200"
                    Try
                        Dim hwnd As IntPtr = r1.Handle
                        Dim Position = GetScrollPos(hwnd, SBS_VERT) + 20
                        If (SetScrollPos(hwnd, SBS_VERT, Position, True) <> -1) Then
                            PostMessage(hwnd, WM_VSCROLL, SB_THUMBPOSITION + _
                                                      &H10000 * Position, Nothing)
                        Else
                            MsgBox("Error: " & GetLastWin32Error() & ")")
                        End If
                    Catch
                    End Try
            End Select
        End If
        MyBase.WndProc(m)
    End Sub

    Private Sub FAQ_FormClosing(ByVal sender As System.Object, ByVal e As                                System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Try
            UnregisterHotKey(Me.Handle, 100)
            UnregisterHotKey(Me.Handle, 200)
        Catch
        End Try
    End Sub

The above code works to an extent. When pressing ALT+DOWN, the r1 (richtextbox)
scroll bar does move down by 20, however, it doesn't scroll into view.

Question: How do I make the richtextbox scroll into view the position which
i have set using the hotkeys? ie, what should I add to case 200 to do this?

Viewing all articles
Browse latest Browse all 27333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>