what's the best way to get to scrollbars on Form - AutoScroll = true

A

AlexS

I would like to get current position of scrollbars when they are present for
scrolled content and revert to it later. Without this sometimes Form scrolls
automatically to top when getting focus back.

Any pointers?

Thanks
 
G

Guest

try using interop.

public const int SBS_HORZ = 0;
public const int SBS_VERT = 1;
public const int SBS_CTL = 2;
public const int SBS_BOTH = 3;

public const int WM_VSCROLL = 0x0115;
public const int WM_HSCROLL = 0x114;

public const int SB_LINEUP = 0;
public const int SB_LINEDOWN = 1;
public const int SB_PAGEUP = 2;
public const int SB_PAGEDOWN = 3;
public const int SB_THUMBPOSITION = 4;
public const int SB_THUMBTRACK = 5;
public const int SB_TOP = 6;
public const int SB_BOTTOM = 7;
public const int SB_ENDSCROLL = 8;

[DllImport("User32.dll",CharSet = CharSet.Auto,SetLastError=true)]
public static extern int GetScrollPos(IntPtr hWnd, int nBar);

[DllImport("User32.dll",CharSet = CharSet.Auto,SetLastError=true)]
public static extern bool PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);

[DllImport("User32.dll",CharSet = CharSet.Auto,SetLastError=true)]
public static extern int GetScrollRange(IntPtr hwnd, int nBar, ref int lpMinPos, ref int lpMaxPos);


For example to get the vertical scrollbar position of a RichTextBox:
int pos = GetScrollPos(richTextBox1.Handle, SBS_VERT);

To Set the scrollbar position of a RichTextBox:
int position = 500;
PostMessage (richTextBox1.Handle,WM_VSCROLL ,SB_THUMBPOSITION + 0x10000 * position,0);

To find out the maximum scroll position of a RichTextBox:
int minValue=0, maxValue=0;
GetScrollRange(richTextBox1.Handle, SBS_VERT, ref minValue, ref maxValue);
maxValue -= richTextBox1.Height;
 
A

AlexS

Thanks, Ted

My problem is that when Autoscroll is on for the form and form contains
several controls inside panel, which is scrolled by form, and when form gets
focus back it automatically scrolls to the top of panel and doesn't keep
current scroll position. I tried to use AutoScrollPosition in Form.Activated
to reset position to original one. This helps but with flicker of controls -
it looks like form content - in this case panel with controls - is first
drawn from position 0 and then scrolls to target position. I have text
boxes, labels and picture boxes on panel. Maybe problem is with one of
controls.
I am trying to remove the flicker but it seems the redraw of form content on
activation is built into form activation in .Net. Is it possible to prevent
redraw of form content when focus returns back to scrolled form? I would
want to resort to API only if it is not possible in managed way.
It's problem with form, not with controls on it.

Thanks
Alex

Ted said:
try using interop.

public const int SBS_HORZ = 0;
public const int SBS_VERT = 1;
public const int SBS_CTL = 2;
public const int SBS_BOTH = 3;

public const int WM_VSCROLL = 0x0115;
public const int WM_HSCROLL = 0x114;

public const int SB_LINEUP = 0;
public const int SB_LINEDOWN = 1;
public const int SB_PAGEUP = 2;
public const int SB_PAGEDOWN = 3;
public const int SB_THUMBPOSITION = 4;
public const int SB_THUMBTRACK = 5;
public const int SB_TOP = 6;
public const int SB_BOTTOM = 7;
public const int SB_ENDSCROLL = 8;

[DllImport("User32.dll",CharSet = CharSet.Auto,SetLastError=true)]
public static extern int GetScrollPos(IntPtr hWnd, int nBar);

[DllImport("User32.dll",CharSet = CharSet.Auto,SetLastError=true)]
public static extern bool PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);

[DllImport("User32.dll",CharSet = CharSet.Auto,SetLastError=true)]
public static extern int GetScrollRange(IntPtr hwnd, int nBar, ref int lpMinPos, ref int lpMaxPos);


For example to get the vertical scrollbar position of a RichTextBox:
int pos = GetScrollPos(richTextBox1.Handle, SBS_VERT);

To Set the scrollbar position of a RichTextBox:
int position = 500;
PostMessage (richTextBox1.Handle,WM_VSCROLL ,SB_THUMBPOSITION + 0x10000 * position,0);

To find out the maximum scroll position of a RichTextBox:
int minValue=0, maxValue=0;
GetScrollRange(richTextBox1.Handle, SBS_VERT, ref minValue, ref maxValue);
maxValue -= richTextBox1.Height;

AlexS said:
I would like to get current position of scrollbars when they are present for
scrolled content and revert to it later. Without this sometimes Form scrolls
automatically to top when getting focus back.

Any pointers?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top