在 MFC项目中会经常用到的知识点小结

在使用MFC来进行相应的功能实现的时候,会常常用到的一些点(我这里用到的代码都是Unicode编码模式哦)

知识点一:获取当前系统的时间 需要在某个控件下进行时间的显示 或者用文本进行显示 在程序的标题或者状态栏使用,这个很常见;核心的获取时间的方法如下

方法一:

//获取当前时间 
time_t t = time(0);
char temp[64];
std::string strtemp;
tm *Time = localtime(&t);
strftime(temp, sizeof(temp), "%Y%m", Time); //格式化时间输出 %Y-%m-%d %H:%M:%S
strtemp = temp;

方法二:

char str[50];
SYSTEMTIME st;
GetSystemTime(&st);
sprintf(str, "%u-%u-%u %u:%u:%u", st.wYear,st.wMonth,st.wDay ,st.wHour, st.wMinute, st.wSecond); //这里的小时获取可能会存在问题,需要手动修改下
printf("%s\n", str);
Sleep(1000);	//间隔一秒


知识点二:在MFC中使用的编码问题(Unicode问题) 导致字符串的一些奇奇怪怪的问题,这个我理解不深,相应的知识点可以自行补充,这里只分享下怎么去用一些函数来解决问题

代码片段

CString strInputComb;
GetDlgItemText(IDC_COMBOSELECTMODE, strInputComb);
CString strInputEdit;
GetDlgItemText(IDC_EDITINPUT, strInputEdit); 

/*在Unicode的编码条件下 将CString 转换为 char*  begin*/
wchar_t *wchar = strInputComb.GetBuffer(strInputComb.GetLength());
strInputComb.ReleaseBuffer();
size_t len = wcslen(wchar) + 1;
size_t converted = 0;
char *format = (char*)malloc(len * sizeof(char));
//cChar = (char*)malloc(len * sizeof(char));
wcstombs_s(&converted, format, len, wchar, _TRUNCATE);
/*在Unicode的编码条件下 将CString 转换为 char*  end*/

补充知识点:

wstring是宽char,Unicode编码,一般情况下一个字符占两个字节大小

string是窄char,AscII编码,一个字符占一个字节大小


最常用的就是 将CString转换为String这个在MFC的应用中经常会遇到,有一个很简单的方法,使用W2A这个宏,然后前面要加上USES_CONVERSION; 这个定义

USES_CONVERSION;
std::string strsource(W2A(strInputEdit));
我们可以看下W2A的定义,会提示你缺少头文件 W2A这个宏包含在 头文件 atlconv.h 这个里面,所以用的时候需要注意包含以下,真的很强大这个宏(好用)


知识点补充:

WideCharToMultiByte的代码页用来标记与新转换的字符串相关的代码页。
MultiByteToWideChar的代码页用来标记与一个多字节字符串相关的代码页。
常用的代码页由CP_ACP和CP_UTF8两个。
使用CP_ACP代码页就实现了ANSI与Unicode之间的转换。

使用CP_UTF8代码页就实现了UTF-8与Unicode之间的转换


还有一个方法就是网上摘取的:(好久之前看到的)

1.  ANSI to Unicode
wstring ANSIToUnicode( const string& str )
{
 int  len = 0;
 len = str.length();
 int  unicodeLen = ::MultiByteToWideChar( CP_ACP,
            0,
            str.c_str(),
            -1,
            NULL,
            0 );  
 wchar_t *  pUnicode;  
 pUnicode = new  wchar_t[unicodeLen+1];  
 memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));  
 ::MultiByteToWideChar( CP_ACP,
         0,
         str.c_str(),
         -1,
         (LPWSTR)pUnicode,
         unicodeLen );  
 wstring  rt;  
 rt = ( wchar_t* )pUnicode;
 delete  pUnicode; 
 
 return  rt;  
}

2.  Unicode to ANSI
string UnicodeToANSI( const wstring& str )
{
 char*     pElementText;
 int    iTextLen;
 // wide char to multi char
 iTextLen = WideCharToMultiByte( CP_ACP,
         0,
         str.c_str(),
         -1,
         NULL,
         0,
NULL,
         NULL );
 pElementText = new char[iTextLen + 1];
 memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
 ::WideCharToMultiByte( CP_ACP,
         0,
         str.c_str(),
         -1,
         pElementText,
         iTextLen,
         NULL,
         NULL );
 string strText;
 strText = pElementText;
 delete[] pElementText;
 return strText;
}

3.  UTF-8 to Unicode
wstring UTF8ToUnicode( const string& str )
{
 int  len = 0;
 len = str.length();
 int  unicodeLen = ::MultiByteToWideChar( CP_UTF8,
            0,
            str.c_str(),
            -1,
            NULL,
            0 );  
 wchar_t *  pUnicode;  
 pUnicode = new  wchar_t[unicodeLen+1];  
 memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));  
 ::MultiByteToWideChar( CP_UTF8,
         0,
         str.c_str(),
         -1,
         (LPWSTR)pUnicode,
         unicodeLen );  
 wstring  rt;  
 rt = ( wchar_t* )pUnicode;
 delete  pUnicode; 
 
 return  rt;  
}

4.  Unicode to UTF-8    
string UnicodeToUTF8( const wstring& str )
{
 char*     pElementText;
 int    iTextLen;
 // wide char to multi char
 iTextLen = WideCharToMultiByte( CP_UTF8,
         0,
         str.c_str(),
         -1,
         NULL,
         0,
         NULL,
         NULL );
 pElementText = new char[iTextLen + 1];
 memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
 ::WideCharToMultiByte( CP_UTF8,
         0,
         str.c_str(),
         -1,
         pElementText,
         iTextLen,
         NULL,
         NULL );
 string strText;
 strText = pElementText;
 delete[] pElementText;
 return strText;
}


问题三:获取文件的路径,常用情况就是 需要设定某个文件夹来存放对应的软件处理数据,这个时候需要获取到Debug或者Release的路径 这个时候就需要如下方法进行处理;以下代码片段可以处理此类问题

//获取路径
	HMODULE module = GetModuleHandle(0);
	char pFileName[MAX_PATH];
	::GetModuleFileNameA(module, pFileName, MAX_PATH);
	CString strPath(pFileName);
	size_t nPos = strPath.ReverseFind(L'\\');
	strPath = (nPos < 0) ? CString(L"") : strPath.Left(nPos);	//Debug或者Release的路径
	std::wstring filePath = strPath + L"\\";

然后可以创建文件 或者 文件夹

创建文件:直接在后面加上xxx.xxx

创建文件夹:CreateDirectory()通过这个函数可以进行创建文件夹

然后可以结合使用PathFileExists这个接口来判断是否存在XXX文件夹 这样就可以实现没有就创建 有就直接用的功能了


补充:也可以使用GetCurrentDirectory这个接口来获取当前工程的路径,如果工程的文件和exe分离开了的话 就可能会找到奇怪的路径 保险起见还是使用上面的方法来获取路径


先补充这些,后面再添加




猜你喜欢

转载自blog.csdn.net/Wuzm_/article/details/80627521