如何在下拉样式组合框中隐藏文本光标? [英] How can I hide text cursor in drop down style combo box?
问题描述
我遇到了在下拉式"组合框中隐藏光标的问题.
我使用Visual Studio 2008,MFC应用程序,CDialog,CComboBox类来开发该程序.
大多数链接建议使用放置列表"样式组合框.
但由于其他原因,我不想使用删除列表".
因此,能否请您指导我如何在下拉样式组合框中隐藏/禁用文本光标?
在高级中表示感谢.
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.
推荐答案
作为最佳解决方案,您需要对(下拉)组合框的编辑控件进行子类化.
为此:
首先,创建一个名为CCaretlessCBEdit的MFC类.选择CEdit作为其基类.
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
{
...
};
然后,在头文件的类定义中放入以下行,以处理事件WM_SETFOCUS和EM_SETSEL.
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();
}
然后在承载组合框的对话框中的OnInitDialog()函数内添加以下行.注意IDC_YOURCOMBO1,请用自己的IDC替换.
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());
...
}
最后,将以下行放在对话框类定义中的某处.
CCaretlessCBEdit m_ctrlCBEdit;
不要忘记在对话框的头文件中包含新控件的头文件.
#include "CaretlessCBEdit.h"
PS.作为一种较短的解决方案,在处理对话框类中组合的设置焦点通知时,无需将编辑控件归类.控件在首次获取焦点时获得EM_SETSEL消息后,插入符号仍会存在.
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.
这篇关于如何在下拉样式组合框中隐藏文本光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!