📄 ximawnd.cpp
字号:
}
////////////////////////////////////////////////////////////////////////////////
/**
* 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 pDC;
if (hdc) pDC=hdc; else pDC = ::GetDC(0);
if (pDC==NULL) return 0;
HDC TmpDC=CreateCompatibleDC(pDC);
if (hdc==NULL) ::ReleaseDC(0, pDC);
if (TmpDC==NULL) return 0;
//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);
if (pDC==NULL) return 0;
HDC TmpDC=CreateCompatibleDC(pDC);
if (hdc==NULL) ::ReleaseDC(0, pDC);
if (TmpDC==NULL) return 0;
//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 = 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;
itext.Negative();
if (pTextType->smooth==FALSE){
itext.Threshold(128);
} else {
//itext.TextBlur();
}
//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++)
{
RGBQUAD pcolor = GetPixelColor(x+ix,y+iy);
RGBQUAD tcolor = itext.GetPixelColor(ix,iy);
if (tcolor.rgbBlue!=255){
float a = tcolor.rgbBlue/255.0f;
pcolor.rgbBlue = (unsigned char)(a * (pcolor.rgbBlue - p_forecolor.rgbBlue) + p_forecolor.rgbBlue );
pcolor.rgbRed = (unsigned char)(a * (pcolor.rgbRed - p_forecolor.rgbRed) + p_forecolor.rgbRed ) ;
pcolor.rgbGreen = (unsigned char)(a * (pcolor.rgbGreen - p_forecolor.rgbGreen) + p_forecolor.rgbGreen );
pcolor.rgbReserved = 0;
SetPixelColor(x+ix+frame,y+iy-frame,pcolor,bSetAlpha);
//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( 0, 80,160 ); // default background: light blue
// background
txt->opaque = TRUE; // text has a non-transparent background;
txt->smooth = TRUE;
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;
}
#if CXIMAGE_SUPPORT_LAYERS
////////////////////////////////////////////////////////////////////////////////
long CxImage::LayerDrawAll(HDC hdc, const RECT& rect, RECT* pClipRect, bool bSmooth)
{
return LayerDrawAll(hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, pClipRect,bSmooth);
}
////////////////////////////////////////////////////////////////////////////////
long CxImage::LayerDrawAll(HDC hdc, long x, long y, long cx, long cy, RECT* pClipRect, bool bSmooth)
{
long n=0;
CxImage* pLayer;
while(pLayer=GetLayer(n++)){
if (pLayer->Draw(hdc,x+pLayer->info.xOffset,y+pLayer->info.yOffset,cx,cy,pClipRect,bSmooth)==0)
return 0;
if (pLayer->LayerDrawAll(hdc,x+pLayer->info.xOffset,y+pLayer->info.yOffset,cx,cy,pClipRect,bSmooth)==0)
return 0;
}
return 1;
}
#endif //CXIMAGE_SUPPORT_LAYERS
////////////////////////////////////////////////////////////////////////////////
#endif //CXIMAGE_SUPPORT_WINDOWS
////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -