您可以使用<style type="text/css"> 在HEAD 块内设置内联样式,在设置IFrame width 时影响body 和html 元素size、margin 和padding和height 到100% 作为<iframe> 元素本身的属性。
视频大小成比例。如果您需要将视频限制为特定大小,您始终可以设置 WebBrowser.MaximumSize 属性(或其容器表单的相同属性,如果 WebBrowser 被锚定/停靠)。
请注意,我使用的是插值字符串,但当然使用 string.Format() 也是一样的。
string videoID = "Some ID";
webBrowser1.Navigate("");
var doc = webBrowser1.Document.OpenNew(true);
doc.Write(
"<html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\"/> " +
"<style type=\"text/css\"> " +
"body { background: black; margin: 0px; padding: 0px; border: 0px; width: 100%; height: 100%; }" +
"iframe { margin: 0px; padding: 0px; border: 0px; display: block; }" +
"</style></head><body> " +
$"<iframe width=\"100%\" height=\"100%\" src=\"https://www.youtube.com/embed/{videoID} \"" +
"allow = \"autoplay; encrypted-media\" allowfullscreen></iframe>" +
"</body></html>");
webBrowser1.Refresh();
background: black; 无关紧要。只是视频播放器通常的背景颜色。
激活 WebBrowser 高级兼容模式和 GPU 渲染支持
在项目中添加WebBrowserAdvancedFeatures类,并在Form的构造函数中插入这个函数调用,需要在WebBrowser初始化之前激活这些功能。
在Form.OnFormClosing 中,高级功能被禁用。
using System.Security.AccessControl;
using Microsoft.Win32;
public SomeForm()
{
InitializeComponent();
WebBrowserAdvancedFeatures.ActivateWBAdvancedFeatures(Path.GetFileName(Application.ExecutablePath));
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
WebBrowserAdvancedFeatures.DeactivateWBAdvancedFeatures(Path.GetFileName(Application.ExecutablePath));
base.OnFormClosing(e);
}
// (...)
class WebBrowserAdvancedFeatures
{
private static string baseKeyName = @"Software\Microsoft\Internet Explorer\Main\FeatureControl";
private static string featuresKey = baseKeyName+ @"\FEATURE_BROWSER_EMULATION";
private static string hardwareAccelKey = baseKeyName + @"\FEATURE_GPU_RENDERING";
public static void ActivateWBAdvancedFeatures(string executableName)
{
RegistryKey wbFeatureKey = null;
RegistryKey wbGpuKey = null;
try {
wbFeatureKey = Registry.CurrentUser.OpenSubKey(featuresKey,
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey);
if (wbFeatureKey == null) {
wbFeatureKey = Registry.CurrentUser.CreateSubKey(featuresKey, true);
}
wbFeatureKey.SetValue(executableName, 11001, RegistryValueKind.DWord);
wbGpuKey = Registry.CurrentUser.OpenSubKey(hardwareAccelKey,
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey);
if (wbGpuKey == null) {
wbGpuKey = Registry.CurrentUser.CreateSubKey(hardwareAccelKey, true);
}
wbGpuKey.SetValue(executableName, 11001, RegistryValueKind.DWord);
}
finally {
wbFeatureKey?.Dispose();
wbGpuKey?.Dispose();
}
}
public static void DeactivateWBAdvancedFeatures(string executableName)
{
using (var wbFeatureKey = Registry.CurrentUser.OpenSubKey(featuresKey,
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey)) {
wbFeatureKey.DeleteValue(executableName, false);
}
using (var wbAccelKey = Registry.CurrentUser.OpenSubKey(hardwareAccelKey,
RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.WriteKey)) {
wbAccelKey.DeleteValue(executableName, false);
}
}
}