So erkennen Sie, ob eine Scrollbar am Ende einer Richtextbox (VB.NET) ist oder nicht.
-
19-08-2019 - |
Frage
Meine Frage ist wie: Wie erkennen Sie, ob sich eine Scrollbar am Ende einer Richtextbox befindet oder nicht?
Bearbeiten: Wenn ich am Ende sage, meine ich ganz nach unten, nirgendwo anders.
Lösung
Probier das aus GetScrollRange
und GetScrollPos
API ...
Private Const SBS_HORZ = 0
Private Const SBS_VERT = 1
<DllImport("user32.dll")> _
Public Function GetScrollRange(ByVal hWnd As IntPtr, ByVal nBar As Integer, _
ByRef lpMinPos As Integer, _
ByRef lpMaxPos As Integer) As Boolean
End Function
<DllImport("user32.dll")> _
Public Function GetScrollPos(ByVal hWnd As Integer, _
ByVal nBar As Integer) As Integer
End Function
// ...
Dim scrollMin as Integer = 0
Dim scrollMax as Integer = 0
If(GetScrollRange(rtb.Handle, SBS_VERT, scrollMin, scrollMax) Then
Dim pos as Integer = GetScrollPos(rtb.Handle, SBS_VERT)
// Detect if they're at the bottom
EndIf
Anmerkungen:
Um festzustellen, ob die Scrollbar sichtbar ist, rufen Sie an GetWindowLong
und prüfen Sie WS_VSCROLL
Um den maximalen Wert zu bestimmen, den der Schieberegler erreichen kann, rufen Sie an GetScrollInfo
; Ich denke, der Höchstwert ist
scrollMax - largeChange + 1
Andere Tipps
Ich habe diesen Code verwendet, um die aktuellen und maximalen Positionen korrekt zu erhalten:
const int SB_HORZ = 0x0000;
const int SB_VERT = 0x0001;
const int WM_HSCROLL = 0x0114;
const int WM_VSCROLL = 0x0115;
const int SB_THUMBPOSITION = 4;
private enum ScrollInfoMask : uint
{
SIF_RANGE = 0x1,
SIF_PAGE = 0x2,
SIF_POS = 0x4,
SIF_DISABLENOSCROLL = 0x8,
SIF_TRACKPOS = 0x10,
SIF_ALL = (SIF_RANGE | SIF_PAGE | SIF_POS | SIF_TRACKPOS)
}
[StructLayout(LayoutKind.Sequential)]
struct SCROLLINFO
{
public uint cbSize;
public uint fMask;
public int nMin;
public int nMax;
public uint nPage;
public int nPos;
public int nTrackPos;
}
public int HScrollPosition
{
get
{
return GetScrollPos(Handle, SB_HORZ);
}
set
{
SetScrollPos((IntPtr)Handle, SB_HORZ, value, true);
PostMessageA((IntPtr)Handle, WM_HSCROLL, SB_THUMBPOSITION + 0x10000 * value, 0);
}
}
public int VScrollPosition
{
get
{
return GetScrollPos(Handle, SB_VERT);
}
set
{
SetScrollPos((IntPtr)Handle, SB_VERT, value, true);
PostMessageA((IntPtr)Handle, WM_VSCROLL, SB_THUMBPOSITION + 0x10000 * value, 0);
}
}
public int HScrollPositionMax
{
get
{
SCROLLINFO scrollInfo = new SCROLLINFO();
scrollInfo.fMask = (uint)ScrollInfoMask.SIF_ALL;
scrollInfo.cbSize = (uint)Marshal.SizeOf(scrollInfo);
GetScrollInfo(Handle, SB_HORZ, ref scrollInfo);
return scrollInfo.nMax - (int)scrollInfo.nPage;
}
}
public int VScrollPositionMax
{
get
{
SCROLLINFO scrollInfo = new SCROLLINFO();
scrollInfo.fMask = (uint)ScrollInfoMask.SIF_ALL;
scrollInfo.cbSize = (uint)Marshal.SizeOf(scrollInfo);
GetScrollInfo(Handle, SB_VERT, ref scrollInfo);
return scrollInfo.nMax - (int)scrollInfo.nPage;
}
}
Es ist langweilig, den "korrekten" Maximalwert zu erhalten. Sie müssen den NPAGE -Wert (großer Änderung) des maximalen Wertes GetCrollRange zurückgeben, und können ihn dann mit der Eigenschaft HScrollPosition korrekt vergleichen, wie:
if (_rtb.VScrollPosition == _rtb.VScrollPositionMax)
{
Debug.WriteLine("Scroll is at the bottom most edge");
}
if (richTextBox1.Size.Width - richTextBox1.ClientSize.Width > 10)
Entschuldigung, Sie müssen das selbst in VB.NET konvertieren. Sollte nicht zu schwer sein :)
Eine Methode ist die Verwendung der GetCrollpos -Funktion. Hier ist eine Art langweiliges Beispiel dafür mit VB.NET.
http://www.codeproject.com/kb/vb/vbnetscrolling.aspx?print=true