资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vb点虐 枚举句柄 vb枚举类型enum用法

VB.NET的枚举求教解决方法

这个功能实现起来其实也很简单,就是通过反射去读取 DescriptionAttribute 的 Description 属性的值,代码如下所示:

成都创新互联专业为企业提供海盐网站建设、海盐做网站、海盐网站设计、海盐网站制作等企业网站建设、网页设计与制作、海盐企业网站模板建站服务,十余年海盐做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

/// summary

/// 返回枚举项的描述信息。

/// /summary

/// param name="value"要获取描述信息的枚举项。/param

/// returns枚举想的描述信息。/returns

public static string GetDescription(Enum value)

{

Type enumType = value.GetType();

// 获取枚举常数名称。

string name = Enum.GetName(enumType, value);

if (name != null)

{

// 获取枚举字段。

FieldInfo fieldInfo = enumType.GetField(name);

if (fieldInfo != null)

{

// 获取描述的属性。

DescriptionAttribute attr = Attribute.GetCustomAttribute(fieldInfo,

typeof(DescriptionAttribute), false) as DescriptionAttribute;

if (attr != null)

{

return attr.Description;

}

}

}

return null;

}

这段代码还是很容易看懂的,这里取得枚举常数的名称使用的是 Enum.GetName() 而不是 ToString(),因为前者更快,而且对于不是枚举常数的值会返回 null,不用进行额外的反射。

当然,这段代码仅是一个简单的示例,接下来会进行更详细的分析。

求vb点虐 句柄实例,实现操作其他程序窗口。如我给的例子

Imports System.Text

Imports System.Runtime.InteropServices

Public Class Form1

' 相关API函数声明,注释掉的这里没用到,但是也比较常用吧,这些函数的功能都能搜到。

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

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr

Private Delegate Function EnumChildProc(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean

Private Declare Function EnumChildWindows Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal lpEnumFunc As EnumChildProc, ByVal lParam As Integer) As Boolean

Private Declare Auto Function SendMessage Lib "User32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer

'Private Declare Function CheckDlgButton Lib "user32" Alias "CheckDLGButtonA" (ByVal hDlg As IntPtr, ByVal nIDButton As IntPtr, ByVal wCheck As Integer) As Integer

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As IntPtr, ByVal lpClassName As StringBuilder, ByVal nMaxCount As Integer) As Integer

'Private Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As IntPtr, ByVal lpdwProcessId As Long) As Integer

Private Declare Auto Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLength" (ByVal hwnd As IntPtr) As Integer

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer

' 相关消息定义,也有没用到的

Const WM_SETTEXT = HC

Const WM_GETTEXT = HD

'Const WM_SETFOCUS = H7

'Const WM_KILLFOCUS = H8

'Const WM_CLOSE = H10

'Const WM_SYSCOMMAND = H112

'Const SC_CLOSE = HF060

'Const SC_MINIMIZE = HF020

Const BM_GETCHECK = HF0

Const BM_SETCHECK = HF1

Const BM_GETSTATE = HF2

Const BM_SETSTATE = HF3

Const BM_SETSTYLE = HF4

Const BM_CLICK = HF5

'Const BM_GETIMAGE = HF6

'Const BM_SETIMAGE = HF7

Const BST_UNCHECKED = O0

Const BST_CHECKED = O1

Const BST_INDETERMINATE = O2

' 储存窗口句柄

Dim WindowHandle As IntPtr

' 储存两个(或者多个)编辑框句柄

Dim EditHandle As New List(Of IntPtr)

Dim EditWindowsText As List(Of String)

' 储存复选框句柄

Dim CheckHandle As IntPtr = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Button1_Click(sender, e)

End Sub

' EnumChildWindows 回调函数,该函数名作为API函数EnumChildWindows 的一个参数

' 该函数实现了枚举各个子窗口,找出编辑框属性的功能

Public Function EnumChildProcC(ByVal hwnd As IntPtr, ByVal lParam As Integer) As Boolean

Dim dwWindowClass As StringBuilder = New StringBuilder(100)

' 获得某一个句柄的类名

GetClassName(hwnd, dwWindowClass, 100)

If dwWindowClass.ToString.Contains("EDIT") Or dwWindowClass.ToString.Contains("Edit") Then     ' 类名包含EDIT的为编辑框

EditHandle.Add(hwnd)                        ' 存储该句柄

End If

' 返回 True 一直枚举完

Return True

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

WindowHandle = FindWindow(vbNullString, "登陆")

If WindowHandle.ToInt32 = 0 Then

MsgBox("未捕获到窗口" + "登陆")

Return

End If

' 枚举所有主窗口的子窗口(控件),枚举时自动调用回调函数,完成编辑框句柄的获取

EnumChildWindows(WindowHandle, AddressOf EnumChildProcC, 0)

' 寻找复选框

CheckHandle = FindWindowEx(WindowHandle, IntPtr.Zero, vbNullString, "记住密码")

Dim str As New StringBuilder

Dim j As Integer = 0

' 对编辑框文本赋值

For j = 0 To EditHandle.Count - 1

SendMessage(EditHandle(j), WM_SETTEXT, 0, "Text")

'GetWindowText(EditHandle(j), str, 20)

'EditWindowsText.Add(Str.ToString)

'Str.Clear()

Next

If EditHandle.Count = 0 Then

MsgBox("未找到输入框!")

End If        

If CheckHandle.ToInt32  0 Then

'CheckDlgButton(WindowHandle, id, 1)

' 对复选框进行鼠标单击操作

SendMessage(CheckHandle, BM_CLICK, 0, 0)

'SendMessage(CheckHandle, BM_SETCHECK, True, 0)

End If

End Sub

End Class

VB里面怎么用简单的办法枚举父窗口下所有的子窗口句柄

Private Sub Command1_Click()

Dim h

Do

h = FindWindowEx(Me.hWnd, h, vbNullString, vbNullString)

If h 0 Then

List1.AddItem h

End If

Loop Until h = 0

End Sub

比如这样,就是枚举了当前窗体的所有子窗体(控件),如果需要获得其他窗体的子窗体句柄,把Me.hwnd改成父窗体句柄就行了


当前名称:vb点虐 枚举句柄 vb枚举类型enum用法
本文地址:http://cdkjz.cn/article/ddeipgj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220