Skip to content

Instantly share code, notes, and snippets.

@wilsoncook
Created August 30, 2014 03:47
Show Gist options
  • Save wilsoncook/10750f9aba03c76b6f98 to your computer and use it in GitHub Desktop.
Save wilsoncook/10750f9aba03c76b6f98 to your computer and use it in GitHub Desktop.
C++常用函数集锦
//强制置顶窗口
void ForceForegroundWindow(HWND hWnd) {
DWORD thread1, thread2, pid;
thread1 = ::GetWindowThreadProcessId(::GetForegroundWindow(), &pid);
thread2 = ::GetCurrentThreadId();
::AttachThreadInput(thread2, thread1, TRUE);
::SetForegroundWindow(hWnd);
::AttachThreadInput(thread2, thread1, FALSE);
::BringWindowToTop(hWnd);
}
//将utf8转换为unicode
CString UTF8ToUnicode(const char* UTF8) {
DWORD dwUnicodeLen; //转换后Unicode的长度
TCHAR *pwText = NULL; //保存Unicode的指针
CString strUnicode; //返回值
//获得转换后的长度,并分配内存
dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,UTF8,-1,NULL,0);
pwText = new TCHAR[dwUnicodeLen];
if (!pwText) {
return strUnicode;
}
//转为Unicode
MultiByteToWideChar(CP_UTF8,0,UTF8,-1,pwText,dwUnicodeLen);
//转为CString
strUnicode.Format(_T("%s"),pwText);
//清除内存
delete []pwText;
//返回转换好的Unicode字串
return strUnicode;
}
//将unicode转换为utf8
string UnicodeToUTF8(const CString &szContent) {
DWORD dwSize;
char *pRet = NULL;
string szRet;
//获得转换后所需空间
dwSize = WideCharToMultiByte(CP_UTF8, 0, szContent, -1, NULL, 0, NULL, NULL);
pRet = new char[dwSize];
if (!pRet) return szRet;
//转换
WideCharToMultiByte(CP_UTF8, 0, szContent, -1, pRet, dwSize, NULL, NULL);
//存到string
szRet = pRet;
delete []pRet;
//返回
return szRet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment