WPF实用指南一:在WPF窗体的边框中添加搜索框和按钮
原文:WPF实用指南一:在WPF窗体的边框中添加搜索框和按钮
在边框中加入一些元素,在应用程序的界面设计中,已经开始流行起来。特别是在浏览器(Crome,IE,Firefox,Opera)中都有应用。
在WPF中,如何实现这种效果呢?这正是我们今天需要探讨的问题。先看看实现效果

图一:实现之前的效果 
图二:实现之后的效果
这样的效果依赖于操作系统Aero风格的支持,也就是说在Windows Vista,Windows 7 或者更高版本中可以获得此中效果。如果要在Windows XP中实现,那么您就需要另外想办法了。
好了。我们来看看是怎么实现的吧。
首先:在MainWindow窗体的xaml代码中加入以下代码,这一步没有什么特别的,和平常做的一样。
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center">
<TextBox Width="150" VerticalAlignment="Center" Text="输入关键词" />
<Button Content="查找" VerticalAlignment="Center" Margin="5,0,5,0" />
</StackPanel>
<Grid Background="White" Grid.Row="1">
<Label Content="Hello World"></Label>
</Grid>
然后:为窗体设定背景。这一步比较重要,要实现上面图片的效果,需要将其设定为Transparent
Background="Transparent"
好了,到此xaml的编辑已经结束了,接下来看看后台代码是如何实现的。
如果你创建的是WPF的应用程序,只需要添加System.Drawing引用即可。
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Interop;
using System.Runtime.InteropServices;
要实现上述效果,需要使用一个Win32函数DwmExtendFrameIntoClientArea这个函数需要个MARGINS的结构体。代码定义如下
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cxTopHeight;
public int cxBottomHeight;
}
[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(
IntPtr hWnd, ref MARGINS pMarInset);
Windows
API使用句柄控制着窗体,所以在窗体的Load事件中,第一步我们需要获取窗体的句柄,使用.NET类库提供的WindowInteropHelper类来获取。
然后从句柄中获得HwndSource,它用来宿主WPF的内容。接下来创建MARGINS结构体实例用来存储相关设置。最后调用API。看看代码实现:
void OnLoaded(object sender, RoutedEventArgs e)
{
IntPtr windowHandle = new WindowInteropHelper(this).Handle;
HwndSource window = HwndSource.FromHwnd(windowHandle);
window.CompositionTarget.BackgroundColor = Colors.Transparent;
MARGINS margins = new MARGINS();
margins.cxTopHeight = 30;
margins = AdjustForDPISettings(margins, windowHandle);
int result = DwmExtendFrameIntoClientArea(windowHandle, ref margins);
}
private MARGINS AdjustForDPISettings(MARGINS input, IntPtr hWnd)
{
MARGINS adjusted = new MARGINS();
var graphics = System.Drawing.Graphics.FromHwnd(hWnd);
float dpiRatioX = graphics.DpiX / 96;
float dpiRatioY = graphics.DpiY / 96;
adjusted.cxLeftWidth = (int)(input.cxLeftWidth * dpiRatioX);
adjusted.cxRightWidth = (int)(input.cxRightWidth * dpiRatioX);
adjusted.cxTopHeight = (int)(input.cxTopHeight * dpiRatioY);
adjusted.cxBottomHeight = (int)(input.cxBottomHeight * dpiRatioY);
return adjusted;
}
到此,整个效果就都实现了。完整代码如下:
using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Interop;
using System.Runtime.InteropServices;
namespace WpfTutorial
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += OnLoaded;
}
void OnLoaded(object sender, RoutedEventArgs e)
{
IntPtr windowHandle = new WindowInteropHelper(this).Handle;
HwndSource window = HwndSource.FromHwnd(windowHandle);
window.CompositionTarget.BackgroundColor = Colors.Transparent;
MARGINS margins = new MARGINS();
margins.cxTopHeight = 30;
margins = AdjustForDPISettings(margins, windowHandle);
int result = DwmExtendFrameIntoClientArea(windowHandle, ref margins);
}
private MARGINS AdjustForDPISettings(MARGINS input, IntPtr hWnd)
{
MARGINS adjusted = new MARGINS();
var graphics = System.Drawing.Graphics.FromHwnd(hWnd);
float dpiRatioX = graphics.DpiX / 96;
float dpiRatioY = graphics.DpiY / 96;
adjusted.cxLeftWidth = (int)(input.cxLeftWidth * dpiRatioX);
adjusted.cxRightWidth = (int)(input.cxRightWidth * dpiRatioX);
adjusted.cxTopHeight = (int)(input.cxTopHeight * dpiRatioY);
adjusted.cxBottomHeight = (int)(input.cxBottomHeight * dpiRatioY);
return adjusted;
}
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cxTopHeight;
public int cxBottomHeight;
}
[DllImport("dwmapi.dll")]
public static extern int DwmExtendFrameIntoClientArea(
IntPtr hWnd, ref MARGINS pMarInset);
}
}
WPF实用指南一:在WPF窗体的边框中添加搜索框和按钮的更多相关文章
- extjs在窗体中添加搜索框
在extjs中添加搜索框,搜索框代码如下: this.searchField = new Ext.ux.form.SearchField({ store : this.store ...
- WPF实用指南二:移除窗体的图标
原文:WPF实用指南二:移除窗体的图标 WPF没有提供任何功能来移除窗体上的icon图标.一般的做法是设置一个空白的图标,如下图1: 这种做法在窗体边框与标题之间仍然会保留一片空白. 比较好的做法是使 ...
- WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 自定义 ...
- 【转】WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 自定义Window窗体样式: 基于自定义窗体实现自定义MessageB ...
- [WPF实用技巧]如何使WPF的TreeView节点之间有连线
示例代码:TreeViewEx.zip 原文地址:http://www.codeproject.com/Tips/673071/WPF-TreeView-with-WinForms-Style-Fom ...
- C#Windows窗体界面设计_05_添加菜单栏 工具栏 状态栏 按钮
- c#在panel或groupbox中添加窗体,实现点击不同按钮或combox时panel中窗体切换,在xtratabcontrol中添加窗体
参考panel添加窗体: http://blog.csdn.net/illegalname/article/details/65444249 http://blog.csdn.net/Eastmoun ...
- 《WPF程序设计指南》读书笔记——第1章 应用程序与窗口
1.空白WPF项目的创建: 1)新建项目:在VS2010中,文件-新建-项目-visual c#-windows-空项目: 2)添加引用:PresentationFramework,Presentat ...
- C# WPF 一直保持多个Topmost窗体的置顶顺序
原文:C# WPF 一直保持多个Topmost窗体的置顶顺序 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37862405/article/ ...
随机推荐
- USB 3.0规范中译本 附录
本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 附录A 符号编码 表A-1显示了对于数据字符字节到符号的编码. 表 A-2显示了对于特殊符号的编码. R ...
- js进阶 11-20 弹出层如何制作
js进阶 11-20 弹出层如何制作 一.总结 一句话总结:其实就是一个div,控制显示和隐藏即可.设置成绝对定位更好,就可以控制弹出层出现的位置.关闭的画质需要将display重新设置为none就好 ...
- 【76.57%】【codeforces 721A】One-dimensional Japanese Crossword
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- catalina.out 和 catalina.log 的区别和用途
catalina.out catalina.out其实是tomcat的标准输出(stdout)和标准出错(stderr),这是在tomcat的启动脚本里指定的,如果没有修改的话stdout和stder ...
- [Node.js] Take Screenshots of Multiple Dimensions for Responsive Sites using Nightmare
When developing responsive websites, you will constantly be resizing your browser to make sure your ...
- [Compose] 10. Capture Side Effects in a Task
We examine the data structure Task, see some constructors, familiar methods, and finally how it capt ...
- js进阶正则表达式9量词2(^和&作用:/^HTML5$/g匹配不到aHTML5b中的HTML5,不然是可以匹配到的)(/\d+(?=cm)/g)((?!cm))
js进阶正则表达式9量词2(^和&作用:/^HTML5$/g匹配不到aHTML5b中的HTML5,不然是可以匹配到的)(/\d+(?=cm)/g)((?!cm)) 一.总结 ^和&作用 ...
- [Django] The models
Create a new app: python manage.py startapp tictactoe Then add you app to INSTALLED_APP array in set ...
- 简洁常用权限系统的设计与实现(六):不维护节点的深度level,手动计算level,构造树 (把一颗无序的树,变成有序的)
本篇介绍的方法,参考了网上的代码.在递归过程中,计算level,是受到了这种方法的启发. CSDN上有篇关于树的算法,目标是把一个无序的树,变成有序的. 我看了下代码,并运行了下,感觉是可行的. 我 ...
- BZOJ 1864 三色二叉树 - 树型dp
传送门 题目大意: 给一颗二叉树染色红绿蓝,父亲和儿子颜色必须不同,两个儿子颜色必须不同,问最多和最少能染多少个绿色的. 题目分析: 裸的树型dp:\(dp[u][col][type]\)表示u节点染 ...