Chapter 16: Text-Based Controls Visual C++ and MFC Fundamentals
If an edit box is editable and the user starts typing in it, the new characters would display.
As the user is typing, the caret moves from left to right (for US English). The user can
also move the caret back and forth using the Backspace, Delete, Home, End, or the arrow
keys. At any time you can find out the current position of the caret by calling the
CWnd::GetCaretPos() method. Its syntax is:
static CPoint PASCAL GetCaretPos( );
This method retrieves the left (x) and bottom (y) coordinates of the caret in the control
that called it. These coordinates represent the member variables of a CPoint that this
method returns. The measures are relative to the control and not the control’s parent.
Here is an example:
void CDialogCaret::CaretPosition(void)
{
CEdit *edtBookTitle;
CStatic *stcCaretPos;
CPoint CrtPos;
char Msg[40];
edtBookTitle = reinterpret_cast<CEdit *>(GetDlgItem(IDC_BOOK_TITLE));
stcCaretPos = reinterpret_cast<CStatic *>(GetDlgItem(IDC_CARET_POS));
CrtPos = edtBookTitle->GetCaretPos();
sprintf(Msg, "Caret Position: (%d, %d)", CrtPos.x, CrtPos.y);
stcCaretPos->SetWindowText(Msg);
}
void CDialogCaret::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CaretPosition();
CDialog::OnLButtonDown(nFlags, point);
}
If want to hide the characters that display in an edit box, you can set the Password
property to True. To do this programmatically, add the ES_PASSWORD style to the edit
control. This style makes the edit control displays each character, or changes each one of
its characters into an asterisk. If you prefer another symbol for the password style, call
the CEdit::SetPassword() method. Its syntax is:
void SetPasswordChar(TCHAR ch);
This method takes as argument the character that you want to use.