C#WebBrowser PanningMode

时间:2013-06-18 13:22:04

标签: c# scroll touch webbrowser-control

我正在开发一个实现C#WebBrowser控件的WinForms应用程序。此应用程序正在部署到Windows 7 SP1 Panasonic触摸屏设备。

很多时候,用户将浏览包含非常大图像的页面。为了查看此图像的某些部分,它们需要水平滚动。虽然通过手指垂直滚动很好,但水平滚动非常不合作。

我看到我们有ScrollViewer.SetPanningMode,但我们是否有类似WebBrowser的东西?

问题:我们可以为WebBrowser控件实现流畅的水平触摸滚动吗?

当图像不够高以进行垂直滚动时,无法水平滚动。

1 个答案:

答案 0 :(得分:4)

也许,我有解决这个平移问题的方法。它确实需要IE10(桌面),但我记得在评论中读到(它被删除了吗?)这个项目的目标平台是Windows 7,所以希望你有自由在那里部署IE10。我用我的旧华硕Eee PC T91MT(Windows 7 SP1 w / Platform UpdateIE10)测试了它,即使在那块硬件上也感觉非常好。

WinFormsWPF抓取一个有效的VS2012项目。

主要观点是:

代码(C#):

using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Navigation;

namespace WpfWbApp
{
    //
    // By Noseratio [http://stackoverflow.com/users/1768303/noseratio]
    //
    // Question: http://stackoverflow.com/questions/17170011/c-sharp-webbrowser-panningmode
    //

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            SetBrowserCompatibilityMode();
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
            this.WB.LoadCompleted += WB_LoadCompleted;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.WB.Navigate(new Uri(new Uri(Assembly.GetExecutingAssembly().CodeBase), "content/test.htm").AbsoluteUri);
        }

        void WB_LoadCompleted(object sender, NavigationEventArgs e)
        {
            this.WB.Focus();
            this.WB.InvokeScript("focus");
        }

        private void SetBrowserCompatibilityMode()
        {
            // http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx

            // FeatureControl settings are per-process
            var fileName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);

            if (String.Compare(fileName, "devenv.exe", true) == 0) // make sure we're not running inside Visual Studio
                return;

            using (var key = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION",
                RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                // Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode.
                UInt32 mode = 10000; // 10000; 
                key.SetValue(fileName, mode, RegistryValueKind.DWord);
            }

            using (var key = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BLOCK_LMZ_SCRIPT",
                RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                // enable <scripts> in local machine zone
                UInt32 mode = 0;
                key.SetValue(fileName, mode, RegistryValueKind.DWord);
            }

            using (var key = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_NINPUT_LEGACYMODE",
                RegistryKeyPermissionCheck.ReadWriteSubTree))
            {
                // disable Legacy Input Model
                UInt32 mode = 0;
                key.SetValue(fileName, mode, RegistryValueKind.DWord);
            }

        }

    }
}

XAML:

<Window x:Class="WpfWbApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Touch and pan the picture" Width="1024" Height="800">
    <WebBrowser Name="WB"></WebBrowser>
</Window>

HTML:

<!doctype html>
<html>
<head>
<style>
body { -ms-content-zooming:none; -ms-scroll-rails: none; }  
</style>
</head>
<body style="overflow: auto">
<img src="panorama.jpg">
</body>
</html>

我无法用IE9测试它,因为我没有IE9的触摸屏机器,虽然我确信它不会工作。显然,新的Pointer Events Touch API是针对Windows 7(Platform Updatespecifically for IE10引入的。

告诉我们它是如何为您服务的。祝你好运!

<强> [EDITED] 更新了a WinForms project的链接。