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

來源:互聯網
上載者:User

平時上網都會用到搜尋引擎,但是每次都要去開啟瀏覽器,然後開啟百度或Google。。。   這樣做總覺得很煩 ! 為了不讓自己能快點搜出東西,我就做了小工具  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 = "";
}

搜尋示範:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.