将WPF浏览器的大小设置为其HTML内容 [英] Size WPF Browser to its HTML-Content

查看:140
本文介绍了将WPF浏览器的大小设置为其HTML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在WPF 浏览器控件中显示我的Flair,并将其尺寸调整到最小。



XAML:

 < Window x:Class =TestProject.MainWindow
[...]
Title =MainWindow
MaxHeight =500
MaxWidth =500
Loaded =Window_Loaded
SizeToContent =WidthAndHeight
Height =86.453
Width =163.44>
<网格>
< WebBrowser x:Name =browser>< / WebBrowser>
< / Grid>



这是我的Code-Behind:

  public partial class MainWindow:Window 
{
public MainWindow( )
{
InitializeComponent();


私有字符串CreateHTML()
{
StringBuilder builder = new StringBuilder();
builder.AppendLine(<!DOCTYPE html>);
builder.AppendLine(< html>);
builder.AppendLine(< body>);
builder.AppendLine(< a href = \http://stackexchange.com/users/5852250\>);
builder.AppendLine(< img src = \http://stackexchange.com/users/flair/5852250.png?theme=dark\width = \208 \height = \\ Stackhouse Exchange上的Felix D.的58 \alt = \配置文件,一个免费的社区驱动的Q& A网站\\title = \配置文件,用于Felix D. Exchange,一个由社区驱动的免费Q& A站点网络>);
builder.AppendLine(< / a>);
builder.AppendLine(< / body>);
builder.AppendLine(< html>);
return builder.ToString();


private void Window_Loaded(object sender,RoutedEventArgs e)
{
string htmlString = CreateHTML();
browser.NavigateToString(htmlString);


$ / code $ / pre

我希望浏览器(像 SizeToContent =WidthAndHeight)。



我还没有找到可行的解决方案。 b

我嵌入了 Microsoft.mshtml 并试图获得某物。出于 browser.Document 但我迄今没有取得任何进展。



任何人都做过某事。相似?

解决方案

您可以将浏览器的大小设置为 0,0 在导航之前,然后在文档加载完成后,可以将控件的宽度高度设置为 scrollWidth scrollHeight 文档:

  private void browser_LoadCompleted(object sender,NavigationEventArgs e)
{
browser.Width =((dynamic)browser.Document).Body.parentElement.scrollWidth;
browser.Height =((dynamic)browser.Document).Body.parentElement.scrollHeight;
}
private void browser_Navigating(object sender,NavigatingCancelEventArgs e)
{
browser.Width = 0;
browser.Height = 0;
}

注意

1)这篇文章是对大小WPF浏览器对其HTML内容的回答。如果关注点正好显示图像,则可以依赖于第一条评论中提到的 Image 控件。

2)当使用 WebBrowser 控件显示您的天赋时,您需要关闭通过为 body 标签设置 scroll =no属性进行滚动。您还需要为 body 标签设置 style = \padding:0px; margin:0px; \ 。通过为 img 标记设置 style =border:0px;,也可以删除一些图像边框和css更正。



3)在DPI设置设置为125%的窗口中,您需要将值乘以0.8。


I want to show my Flair in a WPF Browser Control and size it to the smallest size possible.

XAML:

<Window x:Class="TestProject.MainWindow"
        [...]
        Title="MainWindow" 
        MaxHeight="500" 
        MaxWidth="500"
        Loaded="Window_Loaded"
        SizeToContent="WidthAndHeight" 
        Height="86.453" 
        Width="163.44">
<Grid>
    <WebBrowser x:Name="browser"></WebBrowser>        
</Grid>

Here is my Code-Behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private string CreateHTML()
    {
        StringBuilder builder = new StringBuilder();
        builder.AppendLine("<!DOCTYPE html>");
        builder.AppendLine("<html>");
        builder.AppendLine("<body>");
        builder.AppendLine("<a href=\"http://stackexchange.com/users/5852250\">");
        builder.AppendLine("<img src=\"http://stackexchange.com/users/flair/5852250.png?theme=dark\" width=\"208\" height=\"58\" alt=\"profile for Felix D. on Stack Exchange, a network of free, community-driven Q&amp;A sites\" title=\"profile for Felix D. on Stack Exchange, a network of free, community-driven Q&amp;A sites\">");
        builder.AppendLine("</a>");
        builder.AppendLine("</body>");
        builder.AppendLine("<html>");
        return builder.ToString();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        string htmlString = CreateHTML();            
        browser.NavigateToString(htmlString);            
    }        
}

I want the browser(to behave like SizeToContent="WidthAndHeight" for its content).

I have not found a working solution.

I embedded Microsoft.mshtml and tried to get sth. out of browser.Document but I did not make any progress so far.

Anyone done sth. similar ?

解决方案

You can set the size of browser to 0,0 before navigating and then after document load completed, you can set Width and Height of control to scrollWidth and scrollHeight of document:

private void browser_LoadCompleted(object sender, NavigationEventArgs e)
{
    browser.Width = ((dynamic)browser.Document).Body.parentElement.scrollWidth;
    browser.Height = ((dynamic)browser.Document).Body.parentElement.scrollHeight;
}
private void browser_Navigating(object sender, NavigatingCancelEventArgs e)
{
    browser.Width = 0;
    browser.Height = 0;
}

Note

1) The post is an answer to Size WPF Browser to its HTML-Content. If the concern is exactly showing the image, you can rely on an Image control as mentioned also in the first comment.

2) When showing your flair using WebBrowser control, you need to turn off scrolling by setting scroll="no" attribute for body tag. Also you need to set style=\"padding:0px;margin:0px;\" for body tag. Also remove borders of image by setting style="border:0px;" for img tag and also maybe some other html and css corrections.

3) In a windows with DPI settings set to 125% you need to multiply values by 0.8.

这篇关于将WPF浏览器的大小设置为其HTML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆