1.WPF中AllowsTransparency和WebBrowser兼容性问题
(1)利用WindowChrome设置GlassFrameThickness属性为-1,而不设置AllowsTransparency来达到背景透明效果:
参考网址:点击跳转至相应网页
示例代码(前台xmal代码):
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None" ResizeMode="CanMinimize"
Title="walterlv demo" Height="450" Width="800">
<WindowChrome.WindowChrome>
<WindowChrome GlassFrameThickness="-1" />
</WindowChrome.WindowChrome>
<Window.Template>
<ControlTemplate TargetType="Window">
<Border Padding="20">
<Border.Background>
<ImageBrush ImageSource="Image/123.png"/>
</Border.Background>
</Border>
</ControlTemplate>
</Window.Template>
<Grid>
</Grid>
</Window>
(2)通过调用系统api,将窗体透明
解决方法:点击跳转至相应网页
下面进行简述:
将window 的 AllowsTransparenc属性设为false,将下述代码放置于window类的代码里
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// This can't be done any earlier than the SourceInitialized event:
GlassHelper.ExtendGlassFrame(this, new Thickness(-1));
}
即可解决问题,其中GlassHelper.ExtendGlassFrame的代码如下:
public class GlassHelper
{
[DllImport("dwmapi.dll", PreserveSig=false)]
static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);
[DllImport("dwmapi.dll", PreserveSig=false)]
static extern bool DwmIsCompositionEnabled();
public static bool ExtendGlassFrame(Window window, Thickness margin)
{
if (!DwmIsCompositionEnabled())
return false;
IntPtr hwnd = new WindowInteropHelper(window).Handle;
if (hwnd == IntPtr.Zero)
throw new InvalidOperationException("The Window must be shown before extending glass.");
// Set the background to transparent from both the WPF and Win32 perspectives
window.Background = Brushes.Transparent;
HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;
MARGINS margins = new MARGINS(margin);
DwmExtendFrameIntoClientArea(hwnd, ref margins);
return true;
}
}
struct MARGINS
{
public MARGINS(Thickness t)
{
Left = (int)t.Left;
Right = (int)t.Right;
Top = (int)t.Top;
Bottom = (int)t.Bottom;
}
public int Left;
public int Right;
public int Top;
public int Bottom;
}
2.WPF中WebBrowser控件因其置顶的原因无法让其他控件叠加上去
解决方法:点击跳转至相应网页
3.WPF中经常出现相对路径找不到图片位置的,那么需要先考虑是否图片包含在项目中、是否图片属性为Resource,如果仍不行,那么就将图片路径写成绝对路径,如下所示:
var bitmap = new BitmapImage(new Uri("pack://application:,,,/Images/123.png", UriKind.Absolute));