12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069 |
- #include "StdAfx.h"
- ///////////////////////////////////////////////////////////////////////////////////////
- DECLARE_HANDLE(HZIP); // An HZIP identifies a zip file that has been opened
- typedef DWORD ZRESULT;
- typedef struct
- {
- int index; // index of this file within the zip
- char name[MAX_PATH]; // filename within the zip
- DWORD attr; // attributes, as in GetFileAttributes.
- FILETIME atime,ctime,mtime;// access, create, modify filetimes
- long comp_size; // sizes of item, compressed and uncompressed. These
- long unc_size; // may be -1 if not yet known (e.g. being streamed in)
- } ZIPENTRY;
- typedef struct
- {
- int index; // index of this file within the zip
- TCHAR name[MAX_PATH]; // filename within the zip
- DWORD attr; // attributes, as in GetFileAttributes.
- FILETIME atime,ctime,mtime;// access, create, modify filetimes
- long comp_size; // sizes of item, compressed and uncompressed. These
- long unc_size; // may be -1 if not yet known (e.g. being streamed in)
- } ZIPENTRYW;
- #define OpenZip OpenZipU
- #define CloseZip(hz) CloseZipU(hz)
- extern HZIP OpenZipU(void *z,unsigned int len,DWORD flags);
- extern ZRESULT CloseZipU(HZIP hz);
- #ifdef _UNICODE
- #define ZIPENTRY ZIPENTRYW
- #define GetZipItem GetZipItemW
- #define FindZipItem FindZipItemW
- #else
- #define GetZipItem GetZipItemA
- #define FindZipItem FindZipItemA
- #endif
- extern ZRESULT GetZipItemA(HZIP hz, int index, ZIPENTRY *ze);
- extern ZRESULT GetZipItemW(HZIP hz, int index, ZIPENTRYW *ze);
- extern ZRESULT FindZipItemA(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRY *ze);
- extern ZRESULT FindZipItemW(HZIP hz, const TCHAR *name, bool ic, int *index, ZIPENTRYW *ze);
- extern ZRESULT UnzipItem(HZIP hz, int index, void *dst, unsigned int len, DWORD flags);
- ///////////////////////////////////////////////////////////////////////////////////////
- extern "C"
- {
- extern unsigned char *stbi_load_from_memory(unsigned char const *buffer, int len, int *x, int *y, \
- int *comp, int req_comp);
- extern void stbi_image_free(void *retval_from_stbi_load);
- };
- namespace DuiLib {
- static int g_iFontID = MAX_FONT_ID;
- /////////////////////////////////////////////////////////////////////////////////////
- //
- //
- CRenderClip::~CRenderClip()
- {
- ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
- ASSERT(::GetObjectType(hRgn)==OBJ_REGION);
- ASSERT(::GetObjectType(hOldRgn)==OBJ_REGION);
- ::SelectClipRgn(hDC, hOldRgn);
- ::DeleteObject(hOldRgn);
- ::DeleteObject(hRgn);
- }
- void CRenderClip::GenerateClip(HDC hDC, RECT rc, CRenderClip& clip)
- {
- RECT rcClip = { 0 };
- ::GetClipBox(hDC, &rcClip);
- clip.hOldRgn = ::CreateRectRgnIndirect(&rcClip);
- clip.hRgn = ::CreateRectRgnIndirect(&rc);
- ::ExtSelectClipRgn(hDC, clip.hRgn, RGN_AND);
- clip.hDC = hDC;
- clip.rcItem = rc;
- }
- void CRenderClip::GenerateRoundClip(HDC hDC, RECT rc, RECT rcItem, int width, int height, CRenderClip& clip)
- {
- RECT rcClip = { 0 };
- ::GetClipBox(hDC, &rcClip);
- clip.hOldRgn = ::CreateRectRgnIndirect(&rcClip);
- clip.hRgn = ::CreateRectRgnIndirect(&rc);
- HRGN hRgnItem = ::CreateRoundRectRgn(rcItem.left, rcItem.top, rcItem.right + 1, rcItem.bottom + 1, width, height);
- ::CombineRgn(clip.hRgn, clip.hRgn, hRgnItem, RGN_AND);
- ::ExtSelectClipRgn(hDC, clip.hRgn, RGN_AND);
- clip.hDC = hDC;
- clip.rcItem = rc;
- ::DeleteObject(hRgnItem);
- }
- void CRenderClip::UseOldClipBegin(HDC hDC, CRenderClip& clip)
- {
- ::SelectClipRgn(hDC, clip.hOldRgn);
- }
- void CRenderClip::UseOldClipEnd(HDC hDC, CRenderClip& clip)
- {
- ::SelectClipRgn(hDC, clip.hRgn);
- }
- /////////////////////////////////////////////////////////////////////////////////////
- //
- //
- static const float OneThird = 1.0f / 3;
- static void RGBtoHSL(DWORD ARGB, float* H, float* S, float* L) {
- const float
- R = (float)GetRValue(ARGB),
- G = (float)GetGValue(ARGB),
- B = (float)GetBValue(ARGB),
- nR = (R<0?0:(R>255?255:R))/255,
- nG = (G<0?0:(G>255?255:G))/255,
- nB = (B<0?0:(B>255?255:B))/255,
- m = min(min(nR,nG),nB),
- M = max(max(nR,nG),nB);
- *L = (m + M)/2;
- if (M==m) *H = *S = 0;
- else {
- const float
- f = (nR==m)?(nG-nB):((nG==m)?(nB-nR):(nR-nG)),
- i = (nR==m)?3.0f:((nG==m)?5.0f:1.0f);
- *H = (i-f/(M-m));
- if (*H>=6) *H-=6;
- *H*=60;
- *S = (2*(*L)<=1)?((M-m)/(M+m)):((M-m)/(2-M-m));
- }
- }
- static void HSLtoRGB(DWORD* ARGB, float H, float S, float L) {
- const float
- q = 2*L<1?L*(1+S):(L+S-L*S),
- p = 2*L-q,
- h = H/360,
- tr = h + OneThird,
- tg = h,
- tb = h - OneThird,
- ntr = tr<0?tr+1:(tr>1?tr-1:tr),
- ntg = tg<0?tg+1:(tg>1?tg-1:tg),
- ntb = tb<0?tb+1:(tb>1?tb-1:tb),
- B = 255*(6*ntr<1?p+(q-p)*6*ntr:(2*ntr<1?q:(3*ntr<2?p+(q-p)*6*(2.0f*OneThird-ntr):p))),
- G = 255*(6*ntg<1?p+(q-p)*6*ntg:(2*ntg<1?q:(3*ntg<2?p+(q-p)*6*(2.0f*OneThird-ntg):p))),
- R = 255*(6*ntb<1?p+(q-p)*6*ntb:(2*ntb<1?q:(3*ntb<2?p+(q-p)*6*(2.0f*OneThird-ntb):p)));
- *ARGB &= 0xFF000000;
- *ARGB |= RGB( (BYTE)(R<0?0:(R>255?255:R)), (BYTE)(G<0?0:(G>255?255:G)), (BYTE)(B<0?0:(B>255?255:B)) );
- }
- static COLORREF PixelAlpha(COLORREF clrSrc, double src_darken, COLORREF clrDest, double dest_darken)
- {
- return RGB (GetRValue (clrSrc) * src_darken + GetRValue (clrDest) * dest_darken,
- GetGValue (clrSrc) * src_darken + GetGValue (clrDest) * dest_darken,
- GetBValue (clrSrc) * src_darken + GetBValue (clrDest) * dest_darken);
- }
- static BOOL WINAPI AlphaBitBlt(HDC hDC, int nDestX, int nDestY, int dwWidth, int dwHeight, HDC hSrcDC, \
- int nSrcX, int nSrcY, int wSrc, int hSrc, BLENDFUNCTION ftn)
- {
- HDC hTempDC = ::CreateCompatibleDC(hDC);
- if (NULL == hTempDC)
- return FALSE;
- //Creates Source DIB
- LPBITMAPINFO lpbiSrc = NULL;
- // Fill in the BITMAPINFOHEADER
- lpbiSrc = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)];
- if (lpbiSrc == NULL)
- {
- ::DeleteDC(hTempDC);
- return FALSE;
- }
- lpbiSrc->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- lpbiSrc->bmiHeader.biWidth = dwWidth;
- lpbiSrc->bmiHeader.biHeight = dwHeight;
- lpbiSrc->bmiHeader.biPlanes = 1;
- lpbiSrc->bmiHeader.biBitCount = 32;
- lpbiSrc->bmiHeader.biCompression = BI_RGB;
- lpbiSrc->bmiHeader.biSizeImage = dwWidth * dwHeight;
- lpbiSrc->bmiHeader.biXPelsPerMeter = 0;
- lpbiSrc->bmiHeader.biYPelsPerMeter = 0;
- lpbiSrc->bmiHeader.biClrUsed = 0;
- lpbiSrc->bmiHeader.biClrImportant = 0;
- COLORREF* pSrcBits = NULL;
- HBITMAP hSrcDib = CreateDIBSection (
- hSrcDC, lpbiSrc, DIB_RGB_COLORS, (void **)&pSrcBits,
- NULL, NULL);
- if ((NULL == hSrcDib) || (NULL == pSrcBits))
- {
- delete [] lpbiSrc;
- ::DeleteDC(hTempDC);
- return FALSE;
- }
- HBITMAP hOldTempBmp = (HBITMAP)::SelectObject (hTempDC, hSrcDib);
- ::StretchBlt(hTempDC, 0, 0, dwWidth, dwHeight, hSrcDC, nSrcX, nSrcY, wSrc, hSrc, SRCCOPY);
- ::SelectObject (hTempDC, hOldTempBmp);
- //Creates Destination DIB
- LPBITMAPINFO lpbiDest = NULL;
- // Fill in the BITMAPINFOHEADER
- lpbiDest = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)];
- if (lpbiDest == NULL)
- {
- delete [] lpbiSrc;
- ::DeleteObject(hSrcDib);
- ::DeleteDC(hTempDC);
- return FALSE;
- }
- lpbiDest->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- lpbiDest->bmiHeader.biWidth = dwWidth;
- lpbiDest->bmiHeader.biHeight = dwHeight;
- lpbiDest->bmiHeader.biPlanes = 1;
- lpbiDest->bmiHeader.biBitCount = 32;
- lpbiDest->bmiHeader.biCompression = BI_RGB;
- lpbiDest->bmiHeader.biSizeImage = dwWidth * dwHeight;
- lpbiDest->bmiHeader.biXPelsPerMeter = 0;
- lpbiDest->bmiHeader.biYPelsPerMeter = 0;
- lpbiDest->bmiHeader.biClrUsed = 0;
- lpbiDest->bmiHeader.biClrImportant = 0;
- COLORREF* pDestBits = NULL;
- HBITMAP hDestDib = CreateDIBSection (
- hDC, lpbiDest, DIB_RGB_COLORS, (void **)&pDestBits,
- NULL, NULL);
- if ((NULL == hDestDib) || (NULL == pDestBits))
- {
- delete [] lpbiSrc;
- ::DeleteObject(hSrcDib);
- ::DeleteDC(hTempDC);
- return FALSE;
- }
- ::SelectObject (hTempDC, hDestDib);
- ::BitBlt (hTempDC, 0, 0, dwWidth, dwHeight, hDC, nDestX, nDestY, SRCCOPY);
- ::SelectObject (hTempDC, hOldTempBmp);
- double src_darken;
- BYTE nAlpha;
- for (int pixel = 0; pixel < dwWidth * dwHeight; pixel++, pSrcBits++, pDestBits++)
- {
- nAlpha = LOBYTE(*pSrcBits >> 24);
- src_darken = (double) (nAlpha * ftn.SourceConstantAlpha) / 255.0 / 255.0;
- if( src_darken < 0.0 ) src_darken = 0.0;
- *pDestBits = PixelAlpha(*pSrcBits, src_darken, *pDestBits, 1.0 - src_darken);
- } //for
- ::SelectObject (hTempDC, hDestDib);
- ::BitBlt (hDC, nDestX, nDestY, dwWidth, dwHeight, hTempDC, 0, 0, SRCCOPY);
- ::SelectObject (hTempDC, hOldTempBmp);
- delete [] lpbiDest;
- ::DeleteObject(hDestDib);
- delete [] lpbiSrc;
- ::DeleteObject(hSrcDib);
- ::DeleteDC(hTempDC);
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////////////
- //
- //
- DWORD CRenderEngine::AdjustColor(DWORD dwColor, short H, short S, short L)
- {
- if( H == 180 && S == 100 && L == 100 ) return dwColor;
- float fH, fS, fL;
- float S1 = S / 100.0f;
- float L1 = L / 100.0f;
- RGBtoHSL(dwColor, &fH, &fS, &fL);
- fH += (H - 180);
- fH = fH > 0 ? fH : fH + 360;
- fS *= S1;
- fL *= L1;
- HSLtoRGB(&dwColor, fH, fS, fL);
- return dwColor;
- }
- HBITMAP CRenderEngine::CreateARGB32Bitmap(HDC hDC, int cx, int cy, COLORREF** pBits)
- {
- LPBITMAPINFO lpbiSrc = NULL;
- lpbiSrc = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)];
- if (lpbiSrc == NULL) return NULL;
- lpbiSrc->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- lpbiSrc->bmiHeader.biWidth = cx;
- lpbiSrc->bmiHeader.biHeight = cy;
- lpbiSrc->bmiHeader.biPlanes = 1;
- lpbiSrc->bmiHeader.biBitCount = 32;
- lpbiSrc->bmiHeader.biCompression = BI_RGB;
- lpbiSrc->bmiHeader.biSizeImage = cx * cy;
- lpbiSrc->bmiHeader.biXPelsPerMeter = 0;
- lpbiSrc->bmiHeader.biYPelsPerMeter = 0;
- lpbiSrc->bmiHeader.biClrUsed = 0;
- lpbiSrc->bmiHeader.biClrImportant = 0;
- HBITMAP hBitmap = CreateDIBSection (hDC, lpbiSrc, DIB_RGB_COLORS, (void **)pBits, NULL, NULL);
- delete [] lpbiSrc;
- return hBitmap;
- }
- void CRenderEngine::AdjustImage(bool bUseHSL, TImageInfo* imageInfo, short H, short S, short L)
- {
- if( imageInfo == NULL || imageInfo->bUseHSL == false || imageInfo->hBitmap == NULL ||
- imageInfo->pBits == NULL || imageInfo->pSrcBits == NULL )
- return;
- if( bUseHSL == false || (H == 180 && S == 100 && L == 100)) {
- ::CopyMemory(imageInfo->pBits, imageInfo->pSrcBits, imageInfo->nX * imageInfo->nY * 4);
- return;
- }
- float fH, fS, fL;
- float S1 = S / 100.0f;
- float L1 = L / 100.0f;
- for( int i = 0; i < imageInfo->nX * imageInfo->nY; i++ ) {
- RGBtoHSL(*(DWORD*)(imageInfo->pSrcBits + i*4), &fH, &fS, &fL);
- fH += (H - 180);
- fH = fH > 0 ? fH : fH + 360;
- fS *= S1;
- fL *= L1;
- HSLtoRGB((DWORD*)(imageInfo->pBits + i*4), fH, fS, fL);
- }
- }
- TImageInfo* CRenderEngine::LoadImage(STRINGorID bitmap, LPCTSTR type, DWORD mask)
- {
- LPBYTE pData = NULL;
- DWORD dwSize = 0;
- do
- {
- if( type == NULL ) {
- CDuiString sFile = CPaintManagerUI::GetResourcePath();
- if( CPaintManagerUI::GetResourceZip().IsEmpty() ) {
- sFile += bitmap.m_lpstr;
- HANDLE hFile = ::CreateFile(sFile.GetData(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
- FILE_ATTRIBUTE_NORMAL, NULL);
- if( hFile == INVALID_HANDLE_VALUE ) break;
- dwSize = ::GetFileSize(hFile, NULL);
- if( dwSize == 0 ) break;
- DWORD dwRead = 0;
- pData = new BYTE[ dwSize ];
- ::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
- ::CloseHandle( hFile );
- if( dwRead != dwSize ) {
- delete[] pData;
- pData = NULL;
- break;
- }
- }
- else {
- sFile += CPaintManagerUI::GetResourceZip();
- HZIP hz = NULL;
- if( CPaintManagerUI::IsCachedResourceZip() ) hz = (HZIP)CPaintManagerUI::GetResourceZipHandle();
- else hz = OpenZip((void*)sFile.GetData(), 0, 2);
- if( hz == NULL ) break;
- ZIPENTRY ze;
- int i;
- if( FindZipItem(hz, bitmap.m_lpstr, true, &i, &ze) != 0 ) break;
- dwSize = ze.unc_size;
- if( dwSize == 0 ) break;
- pData = new BYTE[ dwSize ];
- int res = UnzipItem(hz, i, pData, dwSize, 3);
- if( res != 0x00000000 && res != 0x00000600) {
- delete[] pData;
- pData = NULL;
- if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
- break;
- }
- if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
- }
- }
- else {
- HRSRC hResource = ::FindResource(CPaintManagerUI::GetResourceDll(), bitmap.m_lpstr, type);
- if( hResource == NULL ) break;
- HGLOBAL hGlobal = ::LoadResource(CPaintManagerUI::GetResourceDll(), hResource);
- if( hGlobal == NULL ) {
- FreeResource(hResource);
- break;
- }
- dwSize = ::SizeofResource(CPaintManagerUI::GetResourceDll(), hResource);
- if( dwSize == 0 ) break;
- pData = new BYTE[ dwSize ];
- ::CopyMemory(pData, (LPBYTE)::LockResource(hGlobal), dwSize);
- ::FreeResource(hResource);
- }
- } while (0);
- while (!pData)
- {
- //读不到图片, 则直接去读取bitmap.m_lpstr指向的路径
- HANDLE hFile = ::CreateFile(bitmap.m_lpstr, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
- FILE_ATTRIBUTE_NORMAL, NULL);
- if( hFile == INVALID_HANDLE_VALUE ) break;
- dwSize = ::GetFileSize(hFile, NULL);
- if( dwSize == 0 ) break;
- DWORD dwRead = 0;
- pData = new BYTE[ dwSize ];
- ::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
- ::CloseHandle( hFile );
- if( dwRead != dwSize ) {
- delete[] pData;
- pData = NULL;
- }
- break;
- }
- if (!pData)
- {
- //::MessageBox(0, _T("读取图片数据失败!"), _T("抓BUG"), MB_OK);
- return NULL;
- }
- LPBYTE pImage = NULL;
- int x,y,n;
- pImage = stbi_load_from_memory(pData, dwSize, &x, &y, &n, 4);
- delete[] pData;
- if( !pImage ) {
- //::MessageBox(0, _T("解析图片失败"), _T("抓BUG"), MB_OK);
- return NULL;
- }
- BITMAPINFO bmi;
- ::ZeroMemory(&bmi, sizeof(BITMAPINFO));
- bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- bmi.bmiHeader.biWidth = x;
- bmi.bmiHeader.biHeight = -y;
- bmi.bmiHeader.biPlanes = 1;
- bmi.bmiHeader.biBitCount = 32;
- bmi.bmiHeader.biCompression = BI_RGB;
- bmi.bmiHeader.biSizeImage = x * y * 4;
- bool bAlphaChannel = false;
- LPBYTE pDest = NULL;
- HBITMAP hBitmap = ::CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void**)&pDest, NULL, 0);
- if( !hBitmap ) {
- //::MessageBox(0, _T("CreateDIBSection失败"), _T("抓BUG"), MB_OK);
- return NULL;
- }
- for( int i = 0; i < x * y; i++ )
- {
- pDest[i*4 + 3] = pImage[i*4 + 3];
- if( pDest[i*4 + 3] < 255 )
- {
- pDest[i*4] = (BYTE)(DWORD(pImage[i*4 + 2])*pImage[i*4 + 3]/255);
- pDest[i*4 + 1] = (BYTE)(DWORD(pImage[i*4 + 1])*pImage[i*4 + 3]/255);
- pDest[i*4 + 2] = (BYTE)(DWORD(pImage[i*4])*pImage[i*4 + 3]/255);
- bAlphaChannel = true;
- }
- else
- {
- pDest[i*4] = pImage[i*4 + 2];
- pDest[i*4 + 1] = pImage[i*4 + 1];
- pDest[i*4 + 2] = pImage[i*4];
- }
- if( *(DWORD*)(&pDest[i*4]) == mask ) {
- pDest[i*4] = (BYTE)0;
- pDest[i*4 + 1] = (BYTE)0;
- pDest[i*4 + 2] = (BYTE)0;
- pDest[i*4 + 3] = (BYTE)0;
- bAlphaChannel = true;
- }
- }
- stbi_image_free(pImage);
- TImageInfo* data = new TImageInfo;
- data->hBitmap = hBitmap;
- data->pBits = pDest;
- data->nX = x;
- data->nY = y;
- data->bAlpha = bAlphaChannel;
- data->bUseHSL = false;
- data->pSrcBits = NULL;
- return data;
- }
- void CRenderEngine::FreeImage(TImageInfo* bitmap, bool bDelete)
- {
- if (bitmap == NULL) return;
- if (bitmap->hBitmap) {
- ::DeleteObject(bitmap->hBitmap);
- bitmap->hBitmap = NULL;
- }
- if (bitmap->pSrcBits) {
- delete[] bitmap->pSrcBits;
- bitmap->pSrcBits = NULL;
- }
- if (bDelete) delete bitmap ;
- }
- void CRenderEngine::DrawImage(HDC hDC, HBITMAP hBitmap, const RECT& rc, const RECT& rcPaint,
- const RECT& rcBmpPart, const RECT& rcCorners, bool bAlpha,
- BYTE uFade, bool bHole, bool bTiledX, bool bTiledY)
- {
- ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
- typedef BOOL (WINAPI *LPALPHABLEND)(HDC, int, int, int, int,HDC, int, int, int, int, BLENDFUNCTION);
- static LPALPHABLEND lpAlphaBlend = (LPALPHABLEND) ::GetProcAddress(::GetModuleHandle(_T("msimg32.dll")), "AlphaBlend");
- if( lpAlphaBlend == NULL ) lpAlphaBlend = AlphaBitBlt;
- if( hBitmap == NULL ) return;
- HDC hCloneDC = ::CreateCompatibleDC(hDC);
- HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(hCloneDC, hBitmap);
- ::SetStretchBltMode(hDC, COLORONCOLOR);
- RECT rcTemp = {0};
- RECT rcDest = {0};
- if( lpAlphaBlend && (bAlpha || uFade < 255) ) {
- BLENDFUNCTION bf = { AC_SRC_OVER, 0, uFade, AC_SRC_ALPHA };
- // middle
- if( !bHole ) {
- rcDest.left = rc.left + rcCorners.left;
- rcDest.top = rc.top + rcCorners.top;
- rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;
- rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- if( !bTiledX && !bTiledY ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \
- rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, \
- rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);
- }
- else if( bTiledX && bTiledY ) {
- LONG lWidth = rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right;
- LONG lHeight = rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom;
- int iTimesX = (rcDest.right - rcDest.left + lWidth - 1) / lWidth;
- int iTimesY = (rcDest.bottom - rcDest.top + lHeight - 1) / lHeight;
- for( int j = 0; j < iTimesY; ++j ) {
- LONG lDestTop = rcDest.top + lHeight * j;
- LONG lDestBottom = rcDest.top + lHeight * (j + 1);
- LONG lDrawHeight = lHeight;
- if( lDestBottom > rcDest.bottom ) {
- lDrawHeight -= lDestBottom - rcDest.bottom;
- lDestBottom = rcDest.bottom;
- }
- for( int i = 0; i < iTimesX; ++i ) {
- LONG lDestLeft = rcDest.left + lWidth * i;
- LONG lDestRight = rcDest.left + lWidth * (i + 1);
- LONG lDrawWidth = lWidth;
- if( lDestRight > rcDest.right ) {
- lDrawWidth -= lDestRight - rcDest.right;
- lDestRight = rcDest.right;
- }
- lpAlphaBlend(hDC, rcDest.left + lWidth * i, rcDest.top + lHeight * j,
- lDestRight - lDestLeft, lDestBottom - lDestTop, hCloneDC,
- rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, lDrawWidth, lDrawHeight, bf);
- }
- }
- }
- else if( bTiledX ) {
- LONG lWidth = rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right;
- int iTimes = (rcDest.right - rcDest.left + lWidth - 1) / lWidth;
- for( int i = 0; i < iTimes; ++i ) {
- LONG lDestLeft = rcDest.left + lWidth * i;
- LONG lDestRight = rcDest.left + lWidth * (i + 1);
- LONG lDrawWidth = lWidth;
- if( lDestRight > rcDest.right ) {
- lDrawWidth -= lDestRight - rcDest.right;
- lDestRight = rcDest.right;
- }
- rcDest.bottom -= rcDest.top;
- lpAlphaBlend(hDC, lDestLeft, rcDest.top, lDestRight - lDestLeft, rcDest.bottom,
- hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \
- lDrawWidth, rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);
- }
- }
- else { // bTiledY
- LONG lHeight = rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom;
- int iTimes = (rcDest.bottom - rcDest.top + lHeight - 1) / lHeight;
- for( int i = 0; i < iTimes; ++i ) {
- LONG lDestTop = rcDest.top + lHeight * i;
- LONG lDestBottom = rcDest.top + lHeight * (i + 1);
- LONG lDrawHeight = lHeight;
- if( lDestBottom > rcDest.bottom ) {
- lDrawHeight -= lDestBottom - rcDest.bottom;
- lDestBottom = rcDest.bottom;
- }
- rcDest.right -= rcDest.left;
- lpAlphaBlend(hDC, rcDest.left, rcDest.top + lHeight * i, rcDest.right, lDestBottom - lDestTop,
- hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \
- rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, lDrawHeight, bf);
- }
- }
- }
- }
- // left-top
- if( rcCorners.left > 0 && rcCorners.top > 0 ) {
- rcDest.left = rc.left;
- rcDest.top = rc.top;
- rcDest.right = rcCorners.left;
- rcDest.bottom = rcCorners.top;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.left, rcBmpPart.top, rcCorners.left, rcCorners.top, bf);
- }
- }
- // top
- if( rcCorners.top > 0 ) {
- rcDest.left = rc.left + rcCorners.left;
- rcDest.top = rc.top;
- rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;
- rcDest.bottom = rcCorners.top;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.left + rcCorners.left, rcBmpPart.top, rcBmpPart.right - rcBmpPart.left - \
- rcCorners.left - rcCorners.right, rcCorners.top, bf);
- }
- }
- // right-top
- if( rcCorners.right > 0 && rcCorners.top > 0 ) {
- rcDest.left = rc.right - rcCorners.right;
- rcDest.top = rc.top;
- rcDest.right = rcCorners.right;
- rcDest.bottom = rcCorners.top;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.right - rcCorners.right, rcBmpPart.top, rcCorners.right, rcCorners.top, bf);
- }
- }
- // left
- if( rcCorners.left > 0 ) {
- rcDest.left = rc.left;
- rcDest.top = rc.top + rcCorners.top;
- rcDest.right = rcCorners.left;
- rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.left, rcBmpPart.top + rcCorners.top, rcCorners.left, rcBmpPart.bottom - \
- rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);
- }
- }
- // right
- if( rcCorners.right > 0 ) {
- rcDest.left = rc.right - rcCorners.right;
- rcDest.top = rc.top + rcCorners.top;
- rcDest.right = rcCorners.right;
- rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.right - rcCorners.right, rcBmpPart.top + rcCorners.top, rcCorners.right, \
- rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);
- }
- }
- // left-bottom
- if( rcCorners.left > 0 && rcCorners.bottom > 0 ) {
- rcDest.left = rc.left;
- rcDest.top = rc.bottom - rcCorners.bottom;
- rcDest.right = rcCorners.left;
- rcDest.bottom = rcCorners.bottom;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.left, rcBmpPart.bottom - rcCorners.bottom, rcCorners.left, rcCorners.bottom, bf);
- }
- }
- // bottom
- if( rcCorners.bottom > 0 ) {
- rcDest.left = rc.left + rcCorners.left;
- rcDest.top = rc.bottom - rcCorners.bottom;
- rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;
- rcDest.bottom = rcCorners.bottom;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.left + rcCorners.left, rcBmpPart.bottom - rcCorners.bottom, \
- rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, rcCorners.bottom, bf);
- }
- }
- // right-bottom
- if( rcCorners.right > 0 && rcCorners.bottom > 0 ) {
- rcDest.left = rc.right - rcCorners.right;
- rcDest.top = rc.bottom - rcCorners.bottom;
- rcDest.right = rcCorners.right;
- rcDest.bottom = rcCorners.bottom;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.right - rcCorners.right, rcBmpPart.bottom - rcCorners.bottom, rcCorners.right, \
- rcCorners.bottom, bf);
- }
- }
- }
- else
- {
- if (rc.right - rc.left == rcBmpPart.right - rcBmpPart.left \
- && rc.bottom - rc.top == rcBmpPart.bottom - rcBmpPart.top \
- && rcCorners.left == 0 && rcCorners.right == 0 && rcCorners.top == 0 && rcCorners.bottom == 0)
- {
- if( ::IntersectRect(&rcTemp, &rcPaint, &rc) ) {
- ::BitBlt(hDC, rcTemp.left, rcTemp.top, rcTemp.right - rcTemp.left, rcTemp.bottom - rcTemp.top, \
- hCloneDC, rcBmpPart.left + rcTemp.left - rc.left, rcBmpPart.top + rcTemp.top - rc.top, SRCCOPY);
- }
- }
- else
- {
- // middle
- if( !bHole ) {
- rcDest.left = rc.left + rcCorners.left;
- rcDest.top = rc.top + rcCorners.top;
- rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;
- rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- if( !bTiledX && !bTiledY ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \
- rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, \
- rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, SRCCOPY);
- }
- else if( bTiledX && bTiledY ) {
- LONG lWidth = rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right;
- LONG lHeight = rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom;
- int iTimesX = (rcDest.right - rcDest.left + lWidth - 1) / lWidth;
- int iTimesY = (rcDest.bottom - rcDest.top + lHeight - 1) / lHeight;
- for( int j = 0; j < iTimesY; ++j ) {
- LONG lDestTop = rcDest.top + lHeight * j;
- LONG lDestBottom = rcDest.top + lHeight * (j + 1);
- LONG lDrawHeight = lHeight;
- if( lDestBottom > rcDest.bottom ) {
- lDrawHeight -= lDestBottom - rcDest.bottom;
- lDestBottom = rcDest.bottom;
- }
- for( int i = 0; i < iTimesX; ++i ) {
- LONG lDestLeft = rcDest.left + lWidth * i;
- LONG lDestRight = rcDest.left + lWidth * (i + 1);
- LONG lDrawWidth = lWidth;
- if( lDestRight > rcDest.right ) {
- lDrawWidth -= lDestRight - rcDest.right;
- lDestRight = rcDest.right;
- }
- ::BitBlt(hDC, rcDest.left + lWidth * i, rcDest.top + lHeight * j, \
- lDestRight - lDestLeft, lDestBottom - lDestTop, hCloneDC, \
- rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, SRCCOPY);
- }
- }
- }
- else if( bTiledX ) {
- LONG lWidth = rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right;
- int iTimes = (rcDest.right - rcDest.left + lWidth - 1) / lWidth;
- for( int i = 0; i < iTimes; ++i ) {
- LONG lDestLeft = rcDest.left + lWidth * i;
- LONG lDestRight = rcDest.left + lWidth * (i + 1);
- LONG lDrawWidth = lWidth;
- if( lDestRight > rcDest.right ) {
- lDrawWidth -= lDestRight - rcDest.right;
- lDestRight = rcDest.right;
- }
- rcDest.bottom -= rcDest.top;
- ::StretchBlt(hDC, lDestLeft, rcDest.top, lDestRight - lDestLeft, rcDest.bottom,
- hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \
- lDrawWidth, rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, SRCCOPY);
- }
- }
- else { // bTiledY
- LONG lHeight = rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom;
- int iTimes = (rcDest.bottom - rcDest.top + lHeight - 1) / lHeight;
- for( int i = 0; i < iTimes; ++i ) {
- LONG lDestTop = rcDest.top + lHeight * i;
- LONG lDestBottom = rcDest.top + lHeight * (i + 1);
- LONG lDrawHeight = lHeight;
- if( lDestBottom > rcDest.bottom ) {
- lDrawHeight -= lDestBottom - rcDest.bottom;
- lDestBottom = rcDest.bottom;
- }
- rcDest.right -= rcDest.left;
- ::StretchBlt(hDC, rcDest.left, rcDest.top + lHeight * i, rcDest.right, lDestBottom - lDestTop,
- hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \
- rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, lDrawHeight, SRCCOPY);
- }
- }
- }
- }
- // left-top
- if( rcCorners.left > 0 && rcCorners.top > 0 ) {
- rcDest.left = rc.left;
- rcDest.top = rc.top;
- rcDest.right = rcCorners.left;
- rcDest.bottom = rcCorners.top;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.left, rcBmpPart.top, rcCorners.left, rcCorners.top, SRCCOPY);
- }
- }
- // top
- if( rcCorners.top > 0 ) {
- rcDest.left = rc.left + rcCorners.left;
- rcDest.top = rc.top;
- rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;
- rcDest.bottom = rcCorners.top;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.left + rcCorners.left, rcBmpPart.top, rcBmpPart.right - rcBmpPart.left - \
- rcCorners.left - rcCorners.right, rcCorners.top, SRCCOPY);
- }
- }
- // right-top
- if( rcCorners.right > 0 && rcCorners.top > 0 ) {
- rcDest.left = rc.right - rcCorners.right;
- rcDest.top = rc.top;
- rcDest.right = rcCorners.right;
- rcDest.bottom = rcCorners.top;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.right - rcCorners.right, rcBmpPart.top, rcCorners.right, rcCorners.top, SRCCOPY);
- }
- }
- // left
- if( rcCorners.left > 0 ) {
- rcDest.left = rc.left;
- rcDest.top = rc.top + rcCorners.top;
- rcDest.right = rcCorners.left;
- rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.left, rcBmpPart.top + rcCorners.top, rcCorners.left, rcBmpPart.bottom - \
- rcBmpPart.top - rcCorners.top - rcCorners.bottom, SRCCOPY);
- }
- }
- // right
- if( rcCorners.right > 0 ) {
- rcDest.left = rc.right - rcCorners.right;
- rcDest.top = rc.top + rcCorners.top;
- rcDest.right = rcCorners.right;
- rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.right - rcCorners.right, rcBmpPart.top + rcCorners.top, rcCorners.right, \
- rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, SRCCOPY);
- }
- }
- // left-bottom
- if( rcCorners.left > 0 && rcCorners.bottom > 0 ) {
- rcDest.left = rc.left;
- rcDest.top = rc.bottom - rcCorners.bottom;
- rcDest.right = rcCorners.left;
- rcDest.bottom = rcCorners.bottom;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.left, rcBmpPart.bottom - rcCorners.bottom, rcCorners.left, rcCorners.bottom, SRCCOPY);
- }
- }
- // bottom
- if( rcCorners.bottom > 0 ) {
- rcDest.left = rc.left + rcCorners.left;
- rcDest.top = rc.bottom - rcCorners.bottom;
- rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;
- rcDest.bottom = rcCorners.bottom;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.left + rcCorners.left, rcBmpPart.bottom - rcCorners.bottom, \
- rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, rcCorners.bottom, SRCCOPY);
- }
- }
- // right-bottom
- if( rcCorners.right > 0 && rcCorners.bottom > 0 ) {
- rcDest.left = rc.right - rcCorners.right;
- rcDest.top = rc.bottom - rcCorners.bottom;
- rcDest.right = rcCorners.right;
- rcDest.bottom = rcCorners.bottom;
- rcDest.right += rcDest.left;
- rcDest.bottom += rcDest.top;
- if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
- rcDest.right -= rcDest.left;
- rcDest.bottom -= rcDest.top;
- ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
- rcBmpPart.right - rcCorners.right, rcBmpPart.bottom - rcCorners.bottom, rcCorners.right, \
- rcCorners.bottom, SRCCOPY);
- }
- }
- }
- }
- ::SelectObject(hCloneDC, hOldBitmap);
- ::DeleteDC(hCloneDC);
- }
- bool CRenderEngine::DrawImage(HDC hDC, CPaintManagerUI* pManager, const RECT& rcItem, const RECT& rcPaint,
- TDrawInfo& drawInfo)
- {
- // 1、aaa.jpg
- // 2、file='aaa.jpg' res='' restype='0' dest='0,0,0,0' source='0,0,0,0' corner='0,0,0,0'
- // mask='#FF0000' fade='255' hole='false' xtiled='false' ytiled='false' hsl='false'
- if( pManager == NULL ) return true;
- if( drawInfo.pImageInfo == NULL ) {
- if( drawInfo.bLoaded ) return false;
- drawInfo.bLoaded = true;
- if( drawInfo.sDrawString.IsEmpty() ) return false;
- bool bUseRes = false;
- CDuiString sImageName = drawInfo.sDrawString;
- CDuiString sImageResType;
- DWORD dwMask = 0;
- bool bUseHSL = false;
- CDuiString sItem;
- CDuiString sValue;
- LPTSTR pstr = NULL;
- LPCTSTR pstrImage = drawInfo.sDrawString.GetData();
- while( *pstrImage != _T('\0') ) {
- sItem.Empty();
- sValue.Empty();
- while( *pstrImage > _T('\0') && *pstrImage <= _T(' ') ) pstrImage = ::CharNext(pstrImage);
- while( *pstrImage != _T('\0') && *pstrImage != _T('=') && *pstrImage > _T(' ') ) {
- LPTSTR pstrTemp = ::CharNext(pstrImage);
- while( pstrImage < pstrTemp) {
- sItem += *pstrImage++;
- }
- }
- while( *pstrImage > _T('\0') && *pstrImage <= _T(' ') ) pstrImage = ::CharNext(pstrImage);
- if( *pstrImage++ != _T('=') ) break;
- while( *pstrImage > _T('\0') && *pstrImage <= _T(' ') ) pstrImage = ::CharNext(pstrImage);
- if( *pstrImage++ != _T('\'') ) break;
- while( *pstrImage != _T('\0') && *pstrImage != _T('\'') ) {
- LPTSTR pstrTemp = ::CharNext(pstrImage);
- while( pstrImage < pstrTemp) {
- sValue += *pstrImage++;
- }
- }
- if( *pstrImage++ != _T('\'') ) break;
- if( !sValue.IsEmpty() ) {
- if( sItem == _T("file") ) {
- sImageName = sValue;
- }
- else if( sItem == _T("res") ) {
- bUseRes = true;
- sImageName = sValue;
- }
- else if( sItem == _T("restype") ) {
- sImageResType = sValue;
- }
- else if( sItem == _T("dest") ) {
- drawInfo.rcDestOffset.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
- drawInfo.rcDestOffset.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- drawInfo.rcDestOffset.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- drawInfo.rcDestOffset.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- }
- else if( sItem == _T("source") ) {
- drawInfo.rcBmpPart.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
- drawInfo.rcBmpPart.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- drawInfo.rcBmpPart.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- drawInfo.rcBmpPart.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- }
- else if( sItem == _T("corner") ) {
- drawInfo.rcCorner.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
- drawInfo.rcCorner.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- drawInfo.rcCorner.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- drawInfo.rcCorner.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- }
- else if( sItem == _T("mask") ) {
- if( sValue[0] == _T('#')) dwMask = _tcstoul(sValue.GetData() + 1, &pstr, 16);
- else dwMask = _tcstoul(sValue.GetData(), &pstr, 16);
- }
- else if( sItem == _T("fade") ) {
- drawInfo.uFade = (BYTE)_tcstoul(sValue.GetData(), &pstr, 10);
- }
- else if( sItem == _T("hole") ) {
- drawInfo.bHole = (_tcscmp(sValue.GetData(), _T("true")) == 0);
- }
- else if( sItem == _T("xtiled") ) {
- drawInfo.bTiledX = (_tcscmp(sValue.GetData(), _T("true")) == 0);
- }
- else if( sItem == _T("ytiled") ) {
- drawInfo.bTiledY = (_tcscmp(sValue.GetData(), _T("true")) == 0);
- }
- else if( sItem == _T("hsl") ) {
- bUseHSL = (_tcscmp(sValue.GetData(), _T("true")) == 0);
- }
- }
- if( *pstrImage++ != _T(' ') ) break;
- }
- drawInfo.sImageName = sImageName;
- const TImageInfo* data = NULL;
- if( bUseRes == false ) {
- data = pManager->GetImageEx((LPCTSTR)sImageName, NULL, dwMask, bUseHSL);
- }
- else {
- data = pManager->GetImageEx((LPCTSTR)sImageName, (LPCTSTR)sImageResType, dwMask, bUseHSL);
- }
- if( !data ) return false;
- drawInfo.pImageInfo = data;
- if( drawInfo.rcBmpPart.left == 0 && drawInfo.rcBmpPart.right == 0 &&
- drawInfo.rcBmpPart.top == 0 && drawInfo.rcBmpPart.bottom == 0 ) {
- drawInfo.rcBmpPart.right = data->nX;
- drawInfo.rcBmpPart.bottom = data->nY;
- }
- }
- if( drawInfo.rcBmpPart.right > drawInfo.pImageInfo->nX ) drawInfo.rcBmpPart.right = drawInfo.pImageInfo->nX;
- if( drawInfo.rcBmpPart.bottom > drawInfo.pImageInfo->nY ) drawInfo.rcBmpPart.bottom = drawInfo.pImageInfo->nY;
- if( hDC == NULL ) return true;
- RECT rcDest = rcItem;
- if( drawInfo.rcDestOffset.left != 0 || drawInfo.rcDestOffset.top != 0 ||
- drawInfo.rcDestOffset.right != 0 || drawInfo.rcDestOffset.bottom != 0 ) {
- rcDest.left = rcItem.left + drawInfo.rcDestOffset.left;
- rcDest.top = rcItem.top + drawInfo.rcDestOffset.top;
- rcDest.right = rcItem.left + drawInfo.rcDestOffset.right;
- if( rcDest.right > rcItem.right ) rcDest.right = rcItem.right;
- rcDest.bottom = rcItem.top + drawInfo.rcDestOffset.bottom;
- if( rcDest.bottom > rcItem.bottom ) rcDest.bottom = rcItem.bottom;
- }
- RECT rcTemp;
- if( !::IntersectRect(&rcTemp, &rcDest, &rcItem) ) return true;
- if( !::IntersectRect(&rcTemp, &rcDest, &rcPaint) ) return true;
- DrawImage(hDC, drawInfo.pImageInfo->hBitmap, rcDest, rcPaint, drawInfo.rcBmpPart, drawInfo.rcCorner,
- drawInfo.pImageInfo->bAlpha, drawInfo.uFade, drawInfo.bHole, drawInfo.bTiledX, drawInfo.bTiledY);
- return true;
- }
- void CRenderEngine::DrawColor(HDC hDC, const RECT& rc, DWORD color)
- {
- if( color <= 0x00FFFFFF ) return;
- if( color >= 0xFF000000 )
- {
- ::SetBkColor(hDC, RGB(GetBValue(color), GetGValue(color), GetRValue(color)));
- ::ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL);
- }
- else
- {
- // Create a new 32bpp bitmap with room for an alpha channel
- BITMAPINFO bmi = { 0 };
- bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- bmi.bmiHeader.biWidth = 1;
- bmi.bmiHeader.biHeight = 1;
- bmi.bmiHeader.biPlanes = 1;
- bmi.bmiHeader.biBitCount = 32;
- bmi.bmiHeader.biCompression = BI_RGB;
- bmi.bmiHeader.biSizeImage = 1 * 1 * sizeof(DWORD);
- LPDWORD pDest = NULL;
- HBITMAP hBitmap = ::CreateDIBSection(hDC, &bmi, DIB_RGB_COLORS, (LPVOID*) &pDest, NULL, 0);
- if( !hBitmap ) return;
- *pDest = color;
- RECT rcBmpPart = {0, 0, 1, 1};
- RECT rcCorners = {0};
- DrawImage(hDC, hBitmap, rc, rc, rcBmpPart, rcCorners, true, 255);
- ::DeleteObject(hBitmap);
- }
- }
- void CRenderEngine::DrawGradient(HDC hDC, const RECT& rc, DWORD dwFirst, DWORD dwSecond, bool bVertical, int nSteps)
- {
- typedef BOOL (WINAPI *LPALPHABLEND)(HDC, int, int, int, int,HDC, int, int, int, int, BLENDFUNCTION);
- static LPALPHABLEND lpAlphaBlend = (LPALPHABLEND) ::GetProcAddress(::GetModuleHandle(_T("msimg32.dll")), "AlphaBlend");
- if( lpAlphaBlend == NULL ) lpAlphaBlend = AlphaBitBlt;
- typedef BOOL (WINAPI *PGradientFill)(HDC, PTRIVERTEX, ULONG, PVOID, ULONG, ULONG);
- static PGradientFill lpGradientFill = (PGradientFill) ::GetProcAddress(::GetModuleHandle(_T("msimg32.dll")), "GradientFill");
- BYTE bAlpha = (BYTE)(((dwFirst >> 24) + (dwSecond >> 24)) >> 1);
- if( bAlpha == 0 ) return;
- int cx = rc.right - rc.left;
- int cy = rc.bottom - rc.top;
- RECT rcPaint = rc;
- HDC hPaintDC = hDC;
- HBITMAP hPaintBitmap = NULL;
- HBITMAP hOldPaintBitmap = NULL;
- if( bAlpha < 255 ) {
- rcPaint.left = rcPaint.top = 0;
- rcPaint.right = cx;
- rcPaint.bottom = cy;
- hPaintDC = ::CreateCompatibleDC(hDC);
- hPaintBitmap = ::CreateCompatibleBitmap(hDC, cx, cy);
- ASSERT(hPaintDC);
- ASSERT(hPaintBitmap);
- hOldPaintBitmap = (HBITMAP) ::SelectObject(hPaintDC, hPaintBitmap);
- }
- if( lpGradientFill != NULL )
- {
- TRIVERTEX triv[2] =
- {
- { rcPaint.left, rcPaint.top, GetBValue(dwFirst) << 8, GetGValue(dwFirst) << 8, GetRValue(dwFirst) << 8, 0xFF00 },
- { rcPaint.right, rcPaint.bottom, GetBValue(dwSecond) << 8, GetGValue(dwSecond) << 8, GetRValue(dwSecond) << 8, 0xFF00 }
- };
- GRADIENT_RECT grc = { 0, 1 };
- lpGradientFill(hPaintDC, triv, 2, &grc, 1, bVertical ? GRADIENT_FILL_RECT_V : GRADIENT_FILL_RECT_H);
- }
- else
- {
- // Determine how many shades
- int nShift = 1;
- if( nSteps >= 64 ) nShift = 6;
- else if( nSteps >= 32 ) nShift = 5;
- else if( nSteps >= 16 ) nShift = 4;
- else if( nSteps >= 8 ) nShift = 3;
- else if( nSteps >= 4 ) nShift = 2;
- int nLines = 1 << nShift;
- for( int i = 0; i < nLines; i++ ) {
- // Do a little alpha blending
- BYTE bR = (BYTE) ((GetBValue(dwSecond) * (nLines - i) + GetBValue(dwFirst) * i) >> nShift);
- BYTE bG = (BYTE) ((GetGValue(dwSecond) * (nLines - i) + GetGValue(dwFirst) * i) >> nShift);
- BYTE bB = (BYTE) ((GetRValue(dwSecond) * (nLines - i) + GetRValue(dwFirst) * i) >> nShift);
- // ... then paint with the resulting color
- HBRUSH hBrush = ::CreateSolidBrush(RGB(bR,bG,bB));
- RECT r2 = rcPaint;
- if( bVertical ) {
- r2.bottom = rc.bottom - ((i * (rc.bottom - rc.top)) >> nShift);
- r2.top = rc.bottom - (((i + 1) * (rc.bottom - rc.top)) >> nShift);
- if( (r2.bottom - r2.top) > 0 ) ::FillRect(hDC, &r2, hBrush);
- }
- else {
- r2.left = rc.right - (((i + 1) * (rc.right - rc.left)) >> nShift);
- r2.right = rc.right - ((i * (rc.right - rc.left)) >> nShift);
- if( (r2.right - r2.left) > 0 ) ::FillRect(hPaintDC, &r2, hBrush);
- }
- ::DeleteObject(hBrush);
- }
- }
- if( bAlpha < 255 ) {
- BLENDFUNCTION bf = { AC_SRC_OVER, 0, bAlpha, AC_SRC_ALPHA };
- lpAlphaBlend(hDC, rc.left, rc.top, cx, cy, hPaintDC, 0, 0, cx, cy, bf);
- ::SelectObject(hPaintDC, hOldPaintBitmap);
- ::DeleteObject(hPaintBitmap);
- ::DeleteDC(hPaintDC);
- }
- }
- /*
- * ---------------------------- modify ----------------------------------------
- */
- void CRenderEngine::DrawGradientEx(HDC hDC,const RECT& rc,COLORREF crColors[],size_t nColors, DWORD dwFillMode)
- {
- if (nColors<2)
- {
- return;
- }
- typedef BOOL (WINAPI *PGradientFill)(HDC, PTRIVERTEX, ULONG, PVOID, ULONG, ULONG);
- static PGradientFill lpGradientFill = (PGradientFill) ::GetProcAddress(::GetModuleHandle(_T("msimg32.dll")), "GradientFill");
- ASSERT(lpGradientFill!=nullptr);
- int l = rc.left;
- int t = rc.top;
- int cx = rc.right-rc.left;
- int cy = rc.bottom-rc.top;
- LPTRIVERTEX pVert = new TRIVERTEX[nColors];
- for (size_t i=0;i<nColors;i++)
- {
- pVert[i].Red = GetBValue(crColors[i])*0x100;
- pVert[i].Green = GetGValue(crColors[i])*0x100;
- pVert[i].Blue = GetRValue(crColors[i])*0x100;
- pVert[i].Alpha = (crColors[i]>>24)*0x100;
- if (GRADIENT_FILL_RECT_H == dwFillMode)
- {
- pVert[i].x = l+(cx*i/(nColors-1));
- pVert[i].y = t+(cy*(i%2));
- }
- else
- {
- pVert[i].x = l+(cx*(i%2));
- pVert[i].y = t+(cy*i/(nColors-1));
- }
- }
- LPGRADIENT_RECT pGr = new GRADIENT_RECT[nColors-1];
- for (size_t i=0;i<nColors-1;i++)
- {
- pGr[i].UpperLeft = i;
- pGr[i].LowerRight = i+1;
- }
- lpGradientFill(hDC,pVert,nColors,pGr,nColors-1,dwFillMode);
- delete[] pGr;
- delete[] pVert;
- }
- /*--------------------------------------------------------------------------------------*/
- void CRenderEngine::DrawLine( HDC hDC, const RECT& rc, int nSize, DWORD dwPenColor, int nStyle)
- {
- ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
- LOGPEN lg;
- lg.lopnColor = RGB(GetBValue(dwPenColor), GetGValue(dwPenColor), GetRValue(dwPenColor));
- lg.lopnStyle = nStyle;
- lg.lopnWidth.x = nSize;
- HPEN hPen = CreatePenIndirect(&lg);
- HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
- POINT ptTemp = { 0 };
- ::MoveToEx(hDC, rc.left, rc.top, &ptTemp);
- ::LineTo(hDC, rc.right, rc.bottom);
- ::SelectObject(hDC, hOldPen);
- ::DeleteObject(hPen);
- }
- void CRenderEngine::DrawRect(HDC hDC, const RECT& rc, int nSize, DWORD dwPenColor, int nStyle)
- {
- ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
- HPEN hPen = ::CreatePen(nStyle | PS_INSIDEFRAME, nSize, RGB(GetBValue(dwPenColor), GetGValue(dwPenColor), GetRValue(dwPenColor)));
- HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
- ::SelectObject(hDC, ::GetStockObject(HOLLOW_BRUSH));
- ::Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
- ::SelectObject(hDC, hOldPen);
- ::DeleteObject(hPen);
- }
- void CRenderEngine::DrawRoundRect(HDC hDC, const RECT& rc, int nSize, int width, int height, DWORD dwPenColor, int nStyle)
- {
- ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
- HPEN hPen = ::CreatePen(nStyle | PS_INSIDEFRAME, nSize, RGB(GetBValue(dwPenColor), GetGValue(dwPenColor), GetRValue(dwPenColor)));
- HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
- ::SelectObject(hDC, ::GetStockObject(HOLLOW_BRUSH));
- ::RoundRect(hDC, rc.left, rc.top, rc.right, rc.bottom, width, height);
- ::SelectObject(hDC, hOldPen);
- ::DeleteObject(hPen);
- }
- void CRenderEngine::DrawText(HDC hDC, CPaintManagerUI* pManager, RECT& rc, LPCTSTR pstrText, DWORD dwTextColor, int iFont, UINT uStyle)
- {
- ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
- if( pstrText == NULL || pManager == NULL ) return;
- CDuiString sText = pstrText;
- CPaintManagerUI::ProcessMultiLanguageTokens(sText);
- pstrText = sText;
- ::SetBkMode(hDC, TRANSPARENT);
- ::SetTextColor(hDC, RGB(GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)));
- HFONT hOldFont = (HFONT)::SelectObject(hDC, pManager->GetFont(iFont));
- ::DrawText(hDC, pstrText, -1, &rc, uStyle | DT_NOPREFIX);
- ::SelectObject(hDC, hOldFont);
- }
- void CRenderEngine::DrawHtmlText(HDC hDC, CPaintManagerUI* pManager, RECT& rc, LPCTSTR pstrText, DWORD dwTextColor, RECT* prcLinks, CDuiString* sLinks, int& nLinkRects, UINT uStyle)
- {
- // 考虑到在xml编辑器中使用<>符号不方便,可以使用{}符号代替
- // 支持标签嵌套(如<l><b>text</b></l>),但是交叉嵌套是应该避免的(如<l><b>text</l></b>)
- // The string formatter supports a kind of "mini-html" that consists of various short tags:
- //
- // Bold: <b>text</b>
- // Color: <c #xxxxxx>text</c> where x = RGB in hex
- // Font: <f x>text</f> where x = font id
- // Italic: <i>text</i>
- // Image: <i x y z> where x = image name and y = imagelist num and z(optional) = imagelist id
- // Link: <a x>text</a> where x(optional) = link content, normal like app:notepad or http:www.xxx.com
- // NewLine <n>
- // Paragraph: <p x>text</p> where x = extra pixels indent in p
- // Raw Text: <r>text</r>
- // Selected: <s>text</s>
- // Underline: <u>text</u>
- // X Indent: <x i> where i = hor indent in pixels
- // Y Indent: <y i> where i = ver indent in pixels
- ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
- if( pstrText == NULL || pManager == NULL ) return;
- if( ::IsRectEmpty(&rc) ) return;
- bool bDraw = (uStyle & DT_CALCRECT) == 0;
- CStdPtrArray aFontArray(10);
- CStdPtrArray aColorArray(10);
- CStdPtrArray aPIndentArray(10);
- RECT rcClip = { 0 };
- ::GetClipBox(hDC, &rcClip);
- HRGN hOldRgn = ::CreateRectRgnIndirect(&rcClip);
- HRGN hRgn = ::CreateRectRgnIndirect(&rc);
- if( bDraw ) ::ExtSelectClipRgn(hDC, hRgn, RGN_AND);
- CDuiString sText = pstrText;
- CPaintManagerUI::ProcessMultiLanguageTokens(sText);
- pstrText = sText;
- TEXTMETRIC* pTm = &pManager->GetDefaultFontInfo()->tm;
- HFONT hOldFont = (HFONT) ::SelectObject(hDC, pManager->GetDefaultFontInfo()->hFont);
- ::SetBkMode(hDC, TRANSPARENT);
- ::SetTextColor(hDC, RGB(GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)));
- DWORD dwBkColor = pManager->GetDefaultSelectedBkColor();
- ::SetBkColor(hDC, RGB(GetBValue(dwBkColor), GetGValue(dwBkColor), GetRValue(dwBkColor)));
- // If the drawstyle include a alignment, we'll need to first determine the text-size so
- // we can draw it at the correct position...
- if( ((uStyle & DT_CENTER) != 0 || (uStyle & DT_RIGHT) != 0 || (uStyle & DT_VCENTER) != 0 || (uStyle & DT_BOTTOM) != 0) && (uStyle & DT_CALCRECT) == 0 ) {
- RECT rcText = { 0, 0, 9999, 100 };
- int nLinks = 0;
- DrawHtmlText(hDC, pManager, rcText, pstrText, dwTextColor, NULL, NULL, nLinks, uStyle | DT_CALCRECT);
- if( (uStyle & DT_SINGLELINE) != 0 ){
- if( (uStyle & DT_CENTER) != 0 ) {
- rc.left = rc.left + ((rc.right - rc.left) / 2) - ((rcText.right - rcText.left) / 2);
- rc.right = rc.left + (rcText.right - rcText.left);
- }
- if( (uStyle & DT_RIGHT) != 0 ) {
- rc.left = rc.right - (rcText.right - rcText.left);
- }
- }
- if( (uStyle & DT_VCENTER) != 0 ) {
- rc.top = rc.top + ((rc.bottom - rc.top) / 2) - ((rcText.bottom - rcText.top) / 2);
- rc.bottom = rc.top + (rcText.bottom - rcText.top);
- }
- if( (uStyle & DT_BOTTOM) != 0 ) {
- rc.top = rc.bottom - (rcText.bottom - rcText.top);
- }
- }
- bool bHoverLink = false;
- CDuiString sHoverLink;
- POINT ptMouse = pManager->GetMousePos();
- for( int i = 0; !bHoverLink && i < nLinkRects; i++ ) {
- if( ::PtInRect(prcLinks + i, ptMouse) ) {
- sHoverLink = *(CDuiString*)(sLinks + i);
- bHoverLink = true;
- }
- }
- POINT pt = { rc.left, rc.top };
- int iLinkIndex = 0;
- int cyLine = pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1);
- int cyMinHeight = 0;
- int cxMaxWidth = 0;
- POINT ptLinkStart = { 0 };
- bool bLineEnd = false;
- bool bInRaw = false;
- bool bInLink = false;
- bool bInSelected = false;
- int iLineLinkIndex = 0;
- // 排版习惯是图文底部对齐,所以每行绘制都要分两步,先计算高度,再绘制
- CStdPtrArray aLineFontArray;
- CStdPtrArray aLineColorArray;
- CStdPtrArray aLinePIndentArray;
- LPCTSTR pstrLineBegin = pstrText;
- bool bLineInRaw = false;
- bool bLineInLink = false;
- bool bLineInSelected = false;
- int cyLineHeight = 0;
- bool bLineDraw = false; // 行的第二阶段:绘制
- while( *pstrText != _T('\0') ) {
- if( pt.x >= rc.right || *pstrText == _T('\n') || bLineEnd ) {
- if( *pstrText == _T('\n') ) pstrText++;
- if( bLineEnd ) bLineEnd = false;
- if( !bLineDraw ) {
- if( bInLink && iLinkIndex < nLinkRects ) {
- ::SetRect(&prcLinks[iLinkIndex++], ptLinkStart.x, ptLinkStart.y, MIN(pt.x, rc.right), pt.y + cyLine);
- CDuiString *pStr1 = (CDuiString*)(sLinks + iLinkIndex - 1);
- CDuiString *pStr2 = (CDuiString*)(sLinks + iLinkIndex);
- *pStr2 = *pStr1;
- }
- for( int i = iLineLinkIndex; i < iLinkIndex; i++ ) {
- prcLinks[i].bottom = pt.y + cyLine;
- }
- if( bDraw ) {
- bInLink = bLineInLink;
- iLinkIndex = iLineLinkIndex;
- }
- }
- else {
- if( bInLink && iLinkIndex < nLinkRects ) iLinkIndex++;
- bLineInLink = bInLink;
- iLineLinkIndex = iLinkIndex;
- }
- if( (uStyle & DT_SINGLELINE) != 0 && (!bDraw || bLineDraw) ) break;
- if( bDraw ) bLineDraw = !bLineDraw; // !
- pt.x = rc.left;
- if( !bLineDraw ) pt.y += cyLine;
- if( pt.y > rc.bottom && bDraw ) break;
- ptLinkStart = pt;
- cyLine = pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1);
- if( pt.x >= rc.right ) break;
- }
- else if( !bInRaw && ( *pstrText == _T('<') || *pstrText == _T('{') )
- && ( pstrText[1] >= _T('a') && pstrText[1] <= _T('z') )
- && ( pstrText[2] == _T(' ') || pstrText[2] == _T('>') || pstrText[2] == _T('}') ) ) {
- pstrText++;
- LPCTSTR pstrNextStart = NULL;
- switch( *pstrText ) {
- case _T('a'): // Link
- {
- pstrText++;
- while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
- if( iLinkIndex < nLinkRects && !bLineDraw ) {
- CDuiString *pStr = (CDuiString*)(sLinks + iLinkIndex);
- pStr->Empty();
- while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') ) {
- LPCTSTR pstrTemp = ::CharNext(pstrText);
- while( pstrText < pstrTemp) {
- *pStr += *pstrText++;
- }
- }
- }
- DWORD clrColor = pManager->GetDefaultLinkFontColor();
- if( bHoverLink && iLinkIndex < nLinkRects ) {
- CDuiString *pStr = (CDuiString*)(sLinks + iLinkIndex);
- if( sHoverLink == *pStr ) clrColor = pManager->GetDefaultLinkHoverFontColor();
- }
- //else if( prcLinks == NULL ) {
- // if( ::PtInRect(&rc, ptMouse) )
- // clrColor = pManager->GetDefaultLinkHoverFontColor();
- //}
- aColorArray.Add((LPVOID)clrColor);
- ::SetTextColor(hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
- TFontInfo* pFontInfo = pManager->GetDefaultFontInfo();
- if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
- if( pFontInfo->bUnderline == false ) {
- HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic);
- if( hFont == NULL ) {
- hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic);
- g_iFontID += 1;
- }
- pFontInfo = pManager->GetFontInfo(hFont);
- aFontArray.Add(pFontInfo);
- pTm = &pFontInfo->tm;
- ::SelectObject(hDC, pFontInfo->hFont);
- cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
- }
- ptLinkStart = pt;
- bInLink = true;
- }
- break;
- case _T('b'): // Bold
- {
- pstrText++;
- TFontInfo* pFontInfo = pManager->GetDefaultFontInfo();
- if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
- if( pFontInfo->bBold == false ) {
- HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, true, pFontInfo->bUnderline, pFontInfo->bItalic);
- if( hFont == NULL ) {
- hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, true, pFontInfo->bUnderline, pFontInfo->bItalic);
- g_iFontID += 1;
- }
- pFontInfo = pManager->GetFontInfo(hFont);
- aFontArray.Add(pFontInfo);
- pTm = &pFontInfo->tm;
- ::SelectObject(hDC, pFontInfo->hFont);
- cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
- }
- }
- break;
- case _T('c'): // Color
- {
- pstrText++;
- while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
- if( *pstrText == _T('#')) pstrText++;
- DWORD clrColor = _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 16);
- aColorArray.Add((LPVOID)clrColor);
- ::SetTextColor(hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
- }
- break;
- case _T('f'): // Font
- {
- pstrText++;
- while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
- LPCTSTR pstrTemp = pstrText;
- int iFont = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
- //if( isdigit(*pstrText) ) { // debug版本会引起异常
- if( pstrTemp != pstrText ) {
- TFontInfo* pFontInfo = pManager->GetFontInfo(iFont);
- aFontArray.Add(pFontInfo);
- pTm = &pFontInfo->tm;
- ::SelectObject(hDC, pFontInfo->hFont);
- }
- else {
- CDuiString sFontName;
- int iFontSize = 10;
- CDuiString sFontAttr;
- bool bBold = false;
- bool bUnderline = false;
- bool bItalic = false;
- while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') && *pstrText != _T(' ') ) {
- pstrTemp = ::CharNext(pstrText);
- while( pstrText < pstrTemp) {
- sFontName += *pstrText++;
- }
- }
- while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
- if( isdigit(*pstrText) ) {
- iFontSize = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
- }
- while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
- while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') ) {
- pstrTemp = ::CharNext(pstrText);
- while( pstrText < pstrTemp) {
- sFontAttr += *pstrText++;
- }
- }
- sFontAttr.MakeLower();
- if( sFontAttr.Find(_T("bold")) >= 0 ) bBold = true;
- if( sFontAttr.Find(_T("underline")) >= 0 ) bUnderline = true;
- if( sFontAttr.Find(_T("italic")) >= 0 ) bItalic = true;
- HFONT hFont = pManager->GetFont(sFontName, iFontSize, bBold, bUnderline, bItalic);
- if( hFont == NULL ) {
- hFont = pManager->AddFont(g_iFontID, sFontName, iFontSize, bBold, bUnderline, bItalic);
- g_iFontID += 1;
- }
- TFontInfo* pFontInfo = pManager->GetFontInfo(hFont);
- aFontArray.Add(pFontInfo);
- pTm = &pFontInfo->tm;
- ::SelectObject(hDC, pFontInfo->hFont);
- }
- cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
- }
- break;
- case _T('i'): // Italic or Image
- {
- pstrNextStart = pstrText - 1;
- pstrText++;
- CDuiString sImageString = pstrText;
- int iWidth = 0;
- int iHeight = 0;
- while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
- const TImageInfo* pImageInfo = NULL;
- CDuiString sName;
- while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') && *pstrText != _T(' ') ) {
- LPCTSTR pstrTemp = ::CharNext(pstrText);
- while( pstrText < pstrTemp) {
- sName += *pstrText++;
- }
- }
- if( sName.IsEmpty() ) { // Italic
- pstrNextStart = NULL;
- TFontInfo* pFontInfo = pManager->GetDefaultFontInfo();
- if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
- if( pFontInfo->bItalic == false ) {
- HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, pFontInfo->bUnderline, true);
- if( hFont == NULL ) {
- hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, pFontInfo->bUnderline, true);
- g_iFontID += 1;
- }
- pFontInfo = pManager->GetFontInfo(hFont);
- aFontArray.Add(pFontInfo);
- pTm = &pFontInfo->tm;
- ::SelectObject(hDC, pFontInfo->hFont);
- cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
- }
- }
- else {
- while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
- int iImageListNum = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
- if( iImageListNum <= 0 ) iImageListNum = 1;
- while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
- int iImageListIndex = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
- if( iImageListIndex < 0 || iImageListIndex >= iImageListNum ) iImageListIndex = 0;
- if( _tcsstr(sImageString.GetData(), _T("file=\'")) != NULL || _tcsstr(sImageString.GetData(), _T("res=\'")) != NULL ) {
- CDuiString sImageResType;
- CDuiString sImageName;
- LPCTSTR pStrImage = sImageString.GetData();
- CDuiString sItem;
- CDuiString sValue;
- while( *pStrImage != _T('\0') ) {
- sItem.Empty();
- sValue.Empty();
- while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) pStrImage = ::CharNext(pStrImage);
- while( *pStrImage != _T('\0') && *pStrImage != _T('=') && *pStrImage > _T(' ') ) {
- LPTSTR pstrTemp = ::CharNext(pStrImage);
- while( pStrImage < pstrTemp) {
- sItem += *pStrImage++;
- }
- }
- while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) pStrImage = ::CharNext(pStrImage);
- if( *pStrImage++ != _T('=') ) break;
- while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) pStrImage = ::CharNext(pStrImage);
- if( *pStrImage++ != _T('\'') ) break;
- while( *pStrImage != _T('\0') && *pStrImage != _T('\'') ) {
- LPTSTR pstrTemp = ::CharNext(pStrImage);
- while( pStrImage < pstrTemp) {
- sValue += *pStrImage++;
- }
- }
- if( *pStrImage++ != _T('\'') ) break;
- if( !sValue.IsEmpty() ) {
- if( sItem == _T("file") || sItem == _T("res") ) {
- sImageName = sValue;
- }
- else if( sItem == _T("restype") ) {
- sImageResType = sValue;
- }
- }
- if( *pStrImage++ != _T(' ') ) break;
- }
- pImageInfo = pManager->GetImageEx((LPCTSTR)sImageName, sImageResType);
- }
- else
- pImageInfo = pManager->GetImageEx((LPCTSTR)sName);
- if( pImageInfo ) {
- iWidth = pImageInfo->nX;
- iHeight = pImageInfo->nY;
- if( iImageListNum > 1 ) iWidth /= iImageListNum;
- if( pt.x + iWidth > rc.right && pt.x > rc.left && (uStyle & DT_SINGLELINE) == 0 ) {
- bLineEnd = true;
- }
- else {
- pstrNextStart = NULL;
- if( bDraw && bLineDraw ) {
- CDuiRect rcImage(pt.x, pt.y + cyLineHeight - iHeight, pt.x + iWidth, pt.y + cyLineHeight);
- if( iHeight < cyLineHeight ) {
- rcImage.bottom -= (cyLineHeight - iHeight) / 2;
- rcImage.top = rcImage.bottom - iHeight;
- }
- CDuiRect rcBmpPart(0, 0, iWidth, iHeight);
- rcBmpPart.left = iWidth * iImageListIndex;
- rcBmpPart.right = iWidth * (iImageListIndex + 1);
- CDuiRect rcCorner(0, 0, 0, 0);
- DrawImage(hDC, pImageInfo->hBitmap, rcImage, rcImage, rcBmpPart, rcCorner, \
- pImageInfo->bAlpha, 255);
- }
- cyLine = MAX(iHeight, cyLine);
- pt.x += iWidth;
- cyMinHeight = pt.y + iHeight;
- cxMaxWidth = MAX(cxMaxWidth, pt.x);
- }
- }
- else pstrNextStart = NULL;
- }
- }
- break;
- case _T('n'): // Newline
- {
- pstrText++;
- if( (uStyle & DT_SINGLELINE) != 0 ) break;
- bLineEnd = true;
- }
- break;
- case _T('p'): // Paragraph
- {
- pstrText++;
- if( pt.x > rc.left ) bLineEnd = true;
- while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
- int cyLineExtra = (int)_tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
- aPIndentArray.Add((LPVOID)cyLineExtra);
- cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + cyLineExtra);
- }
- break;
- case _T('r'): // Raw Text
- {
- pstrText++;
- bInRaw = true;
- }
- break;
- case _T('s'): // Selected text background color
- {
- pstrText++;
- bInSelected = !bInSelected;
- if( bDraw && bLineDraw ) {
- if( bInSelected ) ::SetBkMode(hDC, OPAQUE);
- else ::SetBkMode(hDC, TRANSPARENT);
- }
- }
- break;
- case _T('u'): // Underline text
- {
- pstrText++;
- TFontInfo* pFontInfo = pManager->GetDefaultFontInfo();
- if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
- if( pFontInfo->bUnderline == false ) {
- HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic);
- if( hFont == NULL ) {
- hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic);
- g_iFontID += 1;
- }
- pFontInfo = pManager->GetFontInfo(hFont);
- aFontArray.Add(pFontInfo);
- pTm = &pFontInfo->tm;
- ::SelectObject(hDC, pFontInfo->hFont);
- cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
- }
- }
- break;
- case _T('x'): // X Indent
- {
- pstrText++;
- while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
- int iWidth = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
- pt.x += iWidth;
- cxMaxWidth = MAX(cxMaxWidth, pt.x);
- }
- break;
- case _T('y'): // Y Indent
- {
- pstrText++;
- while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
- cyLine = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
- }
- break;
- }
- if( pstrNextStart != NULL ) pstrText = pstrNextStart;
- else {
- while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') ) pstrText = ::CharNext(pstrText);
- pstrText = ::CharNext(pstrText);
- }
- }
- else if( !bInRaw && ( *pstrText == _T('<') || *pstrText == _T('{') ) && pstrText[1] == _T('/') )
- {
- pstrText++;
- pstrText++;
- switch( *pstrText )
- {
- case _T('c'):
- {
- pstrText++;
- aColorArray.Remove(aColorArray.GetSize() - 1);
- DWORD clrColor = dwTextColor;
- if( aColorArray.GetSize() > 0 ) clrColor = (int)aColorArray.GetAt(aColorArray.GetSize() - 1);
- ::SetTextColor(hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
- }
- break;
- case _T('p'):
- pstrText++;
- if( pt.x > rc.left ) bLineEnd = true;
- aPIndentArray.Remove(aPIndentArray.GetSize() - 1);
- cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
- break;
- case _T('s'):
- {
- pstrText++;
- bInSelected = !bInSelected;
- if( bDraw && bLineDraw ) {
- if( bInSelected ) ::SetBkMode(hDC, OPAQUE);
- else ::SetBkMode(hDC, TRANSPARENT);
- }
- }
- break;
- case _T('a'):
- {
- if( iLinkIndex < nLinkRects ) {
- if( !bLineDraw ) ::SetRect(&prcLinks[iLinkIndex], ptLinkStart.x, ptLinkStart.y, MIN(pt.x, rc.right), pt.y + pTm->tmHeight + pTm->tmExternalLeading);
- iLinkIndex++;
- }
- aColorArray.Remove(aColorArray.GetSize() - 1);
- DWORD clrColor = dwTextColor;
- if( aColorArray.GetSize() > 0 ) clrColor = (int)aColorArray.GetAt(aColorArray.GetSize() - 1);
- ::SetTextColor(hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
- bInLink = false;
- }
- case _T('b'):
- case _T('f'):
- case _T('i'):
- case _T('u'):
- {
- pstrText++;
- aFontArray.Remove(aFontArray.GetSize() - 1);
- TFontInfo* pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
- if( pFontInfo == NULL ) pFontInfo = pManager->GetDefaultFontInfo();
- if( pTm->tmItalic && pFontInfo->bItalic == false ) {
- ABC abc;
- ::GetCharABCWidths(hDC, _T(' '), _T(' '), &abc);
- pt.x += abc.abcC / 2; // 简单修正一下斜体混排的问题, 正确做法应该是http://support.microsoft.com/kb/244798/en-us
- }
- pTm = &pFontInfo->tm;
- ::SelectObject(hDC, pFontInfo->hFont);
- cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
- }
- break;
- }
- while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') ) pstrText = ::CharNext(pstrText);
- pstrText = ::CharNext(pstrText);
- }
- else if( !bInRaw && *pstrText == _T('<') && pstrText[2] == _T('>') && (pstrText[1] == _T('{') || pstrText[1] == _T('}')) )
- {
- SIZE szSpace = { 0 };
- ::GetTextExtentPoint32(hDC, &pstrText[1], 1, &szSpace);
- if( bDraw && bLineDraw ) ::TextOut(hDC, pt.x, pt.y + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, &pstrText[1], 1);
- pt.x += szSpace.cx;
- cxMaxWidth = MAX(cxMaxWidth, pt.x);
- pstrText++;pstrText++;pstrText++;
- }
- else if( !bInRaw && *pstrText == _T('{') && pstrText[2] == _T('}') && (pstrText[1] == _T('<') || pstrText[1] == _T('>')) )
- {
- SIZE szSpace = { 0 };
- ::GetTextExtentPoint32(hDC, &pstrText[1], 1, &szSpace);
- if( bDraw && bLineDraw ) ::TextOut(hDC, pt.x, pt.y + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, &pstrText[1], 1);
- pt.x += szSpace.cx;
- cxMaxWidth = MAX(cxMaxWidth, pt.x);
- pstrText++;pstrText++;pstrText++;
- }
- else if( !bInRaw && *pstrText == _T(' ') )
- {
- SIZE szSpace = { 0 };
- ::GetTextExtentPoint32(hDC, _T(" "), 1, &szSpace);
- // Still need to paint the space because the font might have
- // underline formatting.
- if( bDraw && bLineDraw ) ::TextOut(hDC, pt.x, pt.y + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, _T(" "), 1);
- pt.x += szSpace.cx;
- cxMaxWidth = MAX(cxMaxWidth, pt.x);
- pstrText++;
- }
- else
- {
- POINT ptPos = pt;
- int cchChars = 0;
- int cchSize = 0;
- int cchLastGoodWord = 0;
- int cchLastGoodSize = 0;
- LPCTSTR p = pstrText;
- LPCTSTR pstrNext;
- SIZE szText = { 0 };
- if( !bInRaw && *p == _T('<') || *p == _T('{') ) p++, cchChars++, cchSize++;
- while( *p != _T('\0') && *p != _T('\n') ) {
- // This part makes sure that we're word-wrapping if needed or providing support
- // for DT_END_ELLIPSIS. Unfortunately the GetTextExtentPoint32() call is pretty
- // slow when repeated so often.
- // TODO: Rewrite and use GetTextExtentExPoint() instead!
- if( bInRaw ) {
- if( ( *p == _T('<') || *p == _T('{') ) && p[1] == _T('/')
- && p[2] == _T('r') && ( p[3] == _T('>') || p[3] == _T('}') ) ) {
- p += 4;
- bInRaw = false;
- break;
- }
- }
- else {
- if( *p == _T('<') || *p == _T('{') ) break;
- }
- pstrNext = ::CharNext(p);
- cchChars++;
- cchSize += (int)(pstrNext - p);
- szText.cx = cchChars * pTm->tmMaxCharWidth;
- if( pt.x + szText.cx >= rc.right ) {
- ::GetTextExtentPoint32(hDC, pstrText, cchSize, &szText);
- }
- if( pt.x + szText.cx > rc.right ) {
- if( pt.x + szText.cx > rc.right && pt.x != rc.left) {
- cchChars--;
- cchSize -= (int)(pstrNext - p);
- }
- if( (uStyle & DT_WORDBREAK) != 0 && cchLastGoodWord > 0 ) {
- cchChars = cchLastGoodWord;
- cchSize = cchLastGoodSize;
- }
- if( (uStyle & DT_END_ELLIPSIS) != 0 && cchChars > 0 ) {
- cchChars -= 1;
- LPCTSTR pstrPrev = ::CharPrev(pstrText, p);
- if( cchChars > 0 ) {
- cchChars -= 1;
- pstrPrev = ::CharPrev(pstrText, pstrPrev);
- cchSize -= (int)(p - pstrPrev);
- }
- else
- cchSize -= (int)(p - pstrPrev);
- pt.x = rc.right;
- }
- bLineEnd = true;
- cxMaxWidth = MAX(cxMaxWidth, pt.x);
- break;
- }
- if (!( ( p[0] >= _T('a') && p[0] <= _T('z') ) || ( p[0] >= _T('A') && p[0] <= _T('Z') ) )) {
- cchLastGoodWord = cchChars;
- cchLastGoodSize = cchSize;
- }
- if( *p == _T(' ') ) {
- cchLastGoodWord = cchChars;
- cchLastGoodSize = cchSize;
- }
- p = ::CharNext(p);
- }
-
- ::GetTextExtentPoint32(hDC, pstrText, cchSize, &szText);
- if( bDraw && bLineDraw ) {
- if( (uStyle & DT_SINGLELINE) == 0 && (uStyle & DT_CENTER) != 0 ) {
- ptPos.x += (rc.right - rc.left - szText.cx)/2;
- }
- else if( (uStyle & DT_SINGLELINE) == 0 && (uStyle & DT_RIGHT) != 0) {
- ptPos.x += (rc.right - rc.left - szText.cx);
- }
- ::TextOut(hDC, ptPos.x, ptPos.y + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, pstrText, cchSize);
- if( pt.x >= rc.right && (uStyle & DT_END_ELLIPSIS) != 0 )
- ::TextOut(hDC, ptPos.x + szText.cx, ptPos.y, _T("..."), 3);
- }
- pt.x += szText.cx;
- cxMaxWidth = MAX(cxMaxWidth, pt.x);
- pstrText += cchSize;
- }
- if( pt.x >= rc.right || *pstrText == _T('\n') || *pstrText == _T('\0') ) bLineEnd = true;
- if( bDraw && bLineEnd ) {
- if( !bLineDraw ) {
- aFontArray.Resize(aLineFontArray.GetSize());
- ::CopyMemory(aFontArray.GetData(), aLineFontArray.GetData(), aLineFontArray.GetSize() * sizeof(LPVOID));
- aColorArray.Resize(aLineColorArray.GetSize());
- ::CopyMemory(aColorArray.GetData(), aLineColorArray.GetData(), aLineColorArray.GetSize() * sizeof(LPVOID));
- aPIndentArray.Resize(aLinePIndentArray.GetSize());
- ::CopyMemory(aPIndentArray.GetData(), aLinePIndentArray.GetData(), aLinePIndentArray.GetSize() * sizeof(LPVOID));
- cyLineHeight = cyLine;
- pstrText = pstrLineBegin;
- bInRaw = bLineInRaw;
- bInSelected = bLineInSelected;
- DWORD clrColor = dwTextColor;
- if( aColorArray.GetSize() > 0 ) clrColor = (int)aColorArray.GetAt(aColorArray.GetSize() - 1);
- ::SetTextColor(hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
- TFontInfo* pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
- if( pFontInfo == NULL ) pFontInfo = pManager->GetDefaultFontInfo();
- pTm = &pFontInfo->tm;
- ::SelectObject(hDC, pFontInfo->hFont);
- if( bInSelected ) ::SetBkMode(hDC, OPAQUE);
- }
- else {
- aLineFontArray.Resize(aFontArray.GetSize());
- ::CopyMemory(aLineFontArray.GetData(), aFontArray.GetData(), aFontArray.GetSize() * sizeof(LPVOID));
- aLineColorArray.Resize(aColorArray.GetSize());
- ::CopyMemory(aLineColorArray.GetData(), aColorArray.GetData(), aColorArray.GetSize() * sizeof(LPVOID));
- aLinePIndentArray.Resize(aPIndentArray.GetSize());
- ::CopyMemory(aLinePIndentArray.GetData(), aPIndentArray.GetData(), aPIndentArray.GetSize() * sizeof(LPVOID));
- pstrLineBegin = pstrText;
- bLineInSelected = bInSelected;
- bLineInRaw = bInRaw;
- }
- }
- ASSERT(iLinkIndex<=nLinkRects);
- }
- nLinkRects = iLinkIndex;
- // Return size of text when requested
- if( (uStyle & DT_CALCRECT) != 0 ) {
- rc.bottom = MAX(cyMinHeight, pt.y + cyLine);
- rc.right = MIN(rc.right, cxMaxWidth);
- }
- if( bDraw ) ::SelectClipRgn(hDC, hOldRgn);
- ::DeleteObject(hOldRgn);
- ::DeleteObject(hRgn);
- ::SelectObject(hDC, hOldFont);
- }
- HBITMAP CRenderEngine::GenerateBitmap(CPaintManagerUI* pManager, CControlUI* pControl, RECT rc)
- {
- int cx = rc.right - rc.left;
- int cy = rc.bottom - rc.top;
- HDC hPaintDC = ::CreateCompatibleDC(pManager->GetPaintDC());
- HBITMAP hPaintBitmap = ::CreateCompatibleBitmap(pManager->GetPaintDC(), rc.right, rc.bottom);
- ASSERT(hPaintDC);
- ASSERT(hPaintBitmap);
- HBITMAP hOldPaintBitmap = (HBITMAP) ::SelectObject(hPaintDC, hPaintBitmap);
- pControl->Paint(hPaintDC, rc);
- BITMAPINFO bmi = { 0 };
- bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
- bmi.bmiHeader.biWidth = cx;
- bmi.bmiHeader.biHeight = cy;
- bmi.bmiHeader.biPlanes = 1;
- bmi.bmiHeader.biBitCount = 32;
- bmi.bmiHeader.biCompression = BI_RGB;
- bmi.bmiHeader.biSizeImage = cx * cy * sizeof(DWORD);
- LPDWORD pDest = NULL;
- HDC hCloneDC = ::CreateCompatibleDC(pManager->GetPaintDC());
- HBITMAP hBitmap = ::CreateDIBSection(pManager->GetPaintDC(), &bmi, DIB_RGB_COLORS, (LPVOID*) &pDest, NULL, 0);
- ASSERT(hCloneDC);
- ASSERT(hBitmap);
- if( hBitmap != NULL )
- {
- HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(hCloneDC, hBitmap);
- ::BitBlt(hCloneDC, 0, 0, cx, cy, hPaintDC, rc.left, rc.top, SRCCOPY);
- ::SelectObject(hCloneDC, hOldBitmap);
- ::DeleteDC(hCloneDC);
- ::GdiFlush();
- }
- // Cleanup
- ::SelectObject(hPaintDC, hOldPaintBitmap);
- ::DeleteObject(hPaintBitmap);
- ::DeleteDC(hPaintDC);
- return hBitmap;
- }
- SIZE CRenderEngine::GetTextSize( HDC hDC, CPaintManagerUI* pManager , LPCTSTR pstrText, int iFont, UINT uStyle )
- {
- CDuiString sText = pstrText;
- CPaintManagerUI::ProcessMultiLanguageTokens(sText);
- pstrText = sText;
- SIZE size = {0,0};
- ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
- if( pstrText == NULL || pManager == NULL ) return size;
- ::SetBkMode(hDC, TRANSPARENT);
- HFONT hOldFont = (HFONT)::SelectObject(hDC, pManager->GetFont(iFont));
- GetTextExtentPoint32(hDC, pstrText, _tcslen(pstrText) , &size);
- ::SelectObject(hDC, hOldFont);
- return size;
- }
- } // namespace DuiLib
|