123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- // stdafx.cpp : 只包括标准包含文件的源文件
- // Renderings.pch 将作为预编译头
- // stdafx.obj 将包含预编译类型信息
- #include "stdafx.h"
- #include <Shlwapi.h>
- #include <math.h>
- namespace lyfzGlobal
- {
- TCHAR g_szModulePath[MAX_PATH] = {0};
- CString g_strJson = _T("");
- CString g_strCustomerImagePath;
- CString g_strPathOfPNG = _T("");
- CStringArray g_AryCustomerImage;
- int g_nScreenWidth; // 桌面宽;
- int g_nScreenHeight; // 桌面高;
- void GetDesktopInfo()
- {
- TCHAR szDrive[_MAX_DRIVE] = { 0 };
- TCHAR szDir[_MAX_DIR] = { 0 };
- TCHAR szFna[_MAX_DIR] = { 0 };
- TCHAR szExt[_MAX_DIR] = { 0 };
- ::GetModuleFileName(NULL, g_szModulePath, sizeof(g_szModulePath) / sizeof(TCHAR));
- _tsplitpath_s(g_szModulePath, szDrive, _MAX_DRIVE, szDir, _MAX_DIR, szFna, _MAX_DIR, szExt, _MAX_DIR);
- _tcscpy_s(g_szModulePath, MAX_PATH, szDrive);
- _tcscat_s(g_szModulePath, MAX_PATH, szDir);
- //////////////////////////////////////////////////////////////////////////
- g_nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
- g_nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
- // 系统任务栏的高度;
- HWND hWnd = ::FindWindow(_T("Shell_TrayWnd"), NULL);
- RECT rcTryaWnd;
- ::GetWindowRect(hWnd, &rcTryaWnd);
- int nHeight = rcTryaWnd.bottom - rcTryaWnd.top;
- #if 0
- g_nScreenHeight -= nHeight;
- #endif
- }
- // GetWidth()的长度与CRect的长度是不一样的单位值,需要转换适合的比例;(一个是图片的像素尺寸,一个DC的大小尺寸)
- // 将图片尺寸与设备尺寸进行转换适合的比例;
- void RectFitDes(int width, int height, CRect &rc)
- {
- try
- {
- if(width==0 || height==0)return;
- // 图片长宽比例;
- float fscale=(float)width/(float)height;
- // 设备长宽比例;
- float rcscale=((float)rc.Width())/((float)rc.Height());
- int rcwid=rc.Width();
- int rchei=rc.Height();
- int dt=0;
- // Jeff.如果设备长宽比例 < 图片长宽比例;(即相同长度下,高越大比例值越小,所以此时图片尺寸 与 显示设备的尺寸相比,要显得更长)
- if(rcscale<fscale)
- {
- // Jeff.remarks
- // 调整显示设备的大小,以使之能容纳图片尺寸;(即长宽比例上,要大于或等于图片的长宽比例)
- // 所以有两种方法使得 rcscale >= fscale 表达式成立:
- // -----------------------------------------------
- // 方法1:显示设备宽加x值,计算出下面表达式x的值即可
- // (rc.Width()+x) / rc.Height() >= width/height;
- // 方法2:显示设备高减x值,计算出下面表达式x的值即可
- // (rc.Width()) / (rc.Height()-x) >= width/height;
- //------------------------------------------------
- // 两种方法的最后表达式为:
- // x >= rc.Height() - rcWidth()*(height/width);
- // 即 x >= rc.Height() - rcWidth()/fscale;
- //------------------------------------------------
- dt=(rchei-rcwid/fscale)/2;
- rc.top+=dt;
- rc.bottom-=dt;
- }
- else
- {
- dt=(rcwid-rchei*fscale)/2;
- rc.left +=dt;
- rc.right-=dt;
- }
- }
- catch(...)
- {
- }
- }
- int GetFileNo2()
- {
- CString path;
- int begin=1;
- path.Format(_T("%s\\效果图-%d.png"), g_strPathOfPNG, begin);
- while(PathFileExists(path))
- {
- begin++;
- path.Format(_T("%s\\效果图-%d.png"), g_strPathOfPNG, begin);
- }
- return begin;
- }
- /************************************************************************/
- /* 函数:[4/9/2017 Jeff];
- /* 描述:使用勾股定理,通过两直角边计算出斜边;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- int GetLengFromPt(CPoint pt1, CPoint pt2)
- {
- // c*c = a*a + b*b;
- return (pt1.x-pt2.x)*(pt1.x-pt2.x)+(pt1.y-pt2.y)*(pt1.y-pt2.y);
- }
- int GetLengFromPt2(CPoint pt1, CPoint pt2)
- {
- return sqrt((float)(pt1.x-pt2.x)*(pt1.x-pt2.x)+(pt1.y-pt2.y)*(pt1.y-pt2.y));
- }
- BOOL GetJsonInfo(IN CString& strJsonpath)
- {
- if ( !PathFileExists(strJsonpath) )
- return FALSE;
- CFile cf;
- BYTE *pJsonBuf = NULL;
- DWORD dwJsonlen = 0;
- if ( cf.Open(strJsonpath, CFile::modeReadWrite) )
- {
- dwJsonlen = cf.GetLength();
- pJsonBuf = new BYTE[dwJsonlen+1];
- memset(pJsonBuf, 0, dwJsonlen+1);
- cf.Read(pJsonBuf, dwJsonlen);
- cf.Close();
- }
- else
- {
- return FALSE;
- }
- cJSON *pJson = NULL;
- pJson = cJSON_Parse((char*)pJsonBuf);
- if ( pJsonBuf )
- delete []pJsonBuf;
- pJsonBuf = NULL;
- if ( pJson == NULL )
- return FALSE;
- cJSON *pJsonItem = cJSON_GetObjectItem(pJson, "png");
- if ( pJsonItem == NULL )
- {
- cJSON_Delete(pJson);
- return FALSE;
- }
-
- string strJson = "";
- EncodingConverion::UTF82ASCII(pJsonItem->valuestring, strJson);
- g_strPathOfPNG = strJson.c_str();
- pJsonItem = cJSON_GetObjectItem(pJson, "jpg");
- if ( pJsonItem == NULL )
- {
- cJSON_Delete(pJson);
- return FALSE;
- }
- cJSON *pSubItem = NULL;
- INT nSize = cJSON_GetArraySize(pJsonItem);
-
- g_AryCustomerImage.SetSize(nSize, 1);
- g_AryCustomerImage.RemoveAll();
-
- for ( int i = 0; i < nSize; i++ )
- {
- pSubItem = cJSON_GetArrayItem(pJsonItem, i);
- if ( pSubItem )
- {
- EncodingConverion::UTF82ASCII(cJSON_GetObjectItem(pSubItem,"path")->valuestring,strJson);
- g_AryCustomerImage.Add(strJson.c_str());
- }
- }
- cJSON_Delete(pJson);
- pJson = NULL;
- return TRUE;
- }
- /************************************************************************/
- /* 函数:WriteTextLog[7/28/2016 IT];
- /* 描述:写文本日志;
- /* 参数:;
- /* [IN] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- void WriteTextLog(const TCHAR *format, ...)
- {
- try
- {
- // 解析出日志路径;
- static TCHAR szlogpath[MAX_PATH] = {0};
- if ( szlogpath[0] == _T('\0') )
- {
- TCHAR szDrive[_MAX_DRIVE] = { 0 };
- TCHAR szDir[_MAX_DIR] = { 0 };
- TCHAR szFna[_MAX_DIR] = { 0 };
- TCHAR szExt[_MAX_DIR] = { 0 };
- TCHAR szModulePath[MAX_PATH] = {0};
- ::GetModuleFileName(NULL, szModulePath, sizeof(szModulePath) / sizeof(TCHAR));
- _tsplitpath_s(szModulePath, szDrive, szDir, szFna, szExt);
- _tcscpy_s(szModulePath, szDrive);
- _tcscat_s(szModulePath, szDir);
- _stprintf_s(szlogpath, _T("%s\\log.txt"), szModulePath);
- }
- // 打开或创建文件;
- CStdioFile fp;
- if (PathFileExists(szlogpath))
- {
- if (fp.Open(szlogpath, CFile::modeWrite) == FALSE)
- {
- return;
- }
- ULONGLONG length = fp.GetLength();
- if (length > 10 * 1024 * 1024) // 10M;
- {
- fp.Close();
- ::DeleteFile(szlogpath);
- return;
- }
- fp.SeekToEnd();
- }
- else
- {
- if ( !fp.Open(szlogpath, CFile::modeCreate | CFile::modeWrite) )
- 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 );
- if ( len == -1 )
- {
- goto clear;
- }
- len++;
- buffer = (TCHAR*)malloc( len * sizeof(TCHAR) );
- _vstprintf_s( buffer, len, format, args ); // C4996
- // Note: vsprintf is deprecated; consider using vsprintf_s instead
- // 将日志内容输入到文件中;
- //fp.WriteString( CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S ")) );
- fp.WriteString(buffer);
- fp.WriteString(_T("\n"));
- // 关闭文件,释放资源并设置回原语言区域;
- free( buffer );
- clear:
- _tsetlocale(LC_CTYPE, old_locale);
- free(old_locale);//还原区域设定;
- fp.Close();
- }
- catch (CException *e)
- {
- e->ReportError();
- e->Delete();
- }
- }
- }
|