原文: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窗体的边框中添加搜索框和按钮的更多相关文章

  1. extjs在窗体中添加搜索框

    在extjs中添加搜索框,搜索框代码如下: this.searchField = new Ext.ux.form.SearchField({            store : this.store ...

  2. WPF实用指南二:移除窗体的图标

    原文:WPF实用指南二:移除窗体的图标 WPF没有提供任何功能来移除窗体上的icon图标.一般的做法是设置一个空白的图标,如下图1: 这种做法在窗体边框与标题之间仍然会保留一片空白. 比较好的做法是使 ...

  3. WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 自定义 ...

  4. 【转】WPF自定义控件与样式(13)-自定义窗体Window & 自适应内容大小消息框MessageBox

    一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 自定义Window窗体样式: 基于自定义窗体实现自定义MessageB ...

  5. [WPF实用技巧]如何使WPF的TreeView节点之间有连线

    示例代码:TreeViewEx.zip 原文地址:http://www.codeproject.com/Tips/673071/WPF-TreeView-with-WinForms-Style-Fom ...

  6. C#Windows窗体界面设计_05_添加菜单栏 工具栏 状态栏 按钮

  7. c#在panel或groupbox中添加窗体,实现点击不同按钮或combox时panel中窗体切换,在xtratabcontrol中添加窗体

    参考panel添加窗体: http://blog.csdn.net/illegalname/article/details/65444249 http://blog.csdn.net/Eastmoun ...

  8. 《WPF程序设计指南》读书笔记——第1章 应用程序与窗口

    1.空白WPF项目的创建: 1)新建项目:在VS2010中,文件-新建-项目-visual c#-windows-空项目: 2)添加引用:PresentationFramework,Presentat ...

  9. C# WPF 一直保持多个Topmost窗体的置顶顺序

    原文:C# WPF 一直保持多个Topmost窗体的置顶顺序 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37862405/article/ ...

随机推荐

  1. 从0開始学习 GitHub 系列之「07.GitHub 常见的几种操作」

    之前写了一个 GitHub 系列,反响非常不错,突然发现居然还落下点东西没写,前段时间 GitHub 也改版了,借此机会补充下. 我们都说开源社区最大的魅力是人人多能够參与进去,发挥众人的力量,让一个 ...

  2. [Win 8/WP 8]简单实现弹出页更换头像的功能

    在Win 8应用里,对弹出页(Popup)的灵活操作必不可少,下面我们就来简单实现一个. 一.先让Popup弹出到指定位置 先来看看效果图,如图[1]: 下面是前端代码,代码段[1]: <Gri ...

  3. mysqlbinlog命令使用

    常用参数:--start-datetime=datetime 从二进制日志中第1个日期时间等于或晚于datetime参量的事件开始读取.datetime值相对于运行mysqlbinlog的机器上的本地 ...

  4. System.ArgumentException: 已添加了具有相同键的项。(An item with the same key has already been added) 在 System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) 在 System.Web.Mvc.Js

    最近将一个项目从ASP.NET MVC 3升级至刚刚发布的ASP.NET MVC 5.1,升级后发现一个ajax请求出现了500错误,日志中记录的详细异常信息如下: System.ArgumentEx ...

  5. LUOGU 1137 - 拓扑排序

    传送门 题目分析 拓扑排序:将图从度为0的点不断的剥掉外层的点,即可得到拓扑序,再按照拓扑序进行一遍简单的dp. code #include<bits/stdc++.h> using na ...

  6. Oracle数据库表空间 数据文件 用户 以及表创建的SQL代码

    --create the tablespace CREATE SMALLFILE TABLESPACE "TABLE_CONTAINER" --创建表空间 DATAFILE 'E: ...

  7. SQL表的默认常用数据类型

    分类 字段类型 描述 整数 bit 0或1的整型数字 int 从-2^31(-2,147,483,648)到2^31-1(2,147,483,647)的整型数字 smallint 从-2^15(-32 ...

  8. shell自动化下载、安装、配置nginx

    #!/bin/bash auto config nginx server #by author www.jfedu.net #2018年5月14日17:25:52 N_PAR="vim lr ...

  9. cordova APP 检查更新

    原文:cordova APP 检查更新 //升级程序 .factory('UpdateService', function ($rootScope, $cordovaAppVersion, $cord ...

  10. 修改MessageBox的标题的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 1.用Win API的::MessageBox或CWnd::MessageBox代替AfxMessageBox. 2. ...