软件添加搜索框,实际上就是在边框中加入一些元素,一般是用于应用程序插件程序。特别是在浏览器(Crome,IE,Firefox,Opera)中的右方搜索框的应用。
一般用c#中wpf就可以实现,前提安装visual studio,实现效果:
图一:实现之前的效果
图二:实现之后的效果
这样的效果依赖于操作系统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的结构体。代码click如下
[StructLayout(LayoutKind.Sequential)]
public struct MARGINS
{
public int cxLeftWidth;
public int cxRightWidth;
public int cxTopHeight;
public int cxBottomHeight;
}
[DllImport("dwm