C#通過Win32 API操作IE瀏覽器 --- 獲得IE的URL
阿新 • • 發佈:2019-02-15
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; //操作Win32API必須要引入的名稱空間namespace IEManipulation
{
publicclass IEManipulation
{
[DllImport("User32.dll")] //User32.dll是Windows作業系統的核心動態庫之一staticexternint FindWindow(string lpClassName, string lpWindowName);
[DllImport( "User32.dll")]
staticexternint FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
staticexternint GetWindowText(int hwnd, StringBuilder buf, int nMaxCount);
[DllImport("User32.dll")]
staticexternint SendMessage( int hWnd, int Msg, int wParam, StringBuilder lParam);
constint WM_GETTEXT =0x000D; //獲得文字訊息的16進製表示///<summary>/// Get the URL of the current opened IE
///</summary>publicstaticstring 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;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; //操作Win32API必須要引入的名稱空間namespace IEManipulation
{
publicclass IEManipulation
{
[DllImport("User32.dll")] //User32.dll是Windows作業系統的核心動態庫之一staticexternint FindWindow(string lpClassName, string lpWindowName);
[DllImport(
staticexternint FindWindowEx(int hwndParent, int hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
staticexternint GetWindowText(int hwnd, StringBuilder buf, int nMaxCount);
[DllImport("User32.dll")]
staticexternint SendMessage(
constint WM_GETTEXT =0x000D; //獲得文字訊息的16進製表示///<summary>/// Get the URL of the current opened IE
///</summary>publicstaticstring GetURL()
{
int parent = FindWindow("IEFrame", null);
int child = FindWindowEx(parent,
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;
}
}
}