我尝试根据Microsoft提供here的说明启用DPI支持。我正在使用WPF WebBrowser
控件:
<Grid>
<WebBrowser Name="BrowserPane"
Source="pack://siteoforigin:,,,/Resources/HTML/index.html"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
LoadCompleted="BrowserPane_LoadCompleted"/>
</Grid>
我的代码实现了IDocHostUIHandler
,如下所示:
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.ShowContextMenu([In, MarshalAs(UnmanagedType.U4)] uint dwID, [In] Native.POINT pt, [In, MarshalAs(UnmanagedType.Interface)] object pcmdtReserved, [In, MarshalAs(UnmanagedType.Interface)] object pdispReserved)
{
return S_OK;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.GetHostInfo([In, Out] Native.DOCHOSTUIINFO info)
{
info.dwFlags = (uint)Native.DOCHOSTUIFLAG.DPI_AWARE;
info.dwDoubleClick = 0;
return S_OK;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.ShowUI([In, MarshalAs(UnmanagedType.U4)] uint dwID, [In, MarshalAs(UnmanagedType.Interface)] object activeObject, [In, MarshalAs(UnmanagedType.Interface)] object commandTarget, [In, MarshalAs(UnmanagedType.Interface)] object frame, [In, MarshalAs(UnmanagedType.Interface)] object doc)
{
return E_NOTIMPL;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.HideUI()
{
return E_NOTIMPL;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.UpdateUI()
{
return E_NOTIMPL;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.EnableModeless([In, MarshalAs(UnmanagedType.Bool)] bool fEnable)
{
return E_NOTIMPL;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.OnDocWindowActivate([In, MarshalAs(UnmanagedType.Bool)] bool fActivate)
{
return E_NOTIMPL;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.OnFrameWindowActivate([In, MarshalAs(UnmanagedType.Bool)] bool fActivate)
{
return E_NOTIMPL;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.ResizeBorder([In] Native.COMRECT rect, [In, MarshalAs(UnmanagedType.Interface)] object doc, [In, MarshalAs(UnmanagedType.Bool)] bool fFrameWindow)
{
return E_NOTIMPL;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.TranslateAccelerator([In] ref MSG msg, [In] ref Guid group, [In, MarshalAs(UnmanagedType.U4)] uint nCmdID)
{
return S_FALSE;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.GetOptionKeyPath([MarshalAs(UnmanagedType.LPArray), Out] string[] pbstrKey, [In, MarshalAs(UnmanagedType.U4)] uint dw)
{
return E_NOTIMPL;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.GetDropTarget([In, MarshalAs(UnmanagedType.Interface)] object pDropTarget, [MarshalAs(UnmanagedType.Interface), Out] out object ppDropTarget)
{
ppDropTarget = null;
return E_NOTIMPL;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.GetExternal([MarshalAs(UnmanagedType.IDispatch), Out] out object ppDispatch)
{
ppDispatch = this.Browser.ObjectForScripting;
return S_OK;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.TranslateUrl([In, MarshalAs(UnmanagedType.U4)] uint dwTranslate, [In, MarshalAs(UnmanagedType.LPWStr)] string strURLIn, [MarshalAs(UnmanagedType.LPWStr), Out] out string pstrURLOut)
{
pstrURLOut = null;
return E_NOTIMPL;
}
[return: MarshalAs(UnmanagedType.U4)]
uint Native.IDocHostUIHandler.FilterDataObject([In] IDataObject pDO, [Out] out IDataObject ppDORet)
{
ppDORet = null;
return E_NOTIMPL;
}
我在WebBrowser.LoadCompleted
事件中设置处理程序,如下所示:
var doc = this.Browser.Document as Native.ICustomDoc;
if (doc != null)
doc.SetUIHandler(this);
IDocHostUIHandler
上的一些方法被调用。例如ShowContextMenu
成功禁用上下文菜单。但GetHostInfo
从未被调用过。我是否设置了UI处理程序太晚了?
编辑:已经观察到此代码适用于某些Win10机器,但不适用于MY Win10机器。我的机器上还有其他东西需要配置才能使用吗?