Search engines are usually used for surfing the internet, but every time you go to open a browser, then open Baidu or Google... This is always annoying! In order not to allow myself to quickly search for something, I made a small tool, quicksearch.
In fact, the implementation principle is very simple:
Is to first find those search engine search string, such as Baidu's http://www.baidu.com/s? WD =, we only need to add the keyword after = to search for the content we want, but Baidu's keyword is UTF-8 encoded, therefore, we need to convert the encoding into unicode encoding for C # development.
Conversion Method: First add the application system. Web,
string result = System.Web.HttpUtility.UrlEncode(word, System.Text.Encoding.GetEncoding("utf-8"));
For convenience, I also added the shortcut key Ctrl + Q to implement quick display of the form.
Code:
[System. runtime. interopservices. dllimport ("user32.dll")] // declare an API Function
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")] // declare an API Function
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 );
}
// Display or hide
Private void hidwindows ()
{
If (this. Visible = false)
{
This. Visible = true;
This. topmost = true;
}
Else
{
This. Visible = false;
}
}
Registration shortcut:
/* None = 0,
Alt = 1,
CTRL = 2,
Shift = 4,
Windowskey = 8 */
Registerhotkey (handle, 101, 2, keys. Q );
For good looks, you can also add aero special effects. This is the built-in Effect of Windows 7.
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;
}
}
}
Search code:
String result = system. Web. httputility. urlencode (word, system. Text. encoding. getencoding ("UTF-8 "));
Switch (btnsearch. buttontext)
{
Case "Baidu ":
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 "youdao search ":
String youdao = httputility. urlencode (word, encoding. getencoding ("GBK "));
Process. Start (@ "http://www.youdao.com/search? Q = "+ youdao );
This. topmost = false;
Break;
Case "Search ":
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 = "";
}
Search Demo: