C#操作Win32 API

来源:互联网 发布:hp5200网络打印机设置 编辑:程序博客网 时间:2024/05/22 05:00
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace IEManipulation
{
    public class IEManipulation
    {
        [DllImport("User32.dll")]
         public 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;
        /// <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);  //Use Spy++ to find the tree instruct.
            StringBuilder buffer = new StringBuilder(1024);
            int num = SendMessage(child, WM_GETTEXT, 1024, buffer); //Get 1024 bytes and put them into buffer.
            string URL = buffer.ToString();
            return URL;
        }
    }
}
原创粉丝点击