[winform]怎样获取窗口的hWnd?

mdjsy1991 2008-04-12 08:10:55
如果用Findwindow,那么如果找标题为"Testtesttesttest"的窗口的hWnd只能是这么写
FindWindow(null, "Windows 任务管理器")
但我想获取到所有标题带"test"的窗口的hWnd,请问要用什么函数?
...全文
320 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ConanKid 2008-04-12
  • 打赏
  • 举报
回复

[DllImport("user32.dll")]
private static extern int FindWindow(string lpClassName,string lpWindowName);

private void button1_Click(object sender, EventArgs e)
{

MessageBox.Show(FindWindow("#32770", "Windows 任务管理器").ToString());
}

#32770,通过SPY++取得.
boblaw 2008-04-12
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 zswang 的回复:]
-_-!!! GetWindowText在跨進程中應該是無效的。
是就是,不是就不是,程序还有“应该是”的吗?自己动手运行一下再给一个结论。
[/Quote]
記錯了,跨進程取窗體子控件文本不可以用GetWindowText,取窗體的文本用GetWindowText是可以的。
mdjsy1991 2008-04-12
  • 打赏
  • 举报
回复
-_-
知道了
if (vWindowText.ToString().ToLower().IndexOf("test") > -1) // 判断标题名
这样就好了
王集鹄 2008-04-12
  • 打赏
  • 举报
回复
-_-!!! GetWindowText在跨進程中應該是無效的。
是就是,不是就不是,程序还有“应该是”的吗?自己动手运行一下再给一个结论。

public static bool WindowsProc(IntPtr hWnd, IntPtr lParam)
{
StringBuilder vClassName = new StringBuilder(256);
StringBuilder vWindowText = new StringBuilder(256);
GetClassName(hWnd, vClassName, vClassName.Capacity);
GetWindowText(hWnd, vWindowText, vWindowText.Capacity);
//---------------------------楼主,关键的判断是在这里
//---------------------------判断标题、判断类名 唉~~~~

if (vWindowText.ToString().ToLower() == "无标题 - 记事本") // 判断标题名
{
Console.WriteLine("句柄:{0},类名:{1},标题:{2}",
hWnd, vClassName, vWindowText);
}
return true;
}

boblaw 2008-04-12
  • 打赏
  • 举报
回复
你要獲得窗體的句柄是在EnumWindowsProc中獲得,而不是EnumWindows中,EnumWindows衹會返回0和1表示成功與失敗。
另外指出清潔工的代碼中可能出錯的一個問題,獲得窗口標題時最好使用SendMessage發送WM_GETTEXT消息,GetWindowText在跨進程中應該是無效的。
mdjsy1991 2008-04-12
  • 打赏
  • 举报
回复
MessageBox.Show(EnumWindows(WindowsProc, IntPtr.Zero).ToString());
不管村不存在都是1
mdjsy1991 2008-04-12
  • 打赏
  • 举报
回复
不是,我取的所有东西都是为1
王集鹄 2008-04-12
  • 打赏
  • 举报
回复
-_-!!! 难道这样贴你才明白?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern int EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString,
int nMaxCount);
[DllImport("user32.dll")]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName,
int nMaxCount);

public static bool WindowsProc(IntPtr hWnd, IntPtr lParam)
{
StringBuilder vClassName = new StringBuilder(256);
StringBuilder vWindowText = new StringBuilder(256);
GetClassName(hWnd, vClassName, vClassName.Capacity);
GetWindowText(hWnd, vWindowText, vWindowText.Capacity);
if (vWindowText.ToString().ToLower() == "testtesttesttest") // 判断标题名
{
Console.WriteLine("句柄:{0},类名:{1},标题:{2}",
hWnd, vClassName, vWindowText);
}
return true;
}

private void button1_Click(object sender, EventArgs e)
{
EnumWindows(WindowsProc, IntPtr.Zero);
}
}
}
mdjsy1991 2008-04-12
  • 打赏
  • 举报
回复
楼上的代码如何使用?
王集鹄 2008-04-12
  • 打赏
  • 举报
回复
参考如下代码:
using System.Runtime.InteropServices;

public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
public static extern int EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString,
int nMaxCount);
[DllImport("user32.dll")]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName,
int nMaxCount);

public static bool WindowsProc(IntPtr hWnd, IntPtr lParam)
{
StringBuilder vClassName = new StringBuilder(256);
StringBuilder vWindowText = new StringBuilder(256);
GetClassName(hWnd, vClassName, vClassName.Capacity);
GetWindowText(hWnd, vWindowText, vWindowText.Capacity);
if (vWindowText.ToString().ToLower() == "testtesttesttest") // 判断标题名
{
Console.WriteLine("句柄:{0},类名:{1},标题:{2}",
hWnd, vClassName, vWindowText);
}
return true;
}

private void button3_Click(object sender, EventArgs e)
{
EnumWindows(WindowsProc, IntPtr.Zero);
}
mdjsy1991 2008-04-12
  • 打赏
  • 举报
回复
自己顶....
mdjsy1991 2008-04-12
  • 打赏
  • 举报
回复
可以给些代码吗?
boblaw 2008-04-12
  • 打赏
  • 举报
回复
API EnumWindows和EnumWindowsProc結合使用。
EnumWindows會枚舉所有窗口句柄傳至你提供的EnumWindowsProc函數中,你要在EnumWindowsProc判斷句柄是否是你需要的。
mdjsy1991 2008-04-12
  • 打赏
  • 举报
回复
错了..

FindWindow(null, "Testtesttesttest")

110,536

社区成员

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

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

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