Password-based inputbox

Source: Internet
Author: User
For a VB Program When the program needs to interact with the user (for example, enter the user name or some specific data), inputbox will certainly become the preferred method. indeed, the convenience of inputbox is that it cannot be compared with other languages: In the SDK mode, we must write a window by ourselves, then apply for memory, and manually release the memory after use.
There are no perfect people in the world, and there is no perfect thing. inputbox is not suitable for entering passwords, which is a pity. To solve this problem, we have to add a form, add controls, and add Code Although a series of operations are not very complex, they are also annoying-after all, this is just a very simple problem, but it is always a bit of a taste.

So much nonsense, let's talk about the principle: It's very simple. Just set the input box on inputbox to a password character, but it seems that this step is not easy, because the input box on inputbox is not easy for us to operate-a common input box can be set in "attribute. it seems nonsense ^ _ ^;

There are still mysteries in the world. inputbox is just a standard window in windows, and it cannot escape the control of windows. windows knows everything about her children, creates, activates, and destroys none of her eyes. here we are working on it. We have invited the famous system hook wh_cbt (wh_cbt debuted in a burst of applause and the crazy call of countless mm, 'Thank you! Thank you for your support. ') who is wh_cbt? Detailed information is provided on msdn.

Wh_cbt is a cool and competent person. In less than two minutes, it makes everyone feel the charm of him. The voices of MM make me feel excited. What did he do? How can he do it? The following is our exclusive interview. If you want to repost it, please indicate the source

  1. 'The following code is saved in the module
  2. '================================================ =
  3. Option explicit
  4. 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
  5. Private declare function setwindowshookex lib "USER32" alias "setwindowshookexa" (byval idhook as long, byval lpfn as long, byval hmod as long, byval dwthreadid as long) as long
  6. Private declare function unhookwindowshookex lib "USER32" (byval hhook as long) as long
  7. Private declare function find1_wex lib "USER32" alias "find1_wexa" (byval hwnd1 as long, byval hwnd2 as long, byval lpsz1 as string, byval lpsz2 as string) as long
  8. Private const em_setpasswordchar = & HCC
  9. Private const wh_cbt = 5
  10. Private const hcbt_activate = 5
  11. Private hhook as long
  12. Private function hookproc (byval ncode as long, byval wparam as long, byval lparam as long) as long
  13. If ncode = hcbt_activate then
  14. Dim hedit as long
  15. Hedit = find1_wex (wparam, 0, "edit", vbnullstring)
  16. Sendmessage hedit, em_setpasswordchar, ASC ("*"), byval 0 &
  17. Unhookwindowshookex hhook
  18. End if
  19. End Function
  20. Public Function myinputbox (prompt as string, optional title as string, optional default as string) as string
  21. Hhook = setwindowshookex (wh_cbt, addressof hookproc, app. hinstance, app. threadid)
  22. Myinputbox = inputbox (prompt, title, default)
  23. End Function
  24. '================================================ ====
  25. 'Call the sample code in the form
  26. '================================================ ====
  27. Private sub commandementclick ()
  28. Dim password as string
  29. Password = myinputbox ("enter the password", "Log on ")
  30. If Password = "1234567890" then
  31. Debug. Print "correct password"
  32. Else
  33. Debug. Print "Incorrect password"
  34. End if
  35. End sub

Aside from the declaration, the only thing we do is line 13-24. In other words, we only use 12 lines of code to implement the inputbox we want.
Ghost dragon dancePM 04-10-28

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.