How to fix scrollbars when the window or container

2020-02-14 06:46发布

Currently in my application I am using the HScrollBar and VScrollBar for panning around in a large image. The part of the image that is shown is based on the scrollbar's Value property. However, when resizing the SplitContainer or resizing the form window the scrollbar's Value does not automatically update and it may render something off-screen.

At this point I noticed that if you clicked the scrollbar's arrow it magically fixes the scrollbar. I was wondering if there was any way to simulate clicking the scrollbar to do this in the Form_Resize and SplitContainer_Resize event handlers but I couldn't find anything.

Having to manually adjust the scrollbar's value in all resizing events is slow, ugly, and doesn't work well. I'd really like for the scrollbar to just automatically fix itself when the window resizes like it does when you click its arrow but I'm not sure how.

2条回答
太酷不给撩
2楼-- · 2020-02-14 07:31

Try calling the scrollbar's Invalidate() method in the form's resize event handler:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invalidate.aspx

That should cause it to redraw correctly after the form is resized.

查看更多
淡お忘
3楼-- · 2020-02-14 07:34

Use something like this:

HScrollBarObject.SetStyle(ControlStyles.ResizeRedraw, true);
查看更多
登录 后发表回答