新学WPF,准备把玻璃窗效果封装成方法,以后需要可以直接调用但是遇到了点问题。直接发代码
封装方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;
using Microsoft.WindowsAPICodePack.Shell;
using Dedom;
namespace Dedom
{
public static class MainWindows
{
public static void ExtendAeroGlass(Window window)
{
try
{
// 为WPF程序获取窗口句柄
IntPtr mainWindowPtr = new WindowInteropHelper(window).Handle;
HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent;
// 设置Margins
MARGINS margins = new MARGINS();
// 扩展Aero Glass
margins.cxLeftWidth = -1;
margins.cxRightWidth = -1;
margins.cyTopHeight = -1;
margins.cyBottomHeight = -1;
int hr = DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);
if (hr < 0)
{
MessageBox.Show("DwmExtendFrameIntoClientArea Failed");
}
}
catch (DllNotFoundException)
{
Application.Current.MainWindow.Background = Brushes.White;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
};
[DllImport("DwmApi.dll")]
public static extern int DwmExtendFrameIntoClientArea(
IntPtr hwnd,
ref MARGINS pMarInset);
}
}
窗口调用
public MainWindow()
{
InitializeComponent();
////Window1 w1 = new Window1();
//////w1.Owner = this;
//w1.Show();
this.Activate();
this.WindowState = WindowState.Normal;
his.Background = Brushes.Transparent;
MainWindows.ExtendAeroGlass(this);
}
错误提示:
无法创建在程序集“Dedom, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中定义的“Window1”的实例。 调用的目标发生了异常。 标记文件中“Dedom;component/Window2.xaml”行1位置9处的错误
如果在窗口调用的时候换成按键调用就没有问题,但是我想做成窗体加载就调用求高手帮忙!