📄 ximawnd.cpp
字号:
int nWidth = head.biWidth;
int nHeight = head.biHeight;
// Create two memory dcs for the image and the mask
HDC dcImage=CreateCompatibleDC(hdc);
HDC dcTrans=CreateCompatibleDC(hdc);
// Select the image into the appropriate dc
HBITMAP bm = CreateCompatibleBitmap(hdc, nWidth, nHeight);
HBITMAP pOldBitmapImage = (HBITMAP)SelectObject(dcImage,bm);
SetStretchBltMode(dcImage,COLORONCOLOR);
StretchDIBits(dcImage, 0, 0, nWidth, nHeight, 0, 0, nWidth, nHeight,
info.pImage,(BITMAPINFO*)pDib,DIB_RGB_COLORS,SRCCOPY);
// Create the mask bitmap
HBITMAP bitmapTrans = CreateBitmap(nWidth, nHeight, 1, 1, NULL);
// Select the mask bitmap into the appropriate dc
HBITMAP pOldBitmapTrans = (HBITMAP)SelectObject(dcTrans, bitmapTrans);
// Build mask based on transparent colour
RGBQUAD rgbBG;
if (head.biBitCount<24) rgbBG = GetPaletteColor((BYTE)info.nBkgndIndex);
else rgbBG = info.nBkgndColor;
COLORREF crColour = RGB(rgbBG.rgbRed, rgbBG.rgbGreen, rgbBG.rgbBlue);
COLORREF crOldBack = SetBkColor(dcImage,crColour);
BitBlt(dcTrans,0, 0, nWidth, nHeight, dcImage, 0, 0, SRCCOPY);
// Do the work - True Mask method - cool if not actual display
StretchBlt(hdc,x, y,cx,cy, dcImage, 0, 0, nWidth, nHeight, SRCINVERT);
StretchBlt(hdc,x, y,cx,cy, dcTrans, 0, 0, nWidth, nHeight, SRCAND);
StretchBlt(hdc,x, y,cx,cy, dcImage, 0, 0, nWidth, nHeight, SRCINVERT);
// Restore settings
SelectObject(dcImage,pOldBitmapImage);
SelectObject(dcTrans,pOldBitmapTrans);
SetBkColor(hdc,crOldBack);
DeleteObject( bitmapTrans ); // RG 29/01/2002
DeleteDC(dcImage);
DeleteDC(dcTrans);
DeleteObject(bm);
}
}
return 1;
}
////////////////////////////////////////////////////////////////////////////////
long CxImage::Stretch(HDC hdc, const RECT& rect, DWORD dwRop)
{
return Stretch(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, dwRop);
}
////////////////////////////////////////////////////////////////////////////////
/**
* Stretch the image. Obsolete: use Draw() or Draw2()
* \param hdc : destination device context
* \param xoffset,yoffset : (optional) offset
* \param xsize,ysize : size.
* \param dwRop : raster operation code (see BitBlt documentation)
* \return true if everything is ok
*/
long CxImage::Stretch(HDC hdc, long xoffset, long yoffset, long xsize, long ysize, DWORD dwRop)
{
if((pDib)&&(hdc)) {
//palette must be correctly filled
SetStretchBltMode(hdc,COLORONCOLOR);
StretchDIBits(hdc, xoffset, yoffset,
xsize, ysize, 0, 0, head.biWidth, head.biHeight,
info.pImage,(BITMAPINFO*)pDib,DIB_RGB_COLORS,dwRop);
return 1;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
/**
* Tiles the device context in the specified rectangle with the image.
* \param hdc : destination device context
* \param rc : tiled rectangle in the output device context
* \return true if everything is ok
*/
long CxImage::Tile(HDC hdc, RECT *rc)
{
if((pDib)&&(hdc)&&(rc)) {
int w = rc->right - rc->left;
int h = rc->bottom - rc->top;
int x,y,z;
int bx=head.biWidth;
int by=head.biHeight;
for (y = 0 ; y < h ; y += by){
if ((y+by)>h) by=h-y;
z=bx;
for (x = 0 ; x < w ; x += z){
if ((x+z)>w) z=w-x;
RECT r = {rc->left + x,rc->top + y,rc->left + x + z,rc->top + y + by};
Draw(hdc,rc->left + x, rc->top + y,-1,-1,&r);
}
}
return 1;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// For UNICODE support: char -> TCHAR
long CxImage::DrawString(HDC hdc, long x, long y, const TCHAR* text, RGBQUAD color, const TCHAR* font, long lSize, long lWeight, BYTE bItalic, BYTE bUnderline, bool bSetAlpha)
//long CxImage::DrawString(HDC hdc, long x, long y, const char* text, RGBQUAD color, const char* font, long lSize, long lWeight, BYTE bItalic, BYTE bUnderline, bool bSetAlpha)
{
if (IsValid()){
//get the background
HDC TmpDC=CreateCompatibleDC(hdc);
//choose the font
HFONT m_Font;
LOGFONT* m_pLF;
m_pLF=(LOGFONT*)calloc(1,sizeof(LOGFONT));
_tcsncpy(m_pLF->lfFaceName,font,31); // For UNICODE support
//strncpy(m_pLF->lfFaceName,font,31);
m_pLF->lfHeight=lSize;
m_pLF->lfWeight=lWeight;
m_pLF->lfItalic=bItalic;
m_pLF->lfUnderline=bUnderline;
m_Font=CreateFontIndirect(m_pLF);
//select the font in the dc
HFONT pOldFont=NULL;
if (m_Font)
pOldFont = (HFONT)SelectObject(TmpDC,m_Font);
else
pOldFont = (HFONT)SelectObject(TmpDC,GetStockObject(DEFAULT_GUI_FONT));
//Set text color
SetTextColor(TmpDC,RGB(255,255,255));
SetBkColor(TmpDC,RGB(0,0,0));
//draw the text
SetBkMode(TmpDC,OPAQUE);
//Set text position;
RECT pos = {0,0,0,0};
//long len = (long)strlen(text);
long len = (long)_tcslen(text); // For UNICODE support
::DrawText(TmpDC,text,len,&pos,DT_CALCRECT);
pos.right+=pos.bottom; //for italics
//Preparing Bitmap Info
long width=pos.right;
long height=pos.bottom;
BITMAPINFO bmInfo;
memset(&bmInfo.bmiHeader,0,sizeof(BITMAPINFOHEADER));
bmInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biWidth=width;
bmInfo.bmiHeader.biHeight=height;
bmInfo.bmiHeader.biPlanes=1;
bmInfo.bmiHeader.biBitCount=24;
BYTE *pbase; //points to the final dib
HBITMAP TmpBmp=CreateDIBSection(TmpDC,&bmInfo,DIB_RGB_COLORS,(void**)&pbase,0,0);
HGDIOBJ TmpObj=SelectObject(TmpDC,TmpBmp);
memset(pbase,0,height*((((24 * width) + 31) / 32) * 4));
::DrawText(TmpDC,text,len,&pos,0);
CxImage itext;
itext.CreateFromHBITMAP(TmpBmp);
y=head.biHeight-y-1;
for (long ix=0;ix<width;ix++){
for (long iy=0;iy<height;iy++){
if (itext.GetPixelColor(ix,iy).rgbBlue) SetPixelColor(x+ix,y+iy,color,bSetAlpha);
}
}
//cleanup
if (pOldFont) SelectObject(TmpDC,pOldFont);
DeleteObject(m_Font);
free(m_pLF);
DeleteObject(SelectObject(TmpDC,TmpObj));
DeleteDC(TmpDC);
}
return 1;
}
////////////////////////////////////////////////////////////////////////////////
// <VATI>
long CxImage::DrawStringEx(HDC hdc, long x, long y, CXTEXTINFO *pTextType, bool bSetAlpha )
{
if (!IsValid())
return -1;
//get the background
HDC pDC;
if (hdc) pDC=hdc; else pDC = ::GetDC(0);
HDC TmpDC=CreateCompatibleDC(pDC);
//choose the font
HFONT m_Font;
m_Font=CreateFontIndirect( &pTextType->lfont );
// get colors in RGBQUAD
RGBQUAD p_forecolor = RGBtoRGBQUAD(pTextType->fcolor);
RGBQUAD p_backcolor = RGBtoRGBQUAD(pTextType->bcolor);
// check alignment and re-set default if necessary
if ( pTextType->align != DT_CENTER &&
pTextType->align != DT_LEFT &&
pTextType->align != DT_RIGHT )
pTextType->align = DT_CENTER;
// check rounding radius and re-set default if necessary
if ( pTextType->b_round > 50 || pTextType->b_round < 0 )
pTextType->b_round = 10;
// check opacity and re-set default if necessary
if ( pTextType->b_opacity > 1. || pTextType->b_opacity < .0 )
pTextType->b_opacity = 0.;
//select the font in the dc
HFONT pOldFont=NULL;
if (m_Font)
pOldFont = (HFONT)SelectObject(TmpDC,m_Font);
else
pOldFont = (HFONT)SelectObject(TmpDC,GetStockObject(DEFAULT_GUI_FONT));
//Set text color
SetTextColor(TmpDC,RGB(255,255,255));
SetBkColor(TmpDC,RGB(0,0,0));
SetBkMode(TmpDC,OPAQUE);
//Set text position;
RECT pos = {0,0,0,0};
// get text length and number of lines
long i=0, numlines=1, len=(long)_tcsclen(pTextType->text);
while (i<len)
{
if ( pTextType->text[i++]==13 )
numlines++;
}
::DrawText(TmpDC, pTextType->text, len, &pos, /*DT_EDITCONTROL|DT_EXTERNALLEADING|*/DT_NOPREFIX | DT_CALCRECT );
// increase only if it's really italics, and only one line height
if ( pTextType->lfont.lfItalic )
pos.right += pos.bottom/2/numlines;
// background frame and rounding radius
int frame = 0, roundR = 0;
if ( pTextType->opaque )
{
roundR= (int)(pos.bottom/numlines * pTextType->b_round / 100 ) ;
frame = (int)(/*3.5 + */0.29289*roundR ) ;
pos.right += pos.bottom/numlines/3 ; // JUST FOR BEAUTY
}
//Preparing Bitmap Info
long width=pos.right +frame*2;
long height=pos.bottom +frame*2;
BITMAPINFO bmInfo;
memset(&bmInfo.bmiHeader,0,sizeof(BITMAPINFOHEADER));
bmInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
bmInfo.bmiHeader.biWidth=width;
bmInfo.bmiHeader.biHeight=height;
bmInfo.bmiHeader.biPlanes=1;
bmInfo.bmiHeader.biBitCount=24;
BYTE *pbase; //points to the final dib
HBITMAP TmpBmp=CreateDIBSection(TmpDC,&bmInfo,DIB_RGB_COLORS,(void**)&pbase,0,0);
HGDIOBJ TmpObj=SelectObject(TmpDC,TmpBmp);
memset(pbase,0,height*((((24 * width) + 31) / 32) * 4));
::DrawText(TmpDC,pTextType->text,len, &pos, /*DT_EDITCONTROL|DT_EXTERNALLEADING|*/DT_NOPREFIX| pTextType->align );
CxImage itext;
itext.CreateFromHBITMAP(TmpBmp);
y=head.biHeight-y-1;
//move the insertion point according to alignment type
// DT_CENTER: cursor points to the center of text rectangle
// DT_RIGHT: cursor points to right side end of text rectangle
// DT_LEFT: cursor points to left end of text rectangle
if ( pTextType->align == DT_CENTER )
x -= width/2;
else if ( pTextType->align == DT_RIGHT )
x -= width;
if (x<0) x=0;
//draw the background first, if it exists
long ix,iy;
if ( pTextType->opaque )
{
int ixf=0;
for (ix=0;ix<width;ix++)
{
if ( ix<=roundR )
ixf = (int)(.5+roundR-sqrt((float)(roundR*roundR-(ix-roundR)*(ix-roundR))));
else if ( ix>=width-roundR-1 )
ixf = (int)(.5+roundR-sqrt((float)(roundR*roundR-(width-1-ix-roundR)*(width-1-ix-roundR))));
else
ixf=0;
for (iy=0;iy<height;iy++)
{
if ( (ix<=roundR && ( iy > height-ixf-1 || iy < ixf )) ||
(ix>=width-roundR-1 && ( iy > height-ixf-1 || iy < ixf )) )
continue;
else
if ( pTextType->b_opacity > 0.0 && pTextType->b_opacity < 1.0 )
{
RGBQUAD bcolor, pcolor;
// calculate a transition color from original image to background color:
pcolor = GetPixelColor(x+ix,y+iy);
bcolor.rgbBlue = (unsigned char)(pTextType->b_opacity * pcolor.rgbBlue + (1.0-pTextType->b_opacity) * p_backcolor.rgbBlue );
bcolor.rgbRed = (unsigned char)(pTextType->b_opacity * pcolor.rgbRed + (1.0-pTextType->b_opacity) * p_backcolor.rgbRed ) ;
bcolor.rgbGreen = (unsigned char)(pTextType->b_opacity * pcolor.rgbGreen + (1.0-pTextType->b_opacity) * p_backcolor.rgbGreen ) ;
bcolor.rgbReserved = 0;
SetPixelColor(x+ix,y+iy,bcolor,bSetAlpha);
}
else
SetPixelColor(x+ix,y+iy,p_backcolor,bSetAlpha);
}
}
}
// draw the text itself
for (ix=0;ix<width;ix++)
{
for (iy=0;iy<height;iy++)
{
if (itext.GetPixelColor(ix,iy).rgbBlue )
SetPixelColor(x+ix+frame,y+iy-frame,p_forecolor,bSetAlpha);
}
}
//cleanup
if (pOldFont) SelectObject(TmpDC,pOldFont);
DeleteObject(m_Font);
DeleteObject(SelectObject(TmpDC,TmpObj));
DeleteDC(TmpDC);
return 1;
}
//////////////////////////////////////////////////////////////////////////////
void CxImage::InitTextInfo( CXTEXTINFO *txt )
{
memset( txt, 0, sizeof(CXTEXTINFO));
// LOGFONT defaults
txt->lfont.lfHeight = -36;
txt->lfont.lfCharSet = EASTEUROPE_CHARSET; // just for Central-European users
txt->lfont.lfWeight = FW_NORMAL;
txt->lfont.lfWidth = 0;
txt->lfont.lfEscapement = 0;
txt->lfont.lfOrientation = 0;
txt->lfont.lfItalic = FALSE;
txt->lfont.lfUnderline = FALSE;
txt->lfont.lfStrikeOut = FALSE;
txt->lfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
txt->lfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
txt->lfont.lfQuality = PROOF_QUALITY;
txt->lfont.lfPitchAndFamily= DEFAULT_PITCH | FF_DONTCARE ;
_stprintf( txt->lfont.lfFaceName, _T("Arial")); //use TCHAR mappings <Cesar M>
// initial colors
txt->fcolor = RGB( 255,255,160 ); // default foreground: light goldyellow
txt->bcolor = RGB( 32, 96, 0 ); // default background: deep green
// background
txt->opaque = TRUE; // text has a non-transparent background;
txt->b_opacity = 0.0; // default: opaque background
txt->b_outline = 0; // default: no outline (OUTLINE NOT IMPLEMENTED AT THIS TIME)
txt->b_round = 20; // default: rounding radius is 20% of the rectangle height
// the text
_stprintf( txt->text, _T("Sample Text 01234觖")); // text use TCHAR mappings <Cesar M>
txt->align = DT_CENTER;
return;
}
////////////////////////////////////////////////////////////////////////////////
#endif //CXIMAGE_SUPPORT_WINDOWS
////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -