123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774 |
- #include "stdafx.h"
- #include "Global.h"
- #include <tchar.h>
- #include <direct.h>
- #include <SetupAPI.h>
- #include <InitGuid.h>
- #include <WinIoCtl.h>
- #pragma comment(lib, "SetupAPI.lib")
- namespace Global
- {
- //////////////////////////////////////////////////////////////////////////
- // 全局变量;
- TCHAR g_szCurModuleDir[MAX_PATH] = { 0 };
- TCHAR g_szCurModulePath[MAX_PATH] = { 0 };
- TCHAR g_szFna[MAX_PATH] = { 0 };
- TCHAR g_szConfig[MAX_PATH] = { 0 };
- TCHAR g_szResuorceCfg[MAX_PATH] = { 0 };
- STConfig g_Config;
- TCHAR g_szPython27Dir[MAX_PATH] = {0};
- ULONGLONG g_ulWaitTime = 15000;
- int g_nSysZoomRatio = 100;
- TCHAR g_szVersion[MAX_PATH] = _T("4.45.200421");
- //////////////////////////////////////////////////////////////////////////
- // 全局函数;
- /************************************************************************/
- /* 函数:WriteTextLog[7/28/2009 Jeff];
- /* 描述:写文本日志;
- /* 参数:;
- /* [IN] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- void WriteTextLog(const TCHAR* format, ...)
- {
- // 解析出日志路径;
- TCHAR szlogpath[MAX_PATH] = { 0 };
- _stprintf_s(szlogpath, _T("%s%s.txt"), g_szCurModuleDir, g_szFna);
- // 打开或创建文件;
- FILE* fp = NULL;
- //if (_taccess(szlogpath, 0) != -1)
- #ifndef UNICODE
- if (_access(szlogpath, 0) != -1)
- #else
- if (_taccess(szlogpath, 0) != -1)
- #endif
- {// 存在;
- if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
- // 移动到末尾;
- fseek(fp, 0, SEEK_END);
- }
- else
- {// 不存在;
- _tfopen_s(&fp, szlogpath, _T("w+"));
- }
- if (fp == NULL)
- return;
- // 格式化前设置语言区域;
- TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
- _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
- // 格式化日志内容;
- va_list args = NULL;
- int len = 0;
- TCHAR* buffer = NULL;
- va_start(args, format);
- // _vscprintf doesn't count. terminating '\0'
- len = _vsctprintf(format, args) + 1;
- buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
- _vstprintf_s(buffer, len, format, args);
- // 将日志内容输入到文件中;
- // 获取今年年份;
- __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
- struct tm gmtm = { 0 };
- localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
- _ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);
- // 关闭文件,释放资源并设置回原语言区域;
- free(buffer);
- fclose(fp);
- _tsetlocale(LC_CTYPE, old_locale);
- free(old_locale);//还原区域设定;
- }
- void WriteTextLogEx(int nType, const TCHAR* format, ...)
- {
- // 解析出日志路径;
- TCHAR szlogpath[MAX_PATH] = { 0 };
- _stprintf_s(szlogpath, _T("%s%s-%d.txt"), g_szCurModuleDir, g_szFna, nType);
- // 打开或创建文件;
- FILE* fp = NULL;
- //if (_taccess(szlogpath, 0) != -1)
- #ifndef UNICODE
- if (_access(szlogpath, 0) != -1)
- #else
- if (_taccess(szlogpath, 0) != -1)
- #endif
- {// 存在;
- if (0 == _tfopen_s(&fp, szlogpath, _T("a+")))
- // 移动到末尾;
- fseek(fp, 0, SEEK_END);
- }
- else
- {// 不存在;
- _tfopen_s(&fp, szlogpath, _T("w+"));
- }
- if (fp == NULL)
- return;
- // 格式化前设置语言区域;
- TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
- _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
- // 格式化日志内容;
- va_list args = NULL;
- int len = 0;
- TCHAR* buffer = NULL;
- va_start(args, format);
- // _vscprintf doesn't count. terminating '\0'
- len = _vsctprintf(format, args) + 1;
- buffer = (TCHAR*)malloc(len * sizeof(TCHAR));
- _vstprintf_s(buffer, len, format, args);
- // 将日志内容输入到文件中;
- // 获取今年年份;
- __time64_t gmt = time(NULL);// 获取当前日历时间(1900-01-01开始的Unix时间戳);
- struct tm gmtm = { 0 };
- localtime_s(&gmtm, &gmt); // 时间戳转成本地时间;
- _ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, buffer);
- // 关闭文件,释放资源并设置回原语言区域;
- free(buffer);
- fclose(fp);
- _tsetlocale(LC_CTYPE, old_locale);
- free(old_locale);//还原区域设定;
- }
- int ReadReg(char* path, char* key, char* value)
- {
- HKEY hKey;
- int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_QUERY_VALUE, &hKey);
- if (ret != ERROR_SUCCESS)
- {
- return 1;
- }
- //读取KEY
- DWORD dwType = REG_SZ; //数据类型
- DWORD cbData = 256;
- ret = RegQueryValueEx(hKey, key, NULL, &dwType, (LPBYTE)value, &cbData);
- if (ret != ERROR_SUCCESS)
- {
- RegCloseKey(hKey);
- return 1;
- }
- RegCloseKey(hKey);
- return 0;
- }
- BOOL Python27Dir()
- {
- HKEY hKey;
- int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\WOW6432Node\\Python\\PythonCore\\2.7\\InstallPath"), 0, KEY_QUERY_VALUE, &hKey);
- if (ret != ERROR_SUCCESS)
- return FALSE;
- //读取KEY
- DWORD dwType = REG_SZ; //数据类型
- DWORD cbData = 256;
- ret = RegQueryValueEx(hKey, _T(""), NULL, &dwType, (LPBYTE)g_szPython27Dir, &cbData);
- if (ret != ERROR_SUCCESS)
- {
- RegCloseKey(hKey);
- return FALSE;
- }
- RegCloseKey(hKey);
- return TRUE;
- }
- // 通过注册表查找系统当前串口信息;
- BOOL GetSysSerialPort(std::vector<std::string>& vtports)
- {
- HKEY hKey;
- LSTATUS lReg = 0;
- DWORD dwMaxValLen = 0;
- DWORD dwValNum = 0;
- lReg = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey);
- if (lReg != ERROR_SUCCESS)
- {
- //LOG4C((LOG_WARN, "Open Registry Error"));
- return FALSE;
- }
- lReg = RegQueryInfoKeyA(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &dwValNum, &dwMaxValLen, NULL, NULL, NULL);
- if (lReg != ERROR_SUCCESS)
- {
- //LOG4C((LOG_WARN, "Getting Key Info Error"));
- return FALSE;
- }
- if (vtports.size())
- {
- vtports.clear();
- }
- LPSTR lpValName, lpComNum;
- DWORD dwValName, dwValSize = 6;
- for (DWORD i = 0; i < dwValNum; i++)
- {
- dwValName = dwMaxValLen + 1;
- dwValSize = 6;
- lpValName = (LPSTR)VirtualAlloc(NULL, dwValName, MEM_COMMIT, PAGE_READWRITE);
- lReg = RegEnumValueA(hKey, i, lpValName, &dwValName, NULL, NULL, NULL, NULL);
- if ((lReg != ERROR_SUCCESS) && (lReg != ERROR_NO_MORE_ITEMS))
- {
- //LOG4C((LOG_WARN, "Enum Registry Error or No More Items"));
- continue;
- }
- lpComNum = (LPSTR)VirtualAlloc(NULL, 6, MEM_COMMIT, PAGE_READWRITE);
- lReg = RegQueryValueExA(hKey, lpValName, NULL, NULL, (LPBYTE)lpComNum, &dwValSize);
- if (lReg != ERROR_SUCCESS)
- {
- //LOG4C((LOG_WARN, "Can not get the name of the port"));
- continue;
- }
- vtports.push_back(lpComNum);
- VirtualFree(lpValName, 0, MEM_RELEASE);
- VirtualFree(lpComNum, 0, MEM_RELEASE);
- }
- return TRUE;
- }
- // 读取配置文件-预留;
- void GetConfig()
- {
- // Python27安装目录;
- Python27Dir();
- TCHAR szConfigpath[MAX_PATH] = { 0 };
- _stprintf_s(szConfigpath, _T("%s%s.ini"), g_szCurModuleDir, g_szFna);
- _tcscpy_s(g_szConfig, szConfigpath);
- TCHAR szValue[MAX_PATH] = { 0 };
- // 读取配置内容;
- g_Config.enableTW = GetPrivateProfileInt(_T("SATHelper"), _T("enableTW"), 0, szConfigpath);
- GetPrivateProfileString(_T("SATHelper"), _T("MIInitBat"), NULL, szValue, MAX_PATH, szConfigpath);
- if (_tcslen(szValue) == 0)
- {
- WritePrivateProfileString(_T("SATHelper"), _T("MIInitBat"), _T("D:\\SAT\\tools\\atx-init\\atx-init_mi.bat"), szConfigpath);
- _stprintf_s(szValue, _T("D:\\SAT\\tools\\atx-init\\atx-init_mi.bat"));
- }
- g_Config.strMIInitBat = szValue;
- GetPrivateProfileString(_T("SATHelper"), _T("SCBCInitBat"), NULL, szValue, MAX_PATH, szConfigpath);
- if (_tcslen(szValue) == 0)
- {
- WritePrivateProfileString(_T("SATHelper"), _T("SCBCInitBat"), _T("D:\\SAT\\tools\\atx-init\\atx-init_scbc.bat"), szConfigpath);
- _stprintf_s(szValue, _T("D:\\SAT\\tools\\atx-init\\atx-init_scbc.bat"));
- }
- g_Config.strSCBCInitBat = szValue;
- // svn更新地址;
- GetPrivateProfileString(_T("SATHelper"), _T("SVNUpdateUrl"), NULL, szValue, MAX_PATH, szConfigpath);
- if (_tcslen(szValue) == 0)
- {
- WritePrivateProfileString(_T("SATHelper"), _T("SVNUpdateUrl"), _T("http://10.126.16.60:8580/btc_execute_se/ajaxInteractiveManage!getSvnVersion.action"), szConfigpath);
- _stprintf_s(szValue, _T("http://10.126.16.60:8580/btc_execute_se/ajaxInteractiveManage!getSvnVersion.action"));
- }
- g_Config.svnUpdateUrl = szValue;
- g_Config.bGenerics = GetPrivateProfileInt(_T("ir-device"), _T("generics"), 0, szConfigpath);
- if (PathFileExists(g_szPython27Dir))
- {
- g_Config.redratpath = g_szPython27Dir;
- g_Config.redratpath.append("Tools\\RedRatHub-V4.28\\RedRatHubCmd.exe");
- _stprintf_s(g_szResuorceCfg, _T("%sLib\\site-packages\\ssat_sdk\\config\\resource_run.cfg"), g_szPython27Dir);
- }
- else
- {
- GetPrivateProfileString(_T("ir-device"), _T("redratpath"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.redratpath = szValue;
- memset(szValue, 0, MAX_PATH);
- }
- GetPrivateProfileString(_T("ir-device"), _T("signal"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.signaldir = szValue;
- memset(szValue, 0, MAX_PATH);
- GetPrivateProfileString(_T("ir-device"), _T("use-signal"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.use_signal = szValue;
- memset(szValue, 0, MAX_PATH);
- // Service;
- g_Config.bAutoLogin = GetPrivateProfileInt(_T("SATService"), _T("AutoLogin"), 0, szConfigpath);
- GetPrivateProfileString(_T("SATService"), _T("UserName"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.strSATUserName = szValue;
- memset(szValue, 0, MAX_PATH);
- GetPrivateProfileString(_T("SATService"), _T("Password"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.strSATPassword = szValue;
- memset(szValue, 0, MAX_PATH);
- GetPrivateProfileString(_T("SATService"), _T("Actuator"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.strActuator = szValue;
- memset(szValue, 0, MAX_PATH);
- GetPrivateProfileString(_T("SATService"), _T("ServiceIP"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.strServiceIP = szValue;
- memset(szValue, 0, MAX_PATH);
- GetPrivateProfileString(_T("SATService"), _T("ServicePort"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.strServicePort = szValue;
- memset(szValue, 0, MAX_PATH);
- // 是否开启SATHelper自动重连功能;
- g_Config.bAutoReconnect = GetPrivateProfileInt(_T("UB530"), _T("AutoReconnect"), 0, szConfigpath);
- // 重连周期:分钟为单位;
- g_Config.nReconnectCycle = GetPrivateProfileInt(_T("UB530"), _T("ReconnectCycle"), 60, szConfigpath);
- //////////////////////////////////////////////////////////////////////////
- GetPrivateProfileString(_T("TestWizard"), _T("xmlpath"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.twSignaldir = szValue;
- memset(szValue, 0, MAX_PATH);
- GetPrivateProfileString(_T("TestWizard"), _T("useSignal"), NULL, szValue, MAX_PATH, szConfigpath);
- g_Config.twUseSignal = szValue;
- memset(szValue, 0, MAX_PATH);
- g_Config.twPort = GetPrivateProfileInt(_T("TestWizard"), _T("Com"), 0, szConfigpath);
- }
- BOOL GetOrientation(IN Image* pImg)
- {
- if (pImg == NULL) return FALSE;
- UINT totalBufferSize;
- UINT numProperties;
- pImg->GetPropertySize(&totalBufferSize, &numProperties);
- // Allocate the buffer that will receive the property items.
- PropertyItem* pAllItems = (PropertyItem*)malloc(totalBufferSize);
- // Fill the buffer.
- pImg->GetAllPropertyItems(totalBufferSize, numProperties, pAllItems);
- // Print the id data member of each property item.
- for (UINT j = 0; j < numProperties; ++j)
- {
- if (PropertyTagOrientation == pAllItems[j].id)
- {
- short* ptrLong = (short*)(pAllItems[j].value);
- int ret = (int)*ptrLong;
- free(pAllItems);
- return ret;
- }
- }
- free(pAllItems);
- return TRUE;
- }
- /************************************************************************/
- /*
- 函数:GetEncoderClsid
- 描述:获取GDI+支持的图像格式编码器种类,以及所有种类编码器信息;
- 参数:
- IN: format 要获取的图像格式;
- OUT: pClsid 返回符合条件的图像编码器信息;
- 返回:成功返回编码器索引,否则返回-1;
- */
- /************************************************************************/
- int GetEncoderClsid(IN CONST WCHAR* format, OUT CLSID* pClsid)
- {
- // GDI+支持的图像编码器数量;
- UINT numEncoders = 0;
- // GDI+所有图像格式编码器详细信息所需要的空间大小;
- UINT nSize = 0;
- ImageCodecInfo* pImageCodecInfo = NULL;
- // 2.获取GDI+支持的所有图像格式编码器详细信息所需要的空间大小;
- GetImageEncodersSize(&numEncoders, &nSize);
- if (nSize == 0)
- return -1;
- //3.为ImageCodecInfo数组分配足额空间;
- pImageCodecInfo = (ImageCodecInfo*)(malloc(nSize));
- if (pImageCodecInfo == NULL)
- return -1;
- //4.获取所有的图像编码器信息;
- GetImageEncoders(numEncoders, nSize, pImageCodecInfo);
- //5.查找符合的图像编码器的Clsid;
- for (UINT i = 0; i < numEncoders; ++i)
- {
- if (wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
- {
- *pClsid = pImageCodecInfo[i].Clsid;
- free(pImageCodecInfo);
- return i; // Success
- }
- }
- //6.释放步骤3分配的内存;
- free(pImageCodecInfo);
- return -1;
- }
- BOOL LoadImgFromFile(IN Image** pImg, LPCTSTR lpPath)
- {
- if (!PathFileExists(lpPath))
- return FALSE;
- if (*pImg)
- delete* pImg;
- *pImg = NULL;
- #ifdef UNICODE
- * pImg = Image::FromFile(lpPath);
- #else
- BSTR strtmp = _bstr_t(lpPath);
- *pImg = Image::FromFile(strtmp, TRUE);
- SysFreeString(strtmp);
- #endif
- return (*pImg ? TRUE : FALSE);
- }
- BOOL LoadImgFromBuffer(IN Image** pImg, IN BYTE* pBuffer, IN CONST INT& nBufLen)
- {
- if (pBuffer == NULL)
- return FALSE;
- if (*pImg)
- delete* pImg;
- *pImg = NULL;
- HGLOBAL hMemery = GlobalAlloc(GMEM_MOVEABLE, nBufLen);
- if (hMemery == NULL)
- return FALSE;
- BYTE* pMem = (BYTE*)GlobalLock(hMemery);
- memcpy(pMem, pBuffer, nBufLen);
- IStream* pstream = NULL;
- CreateStreamOnHGlobal(hMemery, TRUE, &pstream);
- *pImg = Image::FromStream(pstream);
- GlobalUnlock(hMemery);
- pstream->Release();
- return (*pImg ? TRUE : FALSE);
- }
- // 先以只读方式从文件中读取二进制流出来,可以做到不独占文件;
- BOOL LoadImgFromBuffer(IN Image** pImg, LPCTSTR lpPath)
- {
- if (!PathFileExists(lpPath))
- return FALSE;
- if (*pImg)
- delete* pImg;
- *pImg = NULL;
- return LoadImgFromFile(pImg, lpPath);
- CFile fp;
- CFileException e;
- BOOL bRet = FALSE;
- if (fp.Open(lpPath, CFile::modeRead, &e))
- {
- DWORD dwLength = (DWORD)fp.GetLength();
- BYTE* pData = new BYTE[dwLength];
- fp.Read(pData, dwLength);
- fp.Close();
- bRet = LoadImgFromBuffer(pImg, pData, dwLength);
- if (pData)
- delete[]pData;
- }
- return bRet;
- }
- /************************************************************************/
- /* 函数:LoadImgFromResource[9/21/2016 IT];
- /* 描述:从资源中加载;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- Image* LoadImgFromResource(IN HMODULE hModule, IN LPCTSTR lpName, IN LPCTSTR lpType)
- {
- HGLOBAL hGlobal = NULL;
- HRSRC hSource = NULL;
- LPVOID lpBuffer = NULL;
- DWORD dwSize = 0;
- // 1.定位我们的自定义资源,这里因为我们是从本模块定位资源,所以将句柄简单地置为NULL即可
- hSource = FindResource(hModule, lpName, lpType);
- if (hSource == NULL)
- {
- _tprintf(_T("载入资源失败:%s"), lpName);
- return NULL;
- }
- // 2.获取资源的大小;
- dwSize = (UINT)SizeofResource(NULL, hSource);
- // 3.加载资源;
- hGlobal = LoadResource(NULL, hSource);
- if (hGlobal == NULL)
- {
- _tprintf(_T("载入资源失败:%s"), lpName);
- return NULL;
- }
- // 4.锁定资源,获取buffer;
- lpBuffer = LockResource(hGlobal);
- if (lpBuffer == NULL)
- {
- _tprintf(_T("载入资源失败:%s"), lpName);
- return NULL;
- }
- // lpFileFullName需要先判断文件是否存在??不需要;
- Image* pImg = NULL;
- LoadImgFromBuffer(&pImg, (BYTE*)lpBuffer, dwSize);
- UnlockResource(hGlobal);
- FreeResource(hGlobal);
- return pImg;
- }
- BOOL SaveImgByRotate(LPCTSTR lpszFileName, BYTE* pBuffer, const INT& nBufLen, BOOL bHoriontal, BOOL bVertically)
- {
- Image* pImg = NULL;
- HGLOBAL hMemery = GlobalAlloc(GMEM_MOVEABLE, nBufLen);
- if (hMemery == NULL)
- return FALSE;
- BYTE* pMem = NULL;
- pMem = (BYTE*)GlobalLock(hMemery);
- if (pMem == NULL)
- return FALSE;
- memcpy(pMem, pBuffer, nBufLen);
- IStream* pstream = NULL;
- HRESULT hr = CreateStreamOnHGlobal(hMemery, TRUE, &pstream);
- if (pstream == NULL)
- return FALSE;
- pImg = Image::FromStream(pstream);
- GlobalUnlock(hMemery);
- pstream->Release();
- if (bHoriontal && !bVertically)
- pImg->RotateFlip(RotateNoneFlipX);// 水平翻转;
- else if (bHoriontal && bVertically)
- pImg->RotateFlip(Rotate180FlipNone);// 270度;
- else if (!bHoriontal && bVertically)
- pImg->RotateFlip(Rotate180FlipX);// 垂直翻转;
- CString strNewfile = lpszFileName;
- // 需要判断路径是否存在,不存在创建目录;
- int nIndex = strNewfile.ReverseFind(_T('\\'));
- if (nIndex == -1)
- return FALSE;
- if (!PathFileExists(strNewfile.Left(nIndex)))
- {
- // 如果文件夹不存在,创建;
- SHCreateDirectoryEx(NULL, strNewfile.Left(nIndex), NULL);
- }
- nIndex = strNewfile.ReverseFind(_T('.'));
- if (nIndex == -1)
- return FALSE;
- Status stat = GenericError;
- CLSID encoderClsid = { 0 };
- BSTR newfile = strNewfile.AllocSysString();
- strNewfile = strNewfile.Mid(nIndex + 1);
- if (strNewfile.CompareNoCase(_T("bmp")) == 0)
- {
- GetEncoderClsid(L"image/bmp", &encoderClsid);
- stat = pImg->Save(newfile, &encoderClsid, NULL);
- }
- else if (strNewfile.CompareNoCase(_T("png")) == 0)
- {
- GetEncoderClsid(L"image/png", &encoderClsid);
- stat = pImg->Save(newfile, &encoderClsid, NULL);
- }
- else// if ( strNewfile.CompareNoCase(_T("jpeg")) == 0 )
- {
- GetEncoderClsid(L"image/jpeg", &encoderClsid);
- EncoderParameters encoderParameters;
- encoderParameters.Count = 1;
- encoderParameters.Parameter[0].Guid = EncoderQuality;
- encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
- encoderParameters.Parameter[0].NumberOfValues = 1;
- // Save the image as a JPEG with quality level 100.
- ULONG uQuality = 100;
- encoderParameters.Parameter[0].Value = &uQuality;
- stat = pImg->Save(newfile, &encoderClsid, &encoderParameters);
- }
- if (pImg)
- delete pImg;
- pImg = NULL;
- SysFreeString(newfile);
- return stat == Ok ? TRUE : FALSE;
- }
- void MKDIR(LPCTSTR dir)
- {
- //////////////////////////////////////////////////////////////////////////
- // 创建目录;
- int nleft = 0;
- int nIndex = -1;
- TString strdir = dir;
- if (strdir.at(strdir.size() - 1) != _T('\\'))
- strdir.append(_T("\\"));
- // 共享路径和硬盘盘符;
- if (_tcscmp(strdir.substr(0, 2).c_str(), _T("\\\\")) == 0)
- nleft = strdir.find_first_of(_T("\\"), 2) + 1; // 去除共享主机名;
- else if (strdir.at(2) == _T('\\'))
- nleft = 3;
- do
- {
- nIndex = strdir.substr(nleft, -1).find_first_of(_T("\\"));
- if (nIndex != TString::npos)
- {
- if (_tmkdir(strdir.substr(0, nIndex + nleft).c_str()) == -1 && (errno != EEXIST))
- {
- WriteTextLog(_T("创建目录失败:%s,错误码:%d"), strdir.substr(0, nIndex + nleft).c_str(), errno);
- break;
- }
- nleft += nIndex + 1;
- }
- } while (nIndex != -1);
- }
- // hModule 模块句柄 NULL表示当前模块;
- bool GetVersion(OUT WORD* pdwFileVersion, OUT WORD* pdwProductVerion)
- {
- TCHAR szFilePath[MAX_PATH] = { 0 };
- DWORD dwRet = GetModuleFileName(NULL, szFilePath, MAX_PATH);
- if (dwRet == 0)
- return false;
- VS_FIXEDFILEINFO* pVi = NULL;
- DWORD dwHandle = 0;
- int size = GetFileVersionInfoSize(szFilePath, &dwHandle);
- if (size > 0)
- {
- BYTE* buffer = new BYTE[size];
- memset(buffer, 0, size);
- if (GetFileVersionInfo(szFilePath, dwHandle, size, buffer))
- {
- if (VerQueryValue(buffer, _T("\\"), (LPVOID*)&pVi, (PUINT)&size))
- {
- pdwFileVersion[0] = HIWORD(pVi->dwFileVersionMS);
- pdwFileVersion[1] = LOWORD(pVi->dwFileVersionMS);
- pdwFileVersion[2] = HIWORD(pVi->dwFileVersionLS);
- pdwFileVersion[3] = LOWORD(pVi->dwFileVersionLS);
- pdwProductVerion[0] = HIWORD(pVi->dwProductVersionMS);
- pdwProductVerion[1] = LOWORD(pVi->dwProductVersionMS);
- pdwProductVerion[2] = HIWORD(pVi->dwProductVersionLS);
- pdwProductVerion[3] = LOWORD(pVi->dwProductVersionLS);
- delete[]buffer;
- return true;
- }
- }
- delete[]buffer;
- }
- return false;
- }
- void GetSysZoomRatio()
- {
- // Get desktop dc
- HDC desktopDc = GetDC(NULL);
- int horizontalDPI = GetDeviceCaps(desktopDc, LOGPIXELSX);
- int verticalDPI = GetDeviceCaps(desktopDc, LOGPIXELSY);
- switch ( horizontalDPI )
- {
- case 96:
- g_nSysZoomRatio = 100; // 100%缩放布局;
- break;
- case 120:
- g_nSysZoomRatio = 125; // 125%缩放布局;
- break;
- case 144:
- g_nSysZoomRatio = 150; // 150%缩放布局;
- break;
- case 192:
- g_nSysZoomRatio = 200; // 200%缩放布局;
- break;
- default:
- break;
- }
- #if 0
- // Get native resolution
- int horizontalResolution = GetDeviceCaps(desktopDc, HORZRES);
- int verticalResolution = GetDeviceCaps(desktopDc, VERTRES);
- // 获取窗口当前显示的监视器
- // 使用桌面的句柄.
- HWND hWnd = GetDesktopWindow();
- HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
- // 获取监视器逻辑宽度与高度
- MONITORINFOEX miex;
- miex.cbSize = sizeof(miex);
- GetMonitorInfo(hMonitor, &miex);
- int cxLogical = (miex.rcMonitor.right - miex.rcMonitor.left);
- int cyLogical = (miex.rcMonitor.bottom - miex.rcMonitor.top);
- // 获取监视器物理宽度与高度
- DEVMODE dm;
- dm.dmSize = sizeof(dm);
- dm.dmDriverExtra = 0;
- EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm);
- int cxPhysical = dm.dmPelsWidth;
- int cyPhysical = dm.dmPelsHeight;
- // 缩放比例计算 实际上使用任何一个即可
- double horzScale = ((double)cxPhysical / (double)cxLogical);
- double vertScale = ((double)cyPhysical / (double)cyLogical);
- assert(horzScale == vertScale); // 宽或高这个缩放值应该是相等的
- #endif
- }
- }
|