问题
Due to an unsolved issue in my current project [ Weird scrollbar UI in hosted WPF composite control ], I've had to defer to workarounds. One such idea I came up with was to keep the ScrollViewer in question in sync with a Windows Forms ScrollBar (horizontal and vertical). How would I go about doing that?
In essence, I want the WinForms scrollbar(s) to work like the ScrollViewers (in terms of dragging and events suchlike).
回答1:
I think there's no any other way than send/listen to the lo-level API Windows messaging. You should have a decent knowledge of the Win APIs, and how to manage them. I'd add also that the scrollers are mid-complexity task to manage, but maybe it's me...
Here is the vertical scroll message reference.
回答2:
I managed to implement it without having to much around with the WinAPI. Here's how I did it:
- Registered a ScrollOffsetChanged event handler with the scrollviewer (the composite control that implemented the IScrollInfo interface exposed the event).
- Registered a ValueChanged event handler with the windows forms scrollbar.
- A couple of signal flags to prevent cyclic calls to the event handler.
- The first handler called an UpdateExternalScrollBars() function that calculated the Maximium Scrollbar property using the following expression: ScrollViewer->ExtentHeight/Width - ScrollViewer->ViewportHeight/Width. The Value property was directly set to ScrollViewer->Vertical/HorizontalOffset.
- The second fetched the Value property from the windows forms scrollbar and passed it to ScrolViewer->ScrollToVertial/HorizontalOffset(). The signal flags were appropriately set beforehand.
来源:https://stackoverflow.com/questions/6966705/synchronizing-a-wpf-scrollviewer-with-a-winforms-scrollbar