API CORNER

PASSWORD TEXTBOX

© 2003, Alyce Watson - Alyce's Restaurant

Home

Password Textbox API

Character Replacement

LB Isam Library

Beginning Games 2

Rubber Band Objects

WMLiberty Primer

LB Browser

Beginning Programming 5

INPUTTO Demo

Chase Button

Questionaire Wizard

MIDI Output Thoughts

MIDI-Tunes

Play MIDI DLL

Directory Search Function

Newsletter help

Index

As documented in [Mastering LB3]

Uses for Password Textbox

The Password Textbox shows asterisks as the user types to mask a password or other sensitive information.

password textbox

Creating a Textbox with CreateWindowExA

Windows and controls are created with the CreateWindowExA function. Before this function can be used, it is necessary to get a handle to the Liberty BASIC window that will contain the control. Use the HWND() function to retrieve the handle.

Once the handle has been retrieved, it is used to get an Instance Handle for the program. This is done with GetWindowLongA, using a flag of _GWL_HINSTANCE. The instance handle is returned by the function.

   calldll #user32, "GetWindowLongA", _
    hWindow as long, _GWL_HINSTANCE as long,_
    hInstance as long

The class parameter for CreateWindowExA is "EDIT." The style must be as follows, with the Windows constants put together with the "OR" operator. The style parameters tell Windows to make this textbox a "childwindow" of the program window, to make it "visible", to give it a "border" and most importantly, to make it a "password" styled box. Windows constants preceded by "WS" are Window Style flags. Constants preceded by "ES" are Edit Style flags.

    style = _WS_CHILDWINDOW OR _WS_BORDER _
    OR _WS_VISIBLE or _ES_PASSWORD

It is now possible to create the password textbox. The x and y parameters are used to specify the x,y location within the window, and the w and h parameters specify the width and height of the textbox:

     calldll #user32, "CreateWindowExA",_
        0 as long,"EDIT" as ptr,_
        "" as ptr, style as long,_
        x as long,y as long,w as long,h as long,_
        hWindow as long, 0 as long, hInstance as long,_
        0 as long, CreateTextboxPW as long

Textbox API Functions

Textboxes and other controls that are created with API calls must be managed with API calls. They do not recognize Liberty BASIC textbox commands. To set focus to a textbox:

    calldll #user32, "SetFocus", hTextbox as long,_
    result as long

It doesn't do much good to have a password textbox, if the program cannot retrieve the text typed by the user! This is very easy to do. It is best to know the length of the text string contained in the textbox, and this is obtained with GetWindowTextLengthA. The return from the function is the length of the text currently displayed in the texbox.

    calldll #user32, "GetWindowTextLengthA",_
    hTextbox as long,TextLength as long

Once the length is known, it is used to set up a buffer. The buffer is just a string variable that is initialized with blank spaces, then terminated with a null character. The null character is needed to tell Liberty BASIC to pass the address of the text, rather than a copy of the text. The function can then access the memory address and fill it with the text retrieved from the textbox. After the call to GetWindowTextA, the buffer contains the text from the textbox, and the return from the function is the length of the text placed in the buffer. The code should remove any extra spaces before using the variable. This can be done with the left$() function, using the return from GetWindowTextA for the length, or the buffer variable can be trimmed with the trim$() function.

    Title$=space$(TextLength)+Chr$(0)
	lenText= Len(Title$)

    calldll #user32, "GetWindowTextA", hTextbox as long,_
    Title$ as ptr, lenText as long, result as long
    password$=trim$(Title$)
	or
    password$=left$(Title$,result)

The password then needs to be evaluated by the program, but of course that is different for each program. Here is a working demo of the password textbox.


nomainwin

Menu #1, "&File","&Read", [readIt],_
        "E&xit", [quit]

open "Password Textbox" for window as #1
print #1, "trapclose [quit]"

hT=CreateTextboxPW(hwnd(#1),10,10,130,26)
call SetFocus hT
wait

[quit]
    close #1:end

[readIt]
    txt$=GetWindowText$(hT)
    notice txt$
    wait

sub SetFocus hWnd
    calldll #user32, "SetFocus", hWnd as long,_
    result as long
    end sub

function GetWindowText$(hWnd)
    total = GetWindowTextLength(hWnd)
    Title$=space$(total)+Chr$(0):l= Len(Title$)

    calldll #user32, "GetWindowTextA", hWnd as long,_
    Title$ as ptr, l as long, result as long
    GetWindowText$=trim$(Title$)
    end function

function GetWindowTextLength(hW)
    calldll #user32, "GetWindowTextLengthA",_
    hW as long,_
    GetWindowTextLength as long
    end function

Function CreateTextboxPW(hW, x, y, w, h)
    style = _WS_CHILDWINDOW OR _WS_BORDER _
    OR _WS_VISIBLE or _ES_PASSWORD
    hInst=GetWindowLong(hW, _GWL_HINSTANCE)

    calldll #user32, "CreateWindowExA",_
        0 as long,"EDIT" as ptr,_
        "" as ptr, style as long,_
        x as long,y as long,w as long,h as long,_
        hW as long, 0 as long, hInst as long,_
        0 as long, CreateTextboxPW as long
    end function

Function GetWindowLong(hW, type)
    calldll #user32, "GetWindowLongA", _
    hW as long, type as long,_
    GetWindowLong as long
    End Function


Home

Password Textbox API

Character Replacement

LB Isam Library

Beginning Games 2

Rubber Band Objects

WMLiberty Primer

LB Browser

Beginning Programming 5

INPUTTO Demo

Chase Button

Questionaire Wizard

MIDI Output Thoughts

MIDI-Tunes

Play MIDI DLL

Directory Search Function

Newsletter help

Index