像素转pbu的两种方法:
/一、像素转pbu的计算方法
int ConvertPBU(int nValue, int nType)
{
static int tmHeight = 0;
static int tmAveCharWidth = 0;
static int nLogPixelsX = 0;
static int nLogPixelsY = 0;
if (!tmHeight && !tmAveCharWidth)
{
HWND hWnd = GetDesktopWindow();
HDC hdc = SysGetDC(hWnd);
HFONT hFont = (HFONT)GetStockObject(SYSTEM_FONT);
HFONT hOldFont = (HFONT)SelectObject(hdc, hFont);
TEXTMETRIC tm;
GetTextMetrics(hdc, &tm);
tmHeight = tm.tmHeight;
tmAveCharWidth = tm.tmAveCharWidth;
nLogPixelsX = GetDeviceCaps(hdc, LOGPIXELSX);
nLogPixelsY = GetDeviceCaps(hdc, LOGPIXELSY);
SelectObject(hdc, hOldFont);
SysReleaseDC(hWnd, hdc);
}
int result = 0;
switch (nType)
{
case 1:
{
int n = nLogPixelsX;
if (nValue < 0)
n = -nLogPixelsX;
result = (6144 * nValue + 7 * n) / (14 * nLogPixelsX);
}
break;
case 2:
{
int n = nLogPixelsY;
if (nValue < 0)
n = -nLogPixelsY;
result = (n + 768 * nValue) / (2 * nLogPixelsY);
}
break;
case 3:
result = ((nValue >= 0 ? 1536 : -1536) + 7 * nValue * nLogPixelsX) / 3072;
break;
case 4:
result = ((nValue >= 0 ? 192 : -192) + nValue * nLogPixelsY) / 384;
break;
default:
result = 0;
break;
}
return result;
}
//二、直接调用PBVMxxx.dll 法
function long FN_ConvertUnits(long unitsORpixels, long type) library "pbvmxxx.dll" //这里的DLL,根据具体自己写,比如 PB9就是 pbvm90.dll,PB12.5就是 pbvm125.dll
参数说明:
1.long unitsORpixels 像素或PBU值
2.long type,转换类型。分别可以是:
int PixelsToUnitsX(int nPixels)
{
return FN_ConvertUnits(nPixels, 1);
}
int PixelsToUnitsY(int nPixels)
{
return FN_ConvertUnits(nPixels, 2);
}
int UnitsToPixelsX(int nUnites)
{
return FN_ConvertUnits(nUnites, 3);
}
int UnitsToPixelsY(int nUnites)
{
return FN_ConvertUnits(nUnites, 4);
}