Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I am facing a problem of hiding cursor in Drop Down style combo box.
I use visual studio 2008, MFC application, CDialog, CComboBox class to develop that program.
Most of the links suggest to use Drop List style combo box.
But I don't want to use Drop List because of my other reasons.

So, could you please guide me how to hide/disable text cursor in Drop Down style combo box?
Thanks in advanced.
Posted

1 solution

As a best solution, you need to subclass edit control of (drop down) combo box.

To do this:
First, create a MFC class named CCaretlessCBEdit. Select CEdit as its base class.
class CCaretlessCBEdit : public CEdit
{
    ...
};


Then, put lines below into its class definition in the header file to handle the events WM_SETFOCUS and EM_SETSEL.

afx_msg void OnSetFocus(CWnd* pOldWnd);<br />
afx_msg LRESULT OnSetSel(WPARAM wParam, LPARAM lParam);


Then, add following lines to implementation file with message map entries.

BEGIN_MESSAGE_MAP(CCaretlessCBEdit, CEdit)
    ON_WM_SETFOCUS()
    ON_MESSAGE(EM_SETSEL, OnSetSel)
END_MESSAGE_MAP()

// CCaretlessCBEdit message handlers

LRESULT CCaretlessCBEdit::OnSetSel(WPARAM wParam, LPARAM lParam)
{
    LRESULT lRet = DefWindowProc(EM_SETSEL, wParam, lParam);
    HideCaret();
    return lRet;
}

void CCaretlessCBEdit::OnSetFocus(CWnd* pOldWnd)
{
    CEdit::OnSetFocus(pOldWnd);
    HideCaret();
}


Then add following lines inside OnInitDialog() function in your dialog that hosts combo box. Beware of IDC_YOURCOMBO1, replace it with your own.

BOOL CYourDlg::OnInitDialog()
{
    ...

    CDialog::OnInitDialog();

    CWnd* pWnd = GetDlgItem(IDC_YOURCOMBO1)->GetWindow(GW_CHILD);
    if(pWnd->GetSafeHwnd())
        m_ctrlCBEdit.SubclassWindow(pWnd->GetSafeHwnd());

    ...
}


Lastly, put following line somewhere within your dialog class definition.

CCaretlessCBEdit m_ctrlCBEdit;

Dont' forget to include new control's header file in your dialog's header file.

#include "CaretlessCBEdit.h"

PS. As a shorter solution, while handling the set focus notification of combo within dialog class without subclassing the edit control. After the control gets EM_SETSEL message when getting focus for the first time, caret would be still there.
 
Share this answer
 
Comments
April2004 4-Mar-11 20:36pm    
Thank you very much for your solution.
It works well and my problem is solved by your solution.^^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900