实例介绍
【实例简介】仿spy 查找窗口句柄
仿spy 查找窗口句柄 vs2010
【实例截图】
【核心代码】
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace WinFinder
{
public partial class FormFindHWnd : Form
{
#region 窗体级变量及引用方法定义
[DllImport("user32.dll")]
public static extern IntPtr WindowFromPoint(Point pt);
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int MaxCount);
[DllImport("user32")]
public static extern uint RealGetWindowClass(IntPtr hWnd, StringBuilder pszType, int MaxCount);
[DllImport("User32.dll")]
public static extern IntPtr GetParent(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hwnd, ref Rect rectangle);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, StringBuilder lParam);
[DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
public delegate bool CallBack(IntPtr hwnd, int lParam);
[DllImport("user32.dll")]
public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
public struct Rect
{
public int left;
public int top;
public int right;
public int bottom;
public int Width
{
get
{
return right - left;
}
}
public int Height
{
get
{
return bottom - top;
}
}
}
IntPtr hwdFinded = IntPtr.Zero;
IntPtr hwdApp = IntPtr.Zero;
IntPtr hwdTemp = IntPtr.Zero;
Rect rect;
Image imagePre;
#endregion
public FormFindHWnd()
{
InitializeComponent();
pictureBoxFindWnd.Image = Image.FromFile("FindWndHome.bmp");
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void pictureBoxFindWnd_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
textBoxGetText.Text = "";
textBoxGetClass.Text = "";
textBoxGetHwnd.Text = "";
textBoxGetAppName.Text = "";
//设置查找图标光标
Cursor.Current = new Cursor("findwnd.cur");
//变更背景图片
imagePre = pictureBoxFindWnd.Image;
pictureBoxFindWnd.Image = Image.FromFile( "FindWndGone.bmp");
//设置本控件捕获鼠标,处理相应的鼠标事件
pictureBoxFindWnd.Capture = true;
hwdFinded = IntPtr.Zero;
}
}
private void pictureBoxFindWnd_MouseUp(object sender, MouseEventArgs e)
{
List<IntPtr> sub= GetIntPtr(hwdFinded);
listBox1.Items.Clear();
foreach (IntPtr i in sub)
{
//输出标题
StringBuilder strTemp = new StringBuilder(256);
//GetWindowText(hwdFinded, strTemp, strTemp.Capacity);
SendMessage(i, 0x000D, 256, strTemp);
listBox1.Items.Add(i.ToString() "/" strTemp.ToString());
}
//恢复初始状态
Cursor.Current = Cursors.Default;
pictureBoxFindWnd.Image = imagePre;
pictureBoxFindWnd.Capture = false;
}
private void pictureBoxFindWnd_MouseMove(object sender, MouseEventArgs e)
{
if (!this.Bounds.Contains(Cursor.Position))
{
hwdFinded = WindowFromPoint(Cursor.Position);
if (hwdFinded != IntPtr.Zero)
{
//输出句柄
textBoxGetHwnd.Text = hwdFinded.ToString("D").PadLeft(8,'0');
//输出标题
StringBuilder strTemp = new StringBuilder(256);
//GetWindowText(hwdFinded, strTemp, strTemp.Capacity);
SendMessage(hwdFinded, 0x000D, 256, strTemp);
textBoxGetText.Text = strTemp.ToString();
//输出类名
RealGetWindowClass(hwdFinded, strTemp, 256);
textBoxGetClass.Text = strTemp.ToString();
//向上查找Windows窗体,应用程序的主窗体的父窗体句柄为IntPtr.Zero
IntPtr hWdParent = GetParent(hwdFinded);
while (hWdParent != IntPtr.Zero)
{
txtParent.Text = hWdParent.ToString();
hwdTemp = hWdParent;
hWdParent = GetParent(hwdTemp);
}
StringBuilder title = new StringBuilder(256);
GetWindowText(hwdTemp, title, title.Capacity);
textBoxGetAppName.Text = title.ToString();
if (hwdTemp != hwdFinded)
{
hwdApp = hwdTemp;
}
else
{
hwdApp = hwdFinded;
}
//输出相对位置
rect = new Rect();
GetWindowRect(hwdFinded, ref rect);
textBoxRevPos.Text = "{X=" (Cursor.Position.X - rect.left).ToString() ",Y=" (Cursor.Position.Y - rect.top).ToString() "}";
label7.Text =rect.left "/" rect.top "," rect.Width.ToString() "/" rect.Height.ToString();
textBoxSrcPos.Text= Cursor.Position.ToString();
// point=curo
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
rect = new Rect();
GetWindowRect(hwdFinded, ref rect);
Point pos = Cursor.Position;
Rectangle r = new Rectangle(rect.left, rect.bottom - 100, rect.Width, 100);
label7.Text = r.ToString() Cursor.Position.ToString();
if(!r.Contains(Cursor.Position))
{
Cursor.Position = new Point((rect.left r.Width)/2, rect.bottom - 50);
//mouse_event(0x8000, rect.left 30, rect.bottom - 50, 0, 0);
mouse_event(0x0002|0x0004, 0, 0, 1, 0); //模拟鼠标按下操作
}
if (WindowFromPoint(Cursor.Position) == hwdFinded)
{
//SendMessage(hwdFinded, 0x0C, 256,new StringBuilder("我是中国人"));
SendKeys.Send("我是中国人");
SendKeys.Send("{ENTER}");
}
System.Threading.Thread.Sleep(1000);
Cursor.Position = pos;
//SendMessage(hwdFinded, 0x0C, 256, new StringBuilder("abc\r\n"));
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = !timer1.Enabled;
//SendMessage(hwdFinded, 0x000C, 256, new StringBuilder("abc\r\n"));
//mouse_event(0x0002, rect.left 20, rect.bottom - 20, 0, 0); //模拟鼠标按下操作
//mouse_event(0x0004, rect.left 20, rect.bottom - 20, 0, 0); //模拟鼠标放开操作
// int h=rect.Height-50-(Cursor.Position.Y - rect.top );
// label7.Text = h.ToString();
// if ( h> 0)
// mouse_event(0x0001, 0, 30, 0, 0);
// mouse_event(0x0001, rect.left 20, rect.bottom - 20, 0, 0);
// SendKeys.Send("我是中国人"); //模拟键盘输入游戏ID
//SendKeys.Send("{TAB}"); //模拟键盘输入TAB
//SendKeys.Send(_GamePass); //模拟键盘输入游戏密码
// SendKeys.Send("{ENTER}"); //模拟键盘输入ENTER
}
public static List<IntPtr> GetIntPtr(IntPtr hwd)
{
List<IntPtr> listIntPtr = new List<IntPtr>();
EnumChildWindows(hwd, delegate(IntPtr hWnd, int lParam)
{
listIntPtr.Add(hWnd);
return true;
}, 0);
return listIntPtr;
}
public static IntPtr FindWindowEx(IntPtr hwnd, string lpszWindow, bool bChild)
{
IntPtr iResult = IntPtr.Zero;
// 首先在父窗体上查找控件
iResult = FindWindowEx(hwnd, (uint)IntPtr.Zero, null, lpszWindow);
// 如果找到直接返回控件句柄
if (iResult != IntPtr.Zero) return iResult;
// 如果设定了不在子窗体中查找
if (!bChild) return iResult;
// 枚举子窗体,查找控件句柄
int i = EnumChildWindows(
hwnd,
(h, l) =>
{
IntPtr f1 = FindWindowEx(h, (uint)IntPtr.Zero, null, lpszWindow);
if (f1 == IntPtr.Zero)
return true;
else
{
iResult = f1;
return false;
}
},
0);
// 返回查找结果
return iResult;
}
}
}
相关软件
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)
支持(0) 盖楼(回复)