这个功能实现起来其实也很简单,就是通过反射去读取 DescriptionAttribute 的 Description 属性的值,代码如下所示:
创新互联公司主营青海网站建设的网络公司,主营网站建设方案,重庆App定制开发,青海h5小程序定制开发搭建,青海网站营销推广欢迎青海等地区企业咨询
/// 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,不用进行额外的反射。
当然,这段代码仅是一个简单的示例,接下来会进行更详细的分析。
1.定义一个模块, 内容为:
Option Explicit
Public Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function FindWindowa Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
2. 给窗口加一个按钮, 在按钮的click事件里写以下内容:
Option Explicit
Private Sub Command1_Click()
Dim hwnd As Long
hwnd = GetForegroundWindow ' FindWindowa("Notepad", "新建 文本文档.txt - 记事本")
Dim str1 As String, len1 As Long
str1 = Space(255) '定义接收字串.
GetWindowText hwnd, str1, 1024
Do While hwnd 0
hwnd = GetNextWindow(hwnd, 2) '只有2才表示找下一个窗口
len1 = GetWindowText(hwnd, str1, Len(str1))
If (InStr(1, str1, "记事", 1) 0) Then
MsgBox "你要的窗口找到了, 它是:" + str1
Exit Sub '这一句看情况修改
End If
Loop
MsgBox "很遣憾, 没有你要找的窗口"
End Sub
3. 测试, 一定会通过.
If App.PrevInstance = True Then
End
End If
如果程序正在运行,结束程序。
在模块中加入每个窗口的标题变量。
然后用if then 来判断是否有相同窗口。
如果你事先不知道有哪些窗口的话,那你就用枚举 FindWindow来查找子窗口句柄。再用SendMessage 获得窗口标题再进行判断。
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 遍历窗口所有子窗体句柄
Private Const GW_CHILD = 5
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) 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 Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Sub FillChild(hWndParent As Long)
Dim hWndChild As Long
Dim szCaption As String
Dim buffer As String
Dim i As Long
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)
szCaption = String$(255, 0)
GetClassName hWndChild, szCaption, 250
szCaption = Left$(szCaption, InStr(szCaption, String$(1, 0)) - 1)
buffer = CStr(hWndChild) "--" szCaption
i = GetWindowTextLength(hWndChild)
szCaption = String$(255, 0)
GetWindowText hWndChild, szCaption, 250
szCaption = Left$(szCaption, i)
buffer = buffer "--" szCaption
List1.AddItem buffer
FillChild hWndChild
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
Wend
End Sub
Private Sub GetChildWindow(hwnd As Long)
Dim szCaption As String
Dim buffer As String
Dim i As Long
List1.Clear
szCaption = String$(255, 0)
GetClassName hwnd, szCaption, 250
szCaption = Left$(szCaption, InStr(szCaption, String$(1, 0)) - 1)
buffer = CStr(hwnd)
buffer = buffer "--" szCaption
i = GetWindowTextLength(hwnd)
szCaption = String$(255, 0)
GetWindowText hwnd, szCaption, 250
szCaption = Left$(szCaption, i)
buffer = buffer "--" szCaption
List1.AddItem buffer
FillChild hwnd
End Sub
调用方法
GetChildWindow hwnd'hwnd是指定的窗口句柄
结果以
窗体句柄--窗体类名称--窗体Text
形式列在列表框List1中