ximawnd.cpp

来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C++ 代码 · 共 1,694 行 · 第 1/4 页

CPP
1,694
字号
		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
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#ifdef CXIMAGE_SUPPORT_UCOS //CXIMAGE_SUPPORT_UCOS
////////////////////////////////////////////////////////////////////////////////
#define MIN(x,y) (x > y? y:x)
#define SETBMRGB(r,g,b) (((unsigned long)r << 16) + ((unsigned long)g << 8) + ((unsigned long)b << 0))

/*
void CxImage::DrawImage(SCRINFO *bf,int x,int y,int cx,int cy,int x1,int y1)
{
	int  i, j;
	BYTE *psrc = info.pImage;
	unsigned int *ptar = bf->buffer;
	int ima_x_size = head.biWidth;
	int ima_y_size = head.biHeight;
	int src_x_size = bf->w;
	int src_y_size = bf->h;
	int dx,dy,src_width;
	unsigned int *colors;
	bool bTransparent = info.nBkgndIndex != -1;
	bool bAlpha = pAlpha != 0;
	cx = MIN(x + cx,src_x_size);
	cy = MIN(y + cy,src_y_size);
	dx = MIN(x1 + cx,ima_x_size) - x1;
	dy = MIN(y1 + cy,ima_y_size) - y1;
	printf("ima_x_size = %d\n ima_y_size = %d\n",ima_x_size,ima_y_size);
	//bitmap 4byte align
	if(head.biBitCount == 8)
	{	
			if(ima_x_size & 3)
				src_width = (ima_x_size /  4 + 1) * 4;
			else
				src_width = ima_x_size;
	}else
	{
		  if(ima_x_size & 3)
				src_width = (ima_x_size * 3 /  4 + 1) * 4;
			else
				src_width = ima_x_size * 3;
	}	
	printf("src_width = %d\n",src_width);
	printf("bTransparent = %d\nbAlpha = %d\ninfo.bAlphaPaletteEnabled = %d\n",
	     bTransparent,
	     bAlpha,
	     info.bAlphaPaletteEnabled);	
	if (!(bTransparent || bAlpha || info.bAlphaPaletteEnabled)){
	//draw not alpha bitmap
		psrc += y1 * 3 * src_width;
		
		ptar += y * src_x_size;
	
		if(head.biBitCount == 8)
		{	
			colors = (unsigned int *)GetPalette();
			for(j = 0; j < dy;j++)
			{
				psrc += y1 * 3;
		    ptar += y * src_x_size;	
				
				psrc += 3 * x1;
				ptar += x;
				
				for(i = 0;i < dx; i++)
					*(ptar+i)= colors[*(psrc + i)];
				ptar += src_x_size;			
				psrc += src_width;
					
			}
		}else{
			for(j = 0; j < dy;j++)
			{
				psrc += 3 * x1;
				ptar += x;				
				for(i = 0;i < dx; i++)
					*(ptar+i)= SETBMRGB(*(psrc+ 2 + i * 3 ),*(psrc+1  + i * 3),*(psrc + i * 3));
				ptar += src_x_size;			
				psrc += src_width;
					
			}
		} 
	}else
	{
		//alpha blend bitmap
		//pixel informations
		RGBQUAD c={0,0,0,0};
		RGBQUAD ct = GetTransColor();
		long* pc = (long*)&c;
		long* pct= (long*)&ct;
		long sx,sy;
		long cit = GetTransIndex();
		long ci;
		unsigned char a,a1,*ppbtar;
		long alphaoffset;
		
		psrc += y1 * 3 * src_width;
		ptar += y * src_x_size;
    sy = y1;
		for(j = 0; j < dy;j++)
		{
			psrc += 3 * x1;
			ptar += x;
			printf("psrc = %x\n",psrc);
			printf("ptar = %x\n",ptar);
				
			sx = x1;
			alphaoffset=sy * ima_x_size;
			for(i = 0;i < dx; i++)
			{
				unsigned int *pdst = (ptar + i);
				unsigned char *ppix = (psrc + i * 3);
				if (bAlpha) a=pAlpha[alphaoffset+sx]; else a=255;
				a =(BYTE)((a*(1+info.nAlphaMax))>>8);
				if (head.biClrUsed){
					ci = GetPixelIndex(sx,sy);
					c = GetPaletteColor(ci);
					if (info.bAlphaPaletteEnabled){
								a = (BYTE)((a*(1+c.rgbReserved))>>8);
					}
				}else
				{
					
					ppix = psrc + sx * 3;
					c.rgbBlue = *ppix++;
					c.rgbGreen= *ppix++;
					c.rgbRed  = *ppix;
				}	
				if ((head.biClrUsed && ci!=cit) || (!head.biClrUsed && *pc!=*pct) || !bTransparent){
					if (a != 0) {			// a == 0 Transparent, retain dest 
						if (a == 255) {	// opaque, ignore dest 
							*pdst = SETBMRGB(c.rgbRed,c.rgbGreen,c.rgbBlue); 
						} else {				// semi transparent 
							
							a1=(BYTE)~a;
							unsigned char *pcdst = (unsigned char*)pdst; 
							*pcdst++=(unsigned char)(((unsigned int)*pcdst * (unsigned int)a1 + (unsigned int)a * (unsigned int)c.rgbBlue)>>8); 
							*pcdst++=(unsigned char)(((unsigned int)*pcdst * (unsigned int)a1 + (unsigned int)a * (unsigned int)c.rgbGreen)>>8); 
							*pcdst++=(unsigned char)(((unsigned int)*pcdst * (unsigned int)a1 + (unsigned int)a * (unsigned int)c.rgbRed)>>8); 
						} 
				  }
				}
				sx++;
		  }
		  ptar += src_x_size;			
			psrc += src_width;
			
		  sy++;
	  }
	}
}
*/
void CxImage::DrawImage(SCRINFO *bf,int xl,int yl,int xr,int yr,int x1,int y1)
{
	int  i, j;
	BYTE *psrc = info.pImage;
	unsigned int *ptar = bf->buffer;
	int ima_x_size = head.biWidth;
	int ima_y_size = head.biHeight;
	int src_x_size = bf->w;
	int src_y_size = bf->h;
	int dx,dy,src_width;
	unsigned int *colors;
	int cx,cy;
	bool bTransparent = info.nBkgndIndex != -1;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?