System.Windows.Forms.WebBrowser控制DPI支持?

时间:2017-03-15 20:18:59

标签: c# winforms webbrowser-control

如何使System.Windows.Forms.WebBrowser控件使用de DPI_AWARE标志显示支持DPI的文档?如果有人可以发布代码示例,将不胜感激。

修改1:添加了更多详情

问题是px单元被视为正常像素单元(不是网状),因此150%dpi配置的大小差别很大,甚至更高的dpi设置。 WebBrowser.Version是11.x

class myWebBrowser: System.Windows.Forms.WebBrowser, IDocHostUIHandler
{
    private const uint E_NOTIMPL = 0x80004001;
    private const uint S_OK = 0;
    private const uint S_FALSE = 1;
    private HostUIFlags flags = HostUIFlags.DPI_AWARE;

    public myWebBrowser()
    {
        this.DocumentCompleted += OnLoadCompleted;
    }

    private void OnLoadCompleted(object sender, WebBrowserDocumentCompletedEventArgs args)
    {
        ICustomDoc doc = this.Document.DomDocument as ICustomDoc;
        if (doc != null)
        {
            \\code reaches this point
            doc.SetUIHandler(this);
        }
    }

    uint GetHostInfo(ref DOCHOSTUIINFO info)
    {
        info.dwFlags = (int)flags;
        info.dwDoubleClick = 0;
        return S_OK;
    }

    \\other IDocHostUIHandler methods...

}

接口在这里定义:

[ComImport, Guid("3050F3F0-98B5-11CF-BB82-00AA00BDCE0B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface ICustomDoc
{
    [PreserveSig]
    int SetUIHandler(IDocHostUIHandler pUIHandler);
}

[ComImport, Guid("BD3F23C0-D43E-11CF-893B-00AA00BDCE1A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IDocHostUIHandler
{
    [PreserveSig]
    uint ShowContextMenu(int dwID, POINT pt, [MarshalAs(UnmanagedType.Interface)] object pcmdtReserved, [MarshalAs(UnmanagedType.Interface)] object pdispReserved);

    [PreserveSig]
    uint GetHostInfo(ref DOCHOSTUIINFO info);

    [PreserveSig]
    uint ShowUI(int dwID, [MarshalAs(UnmanagedType.Interface)] object activeObject, [MarshalAs(UnmanagedType.Interface)] object commandTarget, [MarshalAs(UnmanagedType.Interface)] object frame, [MarshalAs(UnmanagedType.Interface)] object doc);

   \\etc.
}

[StructLayout(LayoutKind.Sequential)]
internal struct DOCHOSTUIINFO
{
    public int cbSize;
    public int dwFlags;
    public int dwDoubleClick;
    public IntPtr dwReserved1;
    public IntPtr dwReserved2;
}

0 个答案:

没有答案