ID Number: Q71457
1.00
WINDOWS
Summary:
In the password style for a control, all characters display as an
asterisk (*) as they are typed into the control. Microsoft Visual
Basic does not have any intrinsic function for creating an edit
control with the password style. You can add the password style to a
Visual Basic text box control by using several Windows API function
calls.
This information applies to Microsoft Visual Basic Programming System
version 1.0 for Windows.
More Information:
Adding the password style to a text box control can be performed by
calling several Windows API function calls. These functions are
GetFocus, GetWindowLong, SetWindowLong, and SendMessage.
The function GetFocus requires no parameters. This function returns an
integer value that represents the handle to the control. Use GetFocus
to get a handle to the control that currently has the focus.
hWd% = GetFocus()
After you have received the handle to the text box control, you can
call GetWindowLong to retrieve the style flags for the control by
using the second parameter of GWL_STYLE. Once you have the style
flags, you can use the bit-wise OR operation to set the ES_PASSWORD
style.
StyleFlags& = GetWindowLong (hWd%, GWL_STYLE)
StyleFlags& = StyleFlags& OR ES_PASSWORD
After adding the password style to the existing style for the control,
you can call SetWindowLong to change the style associated to the
control. You would call SendMessage with the message parameter of
ES_SETPASSWORDCHAR to inform the control what character is to be used
as the password mask; the default character is an asterisk (*).
StyleFlags& = SetWindowLong (hWd%, GWL_STYLE, StyleFlags&)
PasswordMask% = Asc("*")
SendMessage (hWd%, ES_SETPASSWORDCHAR, PasswordMask%, 0&)
To create a text box with the password style within Visual Basic,
create a text box with the name Text1 on Form1.
Declare the following Windows API functions and CONST variables in the
Global section of your code:
Declare Function GetFocus Lib "User" () As Integer
Declare Function GetWindowLong Lib "User" (ByVal hWd%, ByVal nIndex%)
As Long
Declare Function SetWindowLong Lib "User" (ByVal hWd%, ByVal nIndex%,
ByVal dwNewLong&) As Long
Declare Function SendMessage Lib "User" (ByVal hWd%, ByVal wMsg%,
ByVal wParam%, ByVal lParam&) As Long
Global Const WM_USER = &H400
Global Const EM_SETPASSWORDCHAR = WM_USER + 28
Global Const ES_PASSWORD = &H20
Global Const GWL_STYLE = -16
Note: Each Declare statement above must be written on just one line.
Include the following code in the (general) section of the Form1
module:
Sub Make_Password_Control (Flag As Integer, PasswordMask As Integer)
Dim hWd As Integer ' Window Handle for the control
Dim StyleFlags As Long ' Window Style for the control
If Not Flag Then
' The control should have a STATIC flag to minimize the
' execution of the code. We don't want to perform this code
' all the time, just once.
Flag = Not Flag
hWd = GetFocus()
StyleFlags = GetWindowLong(hWd, GWL_STYLE)
StyleFlags = StyleFlags Or ES_PASSWORD
StyleFlags = SetWindowLong(hWd, GWL_STYLE, StyleFlags)
StyleFlags = SendMessage(hWd, EM_SETPASSWORDCHAR, PasswordMask, 0&)
End If
End Sub
Include the following code in the Text1_GotFocus event in the Form1
module:
Sub Text1_GotFocus ()
Static Already_Password As Integer
Make_Password_Control Already_Password, Asc("*")
End Sub
Reference:
1. "Programming Windows: the Microsoft Guide to Writing Applications
for Windows 3," by Charles Petzold (published by Microsoft
Press, 1990).
2. "Microsoft Windows 3.0 Software Development Kit: Reference Volume 1."
3. The WINSDK.HLP file shipped with Microsoft Windows 3.0 Software
Development Kit.