vb中 如何获得窗体中所有控件的句柄

vb中 如何获得窗体中所有控件的句柄,第1张

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private Const GW_HWNDFIRST = 0

Private Const GW_HWNDNEXT = 2

Private Const GW_CHILD = 5

Private Sub Command1_Click()

Call List1_Click

End Sub

Private Sub Command2_Click()

Dim hwnd As Long

Dim s As String, t As String

List1Clear

hwnd = GetDesktopWindow()

s = String(256, Chr(0))

GetClassName hwnd, s, 255

s = Replace(s, Chr(0), "")

t = String(256, Chr(0))

GetWindowText hwnd, t, 255

t = Replace(t, Chr(0), "")

List1AddItem "桌面:" & hwnd & " 类名:" & s & " 标题:" & t & vbCrLf

hwnd = GetWindow(hwnd, GW_CHILD Or GW_HWNDFIRST)

s = String(256, Chr(0))

GetClassName hwnd, s, 255

s = Replace(s, Chr(0), "")

t = String(256, Chr(0))

GetWindowText hwnd, t, 255

t = Replace(t, Chr(0), "")

List1AddItem "窗口:" & hwnd & " 类名:" & s & " 标题:" & t & vbCrLf

While hwnd <> 0

hwnd = GetWindow(hwnd, GW_HWNDNEXT)

s = String(256, Chr(0))

GetClassName hwnd, s, 255

s = Replace(s, Chr(0), "")

t = String(256, Chr(0))

GetWindowText hwnd, t, 255

t = Replace(t, Chr(0), "")

List1AddItem "窗口:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf

Wend

End Sub

Private Sub Form_Load()

Command1Caption = "获取所有控件"

Command2Caption = "遍历所有窗体"

End Sub

Private Sub EnumAllHandles(ByVal hwnd As Long)

Dim hn As Long

Dim firsthd As Long

Dim s As String, t As String

firsthd = GetWindow(hwnd, GW_CHILD)

firsthd = GetWindow(firsthd, GW_HWNDFIRST)

hn = firsthd

Do While hn <> 0

s = String(256, Chr(0))

GetClassName hn, s, 255

s = Replace(s, Chr(0), "")

t = String(256, Chr(0))

GetWindowText hn, t, 255

t = Replace(t, Chr(0), "")

Text1Text = Text1Text & "句柄:" & hn & " 父句柄:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf

TreeView1NodesAdd "k" & hwnd, tvwChild, "k" & hn, "句柄:" & hn & " 类名:" & s & "标题:" & t

EnumAllHandles hn

hn = GetWindow(hn, GW_HWNDNEXT)

If hn = firsthd Then Exit Do

Loop

End Sub

Private Sub List1_Click()

If List1ListIndex = -1 Then Exit Sub

TreeView1NodesClear

TreeView1NodesAdd , , "k" & Trim(Str(Val(Mid(List1Text, 4)))), List1Text

Text1Text = ""

EnumAllHandles Val(Mid(List1Text, 4))

TreeView1Nodes("k" & Trim(Str(Val(Mid(List1Text, 4)))))Expanded = True

End Sub

'添加两个按钮一个文本框一个列表框和一个树形图

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Private Declare Function GetDesktopWindow Lib "user32" () As Long

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private Const GW_HWNDFIRST = 0

Private Const GW_HWNDNEXT = 2

Private Const GW_CHILD = 5

Private Sub Command1_Click()

Call List1_Click

End Sub

Private Sub Command2_Click()

Dim hwnd As Long

Dim s As String, t As String

List1Clear

hwnd = GetDesktopWindow()

s = String(256, Chr(0))

GetClassName hwnd, s, 255

s = Replace(s, Chr(0), "")

t = String(256, Chr(0))

GetWindowText hwnd, t, 255

t = Replace(t, Chr(0), "")

List1AddItem "桌面:" & hwnd & " 类名:" & s & " 标题:" & t & vbCrLf

hwnd = GetWindow(hwnd, GW_CHILD Or GW_HWNDFIRST)

s = String(256, Chr(0))

GetClassName hwnd, s, 255

s = Replace(s, Chr(0), "")

t = String(256, Chr(0))

GetWindowText hwnd, t, 255

t = Replace(t, Chr(0), "")

List1AddItem "窗口:" & hwnd & " 类名:" & s & " 标题:" & t & vbCrLf

While hwnd <> 0

hwnd = GetWindow(hwnd, GW_HWNDNEXT)

s = String(256, Chr(0))

GetClassName hwnd, s, 255

s = Replace(s, Chr(0), "")

t = String(256, Chr(0))

GetWindowText hwnd, t, 255

t = Replace(t, Chr(0), "")

List1AddItem "窗口:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf

Wend

End Sub

Private Sub Form_Load()

Command1Caption = "获取所有控件"

Command2Caption = "遍历所有窗体"

End Sub

Private Sub EnumAllHandles(ByVal hwnd As Long)

Dim hn As Long

Dim firsthd As Long

Dim s As String, t As String

firsthd = GetWindow(hwnd, GW_CHILD)

firsthd = GetWindow(firsthd, GW_HWNDFIRST)

hn = firsthd

Do While hn <> 0

s = String(256, Chr(0))

GetClassName hn, s, 255

s = Replace(s, Chr(0), "")

t = String(256, Chr(0))

GetWindowText hn, t, 255

t = Replace(t, Chr(0), "")

Text1Text = Text1Text & "句柄:" & hn & " 父句柄:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf

TreeView1NodesAdd "k" & hwnd, tvwChild, "k" & hn, "句柄:" & hn & " 类名:" & s & "标题:" & t

EnumAllHandles hn

hn = GetWindow(hn, GW_HWNDNEXT)

If hn = firsthd Then Exit Do

Loop

End Sub

Private Sub List1_Click()

If List1ListIndex = -1 Then Exit Sub

TreeView1NodesClear

TreeView1NodesAdd , , "k" & Trim(Str(Val(Mid(List1Text, 4)))), List1Text

Text1Text = ""

EnumAllHandles Val(Mid(List1Text, 4))

TreeView1Nodes("k" & Trim(Str(Val(Mid(List1Text, 4)))))Expanded = True

End Sub

'添加两个按钮一个文本框一个列表框和一个树形图

这个可以通过构造函数传参获得。

给你举个例子:

父窗体代码如下:

namespace 获取父类对象

{

  public partial class Form1 : Form

  {

      public Form1()

      {

          InitializeComponent();

      }

      private void button1_Click(object sender, EventArgs e)

      {        

          Form2 f2 = new Form2(this);

          f2Show();

      }

  }

}

子窗体代码如下:

namespace 获取父类对象

{

  public partial class Form2 : Form

  {

      public Form1 rec = null;

      //public Form2()

      //{

      //    InitializeComponent();

      //}

      public Form2(Form1 f1)

      {

          InitializeComponent();

          rec = f1;

      }

      private void button1_Click(object sender, EventArgs e)

      {

          recClose();

      }

  }

}

把另一个JFrame对象设为该Swing窗体的成员变量。

import javaawtHeadlessException;

import javaxswingJFrame;

/

@author wsj

@date 2010-8-30, 下午03:27:31

/

public class AFrame extends JFrame{

private BFrame bFrame;//把bFrame设置为该类的成员变量,通过setter方法或构造方法初始化

public void setbFrame(BFrame bFrame) {

thisbFrame = bFrame;

}

private AFrame(BFrame bFrame) throws HeadlessException {

super();

thisbFrame = bFrame;

}

}

class BFrame extends JFrame{

}

在A中增加一数据成员:public Control ctrlParent;

在fmTest中,把引用赋给它: actrlParnt = this;

接下来就随你怎么用了。

以上就是关于vb中 如何获得窗体中所有控件的句柄全部的内容,包括:vb中 如何获得窗体中所有控件的句柄、vb中 如何获得窗体中所有控件的句柄、关于c#如何获取父窗体控件。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/9266704.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存