📄 form1.frm
字号:
VERSION 5.00
Begin VB.Form Form1
BorderStyle = 1 'Fixed Single
Caption = "发布与更新DHCP的IP地址"
ClientHeight = 2925
ClientLeft = 45
ClientTop = 330
ClientWidth = 6690
Icon = "Form1.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 2925
ScaleWidth = 6690
StartUpPosition = 2 '屏幕中心
Begin VB.CommandButton Command2
Caption = "更新IP"
Height = 495
Left = 3360
TabIndex = 3
Top = 2160
Width = 1215
End
Begin VB.CommandButton Command1
Caption = "发布IP"
Height = 495
Left = 2160
TabIndex = 2
Top = 2160
Width = 1215
End
Begin VB.ListBox List2
Height = 1320
Left = 1800
TabIndex = 1
Top = 600
Width = 4815
End
Begin VB.ListBox List1
Height = 1320
Left = 240
TabIndex = 0
Top = 600
Width = 1575
End
Begin VB.Label Label3
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "网卡名"
Height = 180
Left = 2880
TabIndex = 6
Top = 360
Width = 540
End
Begin VB.Label Label2
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "网卡IP"
Height = 180
Left = 1800
TabIndex = 5
Top = 360
Width = 540
End
Begin VB.Label Label1
AutoSize = -1 'True
BackStyle = 0 'Transparent
Caption = "网卡索引"
Height = 180
Left = 240
TabIndex = 4
Top = 360
Width = 720
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Command1_Click()
Dim ip_index As Long
ip_index = CLng(List1.List(List1.ListIndex))
'确保用户选择了一个网卡
If ip_index <> 0 Then
Screen.MousePointer = vbHourglass
'发布选择的网卡的IP地址
If IPRelease(ip_index) Then
'更新显示内容
DisplayAdatersInfo
End If
Screen.MousePointer = vbDefault
End If
End Sub
Private Sub Command2_Click()
Dim ip_index As Long
ip_index = CLng(List1.List(List1.ListIndex))
'确保用户选择了一个网卡
If ip_index <> 0 Then
Screen.MousePointer = vbHourglass
'更新选择的网卡的IP地址
If IPRenew(ip_index) Then
'更新显示内容
DisplayAdatersInfo
End If
Screen.MousePointer = vbDefault
End If
End Sub
Private Sub Form_Load()
ReDim TabArray(0 To 0) As Long
TabArray(0) = 61
'设置列表框制表位
Call SendMessage(List2.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
Call SendMessage(List2.hwnd, LB_SETTABSTOPS, 1&, TabArray(0))
List2.Refresh
'初始化套接字并装载网卡数据
SocketsInitialize
DisplayAdatersInfo
With Command1
.Enabled = List1.ListIndex > -1
End With
With Command2
.Enabled = List1.ListIndex > -1
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
SocketsCleanup
End Sub
Private Sub List1_Click()
Command1.Enabled = List1.ListIndex > -1
Command2.Enabled = List1.ListIndex > -1
End Sub
Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'用户在列表中进行了选择后,调整其它控件的显示信息
List2.ListIndex = List1.ListIndex
End Sub
Sub DisplayAdatersInfo()
'在两个列表框中显示网卡信息
Dim buff() As Byte
Dim cbRequired As Long
Dim Adapter As IP_ADAPTER_INFO
Dim ptr1 As Long
'初始化列表框的显示内容
List1.Clear
List2.Clear
'用0长度的缓存来调用函数GetInterfaceInfo,以获取实际需要的缓存大小
Call GetAdaptersInfo(ByVal 0&, cbRequired)
If cbRequired > 0 Then
ReDim buff(0 To cbRequired - 1) As Byte
If GetAdaptersInfo(buff(0), cbRequired) = ERROR_SUCCESS Then
ptr1 = VarPtr(buff(0))
'系统中不再有网卡时,则ptr1值为0
Do While (ptr1 <> 0)
'将第一个网卡的信息复制到IP_ADAPTER_INFO结构中
CopyMemory Adapter, ByVal ptr1, LenB(Adapter)
With Adapter
'发布与更新网卡的IP地址,我们只需要一个参数值,即Adapter.dwIndex,
'即显示在list1中的内容。而List2中的数据只是为了显示,与发布/更新
'网卡的IP地址的无关。
List1.AddItem .dwIndex
List2.AddItem TrimNull(StrConv(.IpAddressList.IpAddress.IpAddr, vbUnicode)) _
& vbTab & _
TrimNull(StrConv(.sDescription, vbUnicode))
ptr1 = .dwNext
End With 'With Adapter
Loop 'Do While (ptr1 <> 0)
End If
End If
End Sub
Private Function TrimNull(item As String)
Dim pos As Integer
pos = InStr(item, Chr$(0))
If pos Then
TrimNull = Left$(item, pos - 1)
Else: TrimNull = item
End If
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -