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

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

其实实现原理很简单:

就是先去找到那些搜索引擎的搜索字符串,例如百度的 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
= "";
}

搜索演示截图:

 
原文地址:https://www.cnblogs.com/xspaceworld/p/1971462.html