C# WebBrowser PanningMode [英] C# WebBrowser PanningMode
问题描述
我正在开发一个实现 C# WebBrowser 控件的 WinForms 应用程序.此应用程序正在部署到 Windows 7 SP1 Panasonic 触摸屏设备.
I'm working on a WinForms application that is implementing a C# WebBrowser control. This application is deploying to a Windows 7 SP1 Panasonic touch screen device.
很多时候,用户会浏览一个包含非常大图像的页面.为了查看此图像的一部分,他们需要水平滚动.手指纵向滚动还好,横向滚动很不配合.
Many times, the user will be browsing a page containing a very large image. In order to see parts of this image, they will need to scroll horizontally. While the vertical scrolling via finger is fine, horizontal scrolling is very uncooperative.
我看到我们有 ScrollViewer.SetPanningMode,但是我们有类似的 WebBrowser 吗?
I see we have ScrollViewer.SetPanningMode, but do we have something similar for the WebBrowser?
问题:我们能否为 WebBrowser 控件实现平滑的水平触摸滚动?
Question: Can we achieve smooth horizontal touch scrolling for a WebBrowser control?
当图片不够高垂直滚动时,水平滚动变得不可能.
When the image isn't high enough for vertical scrolling, it becomes impossible to scroll horizontally.
推荐答案
也许,我有解决这个平移问题的方法.它确实需要 IE10(桌面),但我记得在评论中读到(它被删除了吗?)这个项目的目标平台是 Windows 7,所以希望你可以自由地在那里部署 IE10.我用我的旧华硕 Eee PC T91MT(Windows 7 SP1 w/平台更新和
Perhaps, I have a solution to this panning problem. It does require IE10 (Desktop), but I remember reading in the comments (was it deleted?) that the target platform for this project is Windows 7, so hopefully you have freedom to deploy IE10 there. I tested it with my old Asus Eee PC T91MT (Windows 7 SP1 w/ Platform Update and IE10) and it felt pretty good even on that piece of hardware.
重点是:
Enable IE10 document mode for
WebBrowser
control.
Disable IE Legacy Input Model for WebBrowser
control.
代码(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(@"SoftwareMicrosoftInternet ExplorerMainFeatureControlFEATURE_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(@"SoftwareMicrosoftInternet ExplorerMainFeatureControlFEATURE_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(@"SoftwareMicrosoftInternet ExplorerMainFeatureControlFEATURE_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 的触摸屏机器,尽管我确定它不会工作.显然,新的 指针事件触摸 API 是为 Windows 7 引入的(带有 平台更新)专门用于 IE10.
I could not test it with IE9 as I don't have a touch-screen machine with IE9, although I'm sure it would not work. Apparently, the new Pointer Events Touch API was introduced for Windows 7 (with Platform Update) specifically for IE10.
让我们知道它是如何为您服务的.祝你好运!
Let us know how it works for you. Good luck!
更新了 WinForms 项目的链接.
这篇关于C# WebBrowser PanningMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!