将 WPF 浏览器调整为 HTML-Content
Size WPF Browser to its HTML-Content
我想在 WPF 中展示我的天赋 Browser
控制它并将其调整到尽可能小的尺寸。
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>
这是我的代码隐藏:
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&A sites\" title=\"profile for Felix D. on Stack Exchange, a network of free, community-driven Q&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);
}
}
我希望浏览器(其内容表现得像 SizeToContent="WidthAndHeight"
)。
我还没有找到可行的解决方案。
我嵌入了 Microsoft.mshtml
并试图得到某物。 browser.Document
但到目前为止我没有取得任何进展。
有人做过某事。相似 ?
http://setmy.browsersize.com/ 这有帮助吗?这似乎允许您将浏览器设置为用于开发目的的设置大小。我觉得这可能是朝着正确方向的推动?
<a href='javascript:(function(w,h){
var n=window,d=document,s=n.screen;
function r(o){
n.resizeTo(Number(w)+o,h);
return n.innerWidth||d.body.clientWidth;
}
function c(t,u){
return t>u?(t-u)>>1:0;
}
if(r(1)==r(0)) {
if(typeof p=="undefined"||p.closed){
$("info").className="no_popup";
p=n.open(n.location.href,
"resize_popup",
"menubar,
toolbar,
location,
directories,
status,
resizable,
titlebar,
".split(",").join("=yes,")+",
width="+w+",height="+h+",
left="+c(s.width,w)+",
top="+c(s.height,h));
} else {
p.focus();
}
p.resizeTo(w,h);n=p;
}
if(w>s.width||h>s.height){
n.moveTo(0,0);
}
})(640, 480);'>640 x 480</a>
您可以在导航前将浏览器的大小设置为0,0
,然后在文档加载完成后,您可以将控件的Width
和Height
设置为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) post 是 Size WPF Browser to its HTML-Content 的答案。如果问题恰恰是显示图像,您可以依赖第一条评论中提到的 Image
控件。
2)使用WebBrowser
控件展示你的才华时,需要通过设置body
标签的scroll="no"
属性来关闭滚动.您还需要为 body
标签设置 style=\"padding:0px;margin:0px;\"
。还可以通过为 img
标记设置 style="border:0px;"
以及其他一些 html 和 css 更正来删除图像边框。
3) 在 DPI 设置为 125% 的 windows 中,您需要将值乘以 0.8。
我想在 WPF 中展示我的天赋 Browser
控制它并将其调整到尽可能小的尺寸。
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>
这是我的代码隐藏:
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&A sites\" title=\"profile for Felix D. on Stack Exchange, a network of free, community-driven Q&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);
}
}
我希望浏览器(其内容表现得像 SizeToContent="WidthAndHeight"
)。
我还没有找到可行的解决方案。
我嵌入了 Microsoft.mshtml
并试图得到某物。 browser.Document
但到目前为止我没有取得任何进展。
有人做过某事。相似 ?
http://setmy.browsersize.com/ 这有帮助吗?这似乎允许您将浏览器设置为用于开发目的的设置大小。我觉得这可能是朝着正确方向的推动?
<a href='javascript:(function(w,h){
var n=window,d=document,s=n.screen;
function r(o){
n.resizeTo(Number(w)+o,h);
return n.innerWidth||d.body.clientWidth;
}
function c(t,u){
return t>u?(t-u)>>1:0;
}
if(r(1)==r(0)) {
if(typeof p=="undefined"||p.closed){
$("info").className="no_popup";
p=n.open(n.location.href,
"resize_popup",
"menubar,
toolbar,
location,
directories,
status,
resizable,
titlebar,
".split(",").join("=yes,")+",
width="+w+",height="+h+",
left="+c(s.width,w)+",
top="+c(s.height,h));
} else {
p.focus();
}
p.resizeTo(w,h);n=p;
}
if(w>s.width||h>s.height){
n.moveTo(0,0);
}
})(640, 480);'>640 x 480</a>
您可以在导航前将浏览器的大小设置为0,0
,然后在文档加载完成后,您可以将控件的Width
和Height
设置为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) post 是 Size WPF Browser to its HTML-Content 的答案。如果问题恰恰是显示图像,您可以依赖第一条评论中提到的 Image
控件。
2)使用WebBrowser
控件展示你的才华时,需要通过设置body
标签的scroll="no"
属性来关闭滚动.您还需要为 body
标签设置 style=\"padding:0px;margin:0px;\"
。还可以通过为 img
标记设置 style="border:0px;"
以及其他一些 html 和 css 更正来删除图像边框。
3) 在 DPI 设置为 125% 的 windows 中,您需要将值乘以 0.8。