📄 global.cpp
字号:
//////////////////////////////////////////////////////////////////////////
// Global.cpp
// implement of global functions
//////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Global.h"
// global variables
// global functions
BOOL IsContainInvalidChars(const TCHAR* l_pchCheckString)
{
while(*l_pchCheckString != '\0')
{
if(!isalnum(*l_pchCheckString))
return TRUE;
l_pchCheckString++;
}
return FALSE;
}
HICON GetFileIcon(CString strFilePath, BOOL bLargeIcon)
{
SHFILEINFO sfi;
// retrieve the small icon
memset(&sfi, 0, sizeof(sfi));
SHGetFileInfo(strFilePath, FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), SHGFI_ICON | ((bLargeIcon)?SHGFI_LARGEICON:SHGFI_SMALLICON));
return sfi.hIcon;
}
CString GetAppPath()
{
char buf[1024];
memset(buf, 0, 1024);
int nLength = GetModuleFileName(NULL, buf, 1024);
for(int i = nLength - 1;;i --){
char ch = buf[i];
buf[i] = 0;
if(ch == '\\' || i <= 0)
break;
}
return CString(buf);
}
BOOL IsFileExist(CString strFilePath)
{
CFileStatus rStatus;
return CFile::GetStatus(strFilePath, rStatus);
}
CString GetFileDir(CString strFilePath)
{
CFileFind finder;
if(finder.FindFile(strFilePath)){
finder.FindNextFile();
return finder.GetRoot();
}
return _T("");
}
CString SelectFolder(HWND hWnd)
{
CString strResult;
strResult.Empty();
LPMALLOC lpMalloc; // pointer to IMalloc
if (::SHGetMalloc(&lpMalloc) != NOERROR) {
AfxMessageBox("Path operation error!");
return _T("");
}
char szDisplayName[_MAX_PATH];
char szBuffer[_MAX_PATH];
BROWSEINFO browseInfo;
browseInfo.hwndOwner = hWnd;
// set root at Desktop
browseInfo.pidlRoot = NULL;
browseInfo.pszDisplayName = szDisplayName;
browseInfo.lpszTitle = "Please select the path";
browseInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;
browseInfo.lpfn = NULL; // not used
browseInfo.lParam = 0; // not used
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("Fail to get directory!", MB_ICONSTOP|MB_OK);
return _T("");
}
// We have a path in szBuffer! Return it.
strResult = szBuffer;
}
else{
// The thing referred to by lpItemIDList
// might not have been a file system object.
// For whatever reason, SHGetPathFromIDList didn't work!
AfxMessageBox("Fail to get directory!", MB_ICONSTOP|MB_OK);
return _T("");
}
lpMalloc->Free(lpItemIDList);
lpMalloc->Release();
}
return strResult;
}
CString GetFileExt(CString strFilePath)
{
CString strExt = _T("");
int nPos = strFilePath.ReverseFind('.');
if(nPos != -1){
strExt = strFilePath.Mid(nPos + 1);
}
return strExt;
}
void SetFullScreen(HWND hWnd, BOOL bFullScreen)
{
if(!hWnd)
return ;
if(bFullScreen){
LONG style = GetWindowLong(hWnd, GWL_STYLE);
::ShowWindow(hWnd, SW_MAXIMIZE);
style = GetWindowLong(hWnd, GWL_STYLE);
style &= ~(WS_DLGFRAME | WS_THICKFRAME);
SetWindowLong(hWnd, GWL_STYLE, style);
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);
::SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, cx, cy, SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_SHOWWINDOW);
}
else{
LONG style = GetWindowLong(hWnd, GWL_STYLE);
style |= WS_DLGFRAME | WS_THICKFRAME;
SetWindowLong(hWnd, GWL_STYLE, style);
SetWindowPos(hWnd, HWND_NOTOPMOST, 0,0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
ShowWindow(hWnd, SW_NORMAL);
}
}
BOOL SetWallpaper(CString strFilePath)
{
char buf[_MAX_PATH];
memset(buf, 0, _MAX_PATH);
GET_BUFFER(buf, strFilePath);
return SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (void *)buf, SPIF_UPDATEINIFILE);
}
// system related functions
CRect GetWorkAreaRect()
{
CRect rect;
SystemParametersInfo(SPI_GETWORKAREA, 0, rect, 0);
return rect;
}
CRect GetScreenRect()
{
CRect rect;
rect.left = rect.top = 0;
rect.right = GetSystemMetrics(SM_CXSCREEN);
rect.bottom = GetSystemMetrics(SM_CYSCREEN);
return rect;
}
BOOL IsInHotArea()
{
static SIZE szHotArea = {96, 8};
CRect rcScreen = GetScreenRect();
CPoint ptCur;
GetCursorPos(&ptCur);
return
ptCur.x >= rcScreen.right - szHotArea.cx &&
ptCur.x <= rcScreen.right &&
ptCur.y >= rcScreen.bottom - szHotArea.cy &&
ptCur.y <= rcScreen.bottom ;
}
// window related functions
void SetWndTopMost(HWND hWnd, BOOL bTopMost)
{
if(!IsWindow(hWnd))
return ;
::SetWindowPos(
hWnd,
bTopMost?HWND_TOPMOST:HWND_NOTOPMOST,
0, 0, 0, 0,
SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
}
CString GetShort(UINT id)
{
CString str;
str.LoadString(id);
int nIndex = str.ReverseFind(_T('\n'));
if(nIndex!=-1)
{
str=str.Mid(nIndex+1);
}
return str;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -