C#通过Win32 API操作IE浏览器 --- 获得IE的URL using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; //操作Win32API必须要引入的命名空间 namespace IEManipulation { public class IEManipulation { [DllImport("User32.dll")] //User32.dll是Windows操作系统的核心动态库之一 static extern int FindWindow(string lpClassName, string lpWindowName); [DllImport("User32.dll")] static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("User32.dll")] static extern int GetWindowText(int hwnd, StringBuilder buf, int nMaxCount); [DllImport("User32.dll")] static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam); const int WM_GETTEXT = 0x000D; //获得文本消息的16进制表示 /// <summary> /// Get the URL of the current opened IE /// </summary> public static string GetURL() { int parent = FindWindow("IEFrame", null); int child = FindWindowEx(parent, 0, "WorkerW", null); child = FindWindowEx(child, 0, "ReBarWindow32", null); child = FindWindowEx(child, 0, "ComboBoxEx32", null); child = FindWindowEx(child, 0, "ComboBox", null); child = FindWindowEx(child, 0, "Edit", null); //通过SPY++获得地址栏的层次结构,然后一层一层获得 StringBuilder buffer = new StringBuilder(1024); //child表示要操作窗体的句柄号 //WM_GETTEXT表示一个消息,怎么样来驱动窗体 //1024表示要获得text的大小 //buffer表示获得text的值存放在内存缓存中 int num = SendMessage(child, WM_GETTEXT, 1024, buffer); string URL = buffer.ToString(); return URL; } } } IE6和IE7的访问方式不太一样,IE7要改为: using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; //操作Win32API必须要引入的命名空间 namespace IEManipulation { public class IEManipulation { [DllImport("User32.dll")] //User32.dll是Windows操作系统的核心动态库之一 static extern int FindWindow(string lpClassName, string lpWindowName); [DllImport("User32.dll")] static extern int FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("User32.dll")] static extern int GetWindowText(int hwnd, StringBuilder buf, int nMaxCount); [DllImport("User32.dll")] static extern int SendMessage(int hWnd, int Msg, int wParam, StringBuilder lParam); const int WM_GETTEXT = 0x000D; //获得文本消息的16进制表示 /// <summary> /// Get the URL of the current opened IE /// </summary> public static string GetURL() { int parent = FindWindow("IEFrame", null); int child = FindWindowEx(parent, 0, "WorkerW", null); child = FindWindowEx(child, 0, "ReBarWindow32", null); child = FindWindowEx(child, 0, ("Address Band Root"), null); child = FindWindowEx(child, 0, "ComboBoxEx32", null); child = FindWindowEx(child, 0, "ComboBox", null); child = FindWindowEx(child, 0, "Edit", null); //通过SPY++获得地址栏的层次结构,然后一层一层获得 StringBuilder buffer = new StringBuilder(1024); //child表示要操作窗体的句柄号 //WM_GETTEXT表示一个消息,怎么样来驱动窗体 //1024表示要获得text的大小 //buffer表示获得text的值存放在内存缓存中 int num = SendMessage(child, WM_GETTEXT, 1024, buffer); string URL = buffer.ToString(); return URL; } } } 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ziqing/archive/2007/11/22/1897685.aspx C#注册全局热键 注册全局热键要用到Windows的API方法RegisterHotKey和UnregisterHotKey。 一、声明注册热键方法 [DllImport("user32.dll")] private static extern int RegisterHotKey(IntPtr hwnd, int id, int fsModifiers, int vk); [DllImport("user32.dll")] private static extern int UnregisterHotKey(IntPtr hwnd, int id); int Space = 32; //热键ID private const int WM_HOTKEY = 0x312; //窗口消息-热键 private const int WM_CREATE = 0x1; //窗口消息-创建 private const int WM_DESTROY = 0x2; //窗口消息-销毁 private const int MOD_ALT = 0x1; //ALT private const int MOD_CONTROL = 0x2; //CTRL private const int MOD_SHIFT = 0x4; //SHIFT private const int VK_SPACE = 0x20; //SPACE 二、注册热键方法 /// <summary> /// 注册热键 /// </summary> /// <param name="hwnd">窗口句柄</param> /// <param name="hotKey_id">热键ID</param> /// <param name="fsModifiers">组合键</param> /// <param name="vk">热键</param> private void RegKey(IntPtr hwnd, int hotKey_id, int fsModifiers, int vk) { bool result; if (RegisterHotKey(hwnd,hotKey_id,fsModifiers,vk) == 0) { result = false; } else { result = true; } if (!result) { MessageBox.Show("注册热键失败!"); } } /// <summary> /// 注销热键 /// </summary> /// <param name="hwnd">窗口句柄</param> /// <param name="hotKey_id">热键ID</param> private void UnRegKey(IntPtr hwnd, int hotKey_id) { UnregisterHotKey(hwnd,hotKey_id); } 三、重写WndProc方法,实现注册 protected override void WndProc(ref Message m) { base.WndProc(ref m); switch(m.Msg) { case WM_HOTKEY: //窗口消息-热键 switch(m.WParam.ToInt32()) { case 32: //热键ID MessageBox.Show("Hot Key : Ctrl + Alt + Shift + Space"); break; default: break; } break; case WM_CREATE: //窗口消息-创建 RegKey(Handle,Space,MOD_ALT | MOD_CONTROL | MOD_SHIFT,VK_SPACE); //注册热键 break; case WM_DESTROY: //窗口消息-销毁 UnRegKey(Handle,Space); //销毁热键 break; default: break; } } 附:虚拟键值表 { Virtual Keys, Standard Set } {$EXTERNALSYM VK_LBUTTON} VK_LBUTTON = 1; {$EXTERNALSYM VK_RBUTTON} VK_RBUTTON = 2; {$EXTERNALSYM VK_CANCEL} VK_CANCEL = 3; {$EXTERNALSYM VK_MBUTTON} VK_MBUTTON = 4; { NOT contiguous with L & RBUTTON } {$EXTERNALSYM VK_BACK} VK_BACK = 8; {$EXTERNALSYM VK_TAB} VK_TAB = 9; {$EXTERNALSYM VK_CLEAR} VK_CLEAR = 12; {$EXTERNALSYM VK_RETURN} VK_RETURN = 13; {$EXTERNALSYM VK_SHIFT} VK_SHIFT = $10; {$EXTERNALSYM VK_CONTROL} VK_CONTROL = 17; {$EXTERNALSYM VK_MENU} VK_MENU = 18; {$EXTERNALSYM VK_PAUSE} VK_PAUSE = 19; {$EXTERNALSYM VK_CAPITAL} VK_CAPITAL = 20; {$EXTERNALSYM VK_KANA } VK_KANA = 21; {$EXTERNALSYM VK_HANGUL } VK_HANGUL = 21; {$EXTERNALSYM VK_JUNJA } VK_JUNJA = 23; {$EXTERNALSYM VK_FINAL } VK_FINAL = 24; {$EXTERNALSYM VK_HANJA } VK_HANJA = 25; {$EXTERNALSYM VK_KANJI } VK_KANJI = 25; {$EXTERNALSYM VK_CONVERT } VK_CONVERT = 28; {$EXTERNALSYM VK_NONCONVERT } VK_NONCONVERT = 29; {$EXTERNALSYM VK_ACCEPT } VK_ACCEPT = 30; {$EXTERNALSYM VK_MODECHANGE } VK_MODECHANGE = 31; {$EXTERNALSYM VK_ESCAPE} VK_ESCAPE = 27; {$EXTERNALSYM VK_SPACE} VK_SPACE = $20; {$EXTERNALSYM VK_PRIOR} VK_PRIOR = 33; {$EXTERNALSYM VK_NEXT} VK_NEXT = 34; {$EXTERNALSYM VK_END} VK_END = 35; {$EXTERNALSYM VK_HOME} VK_HOME = 36; {$EXTERNALSYM VK_LEFT} VK_LEFT = 37; {$EXTERNALSYM VK_UP} VK_UP = 38; {$EXTERNALSYM VK_RIGHT} VK_RIGHT = 39; {$EXTERNALSYM VK_DOWN} VK_DOWN = 40; {$EXTERNALSYM VK_SELECT} VK_SELECT = 41; {$EXTERNALSYM VK_PRINT} VK_PRINT = 42; {$EXTERNALSYM VK_EXECUTE} VK_EXECUTE = 43; {$EXTERNALSYM VK_SNAPSHOT} VK_SNAPSHOT = 44; {$EXTERNALSYM VK_INSERT} VK_INSERT = 45; {$EXTERNALSYM VK_DELETE} VK_DELETE = 46; {$EXTERNALSYM VK_HELP} VK_HELP = 47; { VK_0 thru VK_9 are the same as ASCII '0' thru '9' ($30 - $39) } { VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' ($41 - $5A) } {$EXTERNALSYM VK_LWIN} VK_LWIN = 91; {$EXTERNALSYM VK_RWIN} VK_RWIN = 92; {$EXTERNALSYM VK_APPS} VK_APPS = 93; {$EXTERNALSYM VK_NUMPAD0} VK_NUMPAD0 = 96; {$EXTERNALSYM VK_NUMPAD1} VK_NUMPAD1 = 97; {$EXTERNALSYM VK_NUMPAD2} VK_NUMPAD2 = 98; {$EXTERNALSYM VK_NUMPAD3} VK_NUMPAD3 = 99; {$EXTERNALSYM VK_NUMPAD4} VK_NUMPAD4 = 100; {$EXTERNALSYM VK_NUMPAD5} VK_NUMPAD5 = 101; {$EXTERNALSYM VK_NUMPAD6} VK_NUMPAD6 = 102; {$EXTERNALSYM VK_NUMPAD7} VK_NUMPAD7 = 103; {$EXTERNALSYM VK_NUMPAD8} VK_NUMPAD8 = 104; {$EXTERNALSYM VK_NUMPAD9} VK_NUMPAD9 = 105; {$EXTERNALSYM VK_MULTIPLY} VK_MULTIPLY = 106; {$EXTERNALSYM VK_ADD} VK_ADD = 107; {$EXTERNALSYM VK_SEPARATOR} VK_SEPARATOR = 108; {$EXTERNALSYM VK_SUBTRACT} VK_SUBTRACT = 109; {$EXTERNALSYM VK_DECIMAL} VK_DECIMAL = 110; {$EXTERNALSYM VK_DIVIDE} VK_DIVIDE = 111; {$EXTERNALSYM VK_F1} VK_F1 = 112; {$EXTERNALSYM VK_F2} VK_F2 = 113; {$EXTERNALSYM VK_F3} VK_F3 = 114; {$EXTERNALSYM VK_F4} VK_F4 = 115; {$EXTERNALSYM VK_F5} VK_F5 = 116; {$EXTERNALSYM VK_F6} VK_F6 = 117; {$EXTERNALSYM VK_F7} VK_F7 = 118; {$EXTERNALSYM VK_F8} VK_F8 = 119; {$EXTERNALSYM VK_F9} VK_F9 = 120; {$EXTERNALSYM VK_F10} VK_F10 = 121; {$EXTERNALSYM VK_F11} VK_F11 = 122; {$EXTERNALSYM VK_F12} VK_F12 = 123; {$EXTERNALSYM VK_F13} VK_F13 = 124; {$EXTERNALSYM VK_F14} VK_F14 = 125; {$EXTERNALSYM VK_F15} VK_F15 = 126; {$EXTERNALSYM VK_F16} VK_F16 = 127; {$EXTERNALSYM VK_F17} VK_F17 = 128; {$EXTERNALSYM VK_F18} VK_F18 = 129; {$EXTERNALSYM VK_F19} VK_F19 = 130; {$EXTERNALSYM VK_F20} VK_F20 = 131; {$EXTERNALSYM VK_F21} VK_F21 = 132; {$EXTERNALSYM VK_F22} VK_F22 = 133; {$EXTERNALSYM VK_F23} VK_F23 = 134; {$EXTERNALSYM VK_F24} VK_F24 = 135; {$EXTERNALSYM VK_NUMLOCK} VK_NUMLOCK = 144; {$EXTERNALSYM VK_SCROLL} VK_SCROLL = 145; { VK_L & VK_R - left and right Alt, Ctrl and Shift virtual keys. Used only as parameters to GetAsyncKeyState() and GetKeyState(). No other API or message will distinguish left and right keys in this way. } {$EXTERNALSYM VK_LSHIFT} VK_LSHIFT = 160; 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/s4gg/archive/2008/09/25/2974812.aspx C#程序在托盘后台运行 源代码如下: 请在工具栏把notifyIcon控件拖入窗体再使用: //隐藏窗体 private bool windowCreate = true; private void toolStripMenuItem1_Click(object sender, EventArgs e) //这是菜单选项的一个item点击事件 { if (windowCreate) { base.Visible = false; windowCreate = false; } this.Hide(); base.OnActivated(e); } //显示回窗体(notifyIcon控件双击事件,注:请选择一个ico图标,这样隐藏后在右下角显示有相关图标) private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e) { if (this.Visible == true) { this.Hide(); this.ShowInTaskbar = false; } else { this.Visible = true; this.ShowInTaskbar = true; this.WindowState = FormWindowState.Normal; //this.Show(); this.BringToFront(); windowCreate = true; } } 文章来自学IT网:http://www.xueit.com/html/2008-05/117_125_00.html 快捷键显示: 最近找了一些资料,是讲在C#中设置快捷键运行方法或程序的,要设置快捷键必须使用user32.dll下面的两个方法。 BOOL RegisterHotKey( HWND hWnd, int id, UINT fsModifiers, UINT vk ); 和 BOOL UnregisterHotKey( HWND hWnd, int id ); 转换成C#代码,那么首先就要引用命名空间System.Runtime.InteropServices;来加载非托管类user32.dll。于是有了: [DllImport("user32.dll", SetLastError=true)] public static extern bool RegisterHotKey( IntPtr hWnd, // handle to window int id, // hot key identifier KeyModifiers fsModifiers, // key-modifier options Keys vk // virtual-key code ); [DllImport("user32.dll", SetLastError=true)] public static extern bool UnregisterHotKey( IntPtr hWnd, // handle to window int id // hot key identifier ); [Flags()] public enum KeyModifiers { None = 0, Alt = 1, Control = 2, Shift = 4, Windows = 8 } 这是注册和卸载全局快捷键的方法,那么我们只需要在Form_Load的时候加上注册快捷键的语句,在FormClosing的时候卸载全局快捷键。同时,为了保证剪贴板的内容不受到其他程序调用剪贴板的干扰,在Form_Load的时候,我先将剪贴板里面的内容清空。 于是有了: private void Form1_Load(object sender, System.EventArgs e) { label2.AutoSize = true; Clipboard.Clear();//先清空剪贴板防止剪贴板里面先复制了其他内容 RegisterHotKey(Handle, 100, 0, Keys.F10); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { UnregisterHotKey(Handle, 100);//卸载快捷键 } 那么我们在别的窗口,怎么让按了快捷键以后调用我的主过程ProcessHotkey()呢? 那么我们就必须重写WndProc()方法,通过监视系统消息,来调用过程: protected override void WndProc(ref Message m)//监视Windows消息 { const int WM_HOTKEY = 0x0312;//按快捷键 switch (m.Msg) { case WM_HOTKEY: ProcessHotkey();//调用主处理程序 break; } base.WndProc(ref m); } 这样我的程序就完成了。 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xoray007/archive/2010/03/10/5366643.aspx