c – CRichEditCtrl在获得焦点时选择所有文本

我有一个菜单和CTabCtrl的对话框. CTabCtrl有一个选项卡,其中包含一个CDialog.反过来,它包含一些静态文本和一个CRichEditCtrl.窗口获得和失去焦点没有特别的问题.

我添加了第二个相同的选项卡,现在每次更改选项卡时,显然选择了CRichEditCtrl中的所有文本.它以反转颜色方案显示,如果您按下某个键,则会替换所有文本.

标志ECO_NOHIDESEL的说明(强调我的):

Negates the default behavior for an edit control. The default behavior
hides the selection when the control loses the input focus and shows
the selection when the control receives the input focus.
If you
specify ECO_NOHIDESEL, the selected text is inverted, even if the
control does not have the focus.

“显示选择”对我来说听起来像“显示这个控件最后一次选择的焦点”,这不是正在发生的事情.通常在焦点丢失之前没有选择任何内容,但是如果我尝试留下选择,返回到另一个选项卡并返回,则选择整个文本.

可以预防这种选择吗?

void EditorDialog :: OnTabSelChange(NMHDR * phdr,LRESULT * pResult){

  CTabCtrl* ptab = (CTabCtrl*) GetDlgItem( IDC_TAB );

  int iPageActive = ptab->GetCurSel();

  if ( iPageActive >= appage.N() ) {
      AKS( AKSWarn, "got tab change to tab %d when I only have %d ppages", iPageActive, appage.N() );
      return;
  }

  ppageActive = appage[ iPageActive ];

  SetActivePagePos();

  SCWinUtilSetWindowTextVA( this, "Editor: %s", ppageActive->pszFileName );
}



void EditorDialog::SetActivePagePos() {

  // STEP 1: Make the proper tab page visible.

  for ( int i = 0; i < appage.N(); i++ )
      appage[i]->ShowWindow( SW_HIDE );
  ppageActive->ShowWindow( SW_SHOW );

  // STEP 2: Make the new tab page the right size and position.

  CTabCtrl* ptab = (CTabCtrl*) GetDlgItem( IDC_TAB );

  CRect rectTab, rectItem;

  ptab->GetClientRect( &rectTab );
  ptab->GetItemRect( 0, &rectItem );

  int iPageX = rectItem.left   + 2;
  int iPageY = rectItem.bottom + 4;
  int iPageW = rectTab.right   - 2 - iPageX;
  int iPageH = rectTab.bottom  - 2 - iPageY;

  ppageActive->SetWindowPos( &wndTop, iPageX, iPageY, iPageW, iPageH, SWP_SHOWWINDOW | SWP_NOZORDER );

  // STEP 3: Give the window focus and let it know to redraw.

  ppageActive->SetFocus();

  // When the tab changes the entire content of the RichEdit is selected for some reason.
  // As a workaround I manually clear the selection.
  CRichEditCtrl* prich = (CRichEditCtrl*) ppageActive->GetDlgItem( IDC_PATCH );
  prich->SetSel(-1,-1);

  // Redrawing just the prich, or the ppageActive, or the ptab, doesn't
  // cause the RichEdit to redraw correctly, but Redrawing the entire dialog does.
  RedrawWindow();
}

解决方法:

Edit和RichEdit控件的默认行为是在获得焦点时选择整个内容. ES_NOHIDESEL不会修改此行为,只是指示控件始终显示其选择,即使它没有输入焦点.

要更改RichEdit控件的默认行为以保留其选择,您必须从中派生并提供自定义OnGetDlgCode实现:

UINT RichEditSelectionPreserving::OnGetDlgCode() {
    // Call the base class implementation
    UINT code = CRichEditCtrl::OnGetDlgCode();
    // And mask out the undesired feature
    code = code & ~DLGC_HASSETSEL;
    return code;
}
上一篇:java – 用另一种颜色在jtable中选择行的颜色


下一篇:javascript – 如何在ng-select选项中添加点击监听器?