社区
C#
帖子详情
C#如何遍历其他程序窗体上所有button Edit等控件的句柄
zgwhaha
2008-10-09 05:32:57
做一个小程序,需要修改其他程序中一个Edit框的内容,但是那个程序有4个类似的Edit框,用spy++看标题都是空的,怎么能区分他们得到自己想要操作的那个Edit框呢,遍历可行否?因为我用spy++看发现他们的顺序都是一定的,或者给出其他简单的解决办法也行,谢谢!
...全文
812
16
打赏
收藏
C#如何遍历其他程序窗体上所有button Edit等控件的句柄
做一个小程序,需要修改其他程序中一个Edit框的内容,但是那个程序有4个类似的Edit框,用spy++看标题都是空的,怎么能区分他们得到自己想要操作的那个Edit框呢,遍历可行否?因为我用spy++看发现他们的顺序都是一定的,或者给出其他简单的解决办法也行,谢谢!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
16 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
leochen315531813
2010-10-26
打赏
举报
回复
哎,,没人给个好点的实例瞧瞧
whwhzzz
2009-02-21
打赏
举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace GetAllWindows
{
class GetAllWindows
{
private delegate bool WNDENUMPROC(IntPtr hWnd, int lParam);
[DllImport("user32.dll")]
private static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, int lParam);
//[DllImport("user32.dll")]
//private static extern IntPtr FindWindowW(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
public struct WindowInfo
{
public IntPtr hWnd;
public string szWindowName;
public string szClassName;
}
public WindowInfo[] GetAllDesktopWindows()
{
List<WindowInfo> wndList = new List<WindowInfo>();
//enum all desktop windows
EnumWindows(delegate(IntPtr hWnd, int lParam)
{
WindowInfo wnd = new WindowInfo();
StringBuilder sb = new StringBuilder(256);
//get hwnd
wnd.hWnd = hWnd;
//get window name
GetWindowTextW(hWnd, sb, sb.Capacity);
wnd.szWindowName = sb.ToString();
//get window class
GetClassNameW(hWnd, sb, sb.Capacity);
wnd.szClassName = sb.ToString();
//add it into list 你可以在这里修改 过滤你要的控件进入列表
if (wnd.szWindowName.Length> 0)
wndList.Add(wnd);
return true;
}, 0);
foreach (WindowInfo w in wndList)
{
Console.WriteLine(w.szWindowName + " ");
}
return wndList.ToArray();
}
}
}
heroxxx
2008-10-11
打赏
举报
回复
你必须知道每个Edit的句柄,这样才能够区分他们!
在窗体的Contrls里头包涵Windows的所有子控件!
zgwhaha
2008-10-11
打赏
举报
回复
我就是找不到Edit的句柄,用enumwindows也不行,可能是我的方法有问题。
patrickpan
2008-10-10
打赏
举报
回复
直接遍历
hanzs
2008-10-10
打赏
举报
回复
路过,顶……
bingbingw
2008-10-10
打赏
举报
回复
up~~
Red_angelX
2008-10-10
打赏
举报
回复
自己去baidu msdn 说的够清楚的了
ZengHD
2008-10-10
打赏
举报
回复
[Quote=引用 7 楼 zgwhaha 的回复:]
能有人给个小例子说明么?谢谢
[/Quote]
你自己先试试,可以在网上查找相关API的例子
zgwhaha
2008-10-10
打赏
举报
回复
能有人给个小例子说明么?谢谢
ZengHD
2008-10-09
打赏
举报
回复
可以使用GetWindowRect来获得文本框在窗体内容的位置,可以通过这个来区别
zgwhaha
2008-10-09
打赏
举报
回复
能否给个例子说明下呢,怎么顺序找?谢谢
Red_angelX
2008-10-09
打赏
举报
回复
如果是固定的知道他只有4个Edit FindwindowsEx顺序找就行了
Red_angelX
2008-10-09
打赏
举报
回复
BOOL EnumWindows( WNDENUMPROC lpEnumFunc,
LPARAM lParam
);
wjq
2008-10-09
打赏
举报
回复
是有顺序的,用WinAPI的FindWindowEx函数,可以指定前一个控件句柄来找下一个,如果要找第2个文本框,那么调2次即可
lude8880
2008-10-09
打赏
举报
回复
this.Controls?
C#
写的QQ自动发消息.rar
作者: 三角猫 出处: http://www.zu14.cn/ 版权归 三角猫 和 真有意思网 所有,转载请注明出处 using System; using System.Collections.Generic; using System.Text; namespace QQAutoMsg { /// /// 消息发送 /// internal static class QQMsgSender { /// /// 发送消息 /// /// 所以已打开的QQ
窗体
的列表 /// 消息内容 internal static void Go(List qqChatWindows, string msg) { foreach (EnumQQChatWindows.QQChatWindow win in qqChatWindows) { SendMsg(win.WindowHwnd, msg); } } /// /// 根据
窗体
句柄
,找到输入框和发送按钮,发送消息出去 /// /// 聊天窗口
句柄
/// 消息内容 private static void SendMsg(IntPtr hWnd, string msg) { if (NativeMethods.IsWindow(hWnd)) //确认该聊天窗口仍然有效 { ////找到 发送 按钮 IntPtr hwnd
Button
= NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "
Button
", "发送(S)"); if (IntPtr.Zero != hwnd
Button
) { ////找到
窗体
顺序上的第一个Rich
Edit
20A
控件
,其实就是消息显示框 IntPtr hwndRich
Edit
= NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "Rich
Edit
20A", null); ////利用spy++,可以看到消息输入框的父
窗体
是类名为 AfxWnd42 的
控件
////在顺序上是显示框的下一个
窗体
if (IntPtr.Zero != hwndRich
Edit
) { ////找到 AfxWnd42 这个
窗体
hwndRich
Edit
= NativeMethods.GetWindow(hwndRich
Edit
, NativeMethods.GW_HWNDNEXT); if (IntPtr.Zero != hwndRich
Edit
) { ////这才是真正的消息输入框 hwndRich
Edit
= NativeMethods.FindWindowEx(hwndRich
Edit
, IntPtr.Zero, "Rich
Edit
20A", null); if (hwndRich
Edit
!= IntPtr.Zero) { ////发送消息,因为QQ屏蔽了 WM_SETTEXT, WM_PASTE 命令,所有采用 EM_REPLACESEL 来实现 NativeMethods.SendMessage(hwndRich
Edit
, NativeMethods.EM_REPLACESEL, IntPtr.Zero, msg); ////给发送按钮发 鼠标单击消息 NativeMethods.SendMessage(hwnd
Button
, NativeMethods.BM_CLICK, IntPtr.Zero, IntPtr.Zero); } } } } } } } }
从新手小白入门MFC框架视频精讲
微软基础类库(英语:Microsoft Foundation Classes,简称MFC)是微软公司提供的一个类库(class libraries),以C++类的形式封装了Windows API,并且包含一个应用
程序
框架,以减少应用
程序
开发人员的工作量。其中包含大量Windows
句柄
封装类和很多Windows的内建
控件
和组件的封装类!
C#
110,533
社区成员
642,574
社区内容
发帖
与我相关
我的任务
C#
.NET技术 C#
复制链接
扫一扫
分享
社区描述
.NET技术 C#
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
让您成为最强悍的C#开发者
试试用AI创作助手写篇文章吧
+ 用AI写文章