VC编辑框 怎么统计输入汉字的个数 1.这个文本框输入,别一个文本框显示:要响应第一个文本框的内容变动消息,当输入后,获取它的长度,汉字是unicode,将这人结果显示到另一个文本框 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 本帖最后由 VisualEleven 于 2014-05-20 10:46:35 编辑 Multi-Byte Characterint GetWordCount(CString sContent){ int nCount = 0; byte btChar; for(int i=0; i<sContent.GetLength(); i++) { btChar = sContent.GetAt(i); nCount++; if(::IsDBCSLeadByte(btChar)) { i += 1; } } return nCount; } 限制用户输入汉字个数的做法是不明智的,不如使用下面的函数:void SetLimitText(UINT nMax); 没有想到现在这么多条回复。现在我是这样的,修改三条语句,测试出来符合我的要求。两个字母或者数字算一个汉字。 m_Edit1.SetLimitText(20); //设置输入汉字个数 一个汉字为两个计数单位 str_tmp.Format(_T("已经输入%d个字,还可以输入%d个字"), dw_len/2,10-dw_len/2); SetDlgItemText(IDC_STATIC, str_tmp);//dw_len/2,10-dw_len/2一个汉字为两个计数单位// 520Dlg.cpp : implementation file//#include "stdafx.h"#include "520.h"#include "520Dlg.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/*************************************/class CAboutDlg : public CDialog{public: CAboutDlg();// Dialog Data//{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX };//}}AFX_DATA// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support//}}AFX_VIRTUAL// Implementationprotected://{{AFX_MSG(CAboutDlg)//}}AFX_MSG DECLARE_MESSAGE_MAP()};/*************************************/CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD){//{{AFX_DATA_INIT(CAboutDlg)//}}AFX_DATA_INIT}/*************************************/void CAboutDlg::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CAboutDlg)//}}AFX_DATA_MAP}/*************************************/BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)//{{AFX_MSG_MAP(CAboutDlg)// No message handlers//}}AFX_MSG_MAPEND_MESSAGE_MAP()/*************************************/CMy520Dlg::CMy520Dlg(CWnd* pParent /*=NULL*/) : CDialog(CMy520Dlg::IDD, pParent){//{{AFX_DATA_INIT(CMy520Dlg) m_inttext = _T("");//}}AFX_DATA_INIT// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}/*************************************/void CMy520Dlg::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CMy520Dlg) DDX_Control(pDX, IDC_EDIT2, m_count); DDX_Control(pDX, IDC_EDIT1, m_Edit1); DDX_Text(pDX, IDC_EDIT1, m_inttext);//}}AFX_DATA_MAP}/*************************************/BEGIN_MESSAGE_MAP(CMy520Dlg, CDialog)//{{AFX_MSG_MAP(CMy520Dlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_CLEARFILE, OnClearfile) ON_BN_CLICKED(IDC_CLEARCOUNT, OnClearcount) ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)//}}AFX_MSG_MAPEND_MESSAGE_MAP()/*************************************/BOOL CMy520Dlg::OnInitDialog(){ CDialog::OnInitDialog(); ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon m_Edit1.SetLimitText(20); //设置输入汉字个数 一个汉字为两个计数单位 return TRUE; // return TRUE unless you set the focus to a control}/*************************************/void CMy520Dlg::OnSysCommand(UINT nID, LPARAM lParam){ if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); }}/*************************************/void CMy520Dlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); }}/*************************************/HCURSOR CMy520Dlg::OnQueryDragIcon(){ return (HCURSOR) m_hIcon;}/*************************************/void CMy520Dlg::OnClearfile() { // TODO: Add your control notification handler code here m_Edit1.SetSel(0,-1); //选上所有内容 m_Edit1.ReplaceSel(""); //清除编辑框输入文字内容 }/*************************************/void CMy520Dlg::OnClearcount() { // TODO: Add your control notification handler code here m_count.SetSel(0,-1); //选上所有内容 m_count.ReplaceSel(""); //清除编辑框计数器内容 }/*************************************/void CMy520Dlg::OnChangeEdit1() { // TODO: Add your control notification handler code here CString str_tmp; GetDlgItemText(IDC_EDIT1, str_tmp); dw_len=str_tmp.GetLength(); str_tmp.Format(_T("已经输入%d个字,还可以输入%d个字"), dw_len/2,10-dw_len/2); SetDlgItemText(IDC_STATIC, str_tmp);//dw_len/2,10-dw_len/2一个汉字为两个计数单位 m_count.SetSel(0,-1); //测试代码选上所有内容 m_count.ReplaceSel(""); //测试代码显示编辑框2内容 char sPos[10]; //测试代码 itoa(dw_len/2,sPos,10); //测试代码 m_count.ReplaceSel(sPos); //测试代码 } 好吧,你用CTRL+V试一下,怕有问题吧哥也不要你骂人了哥发个例子你参考专门针对你的问题要求写的有想法了再说回头你觉得可以骂了就主动点http://download.csdn.net/detail/gz_qmc/7375651 VC6.0安装Platform SDK for WinXP的问题 基于对话框的MFC中,directdraw如何在窗口模式中加裁减??? 使用线程遇到的问题,请高手帮忙解决!!! 弱弱的问一声:_T函数是什么意思 怎么用FLASH做透明窗口? 为何我的串口程序最多只能接受到8个字符? 使用setpixel以后如何擦除所画的像素图形? hpp文件是什么? ★关于WSASend的问题。大家帮看看我设计的[发送线程]的缺陷在什么地方,标准的应该怎样写,WSASend后怎样妥善处理?内用详细源码。 请问:wsock32.dll和ws2_32.dll之间有什么区别和联系 用VC2012编译,error C4430出现如下编译错误,怎么也解决不了?求助大侠帮助。 当客户端发消息没收到服务端返回后出错
int GetWordCount(CString sContent)
{
int nCount = 0;
byte btChar; for(int i=0; i<sContent.GetLength(); i++)
{
btChar = sContent.GetAt(i);
nCount++; if(::IsDBCSLeadByte(btChar))
{
i += 1;
}
} return nCount;
}
void SetLimitText(UINT nMax);
现在我是这样的,修改三条语句,测试出来符合我的要求。
两个字母或者数字算一个汉字。 m_Edit1.SetLimitText(20); //设置输入汉字个数 一个汉字为两个计数单位
str_tmp.Format(_T("已经输入%d个字,还可以输入%d个字"), dw_len/2,10-dw_len/2);
SetDlgItemText(IDC_STATIC, str_tmp);//dw_len/2,10-dw_len/2一个汉字为两个计数单位
// 520Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "520.h"
#include "520Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/*************************************/
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/*************************************/
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
/*************************************/
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
/*************************************/
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/*************************************/
CMy520Dlg::CMy520Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy520Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMy520Dlg)
m_inttext = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
/*************************************/
void CMy520Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy520Dlg)
DDX_Control(pDX, IDC_EDIT2, m_count);
DDX_Control(pDX, IDC_EDIT1, m_Edit1);
DDX_Text(pDX, IDC_EDIT1, m_inttext);
//}}AFX_DATA_MAP
}
/*************************************/
BEGIN_MESSAGE_MAP(CMy520Dlg, CDialog)
//{{AFX_MSG_MAP(CMy520Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CLEARFILE, OnClearfile)
ON_BN_CLICKED(IDC_CLEARCOUNT, OnClearcount)
ON_EN_CHANGE(IDC_EDIT1, OnChangeEdit1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/*************************************/
BOOL CMy520Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_Edit1.SetLimitText(20); //设置输入汉字个数 一个汉字为两个计数单位
return TRUE; // return TRUE unless you set the focus to a control
}
/*************************************/
void CMy520Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
/*************************************/
void CMy520Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
/*************************************/
HCURSOR CMy520Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/*************************************/
void CMy520Dlg::OnClearfile()
{
// TODO: Add your control notification handler code here
m_Edit1.SetSel(0,-1); //选上所有内容
m_Edit1.ReplaceSel(""); //清除编辑框输入文字内容
}
/*************************************/
void CMy520Dlg::OnClearcount()
{
// TODO: Add your control notification handler code here
m_count.SetSel(0,-1); //选上所有内容
m_count.ReplaceSel(""); //清除编辑框计数器内容
}
/*************************************/
void CMy520Dlg::OnChangeEdit1()
{
// TODO: Add your control notification handler code here
CString str_tmp;
GetDlgItemText(IDC_EDIT1, str_tmp);
dw_len=str_tmp.GetLength();
str_tmp.Format(_T("已经输入%d个字,还可以输入%d个字"), dw_len/2,10-dw_len/2);
SetDlgItemText(IDC_STATIC, str_tmp);//dw_len/2,10-dw_len/2一个汉字为两个计数单位
m_count.SetSel(0,-1); //测试代码选上所有内容
m_count.ReplaceSel(""); //测试代码显示编辑框2内容
char sPos[10]; //测试代码
itoa(dw_len/2,sPos,10); //测试代码
m_count.ReplaceSel(sPos); //测试代码
}
哥发个例子你参考专门针对你的问题要求写的
有想法了再说回头你觉得可以骂了就主动点
http://download.csdn.net/detail/gz_qmc/7375651