【问题标题】:How can I check if website is already opened in a webbrowser?如何检查网站是否已在网络浏览器中打开?
【发布时间】:2013-10-06 13:42:16
【问题描述】:

我正在这样做:

Process.Start("http://www.google.com");

在打开默认网络浏览器后,我想以某种方式检查该网站是否由网络浏览器打开并关闭该网站的特定选项卡。

制作一个按钮点击事件,检查网站是否已经打开然后关闭它。

【问题讨论】:

  • 这不容易。
  • 查看浏览器帮助对象。 code.msdn.microsoft.com/windowsdesktop/…
  • @P.Brian.Mackey:这对 Chrome 没有帮助。
  • @SLaks - 他可能需要编写多个浏览器扩展。如有必要,可以使用 XML 文件、客户端/服务器应用程序等某种媒介来集中结果。当然不容易,但可以做到。
  • 每个可能的浏览器都会有所不同。

标签: c# winforms


【解决方案1】:

除了 Internet Explorer 之外,您还需要编写一个 API 来操作其他浏览器中的选项卡,但您可以像这样启动 Internet Explorer 进程并枚举打开的窗口/选项卡:

using SHDocVw;
....
    public class IEClass
    {
        List<InternetExplorer> IEWindows;

        public IEClass()
        {
            IEWindows = new List<InternetExplorer>();
        }

        public List<InternetExplorer> GetIEInstances()
        {
            IEWindows.Clear();
            ShellWindows shellWindows = new ShellWindows();
            string filename;

            foreach (InternetExplorer ie in shellWindows)
            {
                filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
                if (filename.Equals("iexplore"))
                {
                    IEWindows.Add(ie);
                }
            }
            return IEWindows;
        }

        public bool QuitInstance(int key)
        {
            InternetExplorer ie = (InternetExplorer)IEWindows[key];

            try
            {
                ie.Quit();
                return true;
            }
            catch (Exception ex)
            {
                // handle any exception
            }
            return false;
        }

        public void StartInstance(string url)
        {
            InternetExplorer ie = new InternetExplorer();

            ieInstance.Visible = true;
            ieInstance.Navigate2(ref (object)url, ref Empty, ref Empty, ref Empty, ref Empty);
            IEWindows.Add(ie);
        }
    }

这可能不是最有效的代码,但它确实适用于获取现有实例、创建新实例和退出 Internet Explorer 窗口/选项卡的实例。我在 Windows XP IE 6 到 Windows 7 IE 10 中对其进行了测试。

我还编写了一些 C++ 来获取前台窗口信息,您可以使用这些信息来读取窗口标题和进程名称以确定特定选项卡是否打开:

HWND foregroundWindow = GetForegroundWindow();
DWORD* processID = new DWORD;
GetWindowText(foregroundWindow, buf, 255);
GetWindowThreadProcessId(foregroundWindow, processID);
DWORD p = *processID;
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
                              PROCESS_VM_READ,
                              FALSE, p);
TCHAR szProcessName[MAX_PATH];

if (NULL != hProcess )
{
    HMODULE hMod;
    DWORD cbNeeded;

    if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
                             &cbNeeded) )
    {
        GetModuleBaseName( hProcess, hMod, szProcessName,
                           sizeof(szProcessName)/sizeof(TCHAR) );
    }
}
CloseHandle(hProcess);

您可以将此代码包装在 C++ DLL 中,或从 C# 中的 pinvoke 调用 Windows API 函数。

这是 C# 中 C++ 代码的大致等价物:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ForegroundWindowTest
{
    class Program
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        static extern int GetWindowTextLength(IntPtr hWnd);

        //  int GetWindowText(
        //      __in   HWND hWnd,
        //      __out  LPTSTR lpString,
        //      __in   int nMaxCount
        //  );
        [DllImport("user32.dll")]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        //  DWORD GetWindowThreadProcessId(
        //      __in   HWND hWnd,
        //      __out  LPDWORD lpdwProcessId
        //  );
        [DllImport("user32.dll")]
        private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

        //HANDLE WINAPI OpenProcess(
        //  __in  DWORD dwDesiredAccess,
        //  __in  BOOL bInheritHandle,
        //  __in  DWORD dwProcessId
        //);
        [DllImport("kernel32.dll")]
        private static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);

        [DllImport("kernel32.dll")]
        private static extern bool CloseHandle(IntPtr handle);

        //  DWORD WINAPI GetModuleBaseName(
        //      __in      HANDLE hProcess,
        //      __in_opt  HMODULE hModule,
        //      __out     LPTSTR lpBaseName,
        //      __in      DWORD nSize
        //  );
        [DllImport("psapi.dll")]
        private static extern uint GetModuleBaseName(IntPtr hWnd, IntPtr hModule, StringBuilder lpFileName, int nSize);

        //  DWORD WINAPI GetModuleFileNameEx(
        //      __in      HANDLE hProcess,
        //      __in_opt  HMODULE hModule,
        //      __out     LPTSTR lpFilename,
        //      __in      DWORD nSize
        //  );
        [DllImport("psapi.dll")]
        private static extern uint GetModuleFileNameEx(IntPtr hWnd, IntPtr hModule, StringBuilder lpFileName, int nSize);

        public static string GetTopWindowText()
        {
            IntPtr hWnd = GetForegroundWindow();
            int length = GetWindowTextLength(hWnd);
            StringBuilder text = new StringBuilder(length + 1);
            GetWindowText(hWnd, text, text.Capacity);
            return text.ToString();
        }

        public static string GetTopWindowName()
        {
            IntPtr hWnd = GetForegroundWindow();
            uint lpdwProcessId;
            GetWindowThreadProcessId(hWnd, out lpdwProcessId);

            IntPtr hProcess = OpenProcess(0x0410, false, lpdwProcessId);

            StringBuilder text = new StringBuilder(1000);
            //GetModuleBaseName(hProcess, IntPtr.Zero, text, text.Capacity);
            GetModuleFileNameEx(hProcess, IntPtr.Zero, text, text.Capacity);

            CloseHandle(hProcess);

            return text.ToString();
        }


        static void Main(string[] args)
        {
            while (!Console.KeyAvailable)
            {
                Console.WriteLine(GetTopWindowText());
                Console.WriteLine(GetTopWindowName());
            }
        }
    }
}

你也可以在这里查看这个答案:How can I get URLs of open pages from Chrome and Firefox?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 2012-05-27
    • 2013-02-06
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 2023-03-14
    相关资源
    最近更新 更多