VC 编辑框 改变背景、字体、文本颜色、长度限制

来源:互联网 发布:泰牛程序员倒闭 编辑:程序博客网 时间:2024/05/15 05:45

原文地址::http://blog.csdn.net/shen_001/article/details/5655956


1。长度限制

OnInitDialog()中:

m_edit1.SetLimitText(8);                           //m_edit1为编辑框的成员变量

或者

CEdit*pEdt=(CEdit*)GetDlgItem(IDC_EDIT1);

pEdt->SetLimitText(8);                              //限制编辑框输入长度为8字节

说明:在使用多字节字符集且不使用XP风格时,SetLimitText设置的是最大字节数;在使用Unicode字符集或使用XP风格时,SetLimitText设置的是最大字符数。

2。汉字判断

方法一、

[cpp] view plaincopy
  1. CString str="ab你c好。。";  
  2. for(int i=0;i<str.GetLength();i++)  
  3. {  
  4.   
  5. if(   (BYTE)str[i]   <   0x80 )  
  6. {    
  7.       MessageBox("非汉字");  
  8. }        
  9.   
  10. else//汉字    
  11. {    
  12.     MessageBox("是汉字");  
  13. }                                     
                                  //方法不好,只能判断有没有汉字

方法二、

[cpp] view plaincopy
  1. CString   ss="dd你aa今bb真cc";  
  2.   
  3. int i=0;  
  4. while(i<ss.GetLength())  
  5. {  
  6.     if(IsDBCSLeadByte(ss[i])) //   是DBCS  
  7.     {        
  8.        i += 2;  
  9.   
  10.        AfxMessageBox("汉字");  
  11.   
  12.      }  
  13.      else//   英文  
  14.   
  15.     {       
  16.   
  17.       i ++;    
  18.       AfxMessageBox("English");  
  19.   
  20.      }  
  21.    }  

 

3。字体及大小

定义一全局变量或成员变量CFont   font;                                        //不要定义成局部变量,否则没效果

CEdit*pEdt=(CEdit*)GetDlgItem(IDC_EDIT1);

font.CreatePointFont(266,"Arial");

pEdt->SetFont(&font);

4。背景及文本颜色

定义一成员变量CBrush m_brush;

OnInitDialog()中进行初始化工作m_brush.CreateSolidBrush(RGB(255,0,0));

然后在OnCtrlColor中

 

[c-sharp] view plaincopy
  1. HBRUSH C***Dlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)  
  2.   
  3. {  
  4.   
  5. HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
  6.   
  7. if(nCtlColor==CTLCOLOR_EDIT &&  
  8.   
  9.                            pWnd->GetDlgCtrlID()==IDC_EDIT1)//注意此处的(pWnd->),否则没效果  
  10.   
  11. {  
  12.   
  13. pDC->SetTextColor(RGB(255,0,0));  
  14.   
  15. pDC->SetBkColor(RGB(255,255,0));//设置文本背景色  
  16.   
  17. pDC->SetBkMode(TRANSPARENT);//设置背景透明  
  18.   
  19. hbr = (HBRUSH)m_brush;  
  20.   
  21. }  
  22.   
  23. return hbr;  
  24.   
  25. }  

 

对于nCtlColor的类型,如下:

CTLCOLOR_BTN   Button control

CTLCOLOR_DLG   Dialog box

CTLCOLOR_EDIT   Edit control

CTLCOLOR_LISTBOX   List-box control

CTLCOLOR_MSGBOX   Message box

CTLCOLOR_SCROLLBAR   Scroll-bar control

CTLCOLOR_STATIC   Static control

 

派生Edit子类,实现字符限制输入!!!

void CEditChar::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)

 if ((nChar>='A'&&nChar<='Z')||(nChar>='a'&&nChar<='z'))
 {
  CEdit::OnChar(nChar, nRepCnt, nFlags);
 } 
}


0 0
原创粉丝点击