循环检索窗口,并检索特定窗口的子窗口.

zixing 2010-06-03 09:28:11
程序需求是,检索是否存在记事本窗口,然后,检测记事本窗口中子窗口的内容.我以前用VB 6.0,实现完全没有问题,现在想学习一下.net里面该怎么写.谁有08里面的实例给我帮个忙好吗?

...全文
179 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zixing 2010-06-07
  • 打赏
  • 举报
回复
多谢多谢.太谢谢了.
我都不知道说什么好了.
兔子-顾问 2010-06-05
  • 打赏
  • 举报
回复
另外,你一句N多异常,这是什么意思?意思就是你没有在我给你回复后去看相关的知识,无法分析出现的问题,试问,你不了解基础的情况下,我如何能救问题给你分析?你听的懂么?如果不懂,那我要回答你这里的N多异常,2个方法:
1.直接替你写好,功能实现出来,你继续忽略学习,下次还是出问题无法解决来要代码。
2.我耐心的讲解,我想我要讲很多。而且很多和msdn重复。

希望你能理解我的意思。你需要搜索,查阅msdn,看懂我给你的例子。然后再就具体问题来问,而不是扔一堆代码让大家帮你给你调试。
兔子-顾问 2010-06-05
  • 打赏
  • 举报
回复
1,我根本不知道你想做什么,只是就你帖子提出的问题,给出了完整,可行的范例。
2.你实际需求只有你自己最清楚,从你的代码上看,实在不知道你想干嘛,我如何知道如何给建议?
3.我只给建议,只给关键的代码,关键的功能已经给出,其他的还是请你自己调试。
就问题而论,非问题的编码问题,调试的体力活,不要推给别人。我既不知道你代码的意思,也不知道你想干什么,更不希望在稀里糊涂的情况下埋到代码堆里。
兔子-顾问 2010-06-05
  • 打赏
  • 举报
回复
提示N多东西异常,还提示"尝试读取或写入受保护的内存。这通常指示其他内存已损坏。",奇怪.


我怎么知道N多东西异常时什么?很抱歉,我只提供范例,到此,是受人渔,在往下你需要自己思考。
zixing 2010-06-04
  • 打赏
  • 举报
回复
Private Sub FillChild(ByVal hWndparent As Long)
Dim HWndChild As Long
Dim SzCaption As String = ""
Dim i As Integer
Dim w As String = " "
Dim Ptr As IntPtr = Marshal.StringToHGlobalAnsi(w)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
HWndChild = GetWindow(hWndparent, GW_CHILD)
If (HWndChild = 0) Then Exit Sub
HWndChild = GetWindow(HWndChild, GW_HWNDFIRST)
If HWndChild = 0 Then Exit Sub

While HWndChild <> 0

i = GetWindowTextLength(HWndChild)
GetWindowText(HWndChild, SzCaption, 250)
SzCaption = Mid(SzCaption, 1, i - 1)
Label1.Text = SzCaption

SendMessage(HWndChild, WM_GETTEXT, 100, Ptr)
Label2.Text = Marshal.PtrToStringAnsi(Ptr)

FillChild(HWndChild)
HWndChild = GetWindow(HWndChild, GW_HWNDNEXT)

End While

End Sub

提示N多东西异常,还提示"尝试读取或写入受保护的内存。这通常指示其他内存已损坏。",奇怪.
兔子-顾问 2010-06-03
  • 打赏
  • 举报
回复
vb6你如何实现的?监视进程么?
zixing 2010-06-03
  • 打赏
  • 举报
回复
恩.行.我写写看看.不行我再问问你.谢谢谢谢.
兔子-顾问 2010-06-03
  • 打赏
  • 举报
回复
遍历部分和vb6的一样。你用vb6做过,不用我这里重写了。你尝试自己翻译试试。
兔子-顾问 2010-06-03
  • 打赏
  • 举报
回复
需要导入3个命名空间导入方法为,在文件最前面写以下内容
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Threading



以下方法测试通过。有效。获得文本框内容。

    <DllImport("user32.dll", EntryPoint:="FindWindowEx")> Public Shared Function FindWindowEx(ByVal hWnd1 As IntPtr, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
End Function
<DllImport("User32 ")>
Public Shared Function SendMessage(ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As Boolean
End Function

Public Const WM_GETTEXT As Integer = &HD
Private Shared Sub Test04()
Dim running As Boolean = True
Dim process_array As Process() = Process.GetProcesses()
For Each p As Process In process_array
If p.MainWindowTitle.IndexOf("记事本") <> -1 Then
Dim hEdit As Integer = FindWindowEx(p.MainWindowHandle, 0, "Edit", "")
Dim w As String = " "
Dim ptr As IntPtr = Marshal.StringToHGlobalAnsi(w)
If SendMessage(hEdit, WM_GETTEXT, 100, ptr) Then
MessageBox.Show(Marshal.PtrToStringAnsi(ptr))
End If
End If
Next
End Sub
zixing 2010-06-03
  • 打赏
  • 举报
回复
你写的这个例子很好,能不能遍历一下里面的所有子窗口?
zixing 2010-06-03
  • 打赏
  • 举报
回复
我是来学习的.c#我根本就看不懂
兔子-顾问 2010-06-03
  • 打赏
  • 举报
回复
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern int FindWindowEx(IntPtr hWnd1, int hWnd2, string lpsz1, string lpsz2);
[DllImport("user32.dll", EntryPoint = "GetWindowText")]
public static extern int GetWindowText(int hwnd, StringBuilder lpString, int cch);
[DllImport("User32 ")]
public static extern bool SendMessage(int hWnd, int Msg, int wParam, IntPtr lParam);

public const int WM_GETTEXT = 0xD;
private static void Test04()
{
bool running = true;
new Thread(() =>
{
while (running)
{
Process[] process_array = Process.GetProcesses();
foreach (Process p in process_array)
{
if (p.MainWindowTitle.IndexOf("记事本") != -1)//找到了
{
int hEdit = FindWindowEx(p.MainWindowHandle, 0, "Edit", "");
string w = " ";
IntPtr ptr = Marshal.StringToHGlobalAnsi(w);
if (SendMessage(hEdit, WM_GETTEXT, 100, ptr))

Console.WriteLine(Marshal.PtrToStringAnsi(ptr));
}
}
int tick = Environment.TickCount;
while (running && Environment.TickCount - tick < 10000)
{
Thread.Sleep(10);
}
}
Console.WriteLine("run over");
}).Start();
while (Console.ReadLine().ToLower() != "quit") ;
running = false;
}


就是这样实现的。不过是c#写的。你如果看不懂,回复跟帖,我给你改成vb.net的。
兔子-顾问 2010-06-03
  • 打赏
  • 举报
回复
vb.net中的方法和vb6的一样。你这个要求也需要用findwindow和findwindowex这2个api。
zixing 2010-06-03
  • 打赏
  • 举报
回复
不是啊.就是检测窗口,findwindowex,再检测下一个窗口,直到到有指定窗口的时候,就找他的child_window,这样的.但是6.0里面的函数在这里边有很多不好用的.结构也不好用.我想看看有没有高手直接给我写个例子我好学习学习.

16,721

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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