stdafx.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // stdafx.cpp : 只包括标准包含文件的源文件
  2. // Renderings.pch 将作为预编译头
  3. // stdafx.obj 将包含预编译类型信息
  4. #include "stdafx.h"
  5. #include <Shlwapi.h>
  6. #include <math.h>
  7. namespace lyfzGlobal
  8. {
  9. TCHAR g_szModulePath[MAX_PATH] = {0};
  10. CString g_strJson = _T("");
  11. CString g_strCustomerImagePath;
  12. CString g_strPathOfPNG = _T("");
  13. CStringArray g_AryCustomerImage;
  14. int g_nScreenWidth; // 桌面宽;
  15. int g_nScreenHeight; // 桌面高;
  16. void GetDesktopInfo()
  17. {
  18. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  19. TCHAR szDir[_MAX_DIR] = { 0 };
  20. TCHAR szFna[_MAX_DIR] = { 0 };
  21. TCHAR szExt[_MAX_DIR] = { 0 };
  22. ::GetModuleFileName(NULL, g_szModulePath, sizeof(g_szModulePath) / sizeof(TCHAR));
  23. _tsplitpath_s(g_szModulePath, szDrive, _MAX_DRIVE, szDir, _MAX_DIR, szFna, _MAX_DIR, szExt, _MAX_DIR);
  24. _tcscpy_s(g_szModulePath, MAX_PATH, szDrive);
  25. _tcscat_s(g_szModulePath, MAX_PATH, szDir);
  26. //////////////////////////////////////////////////////////////////////////
  27. g_nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
  28. g_nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
  29. // 系统任务栏的高度;
  30. HWND hWnd = ::FindWindow(_T("Shell_TrayWnd"), NULL);
  31. RECT rcTryaWnd;
  32. ::GetWindowRect(hWnd, &rcTryaWnd);
  33. int nHeight = rcTryaWnd.bottom - rcTryaWnd.top;
  34. #if 0
  35. g_nScreenHeight -= nHeight;
  36. #endif
  37. }
  38. // GetWidth()的长度与CRect的长度是不一样的单位值,需要转换适合的比例;(一个是图片的像素尺寸,一个DC的大小尺寸)
  39. // 将图片尺寸与设备尺寸进行转换适合的比例;
  40. void RectFitDes(int width, int height, CRect &rc)
  41. {
  42. try
  43. {
  44. if(width==0 || height==0)return;
  45. // 图片长宽比例;
  46. float fscale=(float)width/(float)height;
  47. // 设备长宽比例;
  48. float rcscale=((float)rc.Width())/((float)rc.Height());
  49. int rcwid=rc.Width();
  50. int rchei=rc.Height();
  51. int dt=0;
  52. // Jeff.如果设备长宽比例 < 图片长宽比例;(即相同长度下,高越大比例值越小,所以此时图片尺寸 与 显示设备的尺寸相比,要显得更长)
  53. if(rcscale<fscale)
  54. {
  55. // Jeff.remarks
  56. // 调整显示设备的大小,以使之能容纳图片尺寸;(即长宽比例上,要大于或等于图片的长宽比例)
  57. // 所以有两种方法使得 rcscale >= fscale 表达式成立:
  58. // -----------------------------------------------
  59. // 方法1:显示设备宽加x值,计算出下面表达式x的值即可
  60. // (rc.Width()+x) / rc.Height() >= width/height;
  61. // 方法2:显示设备高减x值,计算出下面表达式x的值即可
  62. // (rc.Width()) / (rc.Height()-x) >= width/height;
  63. //------------------------------------------------
  64. // 两种方法的最后表达式为:
  65. // x >= rc.Height() - rcWidth()*(height/width);
  66. // 即 x >= rc.Height() - rcWidth()/fscale;
  67. //------------------------------------------------
  68. dt=(rchei-rcwid/fscale)/2;
  69. rc.top+=dt;
  70. rc.bottom-=dt;
  71. }
  72. else
  73. {
  74. dt=(rcwid-rchei*fscale)/2;
  75. rc.left +=dt;
  76. rc.right-=dt;
  77. }
  78. }
  79. catch(...)
  80. {
  81. }
  82. }
  83. int GetFileNo2()
  84. {
  85. CString path;
  86. int begin=1;
  87. path.Format(_T("%s\\效果图-%d.png"), g_strPathOfPNG, begin);
  88. while(PathFileExists(path))
  89. {
  90. begin++;
  91. path.Format(_T("%s\\效果图-%d.png"), g_strPathOfPNG, begin);
  92. }
  93. return begin;
  94. }
  95. /************************************************************************/
  96. /* 函数:[4/9/2017 Jeff];
  97. /* 描述:使用勾股定理,通过两直角边计算出斜边;
  98. /* 参数:;
  99. /* [IN] :;
  100. /* [OUT] :;
  101. /* [IN/OUT] :;
  102. /* 返回:void;
  103. /* 注意:;
  104. /* 示例:;
  105. /*
  106. /* 修改:;
  107. /* 日期:;
  108. /* 内容:;
  109. /************************************************************************/
  110. int GetLengFromPt(CPoint pt1, CPoint pt2)
  111. {
  112. // c*c = a*a + b*b;
  113. return (pt1.x-pt2.x)*(pt1.x-pt2.x)+(pt1.y-pt2.y)*(pt1.y-pt2.y);
  114. }
  115. int GetLengFromPt2(CPoint pt1, CPoint pt2)
  116. {
  117. return sqrt((float)(pt1.x-pt2.x)*(pt1.x-pt2.x)+(pt1.y-pt2.y)*(pt1.y-pt2.y));
  118. }
  119. BOOL GetJsonInfo(IN CString& strJsonpath)
  120. {
  121. if ( !PathFileExists(strJsonpath) )
  122. return FALSE;
  123. CFile cf;
  124. BYTE *pJsonBuf = NULL;
  125. DWORD dwJsonlen = 0;
  126. if ( cf.Open(strJsonpath, CFile::modeReadWrite) )
  127. {
  128. dwJsonlen = cf.GetLength();
  129. pJsonBuf = new BYTE[dwJsonlen+1];
  130. memset(pJsonBuf, 0, dwJsonlen+1);
  131. cf.Read(pJsonBuf, dwJsonlen);
  132. cf.Close();
  133. }
  134. else
  135. {
  136. return FALSE;
  137. }
  138. cJSON *pJson = NULL;
  139. pJson = cJSON_Parse((char*)pJsonBuf);
  140. if ( pJsonBuf )
  141. delete []pJsonBuf;
  142. pJsonBuf = NULL;
  143. if ( pJson == NULL )
  144. return FALSE;
  145. cJSON *pJsonItem = cJSON_GetObjectItem(pJson, "png");
  146. if ( pJsonItem == NULL )
  147. {
  148. cJSON_Delete(pJson);
  149. return FALSE;
  150. }
  151. string strJson = "";
  152. EncodingConverion::UTF82ASCII(pJsonItem->valuestring, strJson);
  153. g_strPathOfPNG = strJson.c_str();
  154. pJsonItem = cJSON_GetObjectItem(pJson, "jpg");
  155. if ( pJsonItem == NULL )
  156. {
  157. cJSON_Delete(pJson);
  158. return FALSE;
  159. }
  160. cJSON *pSubItem = NULL;
  161. INT nSize = cJSON_GetArraySize(pJsonItem);
  162. g_AryCustomerImage.SetSize(nSize, 1);
  163. g_AryCustomerImage.RemoveAll();
  164. for ( int i = 0; i < nSize; i++ )
  165. {
  166. pSubItem = cJSON_GetArrayItem(pJsonItem, i);
  167. if ( pSubItem )
  168. {
  169. EncodingConverion::UTF82ASCII(cJSON_GetObjectItem(pSubItem,"path")->valuestring,strJson);
  170. g_AryCustomerImage.Add(strJson.c_str());
  171. }
  172. }
  173. cJSON_Delete(pJson);
  174. pJson = NULL;
  175. return TRUE;
  176. }
  177. /************************************************************************/
  178. /* 函数:WriteTextLog[7/28/2016 IT];
  179. /* 描述:写文本日志;
  180. /* 参数:;
  181. /* [IN] :;
  182. /* 返回:void;
  183. /* 注意:;
  184. /* 示例:;
  185. /*
  186. /* 修改:;
  187. /* 日期:;
  188. /* 内容:;
  189. /************************************************************************/
  190. void WriteTextLog(const TCHAR *format, ...)
  191. {
  192. try
  193. {
  194. // 解析出日志路径;
  195. static TCHAR szlogpath[MAX_PATH] = {0};
  196. if ( szlogpath[0] == _T('\0') )
  197. {
  198. TCHAR szDrive[_MAX_DRIVE] = { 0 };
  199. TCHAR szDir[_MAX_DIR] = { 0 };
  200. TCHAR szFna[_MAX_DIR] = { 0 };
  201. TCHAR szExt[_MAX_DIR] = { 0 };
  202. TCHAR szModulePath[MAX_PATH] = {0};
  203. ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
  204. _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
  205. _tcscpy_s(szModulePath, szDrive);
  206. _tcscat_s(szModulePath, szDir);
  207. _stprintf_s(szlogpath, _T("%s\\log.txt"), szModulePath);
  208. }
  209. // 打开或创建文件;
  210. CStdioFile fp;
  211. if (PathFileExists(szlogpath))
  212. {
  213. if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
  214. {
  215. return;
  216. }
  217. ULONGLONG length = fp.GetLength();
  218. if (length > 10 * 1024 * 1024) // 10M;
  219. {
  220. fp.Close();
  221. ::DeleteFile(szlogpath);
  222. return;
  223. }
  224. fp.SeekToEnd();
  225. }
  226. else
  227. {
  228. if ( !fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite) )
  229. return;
  230. }
  231. // 格式化前设置语言区域;
  232. TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
  233. _tsetlocale(LC_CTYPE, _T("chs"));//设定中文;
  234. // 格式化日志内容;
  235. va_list args = NULL;
  236. int len = 0;
  237. TCHAR *buffer = NULL;
  238. va_start( args, format );
  239. // _vscprintf doesn't count. terminating '\0'
  240. len = _vsctprintf( format, args );
  241. if ( len == -1 )
  242. {
  243. goto clear;
  244. }
  245. len++;
  246. buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
  247. _vstprintf_s( buffer, len, format, args ); // C4996
  248. // Note: vsprintf is deprecated; consider using vsprintf_s instead
  249. // 将日志内容输入到文件中;
  250. //fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
  251. fp.WriteString(buffer);
  252. fp.WriteString(_T("\n"));
  253. // 关闭文件,释放资源并设置回原语言区域;
  254. free( buffer );
  255. clear:
  256. _tsetlocale(LC_CTYPE, old_locale);
  257. free(old_locale);//还原区域设定;
  258. fp.Close();
  259. }
  260. catch (CException *e)
  261. {
  262. e->ReportError();
  263. e->Delete();
  264. }
  265. }
  266. }