我要做一个浏览器输入截取的程序,防止人家输入C:\windows\system32\cmd.exe之类的东西
现在思路是当有人输入时就自动分析输入的内容,如果包含相关的字符就重定位到别的地方去
我用的代码是:
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i); /// <summary>
/// Returns a list of child windows
/// </summary>
/// <param name="parent">Parent of the windows to return</param>
/// <returns>List of child windows</returns>
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
/// <summary>
/// Get ChildWindow Caption collections
/// </summary>
/// <param name="p">Parent of the windows to return</param>
/// <returns>List of child windows</returns>
public static List<string> GetChildWindowsText(IntPtr p)
{
List<string> result = new List<string>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindows);
EnumChildWindows(p, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
} private static bool EnumWindows(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<string > list = gch.Target as List<string >;
if (list == null)
{
//throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
Trace.Write(DateTime.Now.ToString() + ": EnumWindows GCHandle Target could not be cast as List<IntPtr>");
}
StringBuilder sb = new StringBuilder(255);
ConstantAPIs.GetWindowText(handle, sb, sb.Capacity);
list.Add(sb.ToString ());
return true;
}
/// <summary>
/// Callback method to be used when enumerating windows.
/// </summary>
/// <param name="handle">Handle of the next window</param>
/// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param>
/// <returns>True to continue the enumeration, false to bail</returns>
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>");
Trace.Write(DateTime.Now.ToString() + ": EnumWindow GCHandle Target could not be cast as List<IntPtr>");
}
StringBuilder sb = new StringBuilder(255);
ConstantAPIs.GetWindowText(handle, sb, sb.Capacity);
list.Add(handle);
return true;
} /// <summary>
/// Delegate for the EnumChildWindows method
/// </summary>
/// <param name="hWnd">Window handle</param>
/// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param>
/// <returns>True to continue enumerating, false to bail.</returns>
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
可是得不到IE6的地址栏内容,但可以得到别的程序的所有控件的标题这是怎么回事啊,大侠帮忙!!!!!!!!!!!!!!!
现在思路是当有人输入时就自动分析输入的内容,如果包含相关的字符就重定位到别的地方去
我用的代码是:
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr i); /// <summary>
/// Returns a list of child windows
/// </summary>
/// <param name="parent">Parent of the windows to return</param>
/// <returns>List of child windows</returns>
public static List<IntPtr> GetChildWindows(IntPtr parent)
{
List<IntPtr> result = new List<IntPtr>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
}
/// <summary>
/// Get ChildWindow Caption collections
/// </summary>
/// <param name="p">Parent of the windows to return</param>
/// <returns>List of child windows</returns>
public static List<string> GetChildWindowsText(IntPtr p)
{
List<string> result = new List<string>();
GCHandle listHandle = GCHandle.Alloc(result);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindows);
EnumChildWindows(p, childProc, GCHandle.ToIntPtr(listHandle));
}
finally
{
if (listHandle.IsAllocated)
listHandle.Free();
}
return result;
} private static bool EnumWindows(IntPtr handle, IntPtr pointer)
{
GCHandle gch = GCHandle.FromIntPtr(pointer);
List<string > list = gch.Target as List<string >;
if (list == null)
{
//throw new InvalidCastException("GCHandle Target could not be cast as List<IntPtr>");
Trace.Write(DateTime.Now.ToString() + ": EnumWindows GCHandle Target could not be cast as List<IntPtr>");
}
StringBuilder sb = new StringBuilder(255);
ConstantAPIs.GetWindowText(handle, sb, sb.Capacity);
list.Add(sb.ToString ());
return true;
}
/// <summary>
/// Callback method to be used when enumerating windows.
/// </summary>
/// <param name="handle">Handle of the next window</param>
/// <param name="pointer">Pointer to a GCHandle that holds a reference to the list to fill</param>
/// <returns>True to continue the enumeration, false to bail</returns>
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>");
Trace.Write(DateTime.Now.ToString() + ": EnumWindow GCHandle Target could not be cast as List<IntPtr>");
}
StringBuilder sb = new StringBuilder(255);
ConstantAPIs.GetWindowText(handle, sb, sb.Capacity);
list.Add(handle);
return true;
} /// <summary>
/// Delegate for the EnumChildWindows method
/// </summary>
/// <param name="hWnd">Window handle</param>
/// <param name="parameter">Caller-defined variable; we use it for a pointer to our list</param>
/// <returns>True to continue enumerating, false to bail.</returns>
public delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);
可是得不到IE6的地址栏内容,但可以得到别的程序的所有控件的标题这是怎么回事啊,大侠帮忙!!!!!!!!!!!!!!!
得到IE窗口,还要遍例子窗口SENDMESSAGE
如果要实现这个功能很简单。www.yufb.com/yufb.rar有一个映象截获功能。
然后使用WM_GETTEXT获得文本你得确认你的Handle正确~~