﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-wangliu2009</title><link>http://www.cppblog.com/wangliu2009/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 09 Jun 2026 17:42:51 GMT</lastBuildDate><pubDate>Tue, 09 Jun 2026 17:42:51 GMT</pubDate><ttl>60</ttl><item><title>MFC 编辑控件 字符串长度</title><link>http://www.cppblog.com/wangliu2009/archive/2012/04/21/172281.html</link><dc:creator>把酒劝雨雨不住</dc:creator><author>把酒劝雨雨不住</author><pubDate>Sat, 21 Apr 2012 12:55:00 GMT</pubDate><guid>http://www.cppblog.com/wangliu2009/archive/2012/04/21/172281.html</guid><wfw:comment>http://www.cppblog.com/wangliu2009/comments/172281.html</wfw:comment><comments>http://www.cppblog.com/wangliu2009/archive/2012/04/21/172281.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/wangliu2009/comments/commentRss/172281.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/wangliu2009/services/trackbacks/172281.html</trackback:ping><description><![CDATA[<span style="font-family: simsun; line-height: 23px; text-align: left; ">急问：我用CEdit的LimitText()来限制用户输入的长度，比如LimitText(10)，我期望用户只能输入十个字节，如果是英文就是10个字母，中文就是5个汉字；但是我发现当用户输入中文时，仍然可以输入十个汉字，也就是20个字节了，请问有什么办法可以无论用户输入的是什么字符，都限制用户输入的字节数吗？<br /><br /></span><span style="font-family: simsun; line-height: 23px; text-align: left; ">你自己在EN_CHANGE消息中判断吧，用strlen判断的是字节数。<br /><br /></span><span style="font-family: simsun; line-height: 23px; text-align: left; ">需要使用MBCS字符集 &nbsp; ATL+F7 &nbsp; -&gt; &nbsp; C/C++ &nbsp; -&gt; 去掉_UNICODE、UNICODE並增加_MBCS<br /><br /></span><span style="font-family: simsun; line-height: 23px; text-align: left; ">看错了 &nbsp; 原来你用的是LimitText &nbsp; 我看成SetLimitText了<br />这两个函数有区别：前者限制字符数后者才是限制字节数<br />要达到你的要求可以用MBCS同时使用&nbsp;</span><br style="font-family: simsun; line-height: 23px; text-align: left; " /><span style="font-family: simsun; line-height: 23px; text-align: left; ">((CEdit*)GetDialogItem(IDC_EDIT))-&gt; SetLimitText(10);&nbsp;</span><br style="font-family: simsun; line-height: 23px; text-align: left; " /><span style="font-family: simsun; line-height: 23px; text-align: left; ">用UNICODE肯定在两种情况下都无法达到你的需求<br />因为UNICODE中任何字符占的字节数都一样<br />这样你((CEdit*)GetDialogItem(IDC_EDIT))-&gt; SetLimitText(10);以后将只能输入5个中/英文字符。<br /></span><div><br />http://topic.csdn.net/t/20060413/01/4682886.html</div><span style="font-family: simsun; line-height: 23px; text-align: left; "><br /></span><img src ="http://www.cppblog.com/wangliu2009/aggbug/172281.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/wangliu2009/" target="_blank">把酒劝雨雨不住</a> 2012-04-21 20:55 <a href="http://www.cppblog.com/wangliu2009/archive/2012/04/21/172281.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>