📄 commonutil.cpp
字号:
// CommonUtil.cpp: implementation of the CCommonUtil class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "BaseFunc.h"
#include "CommonUtil.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
extern CBaseFuncApp theApp;
CCommonUtil::CCommonUtil()
{
}
CCommonUtil::~CCommonUtil()
{
}
CString CCommonUtil::VarToStr(_variant_t &tVariant, LPCTSTR lNullReturnValue, LPCTSTR lErrReturnValue)
{
CString sReturnValue;
try
{
if(tVariant.vt == VT_NULL)
sReturnValue = lNullReturnValue;
else if(tVariant.vt == VT_R4)
{
sReturnValue.Format(_T("%.12f"),tVariant.fltVal);
sReturnValue.TrimRight("0");
sReturnValue.TrimRight(".");
}
else if(tVariant.vt == VT_R8)
{
sReturnValue.Format(_T("%.12f"),tVariant.dblVal);
sReturnValue.TrimRight("0");
sReturnValue.TrimRight(".");
}
else
{
tVariant.ChangeType(VT_BSTR);
sReturnValue = tVariant.bstrVal;
if(sReturnValue.IsEmpty())
sReturnValue = lNullReturnValue;
}
}
catch(_com_error &e)
{
_bstr_t err = e.Description();
return(lErrReturnValue);
}
return(sReturnValue);
}
long CCommonUtil::VarToLng(_variant_t &tVariant, long lNullReturnValue, long lErrReturnValue)
{
long lReturnValue;
try
{
if(tVariant.vt == VT_NULL)
lReturnValue = lNullReturnValue;
else
{
tVariant.ChangeType(VT_I4);
lReturnValue = tVariant.lVal;
}
}
catch(_com_error &e)
{
_bstr_t err = e.Description();
return(lErrReturnValue);
}
return(lReturnValue);
}
double CCommonUtil::VarToDb(_variant_t &tVariant, double dbNullReturnValue, double dbErrReturnValue)
{
double dbReturnValue;
try
{
if(tVariant.vt == VT_NULL)
dbReturnValue = dbNullReturnValue;
else
{
tVariant.ChangeType(VT_R8);
dbReturnValue = tVariant.dblVal;
}
}
catch(_com_error &e)
{
_bstr_t err = e.Description();
return(dbErrReturnValue);
}
return(dbReturnValue);
}
bool CCommonUtil::VarToBool(_variant_t &tVariant,bool bNullReturnValue,bool bErrReturnValue)
{
bool bReturnValue;
try
{
if(tVariant.vt == VT_NULL)
bReturnValue = bNullReturnValue;
else
{
tVariant.ChangeType(VT_BOOL);
bReturnValue = tVariant.boolVal?true:false;
}
}
catch(_com_error &e)
{
_bstr_t err = e.Description();
return(bErrReturnValue);
}
return(bReturnValue);
}
void CCommonUtil::InitComboCtrl(CComboBox *pCtrl,_RecordsetPtr pRec,long lShowItemData)
{
int iCurSel = -1;
pCtrl->ResetContent();
while(!pRec->GetadoEOF())
{
int iItem = pCtrl->AddString(VarToStr(pRec->GetCollect("GDESC")));
long lItemData = VarToLng(pRec->GetCollect("SEQID"));
pCtrl->SetItemData(iItem,lItemData);
if(lItemData == lShowItemData)
iCurSel = iItem;
pRec->MoveNext();
}
if(iCurSel >= 0)
pCtrl->SetCurSel(iCurSel);
/*
else if(pCtrl->GetCount() > 0)
pCtrl->SetCurSel(0);
*/
}
void CCommonUtil::SetComboCtrlSel(CComboBox *pCtrl,long lItemData)
{
for(int i=0;i<pCtrl->GetCount();i++)
{
if(lItemData == (long)pCtrl->GetItemData(i))
{
pCtrl->SetCurSel(i);
break;
}
}
}
void CCommonUtil::SetComboCtrlSel(CComboBox *pCtrl,CString sItemText)
{
for(int i=0;i<pCtrl->GetCount();i++)
{
CString sText;
pCtrl->GetLBText(i,sText);
if(sItemText == sText)
{
pCtrl->SetCurSel(i);
break;
}
}
}
void CCommonUtil::CustomButton(HINSTANCE hInstance,CBCGPButton *pButton,UINT uiBmpResId, UINT uiBmpHotResId,CString sTip,CBCGPButton::FlatStyle euFlatStyle)
{
HINSTANCE hOld = AfxGetResourceHandle();
AfxSetResourceHandle(hInstance);
//pButton->m_bDrawFocus = FALSE;
pButton->m_bTransparent = TRUE;
pButton->m_nFlatStyle = euFlatStyle/*CBCGPButton::BUTTONSTYLE_FLAT*/;
pButton->SetImage (uiBmpResId,uiBmpHotResId);
if(sTip != "")
pButton->SetTooltip (sTip);
pButton->SizeToContent ();
pButton->Invalidate ();
AfxSetResourceHandle(hOld);
}
void CCommonUtil::CustomButtonURLLink(HINSTANCE hInstance,CBCGPButton *pButton,CFont *pFont,
COLORREF cTextcol, COLORREF cHotTextcol,CString sTip)
{
HINSTANCE hOld = AfxGetResourceHandle();
AfxSetResourceHandle(hInstance);
pButton->SetFont(pFont);
pButton->SetTextColor(cTextcol);
pButton->SetTextHotColor(cHotTextcol);
pButton->SetMouseCursorHand();
pButton->m_bDrawFocus = FALSE;
pButton->m_bTransparent = TRUE;
pButton->m_nFlatStyle = CBCGPButton::BUTTONSTYLE_NOBORDERS;
if(sTip != "")
pButton->SetTooltip (sTip);
pButton->SizeToContent ();
pButton->Invalidate ();
AfxSetResourceHandle(hOld);
}
bool CCommonUtil::CreateVariantFromFile(_variant_t &vr, LPCTSTR lpszFileName)
{
CFile file;
if(file.Open(lpszFileName,CFile::modeRead))
{
DWORD length = file.GetLength();
if(length > 0)
{
BYTE *pBuf = new BYTE[length];
file.Read(pBuf, length);
SAFEARRAYBOUND bound;
bound.lLbound = 0;
bound.cElements = length;
SAFEARRAY *pArray = ::SafeArrayCreate(VT_UI1,1,&bound);
void *pData;
::SafeArrayAccessData(pArray,&pData);
memcpy(pData,pBuf,length);
::SafeArrayUnaccessData(pArray);
vr.vt = VT_ARRAY|VT_UI1;
vr.parray = pArray;
delete pBuf;
}
file.Close();
return true;
}
else
return false;
}
bool CCommonUtil::CreateFileFromVariant(LPCTSTR lpszFileName, _variant_t &vrData)
{
bool bVariant = false;
CFile fileObj;
if(fileObj.Open(lpszFileName, CFile::modeCreate|CFile::modeWrite))
{
long size;
SafeArrayGetUBound(vrData.parray,1,&size);
void *pData = NULL;
::SafeArrayAccessData(vrData.parray,&pData);
fileObj.Write(pData,size);
::SafeArrayUnaccessData(vrData.parray);
fileObj.Flush();
fileObj.Close();
}
else
{
}
return(bVariant);
}
bool CCommonUtil::CopyPictureToClipboard(LPCTSTR lpszFileName)
{
CFile file;
if(!file.Open(lpszFileName, CFile::modeRead))
return false;
int size = file.GetLength();
if(size == 0)
{
file.Close();
return false;
}
HGLOBAL hMem = ::GlobalAlloc(GMEM_FIXED, size);
void *pData = ::GlobalLock(hMem);
file.Read(pData, size);
file.Close();
::GlobalUnlock(hMem);
IStreamPtr pStream;
::CreateStreamOnHGlobal(hMem, TRUE, &pStream);
IPicturePtr pPic;
::OleLoadPicture(pStream, 0, TRUE, IID_IPicture, (void **)&pPic);
long height, width;
pPic->get_Width(&width);
pPic->get_Height(&height);
HDC hdc = ::GetDC(NULL);
CDC *pSrcDC = CDC::FromHandle(hdc);
CSize sz(width, height);
pSrcDC->HIMETRICtoLP(&sz);
CDC mdc;
mdc.CreateCompatibleDC(pSrcDC);
CBitmap bmp;
bmp.CreateCompatibleBitmap(pSrcDC, sz.cx, sz.cy);
mdc.SelectObject(&bmp);
::ReleaseDC(NULL, hdc);
pPic->Render(mdc, 0, 0, sz.cx, sz.cy, 0, height, width, -height, NULL);
if(::OpenClipboard(NULL))
{
::EmptyClipboard();
::SetClipboardData(CF_BITMAP, bmp.m_hObject);
::CloseClipboard();
}
return true;
}
CString CCommonUtil::GetSysPath()
{
CString sReturn;
//取得系统路径
TCHAR buf[_MAX_PATH];
::GetModuleFileName(AfxGetApp()->m_hInstance, buf, _MAX_PATH);
CString str = buf;
int pos = str.ReverseFind(_T('\\'));
if(pos == -1)
return "";
sReturn = str.Left(pos + 1);
return sReturn;
}
bool CCommonUtil::DeleteDirectory_HaveChild(LPCTSTR sDirectory)
{
//删除路径
CFileException err;
CStdioFile File;
CString sBatFilePath = GetSysPath() + "RDFILE.bat";
File.Open(sBatFilePath, CFile::modeCreate | CFile::modeWrite, &err);
CString sCommandString;
sCommandString.Format("rd /s/q \"%s\" \n",sDirectory);
File.WriteString(sCommandString);
File.Flush();
File.Close();
CString sCommand = sBatFilePath;
STARTUPINFO sInfo;
ZeroMemory(&sInfo,sizeof(sInfo));
sInfo.cb = sizeof(sInfo);
PROCESS_INFORMATION pInfo;
::CreateProcess(
NULL,sCommand.LockBuffer(),
NULL,NULL,FALSE,CREATE_NO_WINDOW,NULL,NULL,
&sInfo,&pInfo);
sCommand.UnlockBuffer();
::WaitForSingleObject(pInfo.hProcess, INFINITE);
::CloseHandle(pInfo.hProcess);
::CloseHandle(pInfo.hThread);
CFile::Remove(sBatFilePath);
return true;
}
bool CCommonUtil::CopyDirectory_HaveChild(
CString sExistDirectory,CString sAimDirectory)
{
bool bReturn = true;
//预处理,去掉最后面的\号
if(sExistDirectory.Right(1).CompareNoCase("\\") == 0)
sExistDirectory = sExistDirectory.Left(sExistDirectory.GetLength()-1);
if(sAimDirectory.Right(1).CompareNoCase("\\") != 0)
sAimDirectory += "\\";
//取得系统文件,用于XCOPY
TCHAR sWinPath[_MAX_PATH];
GetSystemDirectory(sWinPath,_MAX_PATH);
CString sCommand;
sCommand.Format("%s\\xcopy.exe \"%s\" \"%s\" /e /y",sWinPath,sExistDirectory,sAimDirectory);
STARTUPINFO sInfo;
ZeroMemory(&sInfo,sizeof(sInfo));
sInfo.cb = sizeof(sInfo);
PROCESS_INFORMATION pInfo;
if(FALSE == ::CreateProcess(
NULL,sCommand.LockBuffer(),
NULL,NULL,FALSE,CREATE_NO_WINDOW,NULL,NULL,
&sInfo,&pInfo))
bReturn = false;
sCommand.UnlockBuffer();
::WaitForSingleObject(pInfo.hProcess, INFINITE);
::CloseHandle(pInfo.hProcess);
::CloseHandle(pInfo.hThread);
return bReturn;
}
bool CCommonUtil::CreateDirectory_HaveChild(LPCTSTR sDirectory)
{
CFileException err;
CStdioFile File;
CString sBatFilePath = GetSysPath() + "MDFILE.bat";
File.Open(sBatFilePath, CFile::modeCreate | CFile::modeWrite, &err);
CString sCommandString;
sCommandString.Format("md \"%s\" \n",sDirectory);
File.WriteString(sCommandString);
File.Flush();
File.Close();
CString sCommand = sBatFilePath;
STARTUPINFO sInfo;
ZeroMemory(&sInfo,sizeof(sInfo));
sInfo.cb = sizeof(sInfo);
PROCESS_INFORMATION pInfo;
::CreateProcess(
NULL,sCommand.LockBuffer(),
NULL,NULL,FALSE,CREATE_NO_WINDOW,NULL,NULL,
&sInfo,&pInfo);
sCommand.UnlockBuffer();
::WaitForSingleObject(pInfo.hProcess, INFINITE);
::CloseHandle(pInfo.hProcess);
::CloseHandle(pInfo.hThread);
CFile::Remove(sBatFilePath);
return true;
}
bool CCommonUtil::RunBatFile(LPCTSTR pzsFilePath)
{
CString sCommand = pzsFilePath;
CString sPath = sCommand.Left(sCommand.ReverseFind('\\'));
STARTUPINFO sInfo;
ZeroMemory(&sInfo,sizeof(sInfo));
sInfo.cb = sizeof(sInfo);
PROCESS_INFORMATION pInfo;
::CreateProcess(
NULL,sCommand.LockBuffer(),
NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,sPath,
&sInfo,&pInfo);
sCommand.UnlockBuffer();
::WaitForSingleObject(pInfo.hProcess, INFINITE);
::CloseHandle(pInfo.hProcess);
::CloseHandle(pInfo.hThread);
return true;
}
bool CCommonUtil::RunCommand(LPCTSTR pzsCommand,DWORD dwCreationFlags)
{
CString sCommand = pzsCommand;
STARTUPINFO sInfo;
ZeroMemory(&sInfo,sizeof(sInfo));
sInfo.cb = sizeof(sInfo);
PROCESS_INFORMATION pInfo;
::CreateProcess(
NULL,sCommand.LockBuffer(),
NULL,NULL,FALSE,dwCreationFlags,NULL,NULL,
&sInfo,&pInfo);
sCommand.UnlockBuffer();
::WaitForSingleObject(pInfo.hProcess, INFINITE);
::CloseHandle(pInfo.hProcess);
::CloseHandle(pInfo.hThread);
return true;
}
bool CCommonUtil::CheckNum(CString sText)
{
if(sText.IsEmpty())
return false;
int sl=sText.GetLength();
if(sText[0] < 45 || sText[0] > 57 || sText[0] == 47)
return false;
for(int i=1;i<sl;i++)
{
if(sText[i] < 46 || sText[i] > 57 || sText[i] == 47)
return false;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -