2#include <string/strcpcvt.h>
8#pragma comment(lib,"shlwapi.lib")
9#pragma comment(lib, "version.lib")
10#pragma warning(disable : 4996)
18int RectWidth(LPCRECT rc) {
return rc->right-rc->left;}
19int RectHeight(LPCRECT rc) {
return rc->bottom-rc->top;}
21#define HIMETRIC_PER_INCH 2540
22#define MAP_PIX_TO_LOGHIM(x,ppli) MulDiv(HIMETRIC_PER_INCH, (x), (ppli))
23#define MAP_LOGHIM_TO_PIX(x,ppli) MulDiv((ppli), (x), HIMETRIC_PER_INCH)
25HANDLE WINAPI LoadImageBuf(
const void* buf, UINT length, UINT type,
26 INT desiredx, INT desiredy, UINT loadflags);
28void SHiMetricToPixel(
const SIZEL * lpSizeInHiMetric, LPSIZEL lpSizeInPix)
33 HDC hDCScreen = GetDC(NULL);
34 nPixelsPerInchX = GetDeviceCaps(hDCScreen, LOGPIXELSX);
35 nPixelsPerInchY = GetDeviceCaps(hDCScreen, LOGPIXELSY);
36 ReleaseDC(NULL, hDCScreen);
38 lpSizeInPix->cx = MAP_LOGHIM_TO_PIX(lpSizeInHiMetric->cx, nPixelsPerInchX);
39 lpSizeInPix->cy = MAP_LOGHIM_TO_PIX(lpSizeInHiMetric->cy, nPixelsPerInchY);
42void SPixelToHiMetric(
const SIZEL * lpSizeInPix, LPSIZEL lpSizeInHiMetric)
47 HDC hDCScreen = GetDC(NULL);
48 nPixelsPerInchX = GetDeviceCaps(hDCScreen, LOGPIXELSX);
49 nPixelsPerInchY = GetDeviceCaps(hDCScreen, LOGPIXELSY);
50 ReleaseDC(NULL, hDCScreen);
52 lpSizeInHiMetric->cx = MAP_PIX_TO_LOGHIM(lpSizeInPix->cx, nPixelsPerInchX);
53 lpSizeInHiMetric->cy = MAP_PIX_TO_LOGHIM(lpSizeInPix->cy, nPixelsPerInchY);
56BOOL UpdateDIBPixmap(HBITMAP bmp,
int wid,
int hei,
int bitsPixel,
int stride, CONST VOID* pjBits) {
58 GetObject(bmp,
sizeof(bm), &bm);
61 if (bm.bmWidth != wid || bm.bmHeight != hei || bm.bmBitsPixel != bitsPixel)
64 memcpy(bm.bmBits, pjBits, hei * stride);
66 memset(bm.bmBits, 0, hei * stride);
71int Str2IntW(LPCWSTR src,BOOL supportHex)
74 StrToIntExW(src, supportHex?STIF_SUPPORT_HEX:STIF_DEFAULT, &nRet);
78int Str2IntA(LPCSTR src, BOOL supportHex)
81 StrToIntExA(src, supportHex ? STIF_SUPPORT_HEX : STIF_DEFAULT, &nRet);
85SNS::IStringA * CreateIStringA(LPCSTR src)
87 return new SNS::SStringA(src);
90SNS::IStringW * CreateIStringW(LPCWSTR src)
92 return new SNS::SStringW(src);
95SNS::IXmlDoc * CreateIXmlDoc()
97 return new SNS::SXmlDoc();
102static int getScaleOld(HWND hWnd = NULL)
104 HDC screen = ::GetDC(hWnd);
106 nScale = GetDeviceCaps(screen, LOGPIXELSX) * 100 / 96;
107 ReleaseDC(hWnd, screen);
112static BOOL PEVersion(LPCTSTR pszFileName, WORD& wMajor, WORD& wMinor, WORD& wVer3, WORD& wVer4)
116 DWORD dwVerInfoSize = GetFileVersionInfoSize(pszFileName, &dwResHandle);
119 pBuf = malloc(dwVerInfoSize);
122 GetFileVersionInfo(pszFileName, dwResHandle, dwVerInfoSize, pBuf);
124 VS_FIXEDFILEINFO* pstFileVersion;
125 if (VerQueryValue(pBuf, _T(
"\\"), (
void**)&pstFileVersion, &nVersionLen) && nVersionLen >=
sizeof(VS_FIXEDFILEINFO))
127 wVer4 = LOWORD(pstFileVersion->dwFileVersionLS);
128 wVer3 = HIWORD(pstFileVersion->dwFileVersionLS);
129 wMinor = LOWORD(pstFileVersion->dwFileVersionMS);
130 wMajor = HIWORD(pstFileVersion->dwFileVersionMS);
136static BOOL IsVerOrGreater(WORD wVers[4], WORD wMajor, WORD wMinor, WORD wSpBuild = 0)
138 if (wVers[0] < wMajor)
140 if (wVers[0] > wMajor)
142 if (wVers[1] < wMinor)
144 if (wVers[1] > wMinor)
147 return wVers[2] >= wSpBuild;
150int GetWindowScale(HWND hWnd)
153 PEVersion(_T(
"ntdll.dll"), wVers[0], wVers[1], wVers[2], wVers[3]);
155 if (!IsVerOrGreater(wVers, 6, 1, 7600))
159 if (IsVerOrGreater(wVers, 10, 0, 14955))
161 HMODULE hModule = LoadLibrary(_T(
"User32.dll"));
164 typedef UINT(WINAPI* FunGetDpiForWindow)(HWND);
166 FunGetDpiForWindow GetDpiForWindow = (FunGetDpiForWindow)GetProcAddress(hModule,
"GetDpiForWindow");
169 UINT dpi = GetDpiForWindow(hWnd);
170 nScale = dpi * 100 / 96;
172 FreeLibrary(hModule);
176 else if (IsVerOrGreater(wVers, 6, 3))
178 HMODULE hModule = LoadLibrary(_T(
"Shcore.dll"));
182 GetWindowRect(hWnd, &winrc);
183 HMONITOR hMonitor = MonitorFromRect(&winrc, MONITOR_DEFAULTTONEAREST);
185 typedef enum _MONITOR_DPI_TYPE
187 MDT_EFFECTIVE_DPI = 0,
190 MDT_DEFAULT = MDT_EFFECTIVE_DPI
192 typedef HRESULT(WINAPI* FunGetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*);
194 FunGetDpiForMonitor GetDpiForMonitor = (FunGetDpiForMonitor)GetProcAddress(hModule,
"GetDpiForMonitor");
195 if (GetDpiForMonitor)
197 HRESULT hr = GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
200 nScale = dpiX * 100 / 96;
203 FreeLibrary(hModule);
208 nScale = getScaleOld(hWnd);
214static int CALLBACK DefFontsEnumProc(CONST LOGFONT* lplf,
215 CONST TEXTMETRIC* lptm,
220 BOOL* pbValidFont = (BOOL*)lpData;
225BOOL HasFont(LPCTSTR fontName) {
226 HDC hdc = GetDC(NULL);
227 BOOL bValidFont = FALSE;
228 EnumFonts(hdc, fontName, DefFontsEnumProc, (LPARAM)&bValidFont);
229 ReleaseDC(NULL, hdc);
233BOOL IsSupportMaxMove()
235 OSVERSIONINFOEX OSVerInfo;
236 BOOL bOsVersionInfoEx;
238 ::ZeroMemory(&OSVerInfo,
sizeof(OSVERSIONINFOEX));
241 OSVerInfo.dwOSVersionInfoSize =
sizeof(OSVERSIONINFOEX);
242 bOsVersionInfoEx = ::GetVersionEx((OSVERSIONINFO*)&OSVerInfo);
243 if (!(bOsVersionInfoEx))
245 OSVerInfo.dwOSVersionInfoSize =
sizeof(OSVERSIONINFO);
246 if (!::GetVersionEx((OSVERSIONINFO*)&OSVerInfo))
258 if (OSVerInfo.wProductType == VER_NT_WORKSTATION && OSVerInfo.dwMajorVersion == 6)
265long file_length_A(
const char* path) {
266 WIN32_FIND_DATAA wfd;
267 HANDLE hf = FindFirstFileA(path, &wfd);
268 if (INVALID_HANDLE_VALUE == hf)
271 return wfd.nFileSizeLow;
274long file_length_W(
const wchar_t* path) {
275 WIN32_FIND_DATAW wfd;
276 HANDLE hf = FindFirstFileW(path, &wfd);
277 if (INVALID_HANDLE_VALUE == hf)
280 return wfd.nFileSizeLow;
285int GetWindowScale(HWND hWnd)
287 int dpi = GetDpiForWindow(hWnd);
291BOOL HasFont(LPCTSTR fontName) {
295BOOL IsSupportMaxMove() {
299long file_length(
const char* path)
302 int fd = stat(path, &st);
305 if (S_ISDIR(st.st_mode))
313HANDLE LoadIconFromMemory(
const void* buf, UINT cbSize, BOOL fIcon,
int width,
int height, UINT cFlag) {
314 UINT type = fIcon ? IMAGE_ICON : IMAGE_CURSOR;
315 return LoadImageBuf(buf, cbSize, type, width, height, cFlag);
320HRGN CreateRegionFromBitmap(HBITMAP hBmp, COLORREF crKey,COLORREF crMask)
323 GetObject(hBmp,
sizeof(bm),&bm);
324 if(bm.bmBitsPixel!=32 || bm.bmBits==0)
327 const COLORREF *bits = (
const COLORREF *)bm.bmBits;
329 SNS::SArray<RECT> lstRc;
330 for (
int y = 0; y < bm.bmHeight; y++, bits += bm.bmWidth)
333 while (x < bm.bmWidth)
335 while (x < bm.bmWidth && (bits[x] & crMask) == crKey)
338 while (x < bm.bmWidth && (bits[x] & crMask) != crKey)
342 RECT rc = { start, y, x, y + 1 };
347 if (!lstRc.IsEmpty())
349 size_t len =
sizeof(RGNDATAHEADER) +
sizeof(RECT) * lstRc.GetCount();
350 RGNDATA *pRgn = (RGNDATA *)malloc(len);
351 pRgn->rdh.nCount = (DWORD)lstRc.GetCount();
352 pRgn->rdh.iType = RDH_RECTANGLES;
353 memcpy(pRgn->Buffer, lstRc.GetData(),
sizeof(RECT) * lstRc.GetCount());
354 HRGN hRgn = ExtCreateRegion(NULL, (DWORD)len, pRgn);
361BOOL IsFilePathValid(LPCTSTR strPath) {
362 DWORD dwAttr = ::GetFileAttributes(strPath);
363 if(dwAttr == INVALID_FILE_ATTRIBUTES)
365 return (dwAttr & FILE_ATTRIBUTE_DIRECTORY)==0;