The Masked Edit control provides restricted data input as well as formatted data output. This control supplies visual cues about the type of data being entered or displayed.
The following example demonstrates how to create a registration-free instance of the Microsoft Masked Edit Control using my OLE Container (OLECON.INC) to host it.
Registration-free means that you don't need to register the control to be able to use it. To use this registration-free version, you must copy msmask32.ocx in the application folder, as if it was an standard DLL.
For using a registered version of the control, change the following code in the example:
LOCAL cp AS OC_CREATEPARAMS
cp.clsid = $CLSID_MaskEdBox
cp.riid = $IID_IMSMask
cp.szLicKey = $RTLKEY_MaskEdBox
cp.szLibName = EXE.Path$ & "MSMASK32.OCX"
hMaskedEd = CreateWindowEx(0, $OC_CLASSNAME, "", _
%WS_CHILD OR %WS_VISIBLE OR %WS_BORDER OR %WS_TABSTOP, _
0, 0, 0, 0, CB.HNDL, %IDC_MASKEDIT, GetModuleHandle(BYVAL %NULL), cp)
to:
hCtl = CreateWindowEx(0, $OC_CLASSNAME, _
"MSMask.MaskEdBox.1;RTLKEY:" & $RTLKEY_MaskEdBox, _
%WS_CHILD OR %WS_VISIBLE OR %WS_TABSTOP, 0, 0, 0, 0, _
CB.HNDL, %IDC_MASKEDIT, GetModuleHandle(BYVAL %NULL), BYVAL %NULL)
Full example code (DDT version)
' ########################################################################################
' Microsoft Masked Edit Control Demo
' ########################################################################################
#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "OLECON.INC"
#INCLUDE ONCE "MSMASK.INC"
%IDC_MASKEDIT = 101
' ========================================================================================
' Main
' ========================================================================================
FUNCTION WINMAIN (BYVAL hInstance AS DWORD, BYVAL hPrevInstance AS DWORD, BYVAL lpszCmdLine AS ASCIIZ PTR, BYVAL nCmdShow AS LONG) AS LONG
LOCAL hDlg AS DWORD
' Required: Initialize the Ole Container
OC_WinInit
DIALOG NEW 0, "Microsoft Masked Edit", , , 200, 44, %WS_OVERLAPPED OR %WS_THICKFRAME OR %WS_SYSMENU OR _
%WS_MINIMIZEBOX OR %WS_MAXIMIZEBOX OR %WS_VISIBLE OR %DS_CENTER TO hDlg
' For icon from resource, instead use something like, LoadIcon(hInst, "APPICON")
DIALOG SEND hDlg, %WM_SETICON, %ICON_SMALL, LoadIcon(%NULL, BYVAL %IDI_APPLICATION)
DIALOG SEND hDlg, %WM_SETICON, %ICON_BIG, LoadIcon(%NULL, BYVAL %IDI_APPLICATION)
' We need to forward the messages to the control for keyboard handling
' and for that we need a message pump, so the dialog must be modeless.
DIALOG SHOW MODELESS hDlg, CALL DlgProc
' Message handler loop
LOCAL uMsg AS tagMsg
WHILE GetMessage(uMsg, %NULL, 0, 0)
IF ISFALSE OC_ForwardMessage(GetFocus, uMsg) THEN
IF IsDialogMessage(hDlg, uMsg) = 0 THEN
TranslateMessage uMsg
DispatchMessage uMsg
END IF
END IF
WEND
END FUNCTION
' ========================================================================================
' ========================================================================================
' Main Dialog procedure
' ========================================================================================
CALLBACK FUNCTION DlgProc() AS LONG
LOCAL hr AS LONG ' // HRESULT
LOCAL rc AS RECT ' // RECT structure
LOCAL hMaskedEd AS DWORD ' // Masked edit control handle
LOCAL pMaskedEd AS IMSMask ' // Masked edit control reference pointer
STATIC pMaskedEvents AS MaskEdBoxEventsImpl ' // Masked edit events class reference
SELECT CASE CB.MSG
CASE %WM_INITDIALOG
' Create a registration-free instance of the MSMask control
LOCAL cp AS OC_CREATEPARAMS
cp.clsid = $CLSID_MaskEdBox
cp.riid = $IID_IMSMask
cp.szLicKey = $RTLKEY_MaskEdBox
cp.szLibName = EXE.Path$ & "MSMASK32.OCX"
hMaskedEd = CreateWindowEx(0, $OC_CLASSNAME, "", _
%WS_CHILD OR %WS_VISIBLE OR %WS_BORDER OR %WS_TABSTOP, _
0, 0, 0, 0, CB.HNDL, %IDC_MASKEDIT, GetModuleHandle(BYVAL %NULL), cp)
SetFocus hMaskedEd
' Get a reference to the mask edit control and set the properties
pMaskedEd = OC_GetDispatch(hMaskedEd)
IF ISOBJECT(pMaskedEd) THEN
' Set the fore and back colors
pMaskedEd.ForeColor = %BLUE
pMaskedEd.BackColor = RGB(255, 250, 250)
' Set the mask
pMaskedEd.Mask = UCODE$("(###) - ### - ####")
' Set the font
LOCAL pFont AS IDispatch
hr = OleCreateFontDisp("Verdana", 8, %FW_BOLD, %ANSI_CHARSET, 0, 0, 0, pFont)
IF ISOBJECT(pFont) THEN
pMaskedEd.putref_Font = pFont
pFont = NOTHING
END IF
' Connect to the events fired by the control
pMaskedEvents = CLASS "CMaskEdBoxEvents"
EVENTS FROM pMaskedEd CALL pMaskedEvents
pMaskedEd = NOTHING
END IF
CASE %WM_SIZE
' Resize the control
IF CB.WPARAM <> %SIZE_MINIMIZED THEN
GetClientRect CB.HNDL, rc
MoveWindow GetDlgItem(CB.HNDL, %IDC_MASKEDIT), 10, 10, (rc.nRight - rc.nLeft) - 20, 20, %TRUE
END IF
CASE %WM_COMMAND
SELECT CASE CB.CTL
CASE %IDOK
IF CB.CTLMSG = %BN_CLICKED THEN
END IF
CASE %IDCANCEL
IF CB.CTLMSG = %BN_CLICKED THEN DIALOG END CB.HNDL, 0
END SELECT
CASE %WM_SYSCOMMAND
' Capture this message and send a %WM_CLOSE message,
' or the program will remain in memory
IF (CB.WPARAM AND &HFFF0) = %SC_CLOSE THEN
DIALOG SEND CB.HNDL, %WM_CLOSE, 0, 0
END IF
CASE %WM_DESTROY
IF ISOBJECT(pMaskedEvents) THEN EVENTS END pMaskedEvents
PostQuitMessage 0
END SELECT
END FUNCTION
' ========================================================================================
' ########################################################################################
' Class CMaskEdBoxEvents
' Interface name = MaskEdBoxEvents
' IID = {C932BA87-4374-101B-A56C-00AA003668DC}
' Events for Masked Edit Control
' Attributes = 4112 [&H1010] [Hidden] [Dispatchable]
' ########################################################################################
CLASS CMaskEdBoxEvents GUID$("{1AE68F0C-DD3F-438F-825E-F15C9B2ABFC8}") AS EVENT
INTERFACE MaskEdBoxEventsImpl GUID$("{C932BA87-4374-101B-A56C-00AA003668DC}") AS EVENT
INHERIT IDispatch
' =====================================================================================
METHOD Change <1>
' *** Insert your code here ***
OutputDebugString FUNCNAME$
END METHOD
' =====================================================================================
' =====================================================================================
METHOD ValidationError <2> ( _
BYREF InvalidText AS STRING _ ' [1] *InvalidText /* *VT_BSTR */
, BYREF StartPosition AS INTEGER _ ' [1] *StartPosition /* *VT_I2 <Integer> */
) ' VOID
' *** Insert your code here ***
OutputDebugString FUNCNAME$
OutputDebugString "Invalid text: " & ACODE$(InvalidText)
OutputDebugString "Start position: " & STR$(StartPosition)
END METHOD
' =====================================================================================
' =====================================================================================
METHOD KeyDown <-602> ( _
BYVAL KeyCode AS INTEGER _ ' [0] KeyCode /* VT_I2 <Integer> */
, BYVAL iShift AS INTEGER _ ' [0] Shift /* VT_I2 <Integer> */
) ' VOID
' *** Insert your code here ***
OutputDebugString FUNCNAME$
END METHOD
' =====================================================================================
' =====================================================================================
METHOD KeyPress <-603> ( _
BYREF KeyAscii AS INTEGER _ ' [1] *KeyAscii /* *VT_I2 <Integer> */
) ' VOID
' *** Insert your code here ***
OutputDebugString FUNCNAME$
END METHOD
' =====================================================================================
' =====================================================================================
METHOD KeyUp <-604> ( _
BYVAL KeyCode AS INTEGER _ ' [0] KeyCode /* VT_I2 <Integer> */
, BYVAL iShift AS INTEGER _ ' [0] Shift /* VT_I2 <Integer> */
) ' VOID
' *** Insert your code here ***
OutputDebugString FUNCNAME$
END METHOD
' =====================================================================================
' =====================================================================================
METHOD OLEStartDrag <1550> ( _
BYREF pData AS IVBDataObject _ ' [2] [in][out] **Data /* **DataObject <coclass> */
, BYREF AllowedEffects AS LONG _ ' [1] [in][out] *AllowedEffects /* *VT_I4 <Long> */
) ' VOID
' *** Insert your code here ***
OutputDebugString FUNCNAME$
END METHOD
' =====================================================================================
' =====================================================================================
METHOD OLEGiveFeedback <1551> ( _
BYREF Effect AS LONG _ ' [1] [in][out] *Effect /* *VT_I4 <Long> */
, BYREF DefaultCursors AS INTEGER _ ' [1] [in][out] *DefaultCursors /* *VT_BOOL <Integer> */
) ' VOID
' *** Insert your code here ***
OutputDebugString FUNCNAME$
END METHOD
' =====================================================================================
' =====================================================================================
METHOD OLESetData <1552> ( _
BYREF pData AS IVBDataObject _ ' [2] [in][out] **Data /* **DataObject <coclass> */
, BYREF DataFormat AS INTEGER _ ' [1] [in][out] *DataFormat /* *VT_I2 <Integer> */
) ' VOID
' *** Insert your code here ***
OutputDebugString FUNCNAME$
END METHOD
' =====================================================================================
' =====================================================================================
METHOD OLECompleteDrag <1553> ( _
BYREF Effect AS LONG _ ' [1] [in][out] *Effect /* *VT_I4 <Long> */
) ' VOID
' *** Insert your code here ***
OutputDebugString FUNCNAME$
END METHOD
' =====================================================================================
' =====================================================================================
METHOD OLEDragOver <1554> ( _
BYREF pData AS IVBDataObject _ ' [2] [in][out] **Data /* **DataObject <coclass> */
, BYREF Effect AS LONG _ ' [1] [in][out] *Effect /* *VT_I4 <Long> */
, BYREF iButton AS INTEGER _ ' [1] [in][out] *Button /* *VT_I2 <Integer> */
, BYREF iShift AS INTEGER _ ' [1] [in][out] *Shift /* *VT_I2 <Integer> */
, BYREF X AS SINGLE _ ' [1] [in][out] *X /* *VT_R4 <Single> */
, BYREF Y AS SINGLE _ ' [1] [in][out] *Y /* *VT_R4 <Single> */
, BYREF iState AS INTEGER _ ' [1] [in][out] *State /* *VT_I2 <Integer> */
) ' VOID
' *** Insert your code here ***
OutputDebugString FUNCNAME$
END METHOD
' =====================================================================================
' =====================================================================================
METHOD OLEDragDrop <1555> ( _
BYREF pData AS IVBDataObject _ ' [2] [in][out] **Data /* **DataObject <coclass> */
, BYREF Effect AS LONG _ ' [1] [in][out] *Effect /* *VT_I4 <Long> */
, BYREF iButton AS INTEGER _ ' [1] [in][out] *Button /* *VT_I2 <Integer> */
, BYREF iShift AS INTEGER _ ' [1] [in][out] *Shift /* *VT_I2 <Integer> */
, BYREF X AS SINGLE _ ' [1] [in][out] *X /* *VT_R4 <Single> */
, BYREF Y AS SINGLE _ ' [1] [in][out] *Y /* *VT_R4 <Single> */
) ' VOID
' *** Insert your code here ***
OutputDebugString FUNCNAME$
END METHOD
' =====================================================================================
END INTERFACE
END CLASS
' ========================================================================================