C#如何遍历其他程序窗体上所有button Edit等控件的句柄

zgwhaha 2008-10-09 05:32:57
做一个小程序,需要修改其他程序中一个Edit框的内容,但是那个程序有4个类似的Edit框,用spy++看标题都是空的,怎么能区分他们得到自己想要操作的那个Edit框呢,遍历可行否?因为我用spy++看发现他们的顺序都是一定的,或者给出其他简单的解决办法也行,谢谢!
...全文
812 16 打赏 收藏 转发到动态 举报
写回复
用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?
作者: 三角猫 出处: 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 hwndButton = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "Button", "发送(S)"); if (IntPtr.Zero != hwndButton) { ////找到窗体顺序上的第一个RichEdit20A控件,其实就是消息显示框 IntPtr hwndRichEdit = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "RichEdit20A", null); ////利用spy++,可以看到消息输入框的父窗体是类名为 AfxWnd42 的控件 ////在顺序上是显示框的下一个窗体 if (IntPtr.Zero != hwndRichEdit) { ////找到 AfxWnd42 这个窗体 hwndRichEdit = NativeMethods.GetWindow(hwndRichEdit, NativeMethods.GW_HWNDNEXT); if (IntPtr.Zero != hwndRichEdit) { ////这才是真正的消息输入框 hwndRichEdit = NativeMethods.FindWindowEx(hwndRichEdit, IntPtr.Zero, "RichEdit20A", null); if (hwndRichEdit != IntPtr.Zero) { ////发送消息,因为QQ屏蔽了 WM_SETTEXT, WM_PASTE 命令,所有采用 EM_REPLACESEL 来实现 NativeMethods.SendMessage(hwndRichEdit, NativeMethods.EM_REPLACESEL, IntPtr.Zero, msg); ////给发送按钮发 鼠标单击消息 NativeMethods.SendMessage(hwndButton, NativeMethods.BM_CLICK, IntPtr.Zero, IntPtr.Zero); } } } } } } } }

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧