关于windows编程的一个宏定义?

关于windows编程的一个宏定义?,第1张

既然是WM_USER+100,这就说明并不是标准windows消息,而是开发者自定义消息,所以只有定义这个消息的人才察渣旅知道

基本上每个程序员都习惯定义WM_USER+100作为自己程序败凳的第一个消息,我就一直这样定义。所以这个消息真的非常常见,且含义无穷无梁闷尽

控制台工程下(就是你说的小文件),使用atoi函数需要加头文件包含:#include<stdlib.h>

win32工程下,因为默认包含了#include<windows.h>,atoi函数是默认可以直接使用的。

你使用不了,一般是因为你的工程是UNICODE模式姿隐的(而不是多字节字符集模式),这种模式下不卖册脊能使用atoi函数,是因为UNICODE模式下默认要求使用的宽字节字符串atoi函数不支持。

这时候,使用atoi在UNICODE下的对应函数wtoi就可以了。

int _wtoi(const 中渗wchar_t *)

而更合适的函数是_ttoi,这个函数可以适应代码在字符集之间切换。

CString->std::string 例子:

CString strMfc=“test“

std::string strStl

strStl=strMfc.GetBuffer(0)

unicode情形下:启陪磨

CStringW strw = _T("test")

CStringA stra(strw.GetBuffer(0))

strw.ReleaseBuffer()

std::string imgpath=stra.GetBuffer(0)

stra.ReleaseBuffer()

std::string->CString 例子:

CString strMfc;

std::string strStl=“test“

strMfc=strStl.c_str()

AfxExtractSubString是截取字符串的函数,很好用,不过美中不足的地方在与它只悄斗能使用单个字符作为分割符。

但是这种情况在很多时候都行不通,如果分割符需要是两个字符以上呢?

之前因为这个问题试了很久,也在网上搜索过。不过可惜的是,网上的大部分关于VC截取字符串的文章都是那么同样的几篇,都是写的满复杂然后可以实现了AfxExtractSubString功能而已的,也就是只能用单个字符截取,但是标题却写着用字符串截取字符串,好笑!

不找了,自己写吧。CString里面有Find,然后再组成数组。

void Split(CString source, CStringArray&dest, CString division)

...{

dest.RemoveAll()

int pos = 0

int pre_pos = 0

while( -1 != pos )...{

pre_pos = pos

pos = source.Find(division,(pos+1))

dest.Add(source.Mid(pre_pos,(pos-pre_pos)))

}

}

CString source是需要截取的原字符串,

CStringArray&dest 是最终结果的数组

CString division 是用来做分割符的字符串

备忘:为了适用于Unicode环境,要养成使用_T()宏的习惯

1、格式化字符串

CString s

s.Format(_T("The num is %d."乱并), i)

2、转为 int

转10进制最好用_ttoi(),它在 ANSI 编码系统中被编译成_atoi(),而在 Unicode 编码系统中编译成_wtoi()。用_tcstoul()或者_tcstol()可以把字符串转化成任意进制的(无符号/有符号)长整数。

CString hex = _T("FAB")

CString decimal = _T("4011")

ASSERT(_tcstoul(hex, 0, 16) == _ttoi(decimal))

3、转为 char *

3.1 强制类型转换为 LPCTSTR,不能修改字符串

LPCTSTR p = s或者直接 (LPCTSTR)s

3.2 使用 GetBuffer 方法

不给 GetBuffer 传递参数时它使用默认值 0,意思是:“给我这个字符串的指针,我保证不加长它”。假设你想增加字符串的长度,就必须将你需要的字符空间大小(注意:是字符而不是字节,因为 CString 是以隐含方式感知 Unicode 的)传给它。当调用 ReleaseBuffer 时,字符串的实际长度会被重新计算,然后存入 CString 对象中。

必须强调一点,在 GetBuffer 和 ReleaseBuffer 之间这个范围,一定不能使用你要 *** 作的这个缓冲的 CString 对象的任何方法。因为 ReleaseBuffer 被调用之前,该 CString 对象的完整性得不到保障。

LPTSTR p = s.GetBuffer()

// do something with p

int m = s.GetLength()// 可能出错!!!

s.ReleaseBuffer()

int n = s.GetLength()// 保证正确

4、其他

4.1 分割字符串

AfxExtractSubString(CString&rString, LPCTSTR lpszFullString, int iSubString, TCHAR chSep = '\n')

CString csFullString(_T("abcd-efg-hijk-lmn"))

CString csTemp

AfxExtractSubString(csTemp, (LPCTSTR)csFullString, 0, '-')// 得到 abcd

AfxExtractSubString(csTemp, (LPCTSTR)csFullString, 1, '-')// 得到 efg

AfxExtractSubString(csTemp, (LPCTSTR)csFullString, 2, '-')// 得到 hijk

AfxExtractSubString(csTemp, (LPCTSTR)csFullString, 3, '-')// 得到 lmn

分隔符可以随便指定:

AfxExtractSubString(csTemp, (LPCTSTR)csFullString, 0, 'f')// 得到 abcd-e

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yysdsyl/archive/2008/05/20/2463662.aspx


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/12265166.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存