設定 InputBox 輸入為密碼字元

設定 InputBox 輸入為密碼字元

如何設定 InputBox 為密碼字元 ( 字變 *) / InputBox Set Password Char

' 表單內

Private Sub Command1_Click()

' 使用計時器

' &H5000& 為 NV_INPUTBOX

' 10 為 Time out ( Interval ) , 單位 Milliseconds ( 1/1000 秒 )

SetTimer hwnd, &H5000&, 10, AddressOf TimerProc ' 建立(設定)計時器

MsgBox InputBox("請輸入密碼 :", " 詢問 ")

End Sub

' 模組內 ( API 宣告及 Timer 事件程序 )

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _

(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private 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 SetTimer Lib "user32" _

(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

Private Declare Function KillTimer Lib "user32" _

(ByVal hwnd As Long, ByVal nIDEvent As Long) As Long

Private 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 Sub TimerProc(ByVal hwnd&, ByVal uMsg&, ByVal idEvent&, ByVal dwTime&)

' 根據 InputBox Title 及 Class 使用 FindWindow 尋找 InputBox 的 Handle 值

' Title 此處設的是 "詢問" ( 可自行調整 , 但注意須與 InputBox 的 Title 一樣)

' FindWindow API 函式只能尋找上層 ( Top-Level window ; Parent ) 的 Handle 值

' 再透過 FindWindowEx API 函式尋找 InputBox 中輸入字串處之 Handle 值

' 依 FindWindow 找出的 Parent Handle 及 Class 去尋找 Child Window 的 Handle

' InputBox 中輸入字串之元件 Class 為 "Edit"

' FindWindow 與 FindWindowEx 差別在於 FindWindowEx 可找 Child Window

' 使用 SendMessage API 函式,傳送 &HCC 設定 PasswordChar 的訊息給 InputBox 內之輸入框

' 指定 Password Char 為 * ( 可設定其他字元 )

SendMessage FindWindowEx(FindWindow("#32770", "詢問"), 0, "Edit", ""), &HCC, Asc("*"), 0

KillTimer hwnd, idEvent ' 刪除(停止)計時器

End Sub