Board logo

標題: [發問] 在自定表單中 如何 請輸密碼時 show **** 代替 文字數字 謝謝! [打印本頁]

作者: user999    時間: 2011-12-2 12:38     標題: 在自定表單中 如何 請輸密碼時 show **** 代替 文字數字 謝謝!

自定表單
strPassword = InputBox("請輸入密碼:",  "訊息視窗")
作者: Hsieh    時間: 2011-12-3 21:43

本帖最後由 Hsieh 於 2011-12-3 21:57 編輯

回復 1# user999


    http://blog.xuite.net/crdotlin/excel/13554651
'一般模組 Module1 程式碼
  1. Option Explicit

  2. 'API宣告
  3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
  4.             (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  5. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
  6.             (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  7. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  8.             (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  9. 'timeSetEvent函數請參考:http://msdn2.microsoft.com/en-us/library/ms713423.aspx
  10. Private Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, _
  11.             ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
  12. Private Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
  13. Private Const EM_SETPASSWORDCHAR = &HCC

  14. Dim lTimeID     As Long          'Timer ID
  15. Const pswdInputBoxTitle = "pswdInputBox"     '輸入密碼的對話框標題

  16.   
  17. 'TimeProc callback 函數請參考:http://msdn2.microsoft.com/en-us/library/ms713420.aspx
  18. Sub TimeProc(ByVal uID As Long, ByVal uMsg As Long, ByVal dwUser As Long, _
  19.     ByVal dw1 As Long, ByVal dw2 As Long)
  20.     Dim hwd As Long         '輸入密碼的對話框句柄
  21.     'VBA InputBox對話框之Class Name是 "#32770",
  22.     '標題為 "pswdInputBox", 這是在InputBox函數的Title引述中自訂的
  23.     '請注意Application.InputBox方法所出現的對話框Class Name是 "bosa_sdm_XL9"
  24.     hwd = FindWindow("#32770", pswdInputBoxTitle)
  25.     If hwd <> 0 Then        '若對話框存在
  26.         '取得輸入的文字框句柄, 該文字框的Class Name是"Edit", 無標題,
  27.         '而Application.InputBox方法所出現的對話框之文字框的Class Name是"EDTBX"
  28.         hwd = FindWindowEx(hwd, 0, "Edit", vbNullString)
  29.         '設定密碼字元為 "*", "*"的ASCII碼為42
  30.         SendMessage hwd, EM_SETPASSWORDCHAR, 42, 0
  31.         '設定完成, 取消計時器
  32.         timeKillEvent lTimeID
  33.     End If
  34. End Sub


  35. '自定義函數pswdInputBox, 是一個輸入密碼使用的InputBox, 輸入的內容都以 "*" 顯示.
  36. Function pswdInputBox() As Variant
  37.     '啟動一個特定的Timer事件, 0.01秒延遲, 0.05秒看一次
  38.     lTimeID = timeSetEvent(10, 50, AddressOf TimeProc, 1, 1)
  39.     '顯示InputBox對話框
  40.     pswdInputBox = InputBox(Prompt:="請輸入編輯密碼", Title:=pswdInputBoxTitle)
  41. End Function
複製代碼
'表單模組Userform1程式碼
表單上設置一個標籤Label1、及1個按鈕CommandButton1
[attach]8706[/attach]
  1. Private Sub CommandButton1_Click()
  2. Label1.Caption = ""
  3.     If pswdInputBox <> 123456 Then
  4.         '密碼不正確, 顯示訊息
  5.         Label1.Caption = "對不起,密碼錯誤,你無編輯權限!"
  6.         Else
  7.         Label1.Caption = "歡迎使用本系統"
  8.     End If
  9. End Sub
複製代碼

作者: user999    時間: 2011-12-5 08:34

回復 2# Hsieh


    非常謝謝您! 看起來好複雜,我會好好學習,測試好用,真感謝.




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)