11using System ;
22using System . Diagnostics . Contracts ;
3+ using System . Runtime . InteropServices ;
34using System . Windows . Forms ;
45
56namespace ReClassNET . UI
67{
78 public class ScrollableCustomControl : UserControl
89 {
10+ private const int WM_HSCROLL = 0x114 ;
11+ private const int WM_VSCROLL = 0x115 ;
12+ private const int SBS_HORZ = 0x0000 ;
13+ private const int SBS_VERT = 0x0001 ;
14+ private const int SIF_ALL = 0x0001 | 0x0002 | 0x0004 | 0x0010 ;
15+
16+ [ DllImport ( "user32.dll" , ExactSpelling = true , CharSet = CharSet . Auto ) ]
17+ private static extern bool GetScrollInfo ( HandleRef hWnd , int fnBar , SCROLLINFO si ) ;
18+
19+ [ StructLayout ( LayoutKind . Sequential ) ]
20+ private class SCROLLINFO
21+ {
22+ public int cbSize = Marshal . SizeOf ( typeof ( SCROLLINFO ) ) ;
23+ public int fMask = SIF_ALL ;
24+ public int nMin ;
25+ public int nMax ;
26+ public int nPage ;
27+ public int nPos ;
28+ public int nTrackPos ;
29+ }
30+
31+ private readonly SCROLLINFO scrollinfo = new SCROLLINFO ( ) ;
32+
933 public ScrollableCustomControl ( )
1034 {
1135 VScroll = true ;
@@ -49,7 +73,7 @@ protected override void OnMouseWheel(MouseEventArgs e)
4973 private const int SB_BOTTOM = 7 ;
5074 private const int SB_ENDSCROLL = 8 ;
5175
52- private ScrollEventType WParamToScrollEventType ( IntPtr wParam )
76+ private static ScrollEventType WParamToScrollEventType ( IntPtr wParam )
5377 {
5478 switch ( LoWord ( ( int ) wParam ) )
5579 {
@@ -76,9 +100,6 @@ private ScrollEventType WParamToScrollEventType(IntPtr wParam)
76100 }
77101 }
78102
79- private const int WM_HSCROLL = 0x114 ;
80- private const int WM_VSCROLL = 0x115 ;
81-
82103 private void SetValue ( ScrollEventType type , ScrollProperties scrollProperties , int newValue )
83104 {
84105 Contract . Requires ( scrollProperties != null ) ;
@@ -163,9 +184,20 @@ public void DoScroll(ScrollOrientation orientation, int amount)
163184 SetValue ( ScrollEventType . ThumbPosition , scrollProperties , scrollProperties . Value + amount ) ;
164185 }
165186
166- private void ProcessMessage ( ref Message msg , ScrollProperties scrollProperties )
187+ private void ProcessMessage ( ref Message msg )
167188 {
168- Contract . Requires ( scrollProperties != null ) ;
189+ ScrollProperties scrollProperties ;
190+ int bar ;
191+ if ( msg . Msg == WM_VSCROLL )
192+ {
193+ scrollProperties = VerticalScroll ;
194+ bar = SBS_VERT ;
195+ }
196+ else
197+ {
198+ scrollProperties = HorizontalScroll ;
199+ bar = SBS_HORZ ;
200+ }
169201
170202 var type = WParamToScrollEventType ( msg . WParam ) ;
171203 switch ( type )
@@ -180,7 +212,11 @@ private void ProcessMessage(ref Message msg, ScrollProperties scrollProperties)
180212 break ;
181213 case ScrollEventType . ThumbTrack :
182214 case ScrollEventType . ThumbPosition :
183- SetValue ( type , scrollProperties , HiWord ( ( int ) msg . WParam ) ) ;
215+
216+ if ( GetScrollInfo ( new HandleRef ( this , Handle ) , bar , scrollinfo ) )
217+ {
218+ SetValue ( type , scrollProperties , scrollinfo . nTrackPos ) ;
219+ }
184220 break ;
185221 }
186222 }
@@ -198,7 +234,7 @@ protected override void WndProc(ref Message msg)
198234 break ;
199235 }
200236
201- ProcessMessage ( ref msg , msg . Msg == WM_VSCROLL ? VerticalScroll : ( ScrollProperties ) HorizontalScroll ) ;
237+ ProcessMessage ( ref msg ) ;
202238
203239 return ;
204240 }
@@ -207,8 +243,6 @@ protected override void WndProc(ref Message msg)
207243 base . WndProc ( ref msg ) ;
208244 }
209245
210- static int HiWord ( int number ) => ( number >> 16 ) & 0xffff ;
211-
212246 static int LoWord ( int number ) => number & 0xffff ;
213247 }
214248}
0 commit comments