Visual C++ and MFC Fundamentals Chapter 17: Track-Based Controls
The above caret appears gray. If you want the caret to be completely black, you can call
the CWnd::CreateSolidCaret() method. Its syntax is:
void CreateSolidCaret( int nWidth, int nHeight );
This method creates a rectangular caret of nWidth x nHeight dimensions. Here is an
example:
BOOL CSolidCaretDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_Username.CreateSolidCaret(5, 15);
m_Username.ShowCaret();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
To provide a better designed caret, you can call the CWnd::CreateCaret() method. Its
syntax is:
void CreateCaret(CBitmap* pBitmap);
Before calling this method, you can first design a bitmap, load it, and then pass it as the
pBitmap argument. After initializing and loading the bitmap, to display it in the edit box,
call the CWnd::ShowCaret() method. Here is an example:
BOOL CDialogCaret::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CEdit *edtBookTitle;
CBitmap *pBitmap = new CBitmap;
edtBookTitle = reinterpret_cast<CEdit *>(GetDlgItem(IDC_BOOK_TITLE));
pBitmap->LoadBitmap(IDB_BMP_CARET);
edtBookTitle->CreateCaret(pBitmap);
edtBookTitle->ShowCaret();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}