关于VB两个数组相互赋值问题
这里先谢谢看贴的各位。因为说的东西可能有点多。希望大家体谅。不用各位顶帖了。我只想看有用东西。谢谢各位。
我想写一个句柄的比较。但是写到句柄储存到数组后有些不知道如何写了。请教一下大家。
我用了EnumWindows的EnumWindowsProc来得到指定的窗口类的句柄。句柄得到写入数组a()然后在EnumWindowsProc用x=x+1来redim a(1 to x)确定了数组a()的大小。然后我建立了一个数组b()然后确定一个值y。重新定义b()为b(1 to y)然后想把a(x)数组里相应的值给b(y),因为2个数组都是从下标1开始的。假如y=3我就想程序把b(1)=a(1),b(2)=a(2),b(3)=a(3)然后a(x)中多出来其他值不要。然后利用b(y)里存的值跟a(x)中对应位数的值比较。不同就对句柄发出关闭。就是想把a(x)里多出b(y)的句柄给关掉。 我觉得也可以x-y后把多余的个数句柄放入c(1 to x-y)直接对c(1 to x-y)里的句柄都发送关闭。大家看哪个好实现就给点代码。复制内存的API用了下好像写的不对。老是不行。觉得麻烦的给个思路。把用什么API或者怎么写赋值循环大概说下谢谢。
主要是把a(x)里的值对应的给b(y)把我难到了。因为EnumWindowsProc是每次都运行的。像个循环。我不知道这个是不是叫指针。希望能理解清楚的各位解释一下。自学的VB基础不行。在EnumWindowsProc里把a(x)里的值对应的给b(y)我觉得不方便。但是在FROM的代码里又没有办法直接引用到模块EnumWindowsProc里的数组a(x)b(y)。希望会的朋友也教我下。不多啰嗦了。代码贴出来。
FORM里的代码:
Option Explicit Private Sub Form_Load() Me.AutoRedraw = True 'call the Enumwindows-function EnumWindows AddressOf EnumWindowsProc, ByVal 0& End Sub模块里的代码: Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long Dim a() As Long Dim b() As Long Dim x As Integer Dim y As Integer Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean Dim sSave As String, Ret As Long Dim lei As String * 128 Dim diylei As String * 128 diylei = "IEFrame" + Chr(0) '定义要判断的窗口类IEFrame为IE窗口类If GetParent(hwnd) = 0 Then '是否是父窗口 If IsWindowVisible(hwnd) Then '是否可见 lei = "" GetClassName hwnd, lei, 128 If lei = diylei Then '判断是否是定义的类 If y = 0 Then 'Y不存在定义Y的值 y = InputBox("", "") Ret = GetWindowTextLength(hwnd) sSave = Space(Ret) GetWindowText hwnd, sSave, Ret + 1 Form1.Print Str$(hwnd) + " " + sSave '输出句柄和标题 x = x + 1 ReDim a(1 To x) a(x) = Str$(hwnd) Form1.Print "x"; x; a(x) If y >= x Then '如果Y>=X ReDim b(1 To y) End If Form1.Print "y"; y; b(y) Form1.Print x <= y Else 'Y存在值时 Ret = GetWindowTextLength(hwnd) sSave = Space(Ret) GetWindowText hwnd, sSave, Ret + 1 Form1.Print Str$(hwnd) + " " + sSave x = x + 1 ReDim a(1 To x) a(x) = Str$(hwnd) Form1.Print "x"; x; a(x) If y >= x Then ReDim b(1 To y) End If Form1.Print "y"; y; b(y) Form1.Print x <= y End If End If End IfEnd If '继续enumeration EnumWindowsProc = True End Function