怎么绘制字体能让任意方向 附代码
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);
}
}
}
if (pOldFont) SelectObject(TmpDC,pOldFont);
DeleteObject(m_Font);
DeleteObject(SelectObject(TmpDC,TmpObj));
DeleteDC(TmpDC);
return 1;
}
现在只能水平方向,-------TEXT------------。我想能不能
-
-
-
T
E
X
T
-
-
大致是这意思 这代码也不是我写的没完全看懂啊 是不是他已经生成位图了 能调整这图吗 我现在在做一个直线的测量 ---------距离-------- 水平的还行斜的就不行了 求教啊