问题描述
我试图让所有打开的IE选项卡标题的列表或搜索特定的标签标题。
我一直用这个,但不起作用对于因某些原因每个标签:
//得到一个处理一个应用程序窗口。
函数[DllImport(USER32.DLL,字符集= CharSet.Unicode)]
公共静态外部的IntPtr FindWindow函数(字符串lpClassName,
串lpWindowName);
IntPtr的explorerHandle = FindWindow函数(IEFrame,谷歌 - Internet Explorer的);
//验证,我们发现窗口。
如果(explorerHandle == IntPtr.Zero)
{
MessageBox.Show(没有找到IE的实例);
的回报;
}
我特别寻找具有标签无法显示此页面的称号。
有什么建议?
最后一次,我需要做类似的东西我用这个代码:(它的工作!)
函数[DllImport(user32.dll中 )
[返回:的MarshalAs(UnmanagedType.Bool)
公共静态的extern BOOL EnumChildWindows(IntPtr的parentHandle,Win32Callback回调,IntPtr的lParam的);
函数[DllImport(user32.dll中,字符集= CharSet.Auto)]
静态公共EXTERN的IntPtr GetClassName(IntPtr的的HWND,System.Text.StringBuilder lpClassName,诠释nMaxCount);
函数[DllImport(USER32.DLL,字符集= CharSet.Unicode)]
公共静态外部的IntPtr FindWindow函数(字符串lpClassName,
串lpWindowName);
公共委托布尔Win32Callback(IntPtr的HWND,IntPtr的lParam的);
私人静态布尔EnumWindow(IntPtr的手柄,IntPtr的指针)
{
的GCHandle GCH = GCHandle.FromIntPtr(指针);
名单,LT; IntPtr的>清单= gch.Target的名单,LT; IntPtr的取代;
如果(名单== NULL)
抛出新InvalidCastException的(目标的GCHandle无法转换为表< IntPtr的>);
list.Add(句柄);
返回真;
}
公共静态列表< IntPtr的> GetChildWindows(IntPtr的父母)
{
名单,LT; IntPtr的>结果=新的List< IntPtr的>();
的GCHandle listHandle = GCHandle.Alloc(结果);
试
{
Win32Callback childProc =新Win32Callback(EnumWindow);
EnumChildWindows(父母,childProc,GCHandle.ToIntPtr(listHandle));
}
终于
{
如果(listHandle.IsAllocated)
listHandle.Free();
}
返回结果;
}
公共静态字符串GetWinClass(IntPtr的HWND)
{
如果(HWND == IntPtr.Zero)
返回NULL;
类名的StringBuilder =新的StringBuilder(100);
IntPtr的结果= GetClassName(HWND,类名,classname.Capacity);
如果(结果= IntPtr.Zero!)
返回classname.ToString();
返回NULL;
}
公共静态的IEnumerable< IntPtr的> EnumAllWindows(IntPtr的HWND,字符串childClassName)
{
名单,LT; IntPtr的>孩子= GetChildWindows(HWND);
如果(孩子== NULL)
产量突破;
的foreach(IntPtr的孩子的孩子)
{
如果(GetWinClass(子)== childClassName)
收益回报的孩子;
的foreach(在EnumAllWindows VAR childchild(儿童,childClassName))
收益率的回报childchild;
}
}
和使用它:
IntPtr的处理= FindWindow函数(IEFrame,谷歌);
VAR hwndChilds = EnumAllWindows(手柄,帧标签);
hwndChilds
是的IntPtr的所有列表
我完成我的答案有:帧标签
修改的接下来的步骤,让您的标签的标题。
函数[DllImport(user32.dll中,字符集= CharSet.Auto) ]
静态外部的IntPtr SendMessage函数(IntPtr的的HWND,UInt32的消息,IntPtr的wParam中,[出]的StringBuilder的lParam);
函数[DllImport(user32.dll中,ExactSpelling = TRUE,字符集= CharSet.Auto)]
公共静态外部的IntPtr的getParent(IntPtr的的hWnd);
公共静态字符串GetWindowTextRaw(IntPtr的HWND)
{
UINT WM_GETTEXT = 0x000D;
UINT WM_GETTEXTLENGTH = 0x000E;
//分配正确的字符串长度的第一
INT长度=(int)的SendMessage函数(HWND,WM_GETTEXTLENGTH,IntPtr.Zero,NULL);
StringBuilder的SB =新的StringBuilder(长+ 1);
的SendMessage(HWND,WM_GETTEXT,(IntPtr的)sb.Capacity,SB);
返回sb.ToString();
}
您可以测试它:
静态无效的主要(字串[] args)
{
IntPtr的处理= FindWindow函数(IEFrame,谷歌);
VAR hwndChild = EnumAllWindows(手柄,帧标签);
的foreach(在hwndChild VAR的IntPtr)
{
VAR PTR =的getParent(IntPtr的);
变种文字= GetWindowTextRaw(PTR);
Console.WriteLine(文本);
}
到Console.ReadLine();
}
和结果:
。
你可以找到
有一个伟大的一天!
I am trying to get a list of all the open IE tab titles or search for a specific tab title.
I've been using this but does not work for every tab for some reason:
// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
IntPtr explorerHandle = FindWindow("IEFrame", "Google - Internet Explorer");
// Verify that we found the Window.
if (explorerHandle == IntPtr.Zero)
{
MessageBox.Show("Didn't find an instance of IE");
return;
}
I am particularly looking for tabs which have "This page can’t be displayed" in the title.
Any suggestions?
The last time I needed to do something like that I used this code: (It's working !)
[DllImport("user32.Dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr parentHandle, Win32Callback callback, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static public extern IntPtr GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount);
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
public delegate bool Win32Callback(IntPtr hwnd, IntPtr lParam);
private static bool EnumWindow(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<IntPtr> list = gch.Target as List<IntPtr>;
if (list == null)
throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
list.Add(handle);
return true;
}
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
Win32Callback childProc = new Win32Callback(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
public static string GetWinClass(IntPtr hwnd)
{
if (hwnd == IntPtr.Zero)
return null;
StringBuilder classname = new StringBuilder(100);
IntPtr result = GetClassName(hwnd, classname, classname.Capacity);
if (result != IntPtr.Zero)
return classname.ToString();
return null;
}
public static IEnumerable<IntPtr> EnumAllWindows(IntPtr hwnd, string childClassName)
{
List<IntPtr> children = GetChildWindows(hwnd);
if (children == null)
yield break;
foreach (IntPtr child in children)
{
if (GetWinClass(child) == childClassName)
yield return child;
foreach (var childchild in EnumAllWindows(child, childClassName))
yield return childchild;
}
}
And to use it :
IntPtr handle = FindWindow("IEFrame", "Google");
var hwndChilds = EnumAllWindows(handle, "Frame Tab");
hwndChilds
is a list of IntPtr to all of Frame Tab
.
EDIT :I complete my answer with the next steps to get the title of your tabs.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [Out] StringBuilder lParam);
[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hWnd);
public static string GetWindowTextRaw(IntPtr hwnd)
{
uint WM_GETTEXT = 0x000D;
uint WM_GETTEXTLENGTH = 0x000E;
// Allocate correct string length first
int length = (int)SendMessage(hwnd, WM_GETTEXTLENGTH, IntPtr.Zero, null);
StringBuilder sb = new StringBuilder(length + 1);
SendMessage(hwnd, WM_GETTEXT, (IntPtr)sb.Capacity, sb);
return sb.ToString();
}
You can test it with :
static void Main(string[] args)
{
IntPtr handle = FindWindow("IEFrame", "Google");
var hwndChild = EnumAllWindows(handle, "Frame Tab");
foreach (var intPtr in hwndChild)
{
var ptr = GetParent(intPtr);
var text = GetWindowTextRaw(ptr);
Console.WriteLine(text);
}
Console.ReadLine();
}
and the result :
If you need more explanations don't hesitate to ask.You can find all pInvoke signatures on http://www.pinvoke.net/
Have a great day !
这篇关于获取Internet Explorer的分页标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!