当前位置:编程学习 > VB >>

软件启动界面检测是否有网络连接

form1,启动界面。

若检测到有网络连接,直接跳转到from2

若检测到无网络连接,跳转到from3

谢谢。 --------------------编程问答--------------------
Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long

· lpdwFlags
[out] Pointer to an unsigned long integer variable where the connection description should be returned. This can be a combination of the following values:

INTERNET_CONNECTION_CONFIGURED Local system has a valid connection to the Internet, but it may or may not be currently connected.
INTERNET_CONNECTION_LAN        Local system uses a local area network to connect to the Internet.
INTERNET_CONNECTION_MODEM      Local system uses a modem to connect to the Internet.
INTERNET_CONNECTION_MODEM_BUSY No longer used.
INTERNET_CONNECTION_OFFLINE    Local system is in offline mode.
INTERNET_CONNECTION_PROXY      Local system uses a proxy server to connect to the Internet.
INTERNET_RAS_INSTALLED         Local system has RAS installed.

· dwReserved
[in] Reserved. Must be set to zero.

Returns TRUE if there is an Internet connection, or FALSE otherwise.
--------------------编程问答-------------------- 不懂,wininet.dll 是什么? --------------------编程问答--------------------
引用 1 楼 of123 的回复:
Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Long, ByVal dwReserved As Long) As Long

· lpdwFlags
[out] Pointer to an unsigned long integer variable where the connection description should be returned. This can be a combination of the following values:

INTERNET_CONNECTION_CONFIGURED Local system has a valid connection to the Internet, but it may or may not be currently connected.
INTERNET_CONNECTION_LAN        Local system uses a local area network to connect to the Internet.
INTERNET_CONNECTION_MODEM      Local system uses a modem to connect to the Internet.
INTERNET_CONNECTION_MODEM_BUSY No longer used.
INTERNET_CONNECTION_OFFLINE    Local system is in offline mode.
INTERNET_CONNECTION_PROXY      Local system uses a proxy server to connect to the Internet.
INTERNET_RAS_INSTALLED         Local system has RAS installed.

· dwReserved
[in] Reserved. Must be set to zero.

Returns TRUE if there is an Internet connection, or FALSE otherwise.


这个api貌似不准 --------------------编程问答-------------------- 谁有更好的办法呢? --------------------编程问答-------------------- Private Sub InetLogin_StateChanged(ByVal State As Integer)    If State = icResponseCompleted Then        Dim StrIn As String                StrIn = InetLogin.GetChunk(0, icString)        If StrIn = "ok" Then            '登陆成功 您可在此添加 登陆成功后的动作me.hideform2.show            MsgBox "登陆成功,您可以在此添加登陆成功后的动作", vbInformation        Else            MsgBox StrIn, vbInformation, "登录失败"        End If    ElseIf State = icError Then        MsgBox "网络连接错误"    End IfEnd Sub


这个代码我加入后,最后一个 End IfEnd Sub提示错误,我不知道哪里修改?
我想换成若检测不到连接,跳转到form3 。 --------------------编程问答--------------------
Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long

Public Function isConnectInet()
    isConnectInet = (InternetCheckConnection("http://www.baidu.com", &H1, 0&) <> 0)
End Function

我用的这个方法检测网络是否连接的,挺准 --------------------编程问答--------------------
引用 6 楼 sysdzw 的回复:
Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long

Public Function isConnectInet()
    isConnectInet = (InternetCheckConnection("http://www.baidu.com", &H1, 0&) <> 0)
End Function

我用的这个方法检测网络是否连接的,挺准




我要的效果是 网络存在,立即跳转到FORM2,检测不到网络跳转到 FORM3 --------------------编程问答--------------------
引用 7 楼 zhail 的回复:
Quote: 引用 6 楼 sysdzw 的回复:

Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long

Public Function isConnectInet()
    isConnectInet = (InternetCheckConnection("http://www.baidu.com", &H1, 0&) <> 0)
End Function

我用的这个方法检测网络是否连接的,挺准




我要的效果是 网络存在,立即跳转到FORM2,检测不到网络跳转到 FORM3

那你就在定时器中检查,每隔1秒钟检查一次、 --------------------编程问答-------------------- 不会啊,求大师兄 --------------------编程问答--------------------

Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
 
Public Function isConnectInet()
    isConnectInet = (InternetCheckConnection("http://www.baidu.com", &H1, 0&) <> 0)
End Function

Private Sub Form_Load()
    If (InternetCheckConnection("http://www.baidu.com", &H1, 0&) <> 0) Then
        Form2.Show
    Else
        Form3.Show
    End If
End Sub

--------------------编程问答--------------------
引用 6 楼 sysdzw 的回复:
Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long

Public Function isConnectInet()
    isConnectInet = (InternetCheckConnection("http://www.baidu.com", &H1, 0&) <> 0)
End Function

我用的这个方法检测网络是否连接的,挺准




简单测试了一下,开搞timer不断检测,然后断开网络,qq,旺旺立刻就离线了,可函数依然提示true ....  --------------------编程问答--------------------
引用 10 楼 dxd0128 的回复:

Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
 
Public Function isConnectInet()
    isConnectInet = (InternetCheckConnection("http://www.baidu.com", &H1, 0&) <> 0)
End Function

Private Sub Form_Load()
    If (InternetCheckConnection("http://www.baidu.com", &H1, 0&) <> 0) Then
        Form2.Show
    Else
        Form3.Show
    End If
End Sub



这种效果是ok的,可是问题是如果无网络,启动软件会很慢,,这个可能解决掉? --------------------编程问答--------------------
引用 10 楼 dxd0128 的回复:

Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
 
Public Function isConnectInet()
    isConnectInet = (InternetCheckConnection("http://www.baidu.com", &H1, 0&) <> 0)
End Function

Private Sub Form_Load()
    If (InternetCheckConnection("http://www.baidu.com", &H1, 0&) <> 0) Then
        Form2.Show
    Else
        Form3.Show
    End If
End Sub




怎么跳转到form2,或者form3的时候自动关闭form1 --------------------编程问答--------------------
Private Declare Function InternetCheckConnection Lib "wininet.dll" Alias "InternetCheckConnectionA" (ByVal lpszUrl As String, ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
 
Public Function isConnectInet()
    isConnectInet = (InternetCheckConnection("http://www.baidu.com", &H1, 0&) <> 0)
End Function

Private Sub Form_Load()
    If (InternetCheckConnection("http://www.baidu.com", &H1, 0&) <> 0) Then
        Unload Me
        Form2.Show
    Else
        Unload Me
        Form3.Show
    End If
End Sub
--------------------编程问答-------------------- 获得本地IP,是127.0.0.1则没有连接。
补充:VB ,  基础类
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,