C#+Winform : Aero 特效的快速搜索框

本文介绍了一个使用C#开发的小工具——QuickSearch,该工具通过快捷键调用并快速执行多个搜索引擎(如百度、Google等)的搜索操作。通过简单的编码转换,实现了关键字的正确传递,并且加入了Windows Aero特效提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

平时上网都会用到搜索引擎,但是每次都要去打开浏览器,然后打开百度或谷歌。。。   这样做总觉得很烦 ! 为了不让自己能快点搜出东西,我就做了小工具  QuickSearch   2011030510141784.png       2011030510150739.png

其实实现原理很简单:

就是先去找到那些搜索引擎的搜索字符串,例如百度的 http://www.baidu.com/s?wd= ,我们只需要在=后面加想搜的关键字就能搜到我们想要的内容,不过百度的关键字是utf-8编码的,所以我们就要进行编码转换 应为我是用C#开发 默认的是Unicode编码 

 转换方法:先添加应用 System.Web,

   string result = System.Web.HttpUtility.UrlEncode(word, System.Text.Encoding.GetEncoding("utf-8"));

为了方便我还添加了快捷键:Ctrl+Q 实现快速显示窗体

Code:


  
[System.Runtime.InteropServices.DllImport( " user32.dll " )] // 申明API函数
  public static extern bool RegisterHotKey(
IntPtr hWnd,
// handle to window
int id, // hot key identifier
uint fsModifiers, // key-modifier options
Keys vk // virtual-key code
);
[System.Runtime.InteropServices.DllImport(
" user32.dll " )] // 申明API函数
public static extern bool UnregisterHotKey(
IntPtr hWnd,
// handle to window
int id // hot key identifier
);


protected override void WndProc( ref Message m)
{
const int WM_HOTKEY = 0x0312 ;

if (WM_HOTKEY == m.Msg)
{
if (m.WParam.ToInt32() == 101 )
{
hidWindows();
}
}
base .WndProc( ref m);
}


// 显示 or 隐藏
private void hidWindows()
{
if ( this .Visible == false )
{
this .Visible = true ;
this .TopMost = true ;
}
else
{
this .Visible = false ;
}
}

注册快捷键:
/* None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
*/
RegisterHotKey(Handle,
101 , 2 , Keys.Q);

为了好看还加入Aero特效 这是Windows7自带的效果 ,直接写了类


  
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace QuickSearch
{
public partial class AeroForm
{
[DllImport(
" dwmapi.dll " )]
private static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins);

[StructLayout(LayoutKind.Sequential)]
private struct MARGINS
{
public int left, right, top, bottom;
}

public static void AeroEffect(Form f1)
{
MARGINS m
= new MARGINS()
{
left
= - 1
};
DwmExtendFrameIntoClientArea(f1.Handle,
ref m);
Color aeroColor
= Color.FromArgb( 164 , 212 , 211 );
f1.TransparencyKey
= aeroColor;
f1.BackColor
= aeroColor;
}
}
}

搜索代码:


  
string result = System.Web.HttpUtility.UrlEncode(word, System.Text.Encoding.GetEncoding( " utf-8 " ));

switch (btnSearch.ButtonText)
{
case " 百度一下 " :
Process.Start(
@" http://www.baidu.com/s?wd= " + result);
this .TopMost = false ;
break ;
case " Google " :
Process.Start(
@" http://www.google.com.hk/search?q= " + result);
this .TopMost = false ;
break ;
case " 有道搜索 " :
string youdao = HttpUtility.UrlEncode(word, Encoding.GetEncoding( " gbk " ));
Process.Start(
@" http://www.youdao.com/search?q= " + youdao);
this .TopMost = false ;
break ;
case " 搜搜 " :
string soso = HttpUtility.UrlEncode(word,Encoding.GetEncoding( " gbk " ));
Process.Start(
@" http://www.soso.com/q?pid=s.idx&w= " + soso);
this .TopMost = false ;
break ;
case " MSDN " :
this .TopMost = false ;
Process.Start(
@" http://social.msdn.microsoft.com/Search/zh-cn?query= " + result + " + " );
break ;
}
this .Visible = false ;
txtKeyWord.Text
= "" ;
}

搜索演示截图:

2011030511074290.png2011030511063443.png

 

转载于:https://www.cnblogs.com/xspaceworld/archive/2011/03/05/1971462.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值