WPF教程

win7下 窗体玻璃效果的实现和WindowStyle None模式下的移动 wpfhtml教程

时间:2013-6-20 1:03:53  作者:WPF之家  来源:http://www.wpf123.com  查看:623  评论:0
内容摘要:这些技术在上一篇文章的介绍的软件里有用到,现在单独摘出来说明一下。添加 using System.Runtime.InteropServices;[StructLayout(LayoutKind.Sequential)]public struct MARGINS{public i...

这些技术在上一篇文章的介绍的软件里有用到,现在单独摘出来说明一下。

添加 using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cyTopHeight;
public int cyBottomHeight;
}
#region 引用 Dll
[DllImport("DwmApi.dll")] //玻璃
public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS pMarInset);
[DllImport("user32.dll", EntryPoint = "SendMessage")]// 移动
public static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
[DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
public static extern int ReleaseCapture();
public const int WM_SysCommand = 0x0112;
public const int SC_MOVE = 0xF012;

#endregion

public MainWindow()
{
InitializeComponent();

this.Background = Brushes.Transparent;
ExtendAeroGlass(this);

}

private 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)
{
checkBox1.IsChecked = false;
MessageBox.Show("玻璃效果加载失败!");
}
}
catch (DllNotFoundException)
{
Application.Current.MainWindow.Background = Brushes.White;
}
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e) //鼠标按下时,移动
{
IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
ReleaseCapture();
SendMessage(mainWindowSrc.Handle.ToInt32(), WM_SysCommand, SC_MOVE, 0);
}

完成 。。

作者: 无限的天空1989 发表


标签:win7下 窗体玻璃效果的实现和WindowStyle None模式下的移动 wpf 

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。本文出自“wpf之家”,请务必保留此出处:http://www.wpf123.com

相关评论
Copyright © 2009-2014 WPF之家(http://www.wpf123.com/) All rights reserved
 Powered by WPF之家
鄂ICP备13006396号