Help Center

CMaskedEdit.Constructorsconstructor

Creates an instance of the class and the control.

WindowsconstructorCMaskedEdit.incdoc-orphan
No implementation located. This member is documented, but the source scan found no matching declaration. It is likely declared in a header this scan does not resolve, or provided by a macro.

Syntax

CONSTRUCTOR CMaskedEdit
CONSTRUCTOR CMaskedEdit (BYVAL pWindow AS CWindow PTR, BYVAL cID AS LONG_PTR, BYVAL x AS LONG = 0, BYVAL y AS LONG = 0, BYVAL nWidth AS LONG = 0, BYVAL nHeight AS LONG = 0, BYVAL dwStyle AS DWORD = -1, BYVAL dwExStyle AS DWORD = -1)

Parameters

NameDescription
[Constructors](#constructors1)Creates an instance of the class and the control.
[AfxCMaskedEditPtr](#afxcmaskededitptr)Returns a pointer to the CMaskedEdit class.
[Create](#create)Creates an instance of the control
[EnableMask](#enablemask)Initializes the masked edit control.
[GetText](#gettext)Retrieves validated text from the masked edit control.
[GetTextLength](#gettextlength)Retrieves the length of the text.
[hWindow](#hWindow)Gets the control window handle.
[SetPos](#setpos)Sets the position of the cursor.
[SetValidChars](#setvalidchars)Specifies a string of valid characters that the user can enter.
[SetText](#settext)Displays a prompt in the masked edit control.
pWindowPointer to the parent CWindowclass.
cIDThe control identifier, an integer value used to notify its parent about events. The application determines the control identifier; it must be unique for all controls with the same parent window.
XThe x-coordinate of the upper-left corner of the window relative to the upper-left corner of the parent window's client area.
yThe initial y-coordinate of the upper-left corner of the window relative to the upper-left corner of the parent window's client area.
nWidthThe width of the control.
nHeightThe height of the control.
dwStyleThe window styles of the control being created.
Default styles: WS_VISIBLE OR WS_TABSTOP OR ES_LEFT OR ES_AUTOHSCROLL.
dwExStyleThe extended window styles of the control being created.
Default extended style: WS_EX_CLIENTEDGE

Remarks

Perform the following steps to use the CMaskedEdit control in your application:

  1. Embed a CMaskedEdit object into your window class.
  1. Call the EnableMask method to specify the mask.
  1. Call the SetValidChars method to specify the list of valid characters.
  1. Call the SetText method to specify the default text for the masked edit control.
  1. Call the GetText method to retrieve the validated text.

Examples

DIM pMaskedEdit AS CMaskedEdit = CMaskedEdit(@pWindow, IDC_MASKED, 10, 30, 280, 23) pMaskedEdit.EnableMask(" ddd ddd dddd", "(___) ___-____", "_") pMaskedEdit.SetText("(123) 123-1212"), TRUE

' ######################################################################################## ' Microsoft Windows ' Contents: CWindow - Masked Edit Control ' Compiler: FreeBasic 32 & 64 bit ' Copyright (c) 2025 José Roca. Freeware. Use at your own risk. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER ' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF ' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. ' ########################################################################################

#define UNICODE #INCLUDE ONCE "AfxNova/CWindow.inc" #INCLUDE ONCE "AfxNova/CMaskedEdit.inc" USING AfxNova

DECLARE FUNCTION wWinMain (BYVAL hInstance AS HINSTANCE, _

BYVAL hPrevInstance AS HINSTANCE, _
                       BYVAL pwszCmdLine AS WSTRING PTR, _
                       BYVAL nCmdShow AS LONG) AS LONG

END wWinMain(GetModuleHandleW(NULL), NULL, WCOMMAND(), SW_NORMAL)

' // Forward declaration DECLARE FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

CONST IDC_MASKED = 1001

' ======================================================================================== ' Main ' ======================================================================================== FUNCTION wWinMain (BYVAL hInstance AS HINSTANCE, _

BYVAL hPrevInstance AS HINSTANCE, _
               BYVAL pwszCmdLine AS WSTRING PTR, _
               BYVAL nCmdShow AS LONG) AS LONG

' // Set process DPI aware ' // The recommended way is to use a manifest file SetProcessDpiAwareness(PROCESS_SYSTEM_DPI_AWARE) ' // Enable visual styles without including a manifest file AfxEnableVisualStyles

' // Creates the main window DIM pWindow AS CWindow ' -or- DIM pWindow AS CWindow = "MyClassName" (use the name that you wish) pWindow.Create(NULL, "Masked edit control", @WndProc) ' // Sizes it by setting the wanted width and height of its client area pWindow.SetClientSize(300, 150) ' // Centers the window pWindow.Center

' // Add a masked edit control DIM pMaskedEdit AS CMaskedEdit = CMaskedEdit(@pWindow, IDC_MASKED, 50, 30, 200, 23) SetFocus pMaskedEdit.hWindow pMaskedEdit.EnableMask(" ddd ddd dddd", "(___) ___-____", "_") pMaskedEdit.SetText("(123) 123-1212"), TRUE

' // Adds a button pWindow.AddControl("Button", , IDCANCEL, "&Close", 190, 100, 75, 23)

' // Anchor controls pWindow.AnchorControl(IDC_MASKED, AFX_ANCHOR_WIDTH) pWindow.AnchorControl(IDCANCEL, AFX_ANCHOR_BOTTOM_RIGHT)

' // Displays the window and dispatches the Windows messages FUNCTION = pWindow.DoEvents(nCmdShow)

END FUNCTION ' ========================================================================================

' ======================================================================================== ' Main window procedure ' ======================================================================================== FUNCTION WndProc (BYVAL hwnd AS HWND, BYVAL uMsg AS UINT, BYVAL wParam AS WPARAM, BYVAL lParam AS LPARAM) AS LRESULT

SELECT CASE uMsg

CASE WM_CREATE
     EXIT FUNCTION

  CASE WM_COMMAND
     SELECT CASE CBCTL(wParam, lParam)
        CASE IDCANCEL
           ' // If ESC key pressed, close the application by sending an WM_CLOSE message
           IF GET_WM_COMMAND_CMD(wParam, lParam) = BN_CLICKED THEN
              SendMessageW hwnd, WM_CLOSE, 0, 0
              EXIT FUNCTION
           END IF
        CASE IDC_MASKED
           IF CBCTLMSG(wPAram, lParam) = EN_CHANGE THEN
              DIM hEdit AS HWND = GetDlgItem(hwnd, LOWORD(wParam))
              DIM pMaskedEdit AS CMaskedEdit PTR = AfxCMaskedEditPtr(hEdit)
              IF pMaskedEdit THEN
                 DIM dwsText AS DWSTRING
                 ' // Get the text including the mask
                 dwsText = pMaskedEdit->GetText(FALSE)
                 ' // Get the text excluding the mask
                 dwsText = pMaskedEdit->GetText(TRUE)
              END IF
           END IF
     END SELECT

CASE WM_DESTROY

' // Ends the application by sending a WM_QUIT message
     PostQuitMessage(0)
     EXIT FUNCTION

END SELECT

' // Default processing of Windows messages FUNCTION = DefWindowProcW(hwnd, uMsg, wParam, lParam)

END FUNCTION ' ========================================================================================

The following example demonstrates how to set up a mask (for example a phone number) by using the EnableMask method to create the mask for the masked edit control, and SetTextmethod to display a prompt in the masked edit control.

Reference

  • Include file CMaskedEdit.inc
  • Documented in Windows/Windows GUI/CMaskedEdit Class.md
  • Topic: CMaskedEdit Class