资源管理站

VBA input输入框输入密码不可见(*)

zyglz 4797 0

特别声明:本文为原创,可自由转载、引用,但需署名作者且注明文章出处,如有侵权请联系!

VBA input输入框输入密码不可见(*)

<!--more-->在使用VBA工程时,有时候需要让用户输入密码等内容,input输入框输入的时候,所有内容是可见状态的。通过以下代码,可以实现input输入框输入时为*,达到隐藏输入内容的目的。


20190914204311.png


Option Explicit
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
Public Declare Function GetTickCount Lib "kernel32" () As Long
Public Const EM_SETPASSWORDCHAR = &HCC
Public lTimeID As Long
Sub TimeProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
    Dim hwd As Long
    hwd = FindWindow("#32770", "密码")
    If hwd <> 0 Then
        hwd = FindWindowEx(hwd, 0, "edit", vbNullString)
        SendMessage hwd, EM_SETPASSWORDCHAR, 42, 0
        timeKillEvent lTimeID
    End If
  End Sub
Sub Password()
    Dim Password As Variant
    lTimeID = timeSetEvent(10, 0, AddressOf TimeProc, 1, 1)
    Password = InputBox("请输入密码:", "密码")
    If Password = "ghc516" Then
        MsgBox "密码正确!"
        '执行主程序代码
        Application.Visible = True
    Else
        MsgBox "密码错误!"
    End If
End Sub

评论列表 (已有0条评论)

消灭零回复

发表评论 (已有0条评论)

icon_lol.gif2016zhh.gif2016fendou.gif2016lengh.gificon_exclaim.gif2016gg.gif2016yhh.gificon_cry.gif2016bs.gif2016qd.gif2016bz.gificon_eek.gif2016ka.gif2016zhem.gificon_confused.gif2016qq.gif2016db.gif2016jk.gif2016tuu.gif2016zk.gif2016kk.gificon_neutral.gif