📄 voimage.cpp
字号:
return TRUE;
}
BOOL CVOImage::DrawTransparent(HDC hdc,
int x,
int y,
int cx,
int cy,
COLORREF crTransparent)
{
if ( -1 == cx )
{
cx = GetWidth();
}
if ( -1 == cy )
{
cy = GetHeight();
}
if (crTransparent == RGB(192, 192, 192))
{
crTransparent = GetPixel(0, 0); // If none of the above conditions are met, use the pixel at 0,0 (Uppler Left)
}
#ifdef _WIN32_WCE
return ::TransparentImage(hdc,
x,
y,
cx,
cy,
m_hDC,
0,
0,
GetWidth(),
GetHeight(),
crTransparent);
#else
return FALSE;
#endif
}
DWORD CVOImage::GetHeight() const
{
return m_dwHeight;
}
DWORD CVOImage::GetWidth() const
{
return m_dwWidth;
}
WORD CVOImage::GetPlanes() const
{
return m_wPlanes;
}
WORD CVOImage::GetBitsPixel() const
{
return m_wBitsPixel;
}
BOOL CVOImage::SetBitmap(HDC hdc,
DWORD dwResourceID,
LPCTSTR pcszClass,
HMODULE hModule)
{
if ( NULL != m_hBitmap )
{
if (m_hModuleResource == hModule && m_dwResourceID == dwResourceID)
{
return TRUE; // Already loaded
}
::DeleteObject(m_hBitmap);
}
if ( NULL == m_hDC )
{
m_hDC = ::CreateCompatibleDC(hdc);
HBITMAP hbitmap = ::CreateCompatibleBitmap(hdc,
::GetDeviceCaps(hdc, HORZRES),
::GetDeviceCaps(hdc, VERTRES));
::SelectObject(m_hDC, hbitmap);
}
BYTE szBuffer[1024] = {0};
#ifdef _WIN32_WCE
HRESULT hr;
DecompressImageInfo dii;
#endif
if (hModule == 0)
{
hModule = ::GetModuleHandle(NULL);
}
CVOResource res(hModule, dwResourceID, pcszClass);
if ( !res.IsLoaded() )
{
return FALSE;
}
res.SetUserData(0); // Use this for the current resource offset
#ifdef _WIN32_WCE
// Fill in the 'DecompressImageInfo' structure
dii.dwSize = sizeof( DecompressImageInfo ); // Size of this structure
dii.pbBuffer = szBuffer; // Pointer to the buffer to use for data
dii.dwBufferMax = 1024; // Size of the buffer
dii.dwBufferCurrent = 0; // The amount of data which is current in the buffer
dii.phBM = &m_hBitmap; // Pointer to the bitmap returned (can be NULL)
dii.ppImageRender = NULL; // Pointer to an IImageRender object (can be NULL)
dii.iBitDepth = GetDeviceCaps(hdc, BITSPIXEL); // Bit depth of the output image
dii.lParam = ( LPARAM ) & res; // User parameter for callback functions
dii.hdc = m_hDC; // HDC to use for retrieving palettes
dii.iScale = g_iScale; // Scale factor (1 - 100)
dii.iMaxWidth = g_iMaxWidth; // Maximum width of the output image
dii.iMaxHeight = g_iMaxHeight; // Maximum height of the output image
dii.pfnGetData = GetImageResourceData; // Callback function to get image data
dii.pfnImageProgress = ImageProgress; // Callback function to notify caller of progress decoding the image
dii.crTransparentOverride = ( UINT ) - 1; // If this color is not (UINT)-1, it will override the
// transparent color in the image with this color. (GIF ONLY)
// Process and decompress the image data
hr = ::DecompressImageIndirect( &dii );
#endif
HBITMAP hbitmapOld = (HBITMAP)::SelectObject(m_hDC, m_hBitmap);
::DeleteObject(hbitmapOld);
BITMAP bmp;
::GetObject(m_hBitmap, sizeof(BITMAP), &bmp);
m_dwWidth = bmp.bmWidth;
m_dwHeight = bmp.bmHeight;
m_wPlanes = bmp.bmPlanes;
m_wBitsPixel = bmp.bmBitsPixel;
m_strFileName.Empty();
m_hModuleResource = hModule;
m_dwResourceID = dwResourceID;
return TRUE;
}
DWORD CALLBACK CVOImage::GetImageData(LPSTR szBuffer,
DWORD dwBufferMax,
LPARAM lParam)
{
DWORD dwNumberOfBytesRead;
if ( (HANDLE)lParam == INVALID_HANDLE_VALUE )
{
return 0;
}
::ReadFile((HANDLE)lParam,
szBuffer,
dwBufferMax,
&dwNumberOfBytesRead,
NULL);
// Return number of bytes read
return dwNumberOfBytesRead;
}
DWORD CALLBACK CVOImage::GetImageResourceData(LPSTR szBuffer,
DWORD dwBufferMax,
LPARAM lParam)
{
DWORD dwNumberOfBytesToRead = dwBufferMax;
CVOResource* pRes = (CVOResource*)lParam;
if ( NULL == pRes )
{
return 0;
}
DWORD dwResourceOffset = pRes->GetUserData();
if ( dwResourceOffset + dwNumberOfBytesToRead > pRes->GetSize() )
{
dwNumberOfBytesToRead = pRes->GetSize() - dwResourceOffset;
}
::memmove(szBuffer,
pRes->GetData() + dwResourceOffset,
dwNumberOfBytesToRead);
pRes->SetUserData(dwResourceOffset + dwNumberOfBytesToRead);
return dwNumberOfBytesToRead; // return amount read
}
#ifdef _WIN32_WCE
void CALLBACK CVOImage::ImageProgress(IImageRender* /*pRender*/,
BOOL bComplete,
LPARAM /*lParam*/)
{
if ( bComplete )
{
;// (Optional) add code here for completion processing
}
}
#endif
BOOL CVOImage::IsLoaded() const
{
return ( NULL != m_hBitmap );
}
CVOResource::CVOResource(HMODULE hModule,
DWORD dwResourceID,
LPCTSTR pcszClass)
{
m_dwSize = 0;
m_hGlobal = 0;
m_pData = 0;
m_hrsrc = ::FindResource(hModule, (LPCTSTR)dwResourceID, pcszClass);
if ( NULL == m_hrsrc )
{
return;
}
m_dwSize = ::SizeofResource(hModule, m_hrsrc);
m_hGlobal = ::LoadResource(hModule, m_hrsrc);
m_pData = (PBYTE)(::LockResource(m_hGlobal));
}
CVOResource::~CVOResource()
{
if ( NULL != m_hGlobal )
{
::DeleteObject(m_hGlobal);
}
}
DWORD CVOResource::GetSize()
{
return m_dwSize;
}
PBYTE CVOResource::GetData()
{
return m_pData;
}
void CVOResource::SetUserData(DWORD dwValue)
{
m_dwUser = dwValue;
}
DWORD CVOResource::GetUserData()
{
return m_dwUser;
}
BOOL CVOResource::IsLoaded()
{
return ( NULL != m_pData );
}
COLORREF CVOImage::GetPixel(int x, int y)
{
if ( NULL == m_hDC )
{
return RGB(0, 0, 0);
}
return ::GetPixel(m_hDC, x, y);
}
BOOL CVOImage::SetCanvasSize(int x,
int y,
COLORREF rgbBackground,
BOOL bfHCenter,
BOOL bfVCenter)
{
if ( RGB(1, 1, 1) == rgbBackground )
{
rgbBackground = GetPixel(0, 0);
}
if ( x < (int)GetWidth() || y < (int)GetHeight() )
{
return FALSE; // Cropping may be implemented at some point in the future
}
int nOffsetX = 0;
int nOffsetY = 0;
if ( bfHCenter )
{
nOffsetX = (x - GetWidth()) / 2;
}
if ( bfVCenter )
{
nOffsetY = (y - GetHeight()) / 2;
}
BITMAP bm, bmNew;
::GetObject(m_hBitmap, sizeof(BITMAP), &bm);
HDC hdc = ::CreateCompatibleDC(m_hDC);
HBITMAP hbitmapNew = ::CreateCompatibleBitmap(m_hDC, x, y);
::SelectObject(hdc, hbitmapNew);
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = x;
rect.bottom = y;
::FillRect(hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
if ( !::BitBlt(hdc,
nOffsetX,
nOffsetY,
GetWidth(),
GetHeight(),
m_hDC,
0,
0,
SRCCOPY) )
{
::DeleteDC(hdc);
::DeleteObject(hbitmapNew);
return FALSE;
}
HBITMAP hPrev = (HBITMAP)::GetObject(hbitmapNew, sizeof(BITMAP), &bmNew);
::SelectObject(hdc, hPrev);
::DeleteDC(m_hDC);
::DeleteObject(m_hBitmap);
m_hDC = hdc;
m_hBitmap = hbitmapNew;
return TRUE;
}
CVOImage::operator HBITMAP()
{
return m_hBitmap;
}
const CVOString& CVOImage::GetFileName() const
{
return m_strFileName;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -