📄 stdafx.cpp
字号:
// stdafx.cpp : source file that includes just the standard includes
// Server.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
/********************************************************************/
/* */
/* Function name : WaitWithMessageLoop */
/* Description : Pump messages while waiting for event */
/* */
/********************************************************************/
BOOL WaitWithMessageLoop(HANDLE hEvent, int nTimeout)
{
DWORD dwRet;
DWORD dwMaxTick = GetTickCount() + nTimeout;
while (1)
{
// wait for event or message, if it's a message, process it and return to waiting state
dwRet = MsgWaitForMultipleObjects(1, &hEvent, FALSE, dwMaxTick - GetTickCount(), QS_ALLINPUT);
if (dwRet == WAIT_OBJECT_0)
{
TRACE0("WaitWithMessageLoop() event triggered.\n");
return TRUE;
}
else
if (dwRet == WAIT_OBJECT_0 + 1)
{
// process window messages
DoEvents();
}
else
{
// timed out !
return FALSE;
}
}
}
void DoEvents()
{
MSG msg;
// window message
while (PeekMessage(&msg,NULL,NULL,NULL,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
/********************************************************************/
/* */
/* Function name : BrowseForFolder */
/* Description : Browse for folder using SHBrowseForFolder. */
/* */
/********************************************************************/
CString BrowseForFolder(HWND hWnd, LPCSTR lpszTitle, UINT nFlags)
{
// We're going to use the shell to display a
// "Choose Directory" dialog box for the user.
CString strResult = "";
LPMALLOC lpMalloc;
if (::SHGetMalloc(&lpMalloc) != NOERROR)
{
// failed to get allocator
return strResult;
}
char szBuffer[_MAX_PATH];
char szDisplayName[_MAX_PATH];
BROWSEINFO browseInfo;
browseInfo.hwndOwner = hWnd;
// set root at Desktop
browseInfo.pidlRoot = NULL;
browseInfo.pszDisplayName = szDisplayName;
browseInfo.lpszTitle = lpszTitle;
browseInfo.ulFlags = nFlags;
browseInfo.lpfn = NULL;
browseInfo.lParam = 0;
LPITEMIDLIST lpItemIDList;
if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo)) != NULL)
{
// Get the path of the selected folder from the item ID list.
if (::SHGetPathFromIDList(lpItemIDList, szBuffer))
{
// At this point, szBuffer contains the path the user chose.
if (szBuffer[0] == '\0')
{
// SHGetPathFromIDList failed, or SHBrowseForFolder failed.
AfxMessageBox("Failed to get directory", MB_ICONSTOP|MB_OK);
return strResult;
}
// We have a path in szBuffer!
strResult = szBuffer;
return strResult;
}
else
{
// The thing referred to by lpItemIDList
// might not have been a file system object.
// For whatever reason, SHGetPathFromIDList didn't work!
AfxMessageBox("Failed to get directory", MB_ICONSTOP|MB_OK);
return strResult; // strResult is empty
}
lpMalloc->Free(lpItemIDList);
lpMalloc->Release();
}
return strResult;
}
/********************************************************************/
/* */
/* Function name : MakeSureDirectoryPathExists */
/* Description : This function creates all the directories in */
/* the specified DirPath, beginning with the root. */
/* This is a clone a Microsoft function with the */
/* same name. */
/* */
/********************************************************************/
BOOL MakeSureDirectoryPathExists(LPCTSTR lpszDirPath)
{
CString strDirPath = lpszDirPath;
int nPos = 0;
while((nPos = strDirPath.Find('\\', nPos+1)) != -1)
{
CreateDirectory(strDirPath.Left(nPos), NULL);
}
return CreateDirectory(strDirPath, NULL);
}
/********************************************************************/
/* */
/* Function name : FileExists */
/* Description : Check if file or directory exists */
/* */
/********************************************************************/
BOOL FileExists(LPCTSTR lpszFileName, BOOL bIsDirCheck)
{
// A quick'n'easy way to see if a file exists.
DWORD dwAttributes = GetFileAttributes(lpszFileName);
if (dwAttributes == 0xFFFFFFFF)
return FALSE;
if ((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
if (bIsDirCheck)
return TRUE;
else
return FALSE;
}
else
{
if (!bIsDirCheck)
return TRUE;
else
return FALSE;
}
}
/********************************************************************/
/* */
/* Function name : IsSubDirectory */
/* Description : Check if lpszSubDirectory is sub of lpszDirectory*/
/* */
/********************************************************************/
BOOL IsSubDirectory(LPCTSTR lpszDirectory, LPCTSTR lpszSubDirectory)
{
CString strDir1 = lpszDirectory;
CString strDir2 = lpszSubDirectory;
strDir1.MakeLower();
strDir2.MakeLower();
int nLength = strDir1.GetLength();
if (strDir2.Left(nLength) == strDir1)
return TRUE;
return FALSE;
}
/********************************************************************/
/* */
/* Function name : GetFileDate */
/* Description : return file date in unix style */
/* */
/********************************************************************/
CString GetFileDate(CFileFind &find)
{
CString strResult;
CTime time = CTime::GetCurrentTime();
find.GetLastWriteTime(time);
CTimeSpan timeSpan = CTime::GetCurrentTime() - time;
if (timeSpan.GetDays() > 356)
{
strResult = time.Format(" %b %d %Y ");
}
else
{
strResult.Format(" %s %02d:%02d ", time.Format("%b %d"), time.GetHour(), time.GetMinute());
}
return strResult;
}
CString ToString(int nValue)
{
CString strResult;
strResult.Format("%d", nValue);
return strResult;
}
/********************************************************************/
/* */
/* Function name : AutoSizeColumns */
/* Description : */
/* */
/********************************************************************/
void AutoSizeColumns(CListCtrl *pListCtrl)
{
// Call this after your the control is filled
pListCtrl->SetRedraw(FALSE);
int mincol = 0;
int maxcol = pListCtrl->GetHeaderCtrl()->GetItemCount()-1;
for (int col = mincol; col <= maxcol; col++)
{
pListCtrl->SetColumnWidth(col, LVSCW_AUTOSIZE);
int wc1 = pListCtrl->GetColumnWidth(col);
pListCtrl->SetColumnWidth(col, LVSCW_AUTOSIZE_USEHEADER);
int wc2 = pListCtrl->GetColumnWidth(col);
// 10 is minumim column width
int wc = max(10, max(wc1,wc2));
pListCtrl->SetColumnWidth(col,wc);
}
pListCtrl->SetRedraw(TRUE);
}
/********************************************************************/
/* */
/* Function name : AfxHexValue */
/* Description : Get the decimal value of a hexadecimal character */
/* */
/********************************************************************/
short AfxHexValue(char chIn)
{
unsigned char ch = (unsigned char)chIn;
if (ch >= '0' && ch <= '9')
return (short)(ch - '0');
if (ch >= 'A' && ch <= 'F')
return (short)(ch - 'A' + 10);
if (ch >= 'a' && ch <= 'f')
return (short)(ch - 'a' + 10);
return -1;
}
/********************************************************************/
/* */
/* Function name : AfxIsUnsafeUrlChar */
/* Description : Determine if character is unsafe */
/* */
/********************************************************************/
BOOL AfxIsUnsafeUrlChar(char chIn)
{
unsigned char ch = (unsigned char)chIn;
switch(ch)
{
case ';': case '\\': case '?': case '@': case '&':
case '=': case '+': case '$': case ',': case ' ':
case '<': case '>': case '#': case '%': case '\"':
case '{': case '}': case '|':
case '^': case '[': case ']': case '`':
return TRUE;
default:
{
if (ch < 32 || ch > 126)
return TRUE;
return FALSE;
}
}
}
/********************************************************************/
/* */
/* Function name : URLDecode */
/* Description : Convert: http%3A%2F%2Fwww%2Emicrosoft%2Ecom */
/* to: http://www.microsoft.com */
/* */
/********************************************************************/
CString URLDecode(LPCTSTR lpszURL)
{
CString strResult = "";
// Convert all escaped characters in lpszURL to their real values
int nValue = 0;
char ch;
BOOL bContinue = TRUE;
while ((ch = *lpszURL) != 0)
{
if (bContinue)
{
if (ch == '%')
{
if ((*(lpszURL+1) == '\0') || (*(lpszURL+2) == '\0'))
{
bContinue = FALSE;
break;
}
ch = *(++lpszURL);
// currently assuming 2 hex values after '%' as per the RFC 2396 document
nValue = 16*AfxHexValue(ch);
nValue+= AfxHexValue(*(++lpszURL));
strResult += (char)nValue;
}
else
// non-escape character
{
if (bContinue)
strResult += ch;
}
}
lpszURL++;
}
// replace '+' with " "
strResult.Replace("+", " ");
return strResult;
}
/************************************************************************/
/* */
/* Function name : CopyToCString */
/* Description : Copy a substring to a CString reference */
/* */
/************************************************************************/
void CopyToCString(CString& string, LPCSTR pStart, LPCSTR pEnd)
{
ASSERT( pStart != NULL );
ASSERT( pEnd != NULL );
int nLength = (int)(pEnd-pStart);
if( nLength <= 0 )
{
string.Empty();
}
else
{
LPSTR pszBuffer = string.GetBuffer(nLength);
memcpy(pszBuffer, pStart, nLength*sizeof(TCHAR));
string.ReleaseBuffer(nLength);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -