WCHAR转换成CString

genganpeng 2009-07-13 01:16:56
WCHAR [256];
CString tmp;
CString s[256];
USES_CONVERSION;
s = W2A(string);
tmp.Format(("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s,s.GetLength(),linesFilled,codepointsFitted);
头文件中有#include<atlconv.h>
不知哪里错了。
...全文
938 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
genganpeng 2009-07-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 bragi523 的回复:]
引用 7 楼 genganpeng 的回复:
引用 6 楼 Ghost90 的回复:
不知道你要干啥 这样的语法肯定是不对的
tmp.Format(("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s,s.GetLength(),linesFilled,codepointsFitted);

应该
tmp.Format(("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s[0],s[0].GetLength(),linesFil…
[/Quote]
这个是对的!!
_T是什么意思啊??
Ghost90 2009-07-13
  • 打赏
  • 举报
回复
错了
这样
tmp.Format("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符",s,s.GetLength(),linesFilled,codepointsFitted);
bragi523 2009-07-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 bragi523 的回复:]
引用 7 楼 genganpeng 的回复:
引用 6 楼 Ghost90 的回复:
不知道你要干啥 这样的语法肯定是不对的
tmp.Format(("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s,s.GetLength(),linesFilled,codepointsFitted);

应该
tmp.Format(("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s[0],s[0].GetLength(),linesFil…
[/Quote]
不用s[0],那根本不对
就用s就好了
genganpeng 2009-07-13
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 Ghost90 的回复:]
tmp.Format("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符",s[0],s.GetLength(),linesFilled,codepointsFitted);

是s[0] s[0].GetLength()
你只该了一个
[/Quote]
全改了也不对!!
Ghost90 2009-07-13
  • 打赏
  • 举报
回复
tmp.Format("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符",s[0],s.GetLength(),linesFilled,codepointsFitted);

是s[0] s[0].GetLength()
你只该了一个
bragi523 2009-07-13
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 genganpeng 的回复:]
引用 6 楼 Ghost90 的回复:
不知道你要干啥 这样的语法肯定是不对的
tmp.Format(("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s,s.GetLength(),linesFilled,codepointsFitted);

应该
tmp.Format(("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s[0],s[0].GetLength(),linesFilled,codepointsFitted);


还…
[/Quote]
你不会是unicode的工程吧?
那样的话
tmp.Format(_T("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s[0],s.GetLength(),linesFilled,codepointsFitted);



加个_T
genganpeng 2009-07-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 Ghost90 的回复:]
不知道你要干啥 这样的语法肯定是不对的
tmp.Format(("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s,s.GetLength(),linesFilled,codepointsFitted);

应该
tmp.Format(("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s[0],s[0].GetLength(),linesFilled,codepointsFitted);
[/Quote]
还是不对的!!

void CGDIplusDemoView::OnDraw(CDC *pDC) {
Graphics graphics(this->m_hWnd);
//this->RedrawWindow();
WCHAR string[256];
wcscpy(string,L"123456789ABCDEFGHIJKLM");
FontFamily fontFamily(L"Arial");
Font font(&fontFamily,30,FontStyleRegular,UnitPixel);
Font font2(&fontFamily,18,FontStyleRegular,UnitPixel);
RectF layoutRect(10,10,130,90);
SolidBrush brush(Color(255,0,0,0));
RectF boundRect;
int codepointsFitted = 0;
int linesFilled = 0;
int strlen = wcslen(string);
graphics.MeasureString(string,strlen,&font,layoutRect,NULL,&boundRect,&codepointsFitted,&linesFilled);
graphics.DrawRectangle(&Pen(Color::Black,3),layoutRect);
CString s(string);
CString tmp;
tmp.Format("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符",s[0],s.GetLength(),linesFilled,codepointsFitted);
}

这是代码,实现的是测量字符串,我测试过,只有最后一句是错的??不明白为什么??
Ghost90 2009-07-13
  • 打赏
  • 举报
回复
不知道你要干啥 这样的语法肯定是不对的
tmp.Format(("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s,s.GetLength(),linesFilled,codepointsFitted);

应该
tmp.Format(("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s[0],s[0].GetLength(),linesFilled,codepointsFitted);

百事烟 2009-07-13
  • 打赏
  • 举报
回复
	WCHAR *pwszSrc = L"中国23456789";   
int nSize = WideCharToMultiByte( // 先求char*的长度
CP_ACP, // ANSI代码页, 可随意给出
NULL, // 固定为NULL 原因不明
pwszSrc, // 源unicode字符串
-1, // 上一参数字符串被认为是以NULL为结束的字串并自动计算长度。
NULL, // 目标char*字串的缓冲区
0, // 若此值为零,函数返回上一参数的缓冲区所必需的字节数,此时上一参数为NULL。
NULL, // 特殊情况下使用,否则为NULL
FALSE); // 特殊情况下使用,否则为false
char *pszDst = new char[nSize];
WideCharToMultiByte( // 再得出char*字串
CP_ACP,
NULL,
pwszSrc, // 源unicode字符串
-1, // 上一参数字符串被认为是以NULL为结束的字串并自动计算长度。
pszDst, // 目标char*字串的缓冲区
nSize, // 上一参数缓冲区的大小
NULL,
FALSE);
MessageBoxA(pszDst);
delete []pszDst;
genganpeng 2009-07-13
  • 打赏
  • 举报
回复
如果这样写的话:
WCHAR string[256];
CString s(string);
CString tmp;
tmp.Format("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符",s,s.GetLength(),linesFilled,codepointsFitted);
编译器提示这个错误:
error C2664: “void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)”: 不能将参数 1 从“const char [85]”转换为“const wchar_t *”
1> with
1> [
1> BaseType=wchar_t,
1> StringTraits=StrTraitMFC_DLL<wchar_t>
1> ]
1> 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
biweilun 2009-07-13
  • 打赏
  • 举报
回复
CString s[256]?
marrco2005 2009-07-13
  • 打赏
  • 举报
回复
WCHAR [256];
CString tmp;
CString s[256]; // 把这改一下
CString s;
USES_CONVERSION;
s = W2A(string);
tmp.Format(("欲输出的字符串为\n\"%s\"\n共%d个字符,\n其中,在规定的输出矩形中,\n只输出%d行,共%d个字符"),s,s.GetLength(),linesFilled,codepointsFitted);
头文件中有#include <atlconv.h>
不知哪里错了。
fengrx 2009-07-13
  • 打赏
  • 举报
回复
WCHAR [256];

?? 这是怎么定义的?

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧