Use of MFC Advanced Control RichEdit2.0



The use of MFC advanced control RichEdit


RichEdit control, you can set different lines and paragraphs in the edit box to have different fonts and colors.
The effect is as follows:

insert image description here

1. Add a dialog box, set as follows (the upper box in the figure below is the RichEdit2.0 control, and the lower box is the edit box):

insert image description here

2. Set the associated member variables (control types) for the two controls in the above figure:

insert image description here

insert image description here

3. Add dialog virtual function OnInitDialog in the class wizard

BOOL CMFCApplication1Dlg::OnInitDialog()
{
    
    
	
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	DWORD dwOldStyle = m_redit.GetExStyle();	//默认包含透明属性
	m_redit.ModifyStyleEx(dwOldStyle, WS_EX_CLIENTEDGE);	//去除透明属性
	//m_redit.SetBackgroundColor(TRUE, GetSysColor(COLOR_3DFACE));
	m_redit.SetBackgroundColor(FALSE, RGB(30, 30, 30));

	CHARFORMAT2 cf2;
	//memset(&cf2, 0, sizeof(CHARFORMAT2));
	cf2.cbSize = sizeof(CHARFORMAT2);
	cf2.dwMask = CFM_FACE | CFM_SIZE | CFM_COLOR;
	_tcscpy_s(cf2.szFaceName, _countof(cf2.szFaceName), _T("隶书"));
	cf2.yHeight = 320;
	cf2.crTextColor = 255;	设置默认字体颜色
	//cf2.crTextColor = RGB(0, 255, 0);	//也可以用RGB宏
	cf2.dwEffects = CFE_AUTOCOLOR & 0xBFFFFFFF;	//缺省为CFE_AUTOCOLOR 0x40000000
	
	m_redit.SetDefaultCharFormat(cf2);

	m_redit.SetWindowTextW(_T("---欢迎使用本软件---\r\n"));
	m_redit.SetSel(-1, -1);
	m_redit.ReplaceSel(_T("祝你聊天愉快\r\n"));


	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

4. Write the send button processing function

void CMFCApplication1Dlg::OnBnClickedOk()
{
    
    
	//CRichEditCtrl使用大全http://www.cppblog.com/kyelin/archive/2010/11/03/132300.aspx
	UpdateData(TRUE);
	CString str;
	COleDateTime time = COleDateTime::GetCurrentTime();
	str.Format(_T("梦飞龙卷风<[email protected]>   %d:%d:%d\r\n"), time.GetHour(), time.GetMinute(), time.GetSecond());

	CHARFORMAT2 cf2;
	cf2.cbSize = sizeof(CHARFORMAT2);
	cf2.dwMask = CFM_FACE | CFM_SIZE | CFM_COLOR;
	cf2.crTextColor = RGB(210, 115, 25);
	cf2.yHeight = 160;
	cf2.dwEffects = 0;
	_tcscpy_s(cf2.szFaceName, _countof(cf2.szFaceName), _T("宋体"));

	m_redit.SetSel(-1, -1);
	//m_redit.SetSel(m_redit.GetTextLength(), -1);
	//m_redit.SetDefaultCharFormat(cf2);
	m_redit.SetSelectionCharFormat(cf2);
	
	m_redit.ReplaceSel(str);

	GetDlgItemText(IDC_EDT_INPUT, str);
	cf2.dwMask |= CFM_UNDERLINE | CFM_BOLD | CFM_ITALIC;
	cf2.dwEffects |= CFE_UNDERLINE | CFE_BOLD | CFE_ITALIC;
	cf2.dwEffects &= 0xBFFFFFFF;
	_tcscpy_s(cf2.szFaceName, _countof(cf2.szFaceName), _T("楷体"));
	cf2.yHeight = 250;
	cf2.crTextColor = RGB(190, 160, 250);
	m_redit.SetSel(-1, -1);
	//m_redit.SetSel(m_redit.GetTextLength(), -1);
	m_redit.SetSelectionCharFormat(cf2);

	
	m_redit.ReplaceSel(str + _T("\r\n"));

	//cf2.dwEffects &= ~CFE_UNDERLINE;

	m_edit.SetSel(0, -1);
	m_edit.Clear();

	UpdateData(FALSE);
	
	m_edit.SetFocus();
	/*CWnd* pWnd = GetDlgItem(IDC_EDT_INPUT);
	HWND hWnd = pWnd->GetSafeHwnd();
	::SetFocus(hWnd);*/
}

The MFC control sets the focus to clear the content of the edit box after the carriage return, and the focus stays in the edit box


  1. Sets the tab order of dialog controls. Press Ctrl+D to see it. Set the tab order of the control that needs to be focused to 1.

  2. Code setting (the content of the edit box is cleared after the carriage return, and the focus stays in the edit box):

	m_edit.SetSel(0, -1);
	m_edit.Clear();

	UpdateData(FALSE);
	
	m_edit.SetFocus();
  1. Other code setting methods
	//方式一
	myedit1.SetFocus();	//设置焦点
    //myedit1是控件变量名
	
	//方式而
    CWnd* pWnd = GetDlgItem(IDC_EDIT1);//设置焦点
    pWnd->SetFocus();

	//方式三
    CWnd* pWnd2 = GetDlgItem(IDC_EDIT2);
    HWND hWnd = pWnd2->GetSafeHwnd();
    ::SetFocus(hWnd);  //设置焦点
    //全局函数
    //返回值:若函数调用成功,则返回原先拥有键盘焦点的窗口句柄。若hWnd参数无效或窗口未与调用线程的消息队列相关,则返回值为NULL
  1. Other references
    Dialog's OnInitDialog returns FALSE. The explanation is as follows: (excerpt to msdn)

If OnInitDialog returns nonzero, Windows sets the input focus to the first control in the dialog box.
The application can return 0 only if it has explicitly set the input focus to one of the controls in the dialog box.


  1. It is more in line with our input habits to use the Enter key instead of the TAB key to transfer the focus. The implementation in MFC is roughly as follows :

1. Create a dialog-based MFC application;

2. Add several edit boxes and controls in the dialog box, hold down the ctrl+D keys to set the focus order (for example, if you want the edit box IDC_EDIT1 to be the first focus, you only need to click the edit box first, and then Just click in sequence);

3. Set mutilline and want return in the attributes of all edit boxes to true;

4. Overload the PreTranslateMessage function in the dlg class, as shown below:

BOOL ** Dlg::PreTranslateMessage(MSG pMsg) // transfer focus
{ // TODO: add private code here and/or call base class

if((pMsg->message == WM_KEYDOWN) && (VK_RETURN == (int) pMsg->wParam))
{
    if(GetFocus()->GetDlgCtrlID() != IDOK)
    {
        pMsg->wParam = VK_TAB;
    }
}

return CDialogEx::PreTranslateMessage(pMsg);

}

Note: 1. IDOK is the default button ID in the dialog box. If the default button in the dialog box has been changed, write the ID number of the corresponding default button; 2.
This method can realize the sequence between the edit boxes To switch, when encountering the first button control (generally set as the default button), pressing the Enter key is equivalent to executing the function of clicking the button with the mouse.
————————————————
Copyright statement: This article is an original article of CSDN blogger "hswangjiajia", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprinting .
Original link: https://blog.csdn.net/hswangjiajia/article/details/12979379

Guess you like

Origin blog.csdn.net/zhaopeng01zp/article/details/128560052
Recommended