![](/img/trans.png)
[英]How can I programmatically monitor the activity on an already opened serial port?
[英]How can I check if website is already opened in a webbrowser?
我正在這樣做:
Process.Start("http://www.google.com");
打開默認的Web瀏覽器后,我想以某種方式檢查網站是否已由Web瀏覽器打開,並關閉該網站的“ specicif”選項卡。
要進行按鈕單擊事件,它將檢查網站是否已打開,然后將其關閉。
您需要編寫一個API來操縱Internet Explorer之外的其他瀏覽器中的選項卡,但是您可以啟動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 7 IE 10在Windows XP IE 6中對其進行了測試。
我還編寫了一些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());
}
}
}
}
您可能還會在這里查看以下答案: 如何從Chrome和Firefox獲取打開頁面的URL?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.