UIRender.cpp 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615
  1. #include "StdAfx.h"
  2. #define STB_IMAGE_IMPLEMENTATION
  3. #include "..\Utils\stb_image.h"
  4. #ifdef USE_XIMAGE_EFFECT
  5. # include "../../3rd/CxImage/ximage.h"
  6. # include "../../3rd/CxImage/ximage.cpp"
  7. # include "../../3rd/CxImage/ximaenc.cpp"
  8. # include "../../3rd/CxImage/ximagif.cpp"
  9. # include "../../3rd/CxImage/ximainfo.cpp"
  10. # include "../../3rd/CxImage/ximalpha.cpp"
  11. # include "../../3rd/CxImage/ximapal.cpp"
  12. # include "../../3rd/CxImage/ximatran.cpp"
  13. # include "../../3rd/CxImage/ximawnd.cpp"
  14. # include "../../3rd/CxImage/xmemfile.cpp"
  15. #endif
  16. ///////////////////////////////////////////////////////////////////////////////////////
  17. namespace DuiLib {
  18. static int g_iFontID = MAX_FONT_ID;
  19. /////////////////////////////////////////////////////////////////////////////////////
  20. //
  21. //
  22. CRenderClip::~CRenderClip()
  23. {
  24. ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
  25. ASSERT(::GetObjectType(hRgn)==OBJ_REGION);
  26. ASSERT(::GetObjectType(hOldRgn)==OBJ_REGION);
  27. ::SelectClipRgn(hDC, hOldRgn);
  28. ::DeleteObject(hOldRgn);
  29. ::DeleteObject(hRgn);
  30. }
  31. void CRenderClip::GenerateClip(HDC hDC, RECT rc, CRenderClip& clip)
  32. {
  33. RECT rcClip = { 0 };
  34. ::GetClipBox(hDC, &rcClip);
  35. clip.hOldRgn = ::CreateRectRgnIndirect(&rcClip);
  36. clip.hRgn = ::CreateRectRgnIndirect(&rc);
  37. ::ExtSelectClipRgn(hDC, clip.hRgn, RGN_AND);
  38. clip.hDC = hDC;
  39. clip.rcItem = rc;
  40. }
  41. void CRenderClip::GenerateRoundClip(HDC hDC, RECT rc, RECT rcItem, int width, int height, CRenderClip& clip)
  42. {
  43. RECT rcClip = { 0 };
  44. ::GetClipBox(hDC, &rcClip);
  45. clip.hOldRgn = ::CreateRectRgnIndirect(&rcClip);
  46. clip.hRgn = ::CreateRectRgnIndirect(&rc);
  47. HRGN hRgnItem = ::CreateRoundRectRgn(rcItem.left, rcItem.top, rcItem.right + 1, rcItem.bottom + 1, width, height);
  48. ::CombineRgn(clip.hRgn, clip.hRgn, hRgnItem, RGN_AND);
  49. ::ExtSelectClipRgn(hDC, clip.hRgn, RGN_AND);
  50. clip.hDC = hDC;
  51. clip.rcItem = rc;
  52. ::DeleteObject(hRgnItem);
  53. }
  54. void CRenderClip::UseOldClipBegin(HDC hDC, CRenderClip& clip)
  55. {
  56. ::SelectClipRgn(hDC, clip.hOldRgn);
  57. }
  58. void CRenderClip::UseOldClipEnd(HDC hDC, CRenderClip& clip)
  59. {
  60. ::SelectClipRgn(hDC, clip.hRgn);
  61. }
  62. /////////////////////////////////////////////////////////////////////////////////////
  63. //
  64. //
  65. static const float OneThird = 1.0f / 3;
  66. static void RGBtoHSL(DWORD ARGB, float* H, float* S, float* L) {
  67. const float
  68. R = (float)GetRValue(ARGB),
  69. G = (float)GetGValue(ARGB),
  70. B = (float)GetBValue(ARGB),
  71. nR = (R<0?0:(R>255?255:R))/255,
  72. nG = (G<0?0:(G>255?255:G))/255,
  73. nB = (B<0?0:(B>255?255:B))/255,
  74. m = min(min(nR,nG),nB),
  75. M = max(max(nR,nG),nB);
  76. *L = (m + M)/2;
  77. if (M==m) *H = *S = 0;
  78. else {
  79. const float
  80. f = (nR==m)?(nG-nB):((nG==m)?(nB-nR):(nR-nG)),
  81. i = (nR==m)?3.0f:((nG==m)?5.0f:1.0f);
  82. *H = (i-f/(M-m));
  83. if (*H>=6) *H-=6;
  84. *H*=60;
  85. *S = (2*(*L)<=1)?((M-m)/(M+m)):((M-m)/(2-M-m));
  86. }
  87. }
  88. static void HSLtoRGB(DWORD* ARGB, float H, float S, float L) {
  89. const float
  90. q = 2*L<1?L*(1+S):(L+S-L*S),
  91. p = 2*L-q,
  92. h = H/360,
  93. tr = h + OneThird,
  94. tg = h,
  95. tb = h - OneThird,
  96. ntr = tr<0?tr+1:(tr>1?tr-1:tr),
  97. ntg = tg<0?tg+1:(tg>1?tg-1:tg),
  98. ntb = tb<0?tb+1:(tb>1?tb-1:tb),
  99. 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))),
  100. 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))),
  101. 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)));
  102. *ARGB &= 0xFF000000;
  103. *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)) );
  104. }
  105. static COLORREF PixelAlpha(COLORREF clrSrc, double src_darken, COLORREF clrDest, double dest_darken)
  106. {
  107. return RGB (GetRValue (clrSrc) * src_darken + GetRValue (clrDest) * dest_darken,
  108. GetGValue (clrSrc) * src_darken + GetGValue (clrDest) * dest_darken,
  109. GetBValue (clrSrc) * src_darken + GetBValue (clrDest) * dest_darken);
  110. }
  111. 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)
  112. {
  113. HDC hTempDC = ::CreateCompatibleDC(hDC);
  114. if (NULL == hTempDC)
  115. return FALSE;
  116. //Creates Source DIB
  117. LPBITMAPINFO lpbiSrc = NULL;
  118. // Fill in the BITMAPINFOHEADER
  119. lpbiSrc = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)];
  120. if (lpbiSrc == NULL)
  121. {
  122. ::DeleteDC(hTempDC);
  123. return FALSE;
  124. }
  125. lpbiSrc->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  126. lpbiSrc->bmiHeader.biWidth = dwWidth;
  127. lpbiSrc->bmiHeader.biHeight = dwHeight;
  128. lpbiSrc->bmiHeader.biPlanes = 1;
  129. lpbiSrc->bmiHeader.biBitCount = 32;
  130. lpbiSrc->bmiHeader.biCompression = BI_RGB;
  131. lpbiSrc->bmiHeader.biSizeImage = dwWidth * dwHeight;
  132. lpbiSrc->bmiHeader.biXPelsPerMeter = 0;
  133. lpbiSrc->bmiHeader.biYPelsPerMeter = 0;
  134. lpbiSrc->bmiHeader.biClrUsed = 0;
  135. lpbiSrc->bmiHeader.biClrImportant = 0;
  136. COLORREF* pSrcBits = NULL;
  137. HBITMAP hSrcDib = CreateDIBSection (
  138. hSrcDC, lpbiSrc, DIB_RGB_COLORS, (void **)&pSrcBits,
  139. NULL, NULL);
  140. if ((NULL == hSrcDib) || (NULL == pSrcBits))
  141. {
  142. delete [] lpbiSrc;
  143. ::DeleteDC(hTempDC);
  144. return FALSE;
  145. }
  146. HBITMAP hOldTempBmp = (HBITMAP)::SelectObject (hTempDC, hSrcDib);
  147. ::StretchBlt(hTempDC, 0, 0, dwWidth, dwHeight, hSrcDC, nSrcX, nSrcY, wSrc, hSrc, SRCCOPY);
  148. ::SelectObject (hTempDC, hOldTempBmp);
  149. //Creates Destination DIB
  150. LPBITMAPINFO lpbiDest = NULL;
  151. // Fill in the BITMAPINFOHEADER
  152. lpbiDest = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)];
  153. if (lpbiDest == NULL)
  154. {
  155. delete [] lpbiSrc;
  156. ::DeleteObject(hSrcDib);
  157. ::DeleteDC(hTempDC);
  158. return FALSE;
  159. }
  160. lpbiDest->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  161. lpbiDest->bmiHeader.biWidth = dwWidth;
  162. lpbiDest->bmiHeader.biHeight = dwHeight;
  163. lpbiDest->bmiHeader.biPlanes = 1;
  164. lpbiDest->bmiHeader.biBitCount = 32;
  165. lpbiDest->bmiHeader.biCompression = BI_RGB;
  166. lpbiDest->bmiHeader.biSizeImage = dwWidth * dwHeight;
  167. lpbiDest->bmiHeader.biXPelsPerMeter = 0;
  168. lpbiDest->bmiHeader.biYPelsPerMeter = 0;
  169. lpbiDest->bmiHeader.biClrUsed = 0;
  170. lpbiDest->bmiHeader.biClrImportant = 0;
  171. COLORREF* pDestBits = NULL;
  172. HBITMAP hDestDib = CreateDIBSection (
  173. hDC, lpbiDest, DIB_RGB_COLORS, (void **)&pDestBits,
  174. NULL, NULL);
  175. if ((NULL == hDestDib) || (NULL == pDestBits))
  176. {
  177. delete [] lpbiSrc;
  178. ::DeleteObject(hSrcDib);
  179. ::DeleteDC(hTempDC);
  180. return FALSE;
  181. }
  182. ::SelectObject (hTempDC, hDestDib);
  183. ::BitBlt (hTempDC, 0, 0, dwWidth, dwHeight, hDC, nDestX, nDestY, SRCCOPY);
  184. ::SelectObject (hTempDC, hOldTempBmp);
  185. double src_darken;
  186. BYTE nAlpha;
  187. for (int pixel = 0; pixel < dwWidth * dwHeight; pixel++, pSrcBits++, pDestBits++)
  188. {
  189. nAlpha = LOBYTE(*pSrcBits >> 24);
  190. src_darken = (double) (nAlpha * ftn.SourceConstantAlpha) / 255.0 / 255.0;
  191. if( src_darken < 0.0 ) src_darken = 0.0;
  192. *pDestBits = PixelAlpha(*pSrcBits, src_darken, *pDestBits, 1.0 - src_darken);
  193. } //for
  194. ::SelectObject (hTempDC, hDestDib);
  195. ::BitBlt (hDC, nDestX, nDestY, dwWidth, dwHeight, hTempDC, 0, 0, SRCCOPY);
  196. ::SelectObject (hTempDC, hOldTempBmp);
  197. delete [] lpbiDest;
  198. ::DeleteObject(hDestDib);
  199. delete [] lpbiSrc;
  200. ::DeleteObject(hSrcDib);
  201. ::DeleteDC(hTempDC);
  202. return TRUE;
  203. }
  204. /////////////////////////////////////////////////////////////////////////////////////
  205. //
  206. //
  207. bool MakeImageDest(const RECT& rcControl, const CDuiSize& szImage, const CDuiString& sAlign, const RECT& rcPadding, RECT& rcDest)
  208. {
  209. if(sAlign.Find(_T("left")) != -1)
  210. {
  211. rcDest.left = rcControl.left;
  212. rcDest.right = rcDest.left + szImage.cx;
  213. }
  214. else if(sAlign.Find(_T("hcenter")) != -1)
  215. {
  216. rcDest.left = rcControl.left + ((rcControl.right - rcControl.left) - szImage.cx)/2;
  217. rcDest.right = rcDest.left + szImage.cx;
  218. }
  219. else if(sAlign.Find(_T("right")) != -1)
  220. {
  221. rcDest.left = rcControl.right - szImage.cx;
  222. rcDest.right = rcDest.left + szImage.cx;
  223. }
  224. if(sAlign.Find(_T("top")) != -1)
  225. {
  226. rcDest.top = rcControl.top;
  227. rcDest.bottom = rcDest.top + szImage.cy;
  228. }
  229. else if(sAlign.Find(_T("vcenter")) != -1)
  230. {
  231. rcDest.top = rcControl.top + ((rcControl.bottom - rcControl.top) - szImage.cy)/2;
  232. rcDest.bottom = rcDest.top + szImage.cy;
  233. }
  234. else if(sAlign.Find(_T("bottom")) != -1)
  235. {
  236. rcDest.top = rcControl.bottom - szImage.cy;
  237. rcDest.bottom = rcDest.top + rcDest.top;
  238. }
  239. ::OffsetRect(&rcDest, rcPadding.left, rcPadding.top);
  240. ::OffsetRect(&rcDest, -rcPadding.right, -rcPadding.bottom);
  241. if (rcDest.right > rcControl.right)
  242. rcDest.right = rcControl.right;
  243. if (rcDest.bottom > rcControl.bottom)
  244. rcDest.bottom = rcControl.bottom;
  245. return true;
  246. }
  247. bool DrawImage(HDC hDC, CPaintManagerUI* pManager, const RECT& rc, const RECT& rcPaint, const CDuiString& sImageName, const CDuiString& sImageResType, RECT rcItem, RECT rcBmpPart, RECT rcCorner, DWORD dwMask, UINT uFade, UINT uRotate, bool bGdiplus, bool bHole, bool bTiledX, bool bTiledY, HINSTANCE instance = NULL)
  248. {
  249. if (sImageName.IsEmpty()) {
  250. return false;
  251. }
  252. const TImageInfo* data = NULL;
  253. data = pManager->GetImageEx((LPCTSTR)sImageName, sImageResType.IsEmpty() ? NULL : (LPCTSTR)sImageResType, dwMask, false, bGdiplus, instance);
  254. if( !data ) return false;
  255. if( rcBmpPart.left == 0 && rcBmpPart.right == 0 && rcBmpPart.top == 0 && rcBmpPart.bottom == 0 ) {
  256. rcBmpPart.right = data->nX;
  257. rcBmpPart.bottom = data->nY;
  258. }
  259. if (rcBmpPart.right > data->nX) rcBmpPart.right = data->nX;
  260. if (rcBmpPart.bottom > data->nY) rcBmpPart.bottom = data->nY;
  261. RECT rcTemp;
  262. if( !::IntersectRect(&rcTemp, &rcItem, &rc) ) return true;
  263. if( !::IntersectRect(&rcTemp, &rcItem, &rcPaint) ) return true;
  264. if(bGdiplus) {
  265. CRenderEngine::GdiplusDrawImage(hDC, data->pImage, rcItem, rcPaint, rcBmpPart, pManager->IsLayered() ? true : data->bAlpha, uFade, uRotate);
  266. }
  267. else {
  268. CRenderEngine::DrawImage(hDC, data->hBitmap, rcItem, rcPaint, rcBmpPart, rcCorner, pManager->IsLayered() ? true : data->bAlpha, uFade, bHole, bTiledX, bTiledY);
  269. }
  270. return true;
  271. }
  272. /////////////////////////////////////////////////////////////////////////////////////
  273. //
  274. //
  275. #ifdef USE_XIMAGE_EFFECT
  276. static DWORD LoadImage2Memory(const STRINGorID &bitmap, LPCTSTR type,LPBYTE &pData)
  277. {
  278. assert(pData == NULL);
  279. pData = NULL;
  280. DWORD dwSize(0U);
  281. do
  282. {
  283. if( type == NULL )
  284. {
  285. CDuiString sFile = CPaintManagerUI::GetResourcePath();
  286. if( CPaintManagerUI::GetResourceZip().IsEmpty() )
  287. {
  288. sFile += bitmap.m_lpstr;
  289. HANDLE hFile = ::CreateFile(sFile.GetData(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
  290. FILE_ATTRIBUTE_NORMAL, NULL);
  291. if( hFile == INVALID_HANDLE_VALUE ) break;
  292. dwSize = ::GetFileSize(hFile, NULL);
  293. if( dwSize == 0 ) break;
  294. DWORD dwRead = 0;
  295. pData = new BYTE[ dwSize + 1 ];
  296. memset(pData,0,dwSize+1);
  297. ::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
  298. ::CloseHandle( hFile );
  299. if( dwRead != dwSize )
  300. {
  301. delete[] pData;
  302. pData = NULL;
  303. dwSize = 0U;
  304. break;
  305. }
  306. }
  307. else
  308. {
  309. sFile += CPaintManagerUI::GetResourceZip();
  310. HZIP hz = NULL;
  311. if( CPaintManagerUI::IsCachedResourceZip() )
  312. hz = (HZIP)CPaintManagerUI::GetResourceZipHandle();
  313. else {
  314. CDuiString sFilePwd = CPaintManagerUI::GetResourceZipPwd();
  315. #ifdef UNICODE
  316. char* pwd = w2a((wchar_t*)sFilePwd.GetData());
  317. hz = OpenZip((void*)sFile.GetData(), pwd);
  318. if(pwd) delete[] pwd;
  319. #else
  320. hz = OpenZip((void*)sFile.GetData(), sFilePwd.GetData());
  321. #endif
  322. }
  323. if( hz == NULL ) break;
  324. ZIPENTRY ze;
  325. int i = 0;
  326. CDuiString key = bitmap.m_lpstr;
  327. key.Replace(_T("\\"), _T("/"));
  328. if( FindZipItem(hz, key, true, &i, &ze) != 0 ) break;
  329. dwSize = ze.unc_size;
  330. if( dwSize == 0 ) break;
  331. pData = new BYTE[ dwSize ];
  332. int res = UnzipItem(hz, i, pData, dwSize, 3);
  333. if( res != 0x00000000 && res != 0x00000600)
  334. {
  335. delete[] pData;
  336. pData = NULL;
  337. dwSize = 0U;
  338. if( !CPaintManagerUI::IsCachedResourceZip() )
  339. CloseZip(hz);
  340. break;
  341. }
  342. if( !CPaintManagerUI::IsCachedResourceZip() )
  343. CloseZip(hz);
  344. }
  345. }
  346. else
  347. {
  348. HINSTANCE hDll = CPaintManagerUI::GetResourceDll();
  349. HRSRC hResource = ::FindResource(hDll, bitmap.m_lpstr, type);
  350. if( hResource == NULL ) break;
  351. HGLOBAL hGlobal = ::LoadResource(hDll, hResource);
  352. if( hGlobal == NULL )
  353. {
  354. FreeResource(hResource);
  355. break;
  356. }
  357. dwSize = ::SizeofResource(hDll, hResource);
  358. if( dwSize == 0 ) break;
  359. pData = new BYTE[ dwSize ];
  360. ::CopyMemory(pData, (LPBYTE)::LockResource(hGlobal), dwSize);
  361. ::FreeResource(hGlobal);
  362. }
  363. } while (0);
  364. while (!pData)
  365. {
  366. //读不到图片, 则直接去读取bitmap.m_lpstr指向的路径
  367. HANDLE hFile = ::CreateFile(bitmap.m_lpstr, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
  368. FILE_ATTRIBUTE_NORMAL, NULL);
  369. if( hFile == INVALID_HANDLE_VALUE ) break;
  370. dwSize = ::GetFileSize(hFile, NULL);
  371. if( dwSize == 0 ) break;
  372. DWORD dwRead = 0;
  373. pData = new BYTE[ dwSize ];
  374. ::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
  375. ::CloseHandle( hFile );
  376. if( dwRead != dwSize )
  377. {
  378. delete[] pData;
  379. pData = NULL;
  380. dwSize = 0U;
  381. }
  382. break;
  383. }
  384. return dwSize;
  385. }
  386. CxImage* CRenderEngine::LoadGifImageX(STRINGorID bitmap, LPCTSTR type , DWORD mask)
  387. {
  388. //write by wangji
  389. LPBYTE pData = NULL;
  390. DWORD dwSize = LoadImage2Memory(bitmap,type,pData);
  391. if(dwSize == 0U || !pData)
  392. return NULL;
  393. CxImage * pImg = NULL;
  394. if(pImg = new CxImage())
  395. {
  396. pImg->SetRetreiveAllFrames(TRUE);
  397. if(!pImg->Decode(pData,dwSize,CXIMAGE_FORMAT_GIF))
  398. {
  399. delete pImg;
  400. pImg = nullptr;
  401. }
  402. }
  403. delete[] pData;
  404. pData = NULL;
  405. return pImg;
  406. }
  407. #endif//USE_XIMAGE_EFFECT
  408. TImageInfo* CRenderEngine::LoadImage(STRINGorID bitmap, LPCTSTR type, DWORD mask, HINSTANCE instance)
  409. {
  410. LPBYTE pData = NULL;
  411. DWORD dwSize = 0;
  412. do
  413. {
  414. if( type == NULL ) {
  415. CDuiString sFile = CPaintManagerUI::GetResourcePath();
  416. if( CPaintManagerUI::GetResourceZip().IsEmpty() ) {
  417. sFile += bitmap.m_lpstr;
  418. HANDLE hFile = ::CreateFile(sFile.GetData(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
  419. FILE_ATTRIBUTE_NORMAL, NULL);
  420. if( hFile == INVALID_HANDLE_VALUE ) break;
  421. dwSize = ::GetFileSize(hFile, NULL);
  422. if( dwSize == 0 ) break;
  423. DWORD dwRead = 0;
  424. pData = new BYTE[ dwSize ];
  425. ::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
  426. ::CloseHandle( hFile );
  427. if( dwRead != dwSize ) {
  428. delete[] pData;
  429. pData = NULL;
  430. break;
  431. }
  432. }
  433. else {
  434. sFile += CPaintManagerUI::GetResourceZip();
  435. CDuiString sFilePwd = CPaintManagerUI::GetResourceZipPwd();
  436. HZIP hz = NULL;
  437. if( CPaintManagerUI::IsCachedResourceZip() ) hz = (HZIP)CPaintManagerUI::GetResourceZipHandle();
  438. else
  439. {
  440. #ifdef UNICODE
  441. char* pwd = w2a((wchar_t*)sFilePwd.GetData());
  442. hz = OpenZip(sFile.GetData(), pwd);
  443. if(pwd) delete[] pwd;
  444. #else
  445. hz = OpenZip(sFile.GetData(), sFilePwd.GetData());
  446. #endif
  447. }
  448. if( hz == NULL ) break;
  449. ZIPENTRY ze;
  450. int i = 0;
  451. CDuiString key = bitmap.m_lpstr;
  452. key.Replace(_T("\\"), _T("/"));
  453. if( FindZipItem(hz, key, true, &i, &ze) != 0 ) break;
  454. dwSize = ze.unc_size;
  455. if( dwSize == 0 ) break;
  456. pData = new BYTE[ dwSize ];
  457. int res = UnzipItem(hz, i, pData, dwSize);
  458. if( res != 0x00000000 && res != 0x00000600) {
  459. delete[] pData;
  460. pData = NULL;
  461. if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
  462. break;
  463. }
  464. if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
  465. }
  466. }
  467. else {
  468. HINSTANCE dllinstance = NULL;
  469. if (instance) {
  470. dllinstance = instance;
  471. }
  472. else {
  473. dllinstance = CPaintManagerUI::GetResourceDll();
  474. }
  475. HRSRC hResource = ::FindResource(dllinstance, bitmap.m_lpstr, type);
  476. if( hResource == NULL ) break;
  477. HGLOBAL hGlobal = ::LoadResource(dllinstance, hResource);
  478. if( hGlobal == NULL ) {
  479. FreeResource(hResource);
  480. break;
  481. }
  482. dwSize = ::SizeofResource(dllinstance, hResource);
  483. if( dwSize == 0 ) break;
  484. pData = new BYTE[ dwSize ];
  485. ::CopyMemory(pData, (LPBYTE)::LockResource(hGlobal), dwSize);
  486. ::FreeResource(hGlobal);
  487. }
  488. } while (0);
  489. while (!pData)
  490. {
  491. //读不到图片, 则直接去读取bitmap.m_lpstr指向的路径
  492. HANDLE hFile = ::CreateFile(bitmap.m_lpstr, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
  493. FILE_ATTRIBUTE_NORMAL, NULL);
  494. if( hFile == INVALID_HANDLE_VALUE ) break;
  495. dwSize = ::GetFileSize(hFile, NULL);
  496. if( dwSize == 0 ) break;
  497. DWORD dwRead = 0;
  498. pData = new BYTE[ dwSize ];
  499. ::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
  500. ::CloseHandle( hFile );
  501. if( dwRead != dwSize ) {
  502. delete[] pData;
  503. pData = NULL;
  504. }
  505. break;
  506. }
  507. if (!pData)
  508. {
  509. return NULL;
  510. }
  511. LPBYTE pImage = NULL;
  512. int x,y,n;
  513. pImage = stbi_load_from_memory(pData, dwSize, &x, &y, &n, 4);
  514. delete[] pData;
  515. if( !pImage ) {
  516. return NULL;
  517. }
  518. BITMAPINFO bmi;
  519. ::ZeroMemory(&bmi, sizeof(BITMAPINFO));
  520. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  521. bmi.bmiHeader.biWidth = x;
  522. bmi.bmiHeader.biHeight = -y;
  523. bmi.bmiHeader.biPlanes = 1;
  524. bmi.bmiHeader.biBitCount = 32;
  525. bmi.bmiHeader.biCompression = BI_RGB;
  526. bmi.bmiHeader.biSizeImage = x * y * 4;
  527. bool bAlphaChannel = false;
  528. LPBYTE pDest = NULL;
  529. HBITMAP hBitmap = ::CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void**)&pDest, NULL, 0);
  530. if( !hBitmap ) {
  531. return NULL;
  532. }
  533. for( int i = 0; i < x * y; i++ )
  534. {
  535. pDest[i*4 + 3] = pImage[i*4 + 3];
  536. if( pDest[i*4 + 3] < 255 )
  537. {
  538. pDest[i*4] = (BYTE)(DWORD(pImage[i*4 + 2])*pImage[i*4 + 3]/255);
  539. pDest[i*4 + 1] = (BYTE)(DWORD(pImage[i*4 + 1])*pImage[i*4 + 3]/255);
  540. pDest[i*4 + 2] = (BYTE)(DWORD(pImage[i*4])*pImage[i*4 + 3]/255);
  541. bAlphaChannel = true;
  542. }
  543. else
  544. {
  545. pDest[i*4] = pImage[i*4 + 2];
  546. pDest[i*4 + 1] = pImage[i*4 + 1];
  547. pDest[i*4 + 2] = pImage[i*4];
  548. }
  549. if( *(DWORD*)(&pDest[i*4]) == mask ) {
  550. pDest[i*4] = (BYTE)0;
  551. pDest[i*4 + 1] = (BYTE)0;
  552. pDest[i*4 + 2] = (BYTE)0;
  553. pDest[i*4 + 3] = (BYTE)0;
  554. bAlphaChannel = true;
  555. }
  556. }
  557. stbi_image_free(pImage);
  558. TImageInfo* data = new TImageInfo;
  559. data->pBits = NULL;
  560. data->pSrcBits = NULL;
  561. data->hBitmap = hBitmap;
  562. data->nX = x;
  563. data->nY = y;
  564. data->bAlpha = bAlphaChannel;
  565. return data;
  566. }
  567. void CRenderEngine::FreeImage(TImageInfo* pImageInfo, bool bDelete)
  568. {
  569. if (pImageInfo == NULL) return;
  570. if(pImageInfo->pImage) {
  571. delete pImageInfo->pImage;
  572. }
  573. pImageInfo->pImage = NULL;
  574. if (pImageInfo->hBitmap) {
  575. ::DeleteObject(pImageInfo->hBitmap);
  576. }
  577. pImageInfo->hBitmap = NULL;
  578. if (pImageInfo->pBits) {
  579. delete[] pImageInfo->pBits;
  580. }
  581. pImageInfo->pBits = NULL;
  582. if (pImageInfo->pSrcBits) {
  583. delete[] pImageInfo->pSrcBits;
  584. }
  585. pImageInfo->pSrcBits = NULL;
  586. if (bDelete) {
  587. delete pImageInfo;
  588. pImageInfo = NULL;
  589. }
  590. }
  591. TImageInfo* CRenderEngine::LoadImage(LPCTSTR pStrImage, LPCTSTR type, DWORD mask, HINSTANCE instance)
  592. {
  593. if(pStrImage == NULL) return NULL;
  594. CDuiString sStrPath = pStrImage;
  595. if( type == NULL ) {
  596. sStrPath = CResourceManager::GetInstance()->GetImagePath(pStrImage);
  597. if (sStrPath.IsEmpty()) sStrPath = pStrImage;
  598. }
  599. return LoadImage(STRINGorID(sStrPath.GetData()), type, mask, instance);
  600. }
  601. TImageInfo* CRenderEngine::LoadImage(UINT nID, LPCTSTR type, DWORD mask, HINSTANCE instance)
  602. {
  603. return LoadImage(STRINGorID(nID), type, mask, instance);
  604. }
  605. void CRenderEngine::DrawImage(HDC hDC, HBITMAP hBitmap, const RECT& rc, const RECT& rcPaint, const RECT& rcBmpPart, const RECT& rcCorners, bool bAlpha, UINT uFade, bool hole, bool xtiled, bool ytiled)
  606. {
  607. ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
  608. typedef BOOL (WINAPI *LPALPHABLEND)(HDC, int, int, int, int,HDC, int, int, int, int, BLENDFUNCTION);
  609. static LPALPHABLEND lpAlphaBlend = (LPALPHABLEND) ::GetProcAddress(::GetModuleHandle(_T("msimg32.dll")), "AlphaBlend");
  610. if( lpAlphaBlend == NULL ) lpAlphaBlend = AlphaBitBlt;
  611. if( hBitmap == NULL ) return;
  612. HDC hCloneDC = ::CreateCompatibleDC(hDC);
  613. HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(hCloneDC, hBitmap);
  614. ::SetStretchBltMode(hDC, HALFTONE);
  615. RECT rcTemp = {0};
  616. RECT rcDest = {0};
  617. if( lpAlphaBlend && (bAlpha || uFade < 255) ) {
  618. BLENDFUNCTION bf = { AC_SRC_OVER, 0, uFade, AC_SRC_ALPHA };
  619. // middle
  620. if( !hole ) {
  621. rcDest.left = rc.left + rcCorners.left;
  622. rcDest.top = rc.top + rcCorners.top;
  623. rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;
  624. rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;
  625. rcDest.right += rcDest.left;
  626. rcDest.bottom += rcDest.top;
  627. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  628. if( !xtiled && !ytiled ) {
  629. rcDest.right -= rcDest.left;
  630. rcDest.bottom -= rcDest.top;
  631. lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  632. rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \
  633. rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, \
  634. rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);
  635. }
  636. else if( xtiled && ytiled ) {
  637. LONG lWidth = rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right;
  638. LONG lHeight = rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom;
  639. int iTimesX = (rcDest.right - rcDest.left + lWidth - 1) / lWidth;
  640. int iTimesY = (rcDest.bottom - rcDest.top + lHeight - 1) / lHeight;
  641. for( int j = 0; j < iTimesY; ++j ) {
  642. LONG lDestTop = rcDest.top + lHeight * j;
  643. LONG lDestBottom = rcDest.top + lHeight * (j + 1);
  644. LONG lDrawHeight = lHeight;
  645. if( lDestBottom > rcDest.bottom ) {
  646. lDrawHeight -= lDestBottom - rcDest.bottom;
  647. lDestBottom = rcDest.bottom;
  648. }
  649. for( int i = 0; i < iTimesX; ++i ) {
  650. LONG lDestLeft = rcDest.left + lWidth * i;
  651. LONG lDestRight = rcDest.left + lWidth * (i + 1);
  652. LONG lDrawWidth = lWidth;
  653. if( lDestRight > rcDest.right ) {
  654. lDrawWidth -= lDestRight - rcDest.right;
  655. lDestRight = rcDest.right;
  656. }
  657. lpAlphaBlend(hDC, rcDest.left + lWidth * i, rcDest.top + lHeight * j,
  658. lDestRight - lDestLeft, lDestBottom - lDestTop, hCloneDC,
  659. rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, lDrawWidth, lDrawHeight, bf);
  660. }
  661. }
  662. }
  663. else if( xtiled ) {
  664. LONG lWidth = rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right;
  665. int iTimes = (rcDest.right - rcDest.left + lWidth - 1) / lWidth;
  666. for( int i = 0; i < iTimes; ++i ) {
  667. LONG lDestLeft = rcDest.left + lWidth * i;
  668. LONG lDestRight = rcDest.left + lWidth * (i + 1);
  669. LONG lDrawWidth = lWidth;
  670. if( lDestRight > rcDest.right ) {
  671. lDrawWidth -= lDestRight - rcDest.right;
  672. lDestRight = rcDest.right;
  673. }
  674. lpAlphaBlend(hDC, lDestLeft, rcDest.top, lDestRight - lDestLeft, rcDest.bottom,
  675. hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \
  676. lDrawWidth, rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);
  677. }
  678. }
  679. else { // ytiled
  680. LONG lHeight = rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom;
  681. int iTimes = (rcDest.bottom - rcDest.top + lHeight - 1) / lHeight;
  682. for( int i = 0; i < iTimes; ++i ) {
  683. LONG lDestTop = rcDest.top + lHeight * i;
  684. LONG lDestBottom = rcDest.top + lHeight * (i + 1);
  685. LONG lDrawHeight = lHeight;
  686. if( lDestBottom > rcDest.bottom ) {
  687. lDrawHeight -= lDestBottom - rcDest.bottom;
  688. lDestBottom = rcDest.bottom;
  689. }
  690. lpAlphaBlend(hDC, rcDest.left, rcDest.top + lHeight * i, rcDest.right, lDestBottom - lDestTop,
  691. hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \
  692. rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, lDrawHeight, bf);
  693. }
  694. }
  695. }
  696. }
  697. // left-top
  698. if( rcCorners.left > 0 && rcCorners.top > 0 ) {
  699. rcDest.left = rc.left;
  700. rcDest.top = rc.top;
  701. rcDest.right = rcCorners.left;
  702. rcDest.bottom = rcCorners.top;
  703. rcDest.right += rcDest.left;
  704. rcDest.bottom += rcDest.top;
  705. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  706. rcDest.right -= rcDest.left;
  707. rcDest.bottom -= rcDest.top;
  708. lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  709. rcBmpPart.left, rcBmpPart.top, rcCorners.left, rcCorners.top, bf);
  710. }
  711. }
  712. // top
  713. if( rcCorners.top > 0 ) {
  714. rcDest.left = rc.left + rcCorners.left;
  715. rcDest.top = rc.top;
  716. rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;
  717. rcDest.bottom = rcCorners.top;
  718. rcDest.right += rcDest.left;
  719. rcDest.bottom += rcDest.top;
  720. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  721. rcDest.right -= rcDest.left;
  722. rcDest.bottom -= rcDest.top;
  723. lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  724. rcBmpPart.left + rcCorners.left, rcBmpPart.top, rcBmpPart.right - rcBmpPart.left - \
  725. rcCorners.left - rcCorners.right, rcCorners.top, bf);
  726. }
  727. }
  728. // right-top
  729. if( rcCorners.right > 0 && rcCorners.top > 0 ) {
  730. rcDest.left = rc.right - rcCorners.right;
  731. rcDest.top = rc.top;
  732. rcDest.right = rcCorners.right;
  733. rcDest.bottom = rcCorners.top;
  734. rcDest.right += rcDest.left;
  735. rcDest.bottom += rcDest.top;
  736. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  737. rcDest.right -= rcDest.left;
  738. rcDest.bottom -= rcDest.top;
  739. lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  740. rcBmpPart.right - rcCorners.right, rcBmpPart.top, rcCorners.right, rcCorners.top, bf);
  741. }
  742. }
  743. // left
  744. if( rcCorners.left > 0 ) {
  745. rcDest.left = rc.left;
  746. rcDest.top = rc.top + rcCorners.top;
  747. rcDest.right = rcCorners.left;
  748. rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;
  749. rcDest.right += rcDest.left;
  750. rcDest.bottom += rcDest.top;
  751. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  752. rcDest.right -= rcDest.left;
  753. rcDest.bottom -= rcDest.top;
  754. lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  755. rcBmpPart.left, rcBmpPart.top + rcCorners.top, rcCorners.left, rcBmpPart.bottom - \
  756. rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);
  757. }
  758. }
  759. // right
  760. if( rcCorners.right > 0 ) {
  761. rcDest.left = rc.right - rcCorners.right;
  762. rcDest.top = rc.top + rcCorners.top;
  763. rcDest.right = rcCorners.right;
  764. rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;
  765. rcDest.right += rcDest.left;
  766. rcDest.bottom += rcDest.top;
  767. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  768. rcDest.right -= rcDest.left;
  769. rcDest.bottom -= rcDest.top;
  770. lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  771. rcBmpPart.right - rcCorners.right, rcBmpPart.top + rcCorners.top, rcCorners.right, \
  772. rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, bf);
  773. }
  774. }
  775. // left-bottom
  776. if( rcCorners.left > 0 && rcCorners.bottom > 0 ) {
  777. rcDest.left = rc.left;
  778. rcDest.top = rc.bottom - rcCorners.bottom;
  779. rcDest.right = rcCorners.left;
  780. rcDest.bottom = rcCorners.bottom;
  781. rcDest.right += rcDest.left;
  782. rcDest.bottom += rcDest.top;
  783. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  784. rcDest.right -= rcDest.left;
  785. rcDest.bottom -= rcDest.top;
  786. lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  787. rcBmpPart.left, rcBmpPart.bottom - rcCorners.bottom, rcCorners.left, rcCorners.bottom, bf);
  788. }
  789. }
  790. // bottom
  791. if( rcCorners.bottom > 0 ) {
  792. rcDest.left = rc.left + rcCorners.left;
  793. rcDest.top = rc.bottom - rcCorners.bottom;
  794. rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;
  795. rcDest.bottom = rcCorners.bottom;
  796. rcDest.right += rcDest.left;
  797. rcDest.bottom += rcDest.top;
  798. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  799. rcDest.right -= rcDest.left;
  800. rcDest.bottom -= rcDest.top;
  801. lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  802. rcBmpPart.left + rcCorners.left, rcBmpPart.bottom - rcCorners.bottom, \
  803. rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, rcCorners.bottom, bf);
  804. }
  805. }
  806. // right-bottom
  807. if( rcCorners.right > 0 && rcCorners.bottom > 0 ) {
  808. rcDest.left = rc.right - rcCorners.right;
  809. rcDest.top = rc.bottom - rcCorners.bottom;
  810. rcDest.right = rcCorners.right;
  811. rcDest.bottom = rcCorners.bottom;
  812. rcDest.right += rcDest.left;
  813. rcDest.bottom += rcDest.top;
  814. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  815. rcDest.right -= rcDest.left;
  816. rcDest.bottom -= rcDest.top;
  817. lpAlphaBlend(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  818. rcBmpPart.right - rcCorners.right, rcBmpPart.bottom - rcCorners.bottom, rcCorners.right, \
  819. rcCorners.bottom, bf);
  820. }
  821. }
  822. }
  823. else
  824. {
  825. if (rc.right - rc.left == rcBmpPart.right - rcBmpPart.left \
  826. && rc.bottom - rc.top == rcBmpPart.bottom - rcBmpPart.top \
  827. && rcCorners.left == 0 && rcCorners.right == 0 && rcCorners.top == 0 && rcCorners.bottom == 0)
  828. {
  829. if( ::IntersectRect(&rcTemp, &rcPaint, &rc) ) {
  830. ::BitBlt(hDC, rcTemp.left, rcTemp.top, rcTemp.right - rcTemp.left, rcTemp.bottom - rcTemp.top, \
  831. hCloneDC, rcBmpPart.left + rcTemp.left - rc.left, rcBmpPart.top + rcTemp.top - rc.top, SRCCOPY);
  832. }
  833. }
  834. else
  835. {
  836. // middle
  837. if( !hole ) {
  838. rcDest.left = rc.left + rcCorners.left;
  839. rcDest.top = rc.top + rcCorners.top;
  840. rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;
  841. rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;
  842. rcDest.right += rcDest.left;
  843. rcDest.bottom += rcDest.top;
  844. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  845. if( !xtiled && !ytiled ) {
  846. rcDest.right -= rcDest.left;
  847. rcDest.bottom -= rcDest.top;
  848. ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  849. rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \
  850. rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, \
  851. rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, SRCCOPY);
  852. }
  853. else if( xtiled && ytiled ) {
  854. LONG lWidth = rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right;
  855. LONG lHeight = rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom;
  856. int iTimesX = (rcDest.right - rcDest.left + lWidth - 1) / lWidth;
  857. int iTimesY = (rcDest.bottom - rcDest.top + lHeight - 1) / lHeight;
  858. for( int j = 0; j < iTimesY; ++j ) {
  859. LONG lDestTop = rcDest.top + lHeight * j;
  860. LONG lDestBottom = rcDest.top + lHeight * (j + 1);
  861. LONG lDrawHeight = lHeight;
  862. if( lDestBottom > rcDest.bottom ) {
  863. lDrawHeight -= lDestBottom - rcDest.bottom;
  864. lDestBottom = rcDest.bottom;
  865. }
  866. for( int i = 0; i < iTimesX; ++i ) {
  867. LONG lDestLeft = rcDest.left + lWidth * i;
  868. LONG lDestRight = rcDest.left + lWidth * (i + 1);
  869. LONG lDrawWidth = lWidth;
  870. if( lDestRight > rcDest.right ) {
  871. lDrawWidth -= lDestRight - rcDest.right;
  872. lDestRight = rcDest.right;
  873. }
  874. ::BitBlt(hDC, rcDest.left + lWidth * i, rcDest.top + lHeight * j, \
  875. lDestRight - lDestLeft, lDestBottom - lDestTop, hCloneDC, \
  876. rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, SRCCOPY);
  877. }
  878. }
  879. }
  880. else if( xtiled ) {
  881. LONG lWidth = rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right;
  882. int iTimes = (rcDest.right - rcDest.left + lWidth - 1) / lWidth;
  883. for( int i = 0; i < iTimes; ++i ) {
  884. LONG lDestLeft = rcDest.left + lWidth * i;
  885. LONG lDestRight = rcDest.left + lWidth * (i + 1);
  886. LONG lDrawWidth = lWidth;
  887. if( lDestRight > rcDest.right ) {
  888. lDrawWidth -= lDestRight - rcDest.right;
  889. lDestRight = rcDest.right;
  890. }
  891. ::StretchBlt(hDC, lDestLeft, rcDest.top, lDestRight - lDestLeft, rcDest.bottom,
  892. hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \
  893. lDrawWidth, rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, SRCCOPY);
  894. }
  895. }
  896. else { // ytiled
  897. LONG lHeight = rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom;
  898. int iTimes = (rcDest.bottom - rcDest.top + lHeight - 1) / lHeight;
  899. for( int i = 0; i < iTimes; ++i ) {
  900. LONG lDestTop = rcDest.top + lHeight * i;
  901. LONG lDestBottom = rcDest.top + lHeight * (i + 1);
  902. LONG lDrawHeight = lHeight;
  903. if( lDestBottom > rcDest.bottom ) {
  904. lDrawHeight -= lDestBottom - rcDest.bottom;
  905. lDestBottom = rcDest.bottom;
  906. }
  907. ::StretchBlt(hDC, rcDest.left, rcDest.top + lHeight * i, rcDest.right, lDestBottom - lDestTop,
  908. hCloneDC, rcBmpPart.left + rcCorners.left, rcBmpPart.top + rcCorners.top, \
  909. rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, lDrawHeight, SRCCOPY);
  910. }
  911. }
  912. }
  913. }
  914. // left-top
  915. if( rcCorners.left > 0 && rcCorners.top > 0 ) {
  916. rcDest.left = rc.left;
  917. rcDest.top = rc.top;
  918. rcDest.right = rcCorners.left;
  919. rcDest.bottom = rcCorners.top;
  920. rcDest.right += rcDest.left;
  921. rcDest.bottom += rcDest.top;
  922. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  923. rcDest.right -= rcDest.left;
  924. rcDest.bottom -= rcDest.top;
  925. ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  926. rcBmpPart.left, rcBmpPart.top, rcCorners.left, rcCorners.top, SRCCOPY);
  927. }
  928. }
  929. // top
  930. if( rcCorners.top > 0 ) {
  931. rcDest.left = rc.left + rcCorners.left;
  932. rcDest.top = rc.top;
  933. rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;
  934. rcDest.bottom = rcCorners.top;
  935. rcDest.right += rcDest.left;
  936. rcDest.bottom += rcDest.top;
  937. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  938. rcDest.right -= rcDest.left;
  939. rcDest.bottom -= rcDest.top;
  940. ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  941. rcBmpPart.left + rcCorners.left, rcBmpPart.top, rcBmpPart.right - rcBmpPart.left - \
  942. rcCorners.left - rcCorners.right, rcCorners.top, SRCCOPY);
  943. }
  944. }
  945. // right-top
  946. if( rcCorners.right > 0 && rcCorners.top > 0 ) {
  947. rcDest.left = rc.right - rcCorners.right;
  948. rcDest.top = rc.top;
  949. rcDest.right = rcCorners.right;
  950. rcDest.bottom = rcCorners.top;
  951. rcDest.right += rcDest.left;
  952. rcDest.bottom += rcDest.top;
  953. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  954. rcDest.right -= rcDest.left;
  955. rcDest.bottom -= rcDest.top;
  956. ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  957. rcBmpPart.right - rcCorners.right, rcBmpPart.top, rcCorners.right, rcCorners.top, SRCCOPY);
  958. }
  959. }
  960. // left
  961. if( rcCorners.left > 0 ) {
  962. rcDest.left = rc.left;
  963. rcDest.top = rc.top + rcCorners.top;
  964. rcDest.right = rcCorners.left;
  965. rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;
  966. rcDest.right += rcDest.left;
  967. rcDest.bottom += rcDest.top;
  968. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  969. rcDest.right -= rcDest.left;
  970. rcDest.bottom -= rcDest.top;
  971. ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  972. rcBmpPart.left, rcBmpPart.top + rcCorners.top, rcCorners.left, rcBmpPart.bottom - \
  973. rcBmpPart.top - rcCorners.top - rcCorners.bottom, SRCCOPY);
  974. }
  975. }
  976. // right
  977. if( rcCorners.right > 0 ) {
  978. rcDest.left = rc.right - rcCorners.right;
  979. rcDest.top = rc.top + rcCorners.top;
  980. rcDest.right = rcCorners.right;
  981. rcDest.bottom = rc.bottom - rc.top - rcCorners.top - rcCorners.bottom;
  982. rcDest.right += rcDest.left;
  983. rcDest.bottom += rcDest.top;
  984. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  985. rcDest.right -= rcDest.left;
  986. rcDest.bottom -= rcDest.top;
  987. ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  988. rcBmpPart.right - rcCorners.right, rcBmpPart.top + rcCorners.top, rcCorners.right, \
  989. rcBmpPart.bottom - rcBmpPart.top - rcCorners.top - rcCorners.bottom, SRCCOPY);
  990. }
  991. }
  992. // left-bottom
  993. if( rcCorners.left > 0 && rcCorners.bottom > 0 ) {
  994. rcDest.left = rc.left;
  995. rcDest.top = rc.bottom - rcCorners.bottom;
  996. rcDest.right = rcCorners.left;
  997. rcDest.bottom = rcCorners.bottom;
  998. rcDest.right += rcDest.left;
  999. rcDest.bottom += rcDest.top;
  1000. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  1001. rcDest.right -= rcDest.left;
  1002. rcDest.bottom -= rcDest.top;
  1003. ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  1004. rcBmpPart.left, rcBmpPart.bottom - rcCorners.bottom, rcCorners.left, rcCorners.bottom, SRCCOPY);
  1005. }
  1006. }
  1007. // bottom
  1008. if( rcCorners.bottom > 0 ) {
  1009. rcDest.left = rc.left + rcCorners.left;
  1010. rcDest.top = rc.bottom - rcCorners.bottom;
  1011. rcDest.right = rc.right - rc.left - rcCorners.left - rcCorners.right;
  1012. rcDest.bottom = rcCorners.bottom;
  1013. rcDest.right += rcDest.left;
  1014. rcDest.bottom += rcDest.top;
  1015. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  1016. rcDest.right -= rcDest.left;
  1017. rcDest.bottom -= rcDest.top;
  1018. ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  1019. rcBmpPart.left + rcCorners.left, rcBmpPart.bottom - rcCorners.bottom, \
  1020. rcBmpPart.right - rcBmpPart.left - rcCorners.left - rcCorners.right, rcCorners.bottom, SRCCOPY);
  1021. }
  1022. }
  1023. // right-bottom
  1024. if( rcCorners.right > 0 && rcCorners.bottom > 0 ) {
  1025. rcDest.left = rc.right - rcCorners.right;
  1026. rcDest.top = rc.bottom - rcCorners.bottom;
  1027. rcDest.right = rcCorners.right;
  1028. rcDest.bottom = rcCorners.bottom;
  1029. rcDest.right += rcDest.left;
  1030. rcDest.bottom += rcDest.top;
  1031. if( ::IntersectRect(&rcTemp, &rcPaint, &rcDest) ) {
  1032. rcDest.right -= rcDest.left;
  1033. rcDest.bottom -= rcDest.top;
  1034. ::StretchBlt(hDC, rcDest.left, rcDest.top, rcDest.right, rcDest.bottom, hCloneDC, \
  1035. rcBmpPart.right - rcCorners.right, rcBmpPart.bottom - rcCorners.bottom, rcCorners.right, \
  1036. rcCorners.bottom, SRCCOPY);
  1037. }
  1038. }
  1039. }
  1040. }
  1041. ::SelectObject(hCloneDC, hOldBitmap);
  1042. ::DeleteDC(hCloneDC);
  1043. }
  1044. bool CRenderEngine::DrawImageInfo(HDC hDC, CPaintManagerUI* pManager, const RECT& rcItem, const RECT& rcPaint, const TDrawInfo* pDrawInfo, HINSTANCE instance)
  1045. {
  1046. if( pManager == NULL || hDC == NULL || pDrawInfo == NULL ) return false;
  1047. RECT rcDest = rcItem;
  1048. // 计算绘制目标区域
  1049. if( pDrawInfo->rcDest.left != 0 || pDrawInfo->rcDest.top != 0 ||
  1050. pDrawInfo->rcDest.right != 0 || pDrawInfo->rcDest.bottom != 0 ) {
  1051. rcDest.left = rcItem.left + pDrawInfo->rcDest.left;
  1052. rcDest.top = rcItem.top + pDrawInfo->rcDest.top;
  1053. rcDest.right = rcItem.left + pDrawInfo->rcDest.right;
  1054. if( rcDest.right > rcItem.right ) rcDest.right = rcItem.right;
  1055. rcDest.bottom = rcItem.top + pDrawInfo->rcDest.bottom;
  1056. if( rcDest.bottom > rcItem.bottom ) rcDest.bottom = rcItem.bottom;
  1057. }
  1058. // 根据对齐方式计算目标区域
  1059. if(pDrawInfo->szImage.cx > 0 && pDrawInfo->szImage.cy > 0) {
  1060. SIZE szImage = pManager->GetDPIObj()->Scale(pDrawInfo->szImage);
  1061. RECT rcPadding = pManager->GetDPIObj()->Scale(pDrawInfo->rcPadding);
  1062. DuiLib::MakeImageDest(rcItem, szImage, pDrawInfo->sAlign, rcPadding, rcDest);
  1063. }
  1064. bool bRet = DuiLib::DrawImage(hDC, pManager, rcItem, rcPaint, pDrawInfo->sImageName, pDrawInfo->sResType, rcDest, \
  1065. pDrawInfo->rcSource, pDrawInfo->rcCorner, pDrawInfo->dwMask, pDrawInfo->uFade, pDrawInfo->uRotate, pDrawInfo->bGdiplus, pDrawInfo->bHole, pDrawInfo->bTiledX, pDrawInfo->bTiledY, instance);
  1066. return bRet;
  1067. }
  1068. bool CRenderEngine::DrawImageString(HDC hDC, CPaintManagerUI* pManager, const RECT& rcItem, const RECT& rcPaint, LPCTSTR pStrImage, LPCTSTR pStrModify, HINSTANCE instance)
  1069. {
  1070. if ((pManager == NULL) || (hDC == NULL)) return false;
  1071. const TDrawInfo* pDrawInfo = pManager->GetDrawInfo(pStrImage, pStrModify);
  1072. return DrawImageInfo(hDC, pManager, rcItem, rcPaint, pDrawInfo, instance);
  1073. }
  1074. /////////////////////////////////////////////////////////////////////////////////////
  1075. //
  1076. //
  1077. TImageInfo* CRenderEngine::GdiplusLoadImage(STRINGorID bitmap, LPCTSTR type, DWORD mask, HINSTANCE instance)
  1078. {
  1079. LPBYTE pData = NULL;
  1080. DWORD dwSize = 0;
  1081. do
  1082. {
  1083. if( type == NULL ) {
  1084. CDuiString sFile = CPaintManagerUI::GetResourcePath();
  1085. if( CPaintManagerUI::GetResourceZip().IsEmpty() ) {
  1086. sFile += bitmap.m_lpstr;
  1087. HANDLE hFile = ::CreateFile(sFile.GetData(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
  1088. FILE_ATTRIBUTE_NORMAL, NULL);
  1089. if( hFile == INVALID_HANDLE_VALUE ) break;
  1090. dwSize = ::GetFileSize(hFile, NULL);
  1091. if( dwSize == 0 ) break;
  1092. DWORD dwRead = 0;
  1093. pData = new BYTE[ dwSize ];
  1094. ::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
  1095. ::CloseHandle( hFile );
  1096. if( dwRead != dwSize ) {
  1097. delete[] pData;
  1098. pData = NULL;
  1099. break;
  1100. }
  1101. }
  1102. else {
  1103. sFile += CPaintManagerUI::GetResourceZip();
  1104. CDuiString sFilePwd = CPaintManagerUI::GetResourceZipPwd();
  1105. HZIP hz = NULL;
  1106. if( CPaintManagerUI::IsCachedResourceZip() ) hz = (HZIP)CPaintManagerUI::GetResourceZipHandle();
  1107. else
  1108. {
  1109. #ifdef UNICODE
  1110. char* pwd = w2a((wchar_t*)sFilePwd.GetData());
  1111. hz = OpenZip(sFile.GetData(), pwd);
  1112. if(pwd) delete[] pwd;
  1113. #else
  1114. hz = OpenZip(sFile.GetData(), sFilePwd.GetData());
  1115. #endif
  1116. }
  1117. if( hz == NULL ) break;
  1118. ZIPENTRY ze;
  1119. int i = 0;
  1120. CDuiString key = bitmap.m_lpstr;
  1121. key.Replace(_T("\\"), _T("/"));
  1122. if( FindZipItem(hz, key, true, &i, &ze) != 0 ) break;
  1123. dwSize = ze.unc_size;
  1124. if( dwSize == 0 ) break;
  1125. pData = new BYTE[ dwSize ];
  1126. int res = UnzipItem(hz, i, pData, dwSize);
  1127. if( res != 0x00000000 && res != 0x00000600) {
  1128. delete[] pData;
  1129. pData = NULL;
  1130. if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
  1131. break;
  1132. }
  1133. if( !CPaintManagerUI::IsCachedResourceZip() ) CloseZip(hz);
  1134. }
  1135. }
  1136. else {
  1137. HINSTANCE dllinstance = NULL;
  1138. if (instance) {
  1139. dllinstance = instance;
  1140. }
  1141. else {
  1142. dllinstance = CPaintManagerUI::GetResourceDll();
  1143. }
  1144. HRSRC hResource = ::FindResource(dllinstance, bitmap.m_lpstr, type);
  1145. if( hResource == NULL ) break;
  1146. HGLOBAL hGlobal = ::LoadResource(dllinstance, hResource);
  1147. if( hGlobal == NULL ) {
  1148. FreeResource(hResource);
  1149. break;
  1150. }
  1151. dwSize = ::SizeofResource(dllinstance, hResource);
  1152. if( dwSize == 0 ) break;
  1153. pData = new BYTE[ dwSize ];
  1154. ::CopyMemory(pData, (LPBYTE)::LockResource(hGlobal), dwSize);
  1155. ::FreeResource(hGlobal);
  1156. }
  1157. } while (0);
  1158. while (!pData)
  1159. {
  1160. //读不到图片, 则直接去读取bitmap.m_lpstr指向的路径
  1161. HANDLE hFile = ::CreateFile(bitmap.m_lpstr, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
  1162. FILE_ATTRIBUTE_NORMAL, NULL);
  1163. if( hFile == INVALID_HANDLE_VALUE ) break;
  1164. dwSize = ::GetFileSize(hFile, NULL);
  1165. if( dwSize == 0 ) break;
  1166. DWORD dwRead = 0;
  1167. pData = new BYTE[ dwSize ];
  1168. ::ReadFile( hFile, pData, dwSize, &dwRead, NULL );
  1169. ::CloseHandle( hFile );
  1170. if( dwRead != dwSize ) {
  1171. delete[] pData;
  1172. pData = NULL;
  1173. }
  1174. break;
  1175. }
  1176. if(pData == NULL) {
  1177. return NULL;
  1178. }
  1179. // GDI
  1180. bool bAlphaChannel = false;
  1181. HBITMAP hBitmap = NULL;
  1182. int x,y,n;
  1183. LPBYTE pImage = stbi_load_from_memory(pData, dwSize, &x, &y, &n, 4);
  1184. if(pImage != NULL) {
  1185. BITMAPINFO bmi;
  1186. ::ZeroMemory(&bmi, sizeof(BITMAPINFO));
  1187. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  1188. bmi.bmiHeader.biWidth = x;
  1189. bmi.bmiHeader.biHeight = -y;
  1190. bmi.bmiHeader.biPlanes = 1;
  1191. bmi.bmiHeader.biBitCount = 32;
  1192. bmi.bmiHeader.biCompression = BI_RGB;
  1193. bmi.bmiHeader.biSizeImage = x * y * 4;
  1194. LPBYTE pDest = NULL;
  1195. hBitmap = ::CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void**)&pDest, NULL, 0);
  1196. if(hBitmap != NULL) {
  1197. for( int i = 0; i < x * y; i++ )
  1198. {
  1199. pDest[i*4 + 3] = pImage[i*4 + 3];
  1200. if( pDest[i*4 + 3] < 255 )
  1201. {
  1202. pDest[i*4] = (BYTE)(DWORD(pImage[i*4 + 2])*pImage[i*4 + 3]/255);
  1203. pDest[i*4 + 1] = (BYTE)(DWORD(pImage[i*4 + 1])*pImage[i*4 + 3]/255);
  1204. pDest[i*4 + 2] = (BYTE)(DWORD(pImage[i*4])*pImage[i*4 + 3]/255);
  1205. bAlphaChannel = true;
  1206. }
  1207. else
  1208. {
  1209. pDest[i*4] = pImage[i*4 + 2];
  1210. pDest[i*4 + 1] = pImage[i*4 + 1];
  1211. pDest[i*4 + 2] = pImage[i*4];
  1212. }
  1213. if( *(DWORD*)(&pDest[i*4]) == mask ) {
  1214. pDest[i*4] = (BYTE)0;
  1215. pDest[i*4 + 1] = (BYTE)0;
  1216. pDest[i*4 + 2] = (BYTE)0;
  1217. pDest[i*4 + 3] = (BYTE)0;
  1218. bAlphaChannel = true;
  1219. }
  1220. }
  1221. stbi_image_free(pImage);
  1222. }
  1223. }
  1224. // GdiPlus
  1225. HGLOBAL hMem = ::GlobalAlloc(GMEM_FIXED, dwSize);
  1226. BYTE* pMem = (BYTE*)::GlobalLock(hMem);
  1227. memcpy(pMem, pData, dwSize);
  1228. IStream* pStm = NULL;
  1229. ::CreateStreamOnHGlobal(hMem, TRUE, &pStm);
  1230. Gdiplus::Image* pGdiplusImage = Gdiplus::Image::FromStream(pStm);
  1231. if(!pGdiplusImage || pGdiplusImage->GetLastStatus() != Gdiplus::Ok)
  1232. {
  1233. pGdiplusImage = NULL;
  1234. pStm->Release();
  1235. ::GlobalUnlock(hMem);
  1236. }
  1237. delete[] pData;
  1238. pData = NULL;
  1239. if(hBitmap == NULL && pGdiplusImage == NULL) {
  1240. return NULL;
  1241. }
  1242. TImageInfo* data = new TImageInfo;
  1243. data->pBits = NULL;
  1244. data->pSrcBits = NULL;
  1245. data->pImage = pGdiplusImage;
  1246. data->hBitmap = hBitmap;
  1247. data->nX = x;
  1248. data->nY = y;
  1249. data->bAlpha = bAlphaChannel;
  1250. return data;
  1251. }
  1252. void CRenderEngine::GdiplusDrawImage(HDC hDC, Gdiplus::Image* image, const RECT& rc, const RECT& rcPaint, const RECT& rcBmpPart, bool bAlpha, UINT uFade, UINT uRotate)
  1253. {
  1254. Gdiplus::Graphics g(hDC);
  1255. //设置画图时的滤波模式为消除锯齿现象
  1256. g.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
  1257. Gdiplus::ImageAttributes imageAtt;
  1258. if(uFade != 255) {
  1259. Gdiplus::ColorMatrix colorMatrix =
  1260. {
  1261. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  1262. 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  1263. 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  1264. 0.0f, 0.0f, 0.0f, uFade / 255.0f, 0.0f,
  1265. 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  1266. };
  1267. imageAtt.SetColorMatrix (&colorMatrix, Gdiplus::ColorMatrixFlagsDefault, Gdiplus::ColorAdjustTypeBitmap);
  1268. }
  1269. Gdiplus::RectF rcDest(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
  1270. Gdiplus::RectF rcSrc(rcBmpPart.left, rcBmpPart.top, rcBmpPart.right - rcBmpPart.left, rcBmpPart.bottom - rcBmpPart.top);
  1271. if(uRotate > 0) {
  1272. POINT ptCenter = {rc.left + (rc.right - rc.left) / 2, rc.top + (rc.bottom - rc.top) / 2};
  1273. int cx = rc.right - rc.left;
  1274. int cy = rc.bottom - rc.top;
  1275. Gdiplus::Matrix matrix;
  1276. matrix.RotateAt(uRotate, Gdiplus::PointF(ptCenter.x, ptCenter.y));
  1277. g.SetTransform(&matrix);
  1278. g.DrawImage(image, rcDest, rcSrc.GetLeft(), rcSrc.GetTop(), rcSrc.Width, rcSrc.Height, Gdiplus::UnitPixel, &imageAtt);
  1279. g.ResetTransform();
  1280. }
  1281. else {
  1282. g.DrawImage(image, rcDest, rcSrc.GetLeft(), rcSrc.GetTop(), rcSrc.Width, rcSrc.Height, Gdiplus::UnitPixel, &imageAtt);
  1283. }
  1284. g.ReleaseHDC(hDC);
  1285. }
  1286. void CRenderEngine::GdiplusDrawText(HDC hDC, CPaintManagerUI* pManager, RECT& rc, LPCTSTR pstrText, DWORD dwTextColor, int iFont, UINT uStyle)
  1287. {
  1288. ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
  1289. if( pstrText == NULL || pManager == NULL ) return;
  1290. HFONT hOldFont = (HFONT)::SelectObject(hDC, pManager->GetFont(iFont));
  1291. Gdiplus::Graphics graphics( hDC );
  1292. Gdiplus::Font font(hDC, pManager->GetFont(iFont));
  1293. Gdiplus::TextRenderingHint trh = Gdiplus::TextRenderingHintSystemDefault;
  1294. switch(pManager->GetGdiplusTextRenderingHint())
  1295. {
  1296. case 0: {trh = Gdiplus::TextRenderingHintSystemDefault; break;}
  1297. case 1: {trh = Gdiplus::TextRenderingHintSingleBitPerPixelGridFit; break;}
  1298. case 2: {trh = Gdiplus::TextRenderingHintSingleBitPerPixel; break;}
  1299. case 3: {trh = Gdiplus::TextRenderingHintAntiAliasGridFit; break;}
  1300. case 4: {trh = Gdiplus::TextRenderingHintAntiAlias; break;}
  1301. case 5: {trh = Gdiplus::TextRenderingHintClearTypeGridFit; break;}
  1302. }
  1303. graphics.SetTextRenderingHint(trh);
  1304. graphics.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
  1305. graphics.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);
  1306. Gdiplus::RectF rectF((Gdiplus::REAL)rc.left, (Gdiplus::REAL)rc.top, (Gdiplus::REAL)(rc.right - rc.left), (Gdiplus::REAL)(rc.bottom - rc.top));
  1307. Gdiplus::SolidBrush brush(Gdiplus::Color(254, GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)));
  1308. Gdiplus::StringFormat stringFormat = Gdiplus::StringFormat::GenericTypographic();
  1309. if ((uStyle & DT_END_ELLIPSIS) != 0) {
  1310. stringFormat.SetTrimming(Gdiplus::StringTrimmingEllipsisCharacter);
  1311. }
  1312. int formatFlags = 0;
  1313. if ((uStyle & DT_NOCLIP) != 0) {
  1314. formatFlags |= Gdiplus::StringFormatFlagsNoClip;
  1315. }
  1316. if ((uStyle & DT_SINGLELINE) != 0) {
  1317. formatFlags |= Gdiplus::StringFormatFlagsNoWrap;
  1318. }
  1319. if ((uStyle & DT_NOPREFIX) != 0) {
  1320. stringFormat.SetHotkeyPrefix(Gdiplus::HotkeyPrefixHide);
  1321. }
  1322. else {
  1323. stringFormat.SetHotkeyPrefix(Gdiplus::HotkeyPrefixShow);
  1324. }
  1325. stringFormat.SetFormatFlags(formatFlags);
  1326. if ((uStyle & DT_LEFT) != 0) {
  1327. stringFormat.SetAlignment(Gdiplus::StringAlignmentNear);
  1328. }
  1329. else if ((uStyle & DT_CENTER) != 0) {
  1330. stringFormat.SetAlignment(Gdiplus::StringAlignmentCenter);
  1331. }
  1332. else if ((uStyle & DT_RIGHT) != 0) {
  1333. stringFormat.SetAlignment(Gdiplus::StringAlignmentFar);
  1334. }
  1335. else {
  1336. stringFormat.SetAlignment(Gdiplus::StringAlignmentNear);
  1337. }
  1338. stringFormat.GenericTypographic();
  1339. if ((uStyle & DT_TOP) != 0) {
  1340. stringFormat.SetLineAlignment(Gdiplus::StringAlignmentNear);
  1341. }
  1342. else if ((uStyle & DT_VCENTER) != 0) {
  1343. stringFormat.SetLineAlignment(Gdiplus::StringAlignmentCenter);
  1344. }
  1345. else if ((uStyle & DT_BOTTOM) != 0) {
  1346. stringFormat.SetLineAlignment(Gdiplus::StringAlignmentFar);
  1347. }
  1348. else {
  1349. stringFormat.SetLineAlignment(Gdiplus::StringAlignmentNear);
  1350. }
  1351. #ifdef UNICODE
  1352. if ((uStyle & DT_CALCRECT) != 0)
  1353. {
  1354. Gdiplus::RectF bounds;
  1355. graphics.MeasureString(pstrText, -1, &font, rectF, &stringFormat, &bounds);
  1356. // MeasureString存在计算误差,这里加一像素
  1357. rc.bottom = rc.top + (long)bounds.Height + 1;
  1358. rc.right = rc.left + (long)bounds.Width + 1;
  1359. }
  1360. else
  1361. {
  1362. graphics.DrawString(pstrText, -1, &font, rectF, &stringFormat, &brush);
  1363. }
  1364. #else
  1365. DWORD dwSize = MultiByteToWideChar(CP_ACP, 0, pstrText, -1, NULL, 0);
  1366. WCHAR * pcwszDest = new WCHAR[dwSize + 1];
  1367. memset(pcwszDest, 0, (dwSize + 1) * sizeof(WCHAR));
  1368. MultiByteToWideChar(CP_ACP, NULL, pstrText, -1, pcwszDest, dwSize);
  1369. if(pcwszDest != NULL)
  1370. {
  1371. if ((uStyle & DT_CALCRECT) != 0)
  1372. {
  1373. Gdiplus::RectF bounds;
  1374. graphics.MeasureString(pcwszDest, -1, &font, rectF, &stringFormat, &bounds);
  1375. rc.bottom = rc.top + (long)(bounds.Height * 1.06);
  1376. rc.right = rc.left + (long)(bounds.Width * 1.06);
  1377. }
  1378. else
  1379. {
  1380. graphics.DrawString(pcwszDest, -1, &font, rectF, &stringFormat, &brush);
  1381. }
  1382. delete []pcwszDest;
  1383. }
  1384. #endif
  1385. ::SelectObject(hDC, hOldFont);
  1386. }
  1387. // 绘制及填充圆角矩形
  1388. void GdiplusDrawRoundRect(HDC hDC, float x, float y, float width, float height, float arcSize, float lineWidth, Gdiplus::Color lineColor, bool fillPath, Gdiplus::Color fillColor, int nStyle)
  1389. {
  1390. float arcDiameter = arcSize * 2;
  1391. // 创建GDI+对象
  1392. Gdiplus::Graphics g(hDC);
  1393. //设置画图时的滤波模式为消除锯齿现象
  1394. g.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
  1395. // 绘图路径
  1396. Gdiplus::GraphicsPath roundRectPath;
  1397. // 保存绘图路径
  1398. roundRectPath.AddLine(x + arcSize, y, x + width - arcSize, y); // 顶部横线
  1399. roundRectPath.AddArc(x + width - arcDiameter, y, arcDiameter, arcDiameter, 270, 90); // 右上圆角
  1400. roundRectPath.AddLine(x + width, y + arcSize, x + width, y + height - arcSize); // 右侧竖线
  1401. roundRectPath.AddArc(x + width - arcDiameter, y + height - arcDiameter, arcDiameter, arcDiameter, 0, 90); // 右下圆角
  1402. roundRectPath.AddLine(x + width - arcSize, y + height, x + arcSize, y + height); // 底部横线
  1403. roundRectPath.AddArc(x, y + height - arcDiameter, arcDiameter, arcDiameter, 90, 90); // 左下圆角
  1404. roundRectPath.AddLine(x, y + height - arcSize, x, y + arcSize); // 左侧竖线
  1405. roundRectPath.AddArc(x, y, arcDiameter, arcDiameter, 180, 90); // 左上圆角
  1406. //创建画笔
  1407. Gdiplus::Pen pen(lineColor, lineWidth);
  1408. pen.SetDashStyle((Gdiplus::DashStyle)nStyle);
  1409. // 绘制矩形
  1410. g.DrawPath(&pen, &roundRectPath);
  1411. // 是否填充
  1412. if(fillPath) {
  1413. if(fillColor.GetAlpha() == 0) {
  1414. fillColor = lineColor; // 若未指定填充色,则用线条色填充
  1415. }
  1416. // 创建画刷
  1417. Gdiplus::SolidBrush brush(fillColor);
  1418. // 填充
  1419. g.FillPath(&brush, &roundRectPath);
  1420. }
  1421. }
  1422. /////////////////////////////////////////////////////////////////////////////////////
  1423. //
  1424. //
  1425. void CRenderEngine::DrawColor(HDC hDC, const RECT& rc, DWORD color)
  1426. {
  1427. if( color <= 0x00FFFFFF ) return;
  1428. Gdiplus::Graphics graphics( hDC );
  1429. Gdiplus::SolidBrush brush(Gdiplus::Color((LOBYTE((color)>>24)), GetBValue(color), GetGValue(color), GetRValue(color)));
  1430. graphics.FillRectangle(&brush, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
  1431. }
  1432. void CRenderEngine::DrawGradient(HDC hDC, const RECT& rc, DWORD dwFirst, DWORD dwSecond, bool bVertical, int nSteps)
  1433. {
  1434. typedef BOOL (WINAPI *LPALPHABLEND)(HDC, int, int, int, int,HDC, int, int, int, int, BLENDFUNCTION);
  1435. static LPALPHABLEND lpAlphaBlend = (LPALPHABLEND) ::GetProcAddress(::GetModuleHandle(_T("msimg32.dll")), "AlphaBlend");
  1436. if( lpAlphaBlend == NULL ) lpAlphaBlend = AlphaBitBlt;
  1437. typedef BOOL (WINAPI *PGradientFill)(HDC, PTRIVERTEX, ULONG, PVOID, ULONG, ULONG);
  1438. static PGradientFill lpGradientFill = (PGradientFill) ::GetProcAddress(::GetModuleHandle(_T("msimg32.dll")), "GradientFill");
  1439. BYTE bAlpha = (BYTE)(((dwFirst >> 24) + (dwSecond >> 24)) >> 1);
  1440. if( bAlpha == 0 ) return;
  1441. int cx = rc.right - rc.left;
  1442. int cy = rc.bottom - rc.top;
  1443. RECT rcPaint = rc;
  1444. HDC hPaintDC = hDC;
  1445. HBITMAP hPaintBitmap = NULL;
  1446. HBITMAP hOldPaintBitmap = NULL;
  1447. if( bAlpha < 255 ) {
  1448. rcPaint.left = rcPaint.top = 0;
  1449. rcPaint.right = cx;
  1450. rcPaint.bottom = cy;
  1451. hPaintDC = ::CreateCompatibleDC(hDC);
  1452. hPaintBitmap = ::CreateCompatibleBitmap(hDC, cx, cy);
  1453. ASSERT(hPaintDC);
  1454. ASSERT(hPaintBitmap);
  1455. hOldPaintBitmap = (HBITMAP) ::SelectObject(hPaintDC, hPaintBitmap);
  1456. }
  1457. if( lpGradientFill != NULL )
  1458. {
  1459. TRIVERTEX triv[2] =
  1460. {
  1461. { rcPaint.left, rcPaint.top,
  1462. static_cast<COLOR16>(GetBValue(dwFirst) << 8),
  1463. static_cast<COLOR16>(GetGValue(dwFirst) << 8),
  1464. static_cast<COLOR16>(GetRValue(dwFirst) << 8), 0xFF00 },
  1465. { rcPaint.right, rcPaint.bottom,
  1466. static_cast<COLOR16>(GetBValue(dwSecond) << 8),
  1467. static_cast<COLOR16>(GetGValue(dwSecond) << 8),
  1468. static_cast<COLOR16>(GetRValue(dwSecond) << 8), 0xFF00 }
  1469. };
  1470. GRADIENT_RECT grc = { 0, 1 };
  1471. lpGradientFill(hPaintDC, triv, 2, &grc, 1, bVertical ? GRADIENT_FILL_RECT_V : GRADIENT_FILL_RECT_H);
  1472. }
  1473. else
  1474. {
  1475. // Determine how many shades
  1476. int nShift = 1;
  1477. if( nSteps >= 64 ) nShift = 6;
  1478. else if( nSteps >= 32 ) nShift = 5;
  1479. else if( nSteps >= 16 ) nShift = 4;
  1480. else if( nSteps >= 8 ) nShift = 3;
  1481. else if( nSteps >= 4 ) nShift = 2;
  1482. int nLines = 1 << nShift;
  1483. for( int i = 0; i < nLines; i++ ) {
  1484. // Do a little alpha blending
  1485. BYTE bR = (BYTE) ((GetBValue(dwSecond) * (nLines - i) + GetBValue(dwFirst) * i) >> nShift);
  1486. BYTE bG = (BYTE) ((GetGValue(dwSecond) * (nLines - i) + GetGValue(dwFirst) * i) >> nShift);
  1487. BYTE bB = (BYTE) ((GetRValue(dwSecond) * (nLines - i) + GetRValue(dwFirst) * i) >> nShift);
  1488. // ... then paint with the resulting color
  1489. HBRUSH hBrush = ::CreateSolidBrush(RGB(bR,bG,bB));
  1490. RECT r2 = rcPaint;
  1491. if( bVertical ) {
  1492. r2.bottom = rc.bottom - ((i * (rc.bottom - rc.top)) >> nShift);
  1493. r2.top = rc.bottom - (((i + 1) * (rc.bottom - rc.top)) >> nShift);
  1494. if( (r2.bottom - r2.top) > 0 ) ::FillRect(hDC, &r2, hBrush);
  1495. }
  1496. else {
  1497. r2.left = rc.right - (((i + 1) * (rc.right - rc.left)) >> nShift);
  1498. r2.right = rc.right - ((i * (rc.right - rc.left)) >> nShift);
  1499. if( (r2.right - r2.left) > 0 ) ::FillRect(hPaintDC, &r2, hBrush);
  1500. }
  1501. ::DeleteObject(hBrush);
  1502. }
  1503. }
  1504. if( bAlpha < 255 ) {
  1505. BLENDFUNCTION bf = { AC_SRC_OVER, 0, bAlpha, AC_SRC_ALPHA };
  1506. lpAlphaBlend(hDC, rc.left, rc.top, cx, cy, hPaintDC, 0, 0, cx, cy, bf);
  1507. ::SelectObject(hPaintDC, hOldPaintBitmap);
  1508. ::DeleteObject(hPaintBitmap);
  1509. ::DeleteDC(hPaintDC);
  1510. }
  1511. }
  1512. void CRenderEngine::DrawLine( HDC hDC, const RECT& rc, int nSize, DWORD dwPenColor,int nStyle /*= PS_SOLID*/ )
  1513. {
  1514. ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
  1515. LOGPEN lg;
  1516. lg.lopnColor = RGB(GetBValue(dwPenColor), GetGValue(dwPenColor), GetRValue(dwPenColor));
  1517. lg.lopnStyle = nStyle;
  1518. lg.lopnWidth.x = nSize;
  1519. HPEN hPen = CreatePenIndirect(&lg);
  1520. HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
  1521. POINT ptTemp = { 0 };
  1522. ::MoveToEx(hDC, rc.left, rc.top, &ptTemp);
  1523. ::LineTo(hDC, rc.right, rc.bottom);
  1524. ::SelectObject(hDC, hOldPen);
  1525. ::DeleteObject(hPen);
  1526. }
  1527. void CRenderEngine::DrawRect(HDC hDC, const RECT& rc, int nSize, DWORD dwPenColor,int nStyle /*= PS_SOLID*/)
  1528. {
  1529. #ifdef USE_GDI_RENDER
  1530. ASSERT(::GetObjectType(hDC) == OBJ_DC || ::GetObjectType(hDC) == OBJ_MEMDC);
  1531. HPEN hPen = ::CreatePen(nStyle | PS_INSIDEFRAME, nSize, RGB(GetBValue(dwPenColor), GetGValue(dwPenColor), GetRValue(dwPenColor)));
  1532. HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
  1533. ::SelectObject(hDC, ::GetStockObject(HOLLOW_BRUSH));
  1534. ::Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
  1535. ::SelectObject(hDC, hOldPen);
  1536. ::DeleteObject(hPen);
  1537. #else
  1538. ASSERT(::GetObjectType(hDC) == OBJ_DC || ::GetObjectType(hDC) == OBJ_MEMDC);
  1539. Gdiplus::Graphics graphics(hDC);
  1540. Gdiplus::Pen pen(Gdiplus::Color(dwPenColor), (Gdiplus::REAL)nSize);
  1541. pen.SetAlignment(Gdiplus::PenAlignmentInset);
  1542. pen.SetDashStyle((Gdiplus::DashStyle)nStyle);
  1543. graphics.DrawRectangle(&pen, rc.left, rc.top, rc.right - rc.left - 1, rc.bottom - rc.top - 1);
  1544. #endif
  1545. }
  1546. void CRenderEngine::DrawRoundRect(HDC hDC, const RECT& rc, int nSize, int width, int height, DWORD dwPenColor, int nStyle /*= PS_SOLID*/)
  1547. {
  1548. #ifdef USE_GDI_RENDER
  1549. ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
  1550. HPEN hPen = ::CreatePen(nStyle, nSize, RGB(GetBValue(dwPenColor), GetGValue(dwPenColor), GetRValue(dwPenColor)));
  1551. HPEN hOldPen = (HPEN)::SelectObject(hDC, hPen);
  1552. ::SelectObject(hDC, ::GetStockObject(HOLLOW_BRUSH));
  1553. ::RoundRect(hDC, rc.left, rc.top, rc.right, rc.bottom, width, height);
  1554. ::SelectObject(hDC, hOldPen);
  1555. ::DeleteObject(hPen);
  1556. #else
  1557. GdiplusDrawRoundRect(hDC, rc.left, rc.top, rc.right - rc.left - 1, rc.bottom - rc.top - 1, width / 2, nSize, Gdiplus::Color(dwPenColor), false, Gdiplus::Color(dwPenColor), nStyle);
  1558. #endif
  1559. }
  1560. /////////////////////////////////////////////////////////////////////////////////////
  1561. //
  1562. //
  1563. void CRenderEngine::DrawText(HDC hDC, CPaintManagerUI* pManager, RECT& rc, LPCTSTR pstrText,DWORD dwTextColor, int iFont, UINT uStyle, DWORD dwTextBKColor)
  1564. {
  1565. ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
  1566. if( pstrText == NULL || pManager == NULL ) return;
  1567. DrawColor(hDC, rc, dwTextBKColor);
  1568. DrawText(hDC, pManager, rc, pstrText, dwTextColor, iFont, uStyle);
  1569. }
  1570. void CRenderEngine::DrawText(HDC hDC, CPaintManagerUI* pManager, RECT& rc, LPCTSTR pstrText, DWORD dwTextColor, int iFont, UINT uStyle)
  1571. {
  1572. ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
  1573. if( pstrText == NULL || pManager == NULL ) return;
  1574. if (pManager->IsLayered() || pManager->IsUseGdiplusText())
  1575. {
  1576. GdiplusDrawText(hDC, pManager, rc, pstrText, dwTextColor, iFont, uStyle);
  1577. }
  1578. else
  1579. {
  1580. ::SetBkMode(hDC, TRANSPARENT);
  1581. ::SetTextColor(hDC, RGB(GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)));
  1582. HFONT hOldFont = (HFONT)::SelectObject(hDC, pManager->GetFont(iFont));
  1583. int fonticonpos = CDuiString(pstrText).Find(_T("&#x"));
  1584. if (fonticonpos != -1) {
  1585. CDuiString strUnicode = CDuiString(pstrText).Mid(fonticonpos + 3);
  1586. if (strUnicode.GetLength() > 4) strUnicode = strUnicode.Mid(0,4);
  1587. if (strUnicode.Right(1).CompareNoCase(_T(" ")) == 0) {
  1588. strUnicode = strUnicode.Mid(0, strUnicode.GetLength() - 1);
  1589. }
  1590. if (strUnicode.Right(1).CompareNoCase(_T(";")) == 0) {
  1591. strUnicode = strUnicode.Mid(0,strUnicode.GetLength()-1);
  1592. }
  1593. wchar_t wch[2] = { 0 };
  1594. wch[0] = static_cast<wchar_t>(_tcstol(strUnicode.GetData(), 0, 16));
  1595. ::DrawTextW(hDC, wch, -1, &rc, uStyle);
  1596. }
  1597. else {
  1598. ::DrawText(hDC, pstrText, -1, &rc, uStyle);
  1599. }
  1600. ::SelectObject(hDC, hOldFont);
  1601. }
  1602. }
  1603. void CRenderEngine::DrawHtmlText(HDC hDC, CPaintManagerUI* pManager, RECT& rc, LPCTSTR pstrText, DWORD dwTextColor, RECT* prcLinks, CDuiString* sLinks, int& nLinkRects, int iFont, UINT uStyle)
  1604. {
  1605. // 考虑到在xml编辑器中使用<>符号不方便,可以使用{}符号代替
  1606. // 支持标签嵌套(如<l><b>text</b></l>),但是交叉嵌套是应该避免的(如<l><b>text</l></b>)
  1607. // The string formatter supports a kind of "mini-html" that consists of various short tags:
  1608. //
  1609. // Bold: <b>text</b>
  1610. // Color: <c #xxxxxx>text</c> where x = RGB in hex
  1611. // Font: <f x>text</f> where x = font id
  1612. // Italic: <i>text</i>
  1613. // Image: <i x y z> where x = image name and y = imagelist num and z(optional) = imagelist id
  1614. // Link: <a x>text</a> where x(optional) = link content, normal like app:notepad or http:www.xxx.com
  1615. // NewLine <n>
  1616. // Paragraph: <p x>text</p> where x = extra pixels indent in p
  1617. // Raw Text: <r>text</r>
  1618. // Selected: <s>text</s>
  1619. // Underline: <u>text</u>
  1620. // X Indent: <x i> where i = hor indent in pixels
  1621. // Y Indent: <y i> where i = ver indent in pixels
  1622. ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
  1623. if( pstrText == NULL || pManager == NULL ) return;
  1624. if( ::IsRectEmpty(&rc) ) return;
  1625. bool bDraw = (uStyle & DT_CALCRECT) == 0;
  1626. CStdPtrArray aFontArray(10);
  1627. CStdPtrArray aColorArray(10);
  1628. CStdPtrArray aPIndentArray(10);
  1629. RECT rcClip = { 0 };
  1630. ::GetClipBox(hDC, &rcClip);
  1631. HRGN hOldRgn = ::CreateRectRgnIndirect(&rcClip);
  1632. HRGN hRgn = ::CreateRectRgnIndirect(&rc);
  1633. if( bDraw ) ::ExtSelectClipRgn(hDC, hRgn, RGN_AND);
  1634. TFontInfo* pDefFontInfo = pManager->GetFontInfo(iFont);
  1635. if(pDefFontInfo == NULL) {
  1636. pDefFontInfo = pManager->GetDefaultFontInfo();
  1637. }
  1638. TEXTMETRIC* pTm = &pDefFontInfo->tm;
  1639. HFONT hOldFont = (HFONT) ::SelectObject(hDC, pDefFontInfo->hFont);
  1640. ::SetBkMode(hDC, TRANSPARENT);
  1641. ::SetTextColor(hDC, RGB(GetBValue(dwTextColor), GetGValue(dwTextColor), GetRValue(dwTextColor)));
  1642. DWORD dwBkColor = pManager->GetDefaultSelectedBkColor();
  1643. ::SetBkColor(hDC, RGB(GetBValue(dwBkColor), GetGValue(dwBkColor), GetRValue(dwBkColor)));
  1644. // If the drawstyle include a alignment, we'll need to first determine the text-size so
  1645. // we can draw it at the correct position...
  1646. if( ((uStyle & DT_CENTER) != 0 || (uStyle & DT_RIGHT) != 0 || (uStyle & DT_VCENTER) != 0 || (uStyle & DT_BOTTOM) != 0) && (uStyle & DT_CALCRECT) == 0 ) {
  1647. RECT rcText = { 0, 0, 9999, 100 };
  1648. int nLinks = 0;
  1649. DrawHtmlText(hDC, pManager, rcText, pstrText, dwTextColor, NULL, NULL, nLinks, iFont, uStyle | DT_CALCRECT);
  1650. if( (uStyle & DT_SINGLELINE) != 0 ){
  1651. if( (uStyle & DT_CENTER) != 0 ) {
  1652. rc.left = rc.left + ((rc.right - rc.left) / 2) - ((rcText.right - rcText.left) / 2);
  1653. rc.right = rc.left + (rcText.right - rcText.left);
  1654. }
  1655. if( (uStyle & DT_RIGHT) != 0 ) {
  1656. rc.left = rc.right - (rcText.right - rcText.left);
  1657. }
  1658. }
  1659. if( (uStyle & DT_VCENTER) != 0 ) {
  1660. rc.top = rc.top + ((rc.bottom - rc.top) / 2) - ((rcText.bottom - rcText.top) / 2);
  1661. rc.bottom = rc.top + (rcText.bottom - rcText.top);
  1662. }
  1663. if( (uStyle & DT_BOTTOM) != 0 ) {
  1664. rc.top = rc.bottom - (rcText.bottom - rcText.top);
  1665. }
  1666. }
  1667. bool bHoverLink = false;
  1668. CDuiString sHoverLink;
  1669. POINT ptMouse = pManager->GetMousePos();
  1670. for( int i = 0; !bHoverLink && i < nLinkRects; i++ ) {
  1671. if( ::PtInRect(prcLinks + i, ptMouse) ) {
  1672. sHoverLink = *(CDuiString*)(sLinks + i);
  1673. bHoverLink = true;
  1674. }
  1675. }
  1676. POINT pt = { rc.left, rc.top };
  1677. int iLinkIndex = 0;
  1678. int cyLine = pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1);
  1679. int cyMinHeight = 0;
  1680. int cxMaxWidth = 0;
  1681. POINT ptLinkStart = { 0 };
  1682. bool bLineEnd = false;
  1683. bool bInRaw = false;
  1684. bool bInLink = false;
  1685. bool bInSelected = false;
  1686. int iLineLinkIndex = 0;
  1687. // 排版习惯是图文底部对齐,所以每行绘制都要分两步,先计算高度,再绘制
  1688. CStdPtrArray aLineFontArray;
  1689. CStdPtrArray aLineColorArray;
  1690. CStdPtrArray aLinePIndentArray;
  1691. LPCTSTR pstrLineBegin = pstrText;
  1692. bool bLineInRaw = false;
  1693. bool bLineInLink = false;
  1694. bool bLineInSelected = false;
  1695. int cyLineHeight = 0;
  1696. bool bLineDraw = false; // 行的第二阶段:绘制
  1697. while( *pstrText != _T('\0') ) {
  1698. if( pt.x >= rc.right || *pstrText == _T('\n') || bLineEnd ) {
  1699. if( *pstrText == _T('\n') ) pstrText++;
  1700. if( bLineEnd ) bLineEnd = false;
  1701. if( !bLineDraw ) {
  1702. if( bInLink && iLinkIndex < nLinkRects ) {
  1703. ::SetRect(&prcLinks[iLinkIndex++], ptLinkStart.x, ptLinkStart.y, MIN(pt.x, rc.right), pt.y + cyLine);
  1704. CDuiString *pStr1 = (CDuiString*)(sLinks + iLinkIndex - 1);
  1705. CDuiString *pStr2 = (CDuiString*)(sLinks + iLinkIndex);
  1706. *pStr2 = *pStr1;
  1707. }
  1708. for( int i = iLineLinkIndex; i < iLinkIndex; i++ ) {
  1709. prcLinks[i].bottom = pt.y + cyLine;
  1710. }
  1711. if( bDraw ) {
  1712. bInLink = bLineInLink;
  1713. iLinkIndex = iLineLinkIndex;
  1714. }
  1715. }
  1716. else {
  1717. if( bInLink && iLinkIndex < nLinkRects ) iLinkIndex++;
  1718. bLineInLink = bInLink;
  1719. iLineLinkIndex = iLinkIndex;
  1720. }
  1721. if( (uStyle & DT_SINGLELINE) != 0 && (!bDraw || bLineDraw) ) break;
  1722. if( bDraw ) bLineDraw = !bLineDraw; // !
  1723. pt.x = rc.left;
  1724. if( !bLineDraw ) pt.y += cyLine;
  1725. if( pt.y > rc.bottom && bDraw ) break;
  1726. ptLinkStart = pt;
  1727. cyLine = pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1);
  1728. if( pt.x >= rc.right ) break;
  1729. }
  1730. else if( !bInRaw && ( *pstrText == _T('<') || *pstrText == _T('{') )
  1731. && ( pstrText[1] >= _T('a') && pstrText[1] <= _T('z') )
  1732. && ( pstrText[2] == _T(' ') || pstrText[2] == _T('>') || pstrText[2] == _T('}') ) ) {
  1733. pstrText++;
  1734. LPCTSTR pstrNextStart = NULL;
  1735. switch( *pstrText ) {
  1736. case _T('a'): // Link
  1737. {
  1738. pstrText++;
  1739. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  1740. if( iLinkIndex < nLinkRects && !bLineDraw ) {
  1741. CDuiString *pStr = (CDuiString*)(sLinks + iLinkIndex);
  1742. pStr->Empty();
  1743. while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') ) {
  1744. LPCTSTR pstrTemp = ::CharNext(pstrText);
  1745. while( pstrText < pstrTemp) {
  1746. *pStr += *pstrText++;
  1747. }
  1748. }
  1749. }
  1750. DWORD clrColor = dwTextColor;
  1751. if(clrColor == 0) pManager->GetDefaultLinkFontColor();
  1752. if( bHoverLink && iLinkIndex < nLinkRects ) {
  1753. CDuiString *pStr = (CDuiString*)(sLinks + iLinkIndex);
  1754. if( sHoverLink == *pStr ) clrColor = pManager->GetDefaultLinkHoverFontColor();
  1755. }
  1756. //else if( prcLinks == NULL ) {
  1757. // if( ::PtInRect(&rc, ptMouse) )
  1758. // clrColor = pManager->GetDefaultLinkHoverFontColor();
  1759. //}
  1760. aColorArray.Add((LPVOID)clrColor);
  1761. ::SetTextColor(hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
  1762. TFontInfo* pFontInfo = pDefFontInfo;
  1763. if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
  1764. if( pFontInfo->bUnderline == false ) {
  1765. HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic, pFontInfo->bStrikeout);
  1766. if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic, pFontInfo->bStrikeout);
  1767. pFontInfo = pManager->GetFontInfo(hFont);
  1768. aFontArray.Add(pFontInfo);
  1769. pTm = &pFontInfo->tm;
  1770. ::SelectObject(hDC, pFontInfo->hFont);
  1771. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  1772. }
  1773. ptLinkStart = pt;
  1774. bInLink = true;
  1775. }
  1776. break;
  1777. case _T('b'): // Bold
  1778. {
  1779. pstrText++;
  1780. TFontInfo* pFontInfo = pDefFontInfo;
  1781. if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
  1782. if( pFontInfo->bBold == false ) {
  1783. HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, true, pFontInfo->bUnderline, pFontInfo->bItalic, pFontInfo->bStrikeout);
  1784. if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, true, pFontInfo->bUnderline, pFontInfo->bItalic, pFontInfo->bStrikeout);
  1785. pFontInfo = pManager->GetFontInfo(hFont);
  1786. aFontArray.Add(pFontInfo);
  1787. pTm = &pFontInfo->tm;
  1788. ::SelectObject(hDC, pFontInfo->hFont);
  1789. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  1790. }
  1791. }
  1792. break;
  1793. case _T('c'): // Color
  1794. {
  1795. pstrText++;
  1796. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  1797. if( *pstrText == _T('#')) pstrText++;
  1798. DWORD clrColor = _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 16);
  1799. aColorArray.Add((LPVOID)clrColor);
  1800. ::SetTextColor(hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
  1801. }
  1802. break;
  1803. case _T('f'): // Font
  1804. {
  1805. pstrText++;
  1806. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  1807. LPCTSTR pstrTemp = pstrText;
  1808. int iFont = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
  1809. if( pstrTemp != pstrText ) {
  1810. TFontInfo* pFontInfo = pManager->GetFontInfo(iFont);
  1811. aFontArray.Add(pFontInfo);
  1812. pTm = &pFontInfo->tm;
  1813. ::SelectObject(hDC, pFontInfo->hFont);
  1814. }
  1815. else {
  1816. CDuiString sFontName;
  1817. int iFontSize = 10;
  1818. CDuiString sFontAttr;
  1819. bool bBold = false;
  1820. bool bUnderline = false;
  1821. bool bItalic = false;
  1822. bool bStrikeout = false;
  1823. while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') && *pstrText != _T(' ') ) {
  1824. pstrTemp = ::CharNext(pstrText);
  1825. while( pstrText < pstrTemp) {
  1826. sFontName += *pstrText++;
  1827. }
  1828. }
  1829. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  1830. if( isdigit(*pstrText) ) {
  1831. iFontSize = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
  1832. }
  1833. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  1834. while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') ) {
  1835. pstrTemp = ::CharNext(pstrText);
  1836. while( pstrText < pstrTemp) {
  1837. sFontAttr += *pstrText++;
  1838. }
  1839. }
  1840. sFontAttr.MakeLower();
  1841. if( sFontAttr.Find(_T("bold")) >= 0 ) bBold = true;
  1842. if( sFontAttr.Find(_T("underline")) >= 0 ) bUnderline = true;
  1843. if( sFontAttr.Find(_T("italic")) >= 0 ) bItalic = true;
  1844. if( sFontAttr.Find(_T("strikeout")) >= 0 ) bStrikeout = true;
  1845. HFONT hFont = pManager->GetFont(sFontName, iFontSize, bBold, bUnderline, bItalic, bStrikeout);
  1846. if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, sFontName, iFontSize, bBold, bUnderline, bItalic, bStrikeout);
  1847. TFontInfo* pFontInfo = pManager->GetFontInfo(hFont);
  1848. aFontArray.Add(pFontInfo);
  1849. pTm = &pFontInfo->tm;
  1850. ::SelectObject(hDC, pFontInfo->hFont);
  1851. }
  1852. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  1853. }
  1854. break;
  1855. case _T('i'): // Italic or Image
  1856. {
  1857. pstrNextStart = pstrText - 1;
  1858. pstrText++;
  1859. CDuiString sImageString = pstrText;
  1860. int iWidth = 0;
  1861. int iHeight = 0;
  1862. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  1863. const TImageInfo* pImageInfo = NULL;
  1864. CDuiString sName;
  1865. while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') && *pstrText != _T(' ') ) {
  1866. LPCTSTR pstrTemp = ::CharNext(pstrText);
  1867. while( pstrText < pstrTemp) {
  1868. sName += *pstrText++;
  1869. }
  1870. }
  1871. if( sName.IsEmpty() ) { // Italic
  1872. pstrNextStart = NULL;
  1873. TFontInfo* pFontInfo = pDefFontInfo;
  1874. if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
  1875. if( pFontInfo->bItalic == false ) {
  1876. HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, pFontInfo->bUnderline, pFontInfo->bStrikeout, true);
  1877. if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, pFontInfo->bUnderline, pFontInfo->bStrikeout, true);
  1878. pFontInfo = pManager->GetFontInfo(hFont);
  1879. aFontArray.Add(pFontInfo);
  1880. pTm = &pFontInfo->tm;
  1881. ::SelectObject(hDC, pFontInfo->hFont);
  1882. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  1883. }
  1884. }
  1885. else {
  1886. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  1887. int iImageListNum = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
  1888. if( iImageListNum <= 0 ) iImageListNum = 1;
  1889. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  1890. int iImageListIndex = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
  1891. if( iImageListIndex < 0 || iImageListIndex >= iImageListNum ) iImageListIndex = 0;
  1892. if( _tcsstr(sImageString.GetData(), _T("file=\'")) != NULL || _tcsstr(sImageString.GetData(), _T("res=\'")) != NULL ) {
  1893. CDuiString sImageResType;
  1894. CDuiString sImageName;
  1895. LPCTSTR pStrImage = sImageString.GetData();
  1896. CDuiString sItem;
  1897. CDuiString sValue;
  1898. while( *pStrImage != _T('\0') ) {
  1899. sItem.Empty();
  1900. sValue.Empty();
  1901. while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) pStrImage = ::CharNext(pStrImage);
  1902. while( *pStrImage != _T('\0') && *pStrImage != _T('=') && *pStrImage > _T(' ') ) {
  1903. LPTSTR pstrTemp = ::CharNext(pStrImage);
  1904. while( pStrImage < pstrTemp) {
  1905. sItem += *pStrImage++;
  1906. }
  1907. }
  1908. while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) pStrImage = ::CharNext(pStrImage);
  1909. if( *pStrImage++ != _T('=') ) break;
  1910. while( *pStrImage > _T('\0') && *pStrImage <= _T(' ') ) pStrImage = ::CharNext(pStrImage);
  1911. if( *pStrImage++ != _T('\'') ) break;
  1912. while( *pStrImage != _T('\0') && *pStrImage != _T('\'') ) {
  1913. LPTSTR pstrTemp = ::CharNext(pStrImage);
  1914. while( pStrImage < pstrTemp) {
  1915. sValue += *pStrImage++;
  1916. }
  1917. }
  1918. if( *pStrImage++ != _T('\'') ) break;
  1919. if( !sValue.IsEmpty() ) {
  1920. if( sItem == _T("file") || sItem == _T("res") ) {
  1921. sImageName = sValue;
  1922. }
  1923. else if( sItem == _T("restype") ) {
  1924. sImageResType = sValue;
  1925. }
  1926. }
  1927. if( *pStrImage++ != _T(' ') ) break;
  1928. }
  1929. pImageInfo = pManager->GetImageEx((LPCTSTR)sImageName, sImageResType);
  1930. }
  1931. else
  1932. pImageInfo = pManager->GetImageEx((LPCTSTR)sName);
  1933. if( pImageInfo ) {
  1934. iWidth = pImageInfo->nX;
  1935. iHeight = pImageInfo->nY;
  1936. if( iImageListNum > 1 ) iWidth /= iImageListNum;
  1937. if( pt.x + iWidth > rc.right && pt.x > rc.left && (uStyle & DT_SINGLELINE) == 0 ) {
  1938. bLineEnd = true;
  1939. }
  1940. else {
  1941. pstrNextStart = NULL;
  1942. if( bDraw && bLineDraw ) {
  1943. CDuiRect rcImage(pt.x, pt.y + cyLineHeight - iHeight, pt.x + iWidth, pt.y + cyLineHeight);
  1944. if( iHeight < cyLineHeight ) {
  1945. rcImage.bottom -= (cyLineHeight - iHeight) / 2;
  1946. rcImage.top = rcImage.bottom - iHeight;
  1947. }
  1948. CDuiRect rcBmpPart(0, 0, iWidth, iHeight);
  1949. rcBmpPart.left = iWidth * iImageListIndex;
  1950. rcBmpPart.right = iWidth * (iImageListIndex + 1);
  1951. CDuiRect rcCorner(0, 0, 0, 0);
  1952. DrawImage(hDC, pImageInfo->hBitmap, rcImage, rcImage, rcBmpPart, rcCorner, \
  1953. pImageInfo->bAlpha, 255);
  1954. }
  1955. cyLine = MAX(iHeight, cyLine);
  1956. pt.x += iWidth;
  1957. cyMinHeight = pt.y + iHeight;
  1958. cxMaxWidth = MAX(cxMaxWidth, pt.x);
  1959. }
  1960. }
  1961. else pstrNextStart = NULL;
  1962. }
  1963. }
  1964. break;
  1965. case _T('n'): // Newline
  1966. {
  1967. pstrText++;
  1968. if( (uStyle & DT_SINGLELINE) != 0 ) break;
  1969. bLineEnd = true;
  1970. }
  1971. break;
  1972. case _T('p'): // Paragraph
  1973. {
  1974. pstrText++;
  1975. if( pt.x > rc.left ) bLineEnd = true;
  1976. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  1977. int cyLineExtra = (int)_tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
  1978. aPIndentArray.Add((LPVOID)cyLineExtra);
  1979. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + cyLineExtra);
  1980. }
  1981. break;
  1982. case _T('r'): // Raw Text
  1983. {
  1984. pstrText++;
  1985. bInRaw = true;
  1986. }
  1987. break;
  1988. case _T('s'): // Selected text background color
  1989. {
  1990. pstrText++;
  1991. bInSelected = !bInSelected;
  1992. if( bDraw && bLineDraw ) {
  1993. if( bInSelected ) ::SetBkMode(hDC, OPAQUE);
  1994. else ::SetBkMode(hDC, TRANSPARENT);
  1995. }
  1996. }
  1997. break;
  1998. case _T('u'): // Underline text
  1999. {
  2000. pstrText++;
  2001. TFontInfo* pFontInfo = pDefFontInfo;
  2002. if( aFontArray.GetSize() > 0 ) pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
  2003. if( pFontInfo->bUnderline == false ) {
  2004. HFONT hFont = pManager->GetFont(pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic, pFontInfo->bStrikeout);
  2005. if( hFont == NULL ) hFont = pManager->AddFont(g_iFontID, pFontInfo->sFontName, pFontInfo->iSize, pFontInfo->bBold, true, pFontInfo->bItalic, pFontInfo->bStrikeout);
  2006. pFontInfo = pManager->GetFontInfo(hFont);
  2007. aFontArray.Add(pFontInfo);
  2008. pTm = &pFontInfo->tm;
  2009. ::SelectObject(hDC, pFontInfo->hFont);
  2010. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  2011. }
  2012. }
  2013. break;
  2014. case _T('x'): // X Indent
  2015. {
  2016. pstrText++;
  2017. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  2018. int iWidth = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
  2019. pt.x += iWidth;
  2020. cxMaxWidth = MAX(cxMaxWidth, pt.x);
  2021. }
  2022. break;
  2023. case _T('y'): // Y Indent
  2024. {
  2025. pstrText++;
  2026. while( *pstrText > _T('\0') && *pstrText <= _T(' ') ) pstrText = ::CharNext(pstrText);
  2027. cyLine = (int) _tcstol(pstrText, const_cast<LPTSTR*>(&pstrText), 10);
  2028. }
  2029. break;
  2030. }
  2031. if( pstrNextStart != NULL ) pstrText = pstrNextStart;
  2032. else {
  2033. while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') ) pstrText = ::CharNext(pstrText);
  2034. pstrText = ::CharNext(pstrText);
  2035. }
  2036. }
  2037. else if( !bInRaw && ( *pstrText == _T('<') || *pstrText == _T('{') ) && pstrText[1] == _T('/') )
  2038. {
  2039. pstrText++;
  2040. pstrText++;
  2041. switch( *pstrText )
  2042. {
  2043. case _T('c'):
  2044. {
  2045. pstrText++;
  2046. aColorArray.Remove(aColorArray.GetSize() - 1);
  2047. DWORD clrColor = dwTextColor;
  2048. if( aColorArray.GetSize() > 0 ) clrColor = (int)aColorArray.GetAt(aColorArray.GetSize() - 1);
  2049. ::SetTextColor(hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
  2050. }
  2051. break;
  2052. case _T('p'):
  2053. pstrText++;
  2054. if( pt.x > rc.left ) bLineEnd = true;
  2055. aPIndentArray.Remove(aPIndentArray.GetSize() - 1);
  2056. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  2057. break;
  2058. case _T('s'):
  2059. {
  2060. pstrText++;
  2061. bInSelected = !bInSelected;
  2062. if( bDraw && bLineDraw ) {
  2063. if( bInSelected ) ::SetBkMode(hDC, OPAQUE);
  2064. else ::SetBkMode(hDC, TRANSPARENT);
  2065. }
  2066. }
  2067. break;
  2068. case _T('a'):
  2069. {
  2070. if( iLinkIndex < nLinkRects ) {
  2071. if( !bLineDraw ) ::SetRect(&prcLinks[iLinkIndex], ptLinkStart.x, ptLinkStart.y, MIN(pt.x, rc.right), pt.y + pTm->tmHeight + pTm->tmExternalLeading);
  2072. iLinkIndex++;
  2073. }
  2074. aColorArray.Remove(aColorArray.GetSize() - 1);
  2075. DWORD clrColor = dwTextColor;
  2076. if( aColorArray.GetSize() > 0 ) clrColor = (int)aColorArray.GetAt(aColorArray.GetSize() - 1);
  2077. ::SetTextColor(hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
  2078. bInLink = false;
  2079. }
  2080. case _T('b'):
  2081. case _T('f'):
  2082. case _T('i'):
  2083. case _T('u'):
  2084. {
  2085. pstrText++;
  2086. aFontArray.Remove(aFontArray.GetSize() - 1);
  2087. TFontInfo* pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
  2088. if( pFontInfo == NULL ) pFontInfo = pDefFontInfo;
  2089. if( pTm->tmItalic && pFontInfo->bItalic == false ) {
  2090. ABC abc;
  2091. ::GetCharABCWidths(hDC, _T(' '), _T(' '), &abc);
  2092. pt.x += abc.abcC / 2; // 简单修正一下斜体混排的问题, 正确做法应该是http://support.microsoft.com/kb/244798/en-us
  2093. }
  2094. pTm = &pFontInfo->tm;
  2095. ::SelectObject(hDC, pFontInfo->hFont);
  2096. cyLine = MAX(cyLine, pTm->tmHeight + pTm->tmExternalLeading + (int)aPIndentArray.GetAt(aPIndentArray.GetSize() - 1));
  2097. }
  2098. break;
  2099. }
  2100. while( *pstrText != _T('\0') && *pstrText != _T('>') && *pstrText != _T('}') ) pstrText = ::CharNext(pstrText);
  2101. pstrText = ::CharNext(pstrText);
  2102. }
  2103. else if( !bInRaw && *pstrText == _T('<') && pstrText[2] == _T('>') && (pstrText[1] == _T('{') || pstrText[1] == _T('}')) )
  2104. {
  2105. SIZE szSpace = { 0 };
  2106. ::GetTextExtentPoint32(hDC, &pstrText[1], 1, &szSpace);
  2107. if( bDraw && bLineDraw ) ::TextOut(hDC, pt.x, pt.y + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, &pstrText[1], 1);
  2108. pt.x += szSpace.cx;
  2109. cxMaxWidth = MAX(cxMaxWidth, pt.x);
  2110. pstrText++;pstrText++;pstrText++;
  2111. }
  2112. else if( !bInRaw && *pstrText == _T('{') && pstrText[2] == _T('}') && (pstrText[1] == _T('<') || pstrText[1] == _T('>')) )
  2113. {
  2114. SIZE szSpace = { 0 };
  2115. ::GetTextExtentPoint32(hDC, &pstrText[1], 1, &szSpace);
  2116. if( bDraw && bLineDraw ) ::TextOut(hDC, pt.x, pt.y + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, &pstrText[1], 1);
  2117. pt.x += szSpace.cx;
  2118. cxMaxWidth = MAX(cxMaxWidth, pt.x);
  2119. pstrText++;pstrText++;pstrText++;
  2120. }
  2121. else if( !bInRaw && *pstrText == _T(' ') )
  2122. {
  2123. SIZE szSpace = { 0 };
  2124. ::GetTextExtentPoint32(hDC, _T(" "), 1, &szSpace);
  2125. // Still need to paint the space because the font might have
  2126. // underline formatting.
  2127. if( bDraw && bLineDraw ) ::TextOut(hDC, pt.x, pt.y + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, _T(" "), 1);
  2128. pt.x += szSpace.cx;
  2129. cxMaxWidth = MAX(cxMaxWidth, pt.x);
  2130. pstrText++;
  2131. }
  2132. else
  2133. {
  2134. POINT ptPos = pt;
  2135. int cchChars = 0;
  2136. int cchSize = 0;
  2137. int cchLastGoodWord = 0;
  2138. int cchLastGoodSize = 0;
  2139. LPCTSTR p = pstrText;
  2140. LPCTSTR pstrNext;
  2141. SIZE szText = { 0 };
  2142. if( !bInRaw && *p == _T('<') || *p == _T('{') ) p++, cchChars++, cchSize++;
  2143. while( *p != _T('\0') && *p != _T('\n') ) {
  2144. // This part makes sure that we're word-wrapping if needed or providing support
  2145. // for DT_END_ELLIPSIS. Unfortunately the GetTextExtentPoint32() call is pretty
  2146. // slow when repeated so often.
  2147. // TODO: Rewrite and use GetTextExtentExPoint() instead!
  2148. if( bInRaw ) {
  2149. if( ( *p == _T('<') || *p == _T('{') ) && p[1] == _T('/')
  2150. && p[2] == _T('r') && ( p[3] == _T('>') || p[3] == _T('}') ) ) {
  2151. p += 4;
  2152. bInRaw = false;
  2153. break;
  2154. }
  2155. }
  2156. else {
  2157. if( *p == _T('<') || *p == _T('{') ) break;
  2158. }
  2159. pstrNext = ::CharNext(p);
  2160. cchChars++;
  2161. cchSize += (int)(pstrNext - p);
  2162. szText.cx = cchChars * pTm->tmMaxCharWidth;
  2163. if( pt.x + szText.cx >= rc.right ) {
  2164. ::GetTextExtentPoint32(hDC, pstrText, cchSize, &szText);
  2165. }
  2166. if( pt.x + szText.cx > rc.right ) {
  2167. if( pt.x + szText.cx > rc.right && pt.x != rc.left) {
  2168. cchChars--;
  2169. cchSize -= (int)(pstrNext - p);
  2170. }
  2171. if( (uStyle & DT_WORDBREAK) != 0 && cchLastGoodWord > 0 ) {
  2172. cchChars = cchLastGoodWord;
  2173. cchSize = cchLastGoodSize;
  2174. }
  2175. if( (uStyle & DT_END_ELLIPSIS) != 0 && cchChars > 0 ) {
  2176. cchChars -= 1;
  2177. LPCTSTR pstrPrev = ::CharPrev(pstrText, p);
  2178. if( cchChars > 0 ) {
  2179. cchChars -= 1;
  2180. pstrPrev = ::CharPrev(pstrText, pstrPrev);
  2181. cchSize -= (int)(p - pstrPrev);
  2182. }
  2183. else
  2184. cchSize -= (int)(p - pstrPrev);
  2185. pt.x = rc.right;
  2186. }
  2187. bLineEnd = true;
  2188. cxMaxWidth = MAX(cxMaxWidth, pt.x);
  2189. break;
  2190. }
  2191. if (!( ( p[0] >= _T('a') && p[0] <= _T('z') ) || ( p[0] >= _T('A') && p[0] <= _T('Z') ) )) {
  2192. cchLastGoodWord = cchChars;
  2193. cchLastGoodSize = cchSize;
  2194. }
  2195. if( *p == _T(' ') ) {
  2196. cchLastGoodWord = cchChars;
  2197. cchLastGoodSize = cchSize;
  2198. }
  2199. p = ::CharNext(p);
  2200. }
  2201. ::GetTextExtentPoint32(hDC, pstrText, cchSize, &szText);
  2202. if( bDraw && bLineDraw ) {
  2203. if( (uStyle & DT_SINGLELINE) == 0 && (uStyle & DT_CENTER) != 0 ) {
  2204. ptPos.x += (rc.right - rc.left - szText.cx)/2;
  2205. }
  2206. else if( (uStyle & DT_SINGLELINE) == 0 && (uStyle & DT_RIGHT) != 0) {
  2207. ptPos.x += (rc.right - rc.left - szText.cx);
  2208. }
  2209. ::TextOut(hDC, ptPos.x, ptPos.y + cyLineHeight - pTm->tmHeight - pTm->tmExternalLeading, pstrText, cchSize);
  2210. if( pt.x >= rc.right && (uStyle & DT_END_ELLIPSIS) != 0 )
  2211. ::TextOut(hDC, ptPos.x + szText.cx, ptPos.y, _T("..."), 3);
  2212. }
  2213. pt.x += szText.cx;
  2214. cxMaxWidth = MAX(cxMaxWidth, pt.x);
  2215. pstrText += cchSize;
  2216. }
  2217. if( pt.x >= rc.right || *pstrText == _T('\n') || *pstrText == _T('\0') ) bLineEnd = true;
  2218. if( bDraw && bLineEnd ) {
  2219. if( !bLineDraw ) {
  2220. aFontArray.Resize(aLineFontArray.GetSize());
  2221. ::CopyMemory(aFontArray.GetData(), aLineFontArray.GetData(), aLineFontArray.GetSize() * sizeof(LPVOID));
  2222. aColorArray.Resize(aLineColorArray.GetSize());
  2223. ::CopyMemory(aColorArray.GetData(), aLineColorArray.GetData(), aLineColorArray.GetSize() * sizeof(LPVOID));
  2224. aPIndentArray.Resize(aLinePIndentArray.GetSize());
  2225. ::CopyMemory(aPIndentArray.GetData(), aLinePIndentArray.GetData(), aLinePIndentArray.GetSize() * sizeof(LPVOID));
  2226. cyLineHeight = cyLine;
  2227. pstrText = pstrLineBegin;
  2228. bInRaw = bLineInRaw;
  2229. bInSelected = bLineInSelected;
  2230. DWORD clrColor = dwTextColor;
  2231. if( aColorArray.GetSize() > 0 ) clrColor = (int)aColorArray.GetAt(aColorArray.GetSize() - 1);
  2232. ::SetTextColor(hDC, RGB(GetBValue(clrColor), GetGValue(clrColor), GetRValue(clrColor)));
  2233. TFontInfo* pFontInfo = (TFontInfo*)aFontArray.GetAt(aFontArray.GetSize() - 1);
  2234. if( pFontInfo == NULL ) pFontInfo = pDefFontInfo;
  2235. pTm = &pFontInfo->tm;
  2236. ::SelectObject(hDC, pFontInfo->hFont);
  2237. if( bInSelected ) ::SetBkMode(hDC, OPAQUE);
  2238. }
  2239. else {
  2240. aLineFontArray.Resize(aFontArray.GetSize());
  2241. ::CopyMemory(aLineFontArray.GetData(), aFontArray.GetData(), aFontArray.GetSize() * sizeof(LPVOID));
  2242. aLineColorArray.Resize(aColorArray.GetSize());
  2243. ::CopyMemory(aLineColorArray.GetData(), aColorArray.GetData(), aColorArray.GetSize() * sizeof(LPVOID));
  2244. aLinePIndentArray.Resize(aPIndentArray.GetSize());
  2245. ::CopyMemory(aLinePIndentArray.GetData(), aPIndentArray.GetData(), aPIndentArray.GetSize() * sizeof(LPVOID));
  2246. pstrLineBegin = pstrText;
  2247. bLineInSelected = bInSelected;
  2248. bLineInRaw = bInRaw;
  2249. }
  2250. }
  2251. ASSERT(iLinkIndex<=nLinkRects);
  2252. }
  2253. nLinkRects = iLinkIndex;
  2254. // Return size of text when requested
  2255. if( (uStyle & DT_CALCRECT) != 0 ) {
  2256. rc.bottom = MAX(cyMinHeight, pt.y + cyLine);
  2257. rc.right = MIN(rc.right, cxMaxWidth);
  2258. }
  2259. if( bDraw ) ::SelectClipRgn(hDC, hOldRgn);
  2260. ::DeleteObject(hOldRgn);
  2261. ::DeleteObject(hRgn);
  2262. ::SelectObject(hDC, hOldFont);
  2263. }
  2264. HBITMAP CRenderEngine::GenerateBitmap(CPaintManagerUI* pManager, RECT rc, CControlUI* pStopControl, DWORD dwFilterColor)
  2265. {
  2266. if (pManager == NULL) return NULL;
  2267. int cx = rc.right - rc.left;
  2268. int cy = rc.bottom - rc.top;
  2269. bool bUseOffscreenBitmap = true;
  2270. HDC hPaintDC = ::CreateCompatibleDC(pManager->GetPaintDC());
  2271. ASSERT(hPaintDC);
  2272. HBITMAP hPaintBitmap = NULL;
  2273. //if (pStopControl == NULL && !pManager->IsLayered()) hPaintBitmap = pManager->Get();
  2274. if( hPaintBitmap == NULL ) {
  2275. bUseOffscreenBitmap = false;
  2276. hPaintBitmap = ::CreateCompatibleBitmap(pManager->GetPaintDC(), rc.right, rc.bottom);
  2277. ASSERT(hPaintBitmap);
  2278. }
  2279. HBITMAP hOldPaintBitmap = (HBITMAP) ::SelectObject(hPaintDC, hPaintBitmap);
  2280. if (!bUseOffscreenBitmap) {
  2281. CControlUI* pRoot = pManager->GetRoot();
  2282. pRoot->Paint(hPaintDC, rc, pStopControl);
  2283. }
  2284. BITMAPINFO bmi = { 0 };
  2285. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  2286. bmi.bmiHeader.biWidth = cx;
  2287. bmi.bmiHeader.biHeight = cy;
  2288. bmi.bmiHeader.biPlanes = 1;
  2289. bmi.bmiHeader.biBitCount = 32;
  2290. bmi.bmiHeader.biCompression = BI_RGB;
  2291. bmi.bmiHeader.biSizeImage = cx * cy * sizeof(DWORD);
  2292. LPDWORD pDest = NULL;
  2293. HDC hCloneDC = ::CreateCompatibleDC(pManager->GetPaintDC());
  2294. HBITMAP hBitmap = ::CreateDIBSection(pManager->GetPaintDC(), &bmi, DIB_RGB_COLORS, (LPVOID*) &pDest, NULL, 0);
  2295. ASSERT(hCloneDC);
  2296. ASSERT(hBitmap);
  2297. if( hBitmap != NULL )
  2298. {
  2299. HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(hCloneDC, hBitmap);
  2300. ::BitBlt(hCloneDC, 0, 0, cx, cy, hPaintDC, rc.left, rc.top, SRCCOPY);
  2301. RECT rcClone = {0, 0, cx, cy};
  2302. if (dwFilterColor > 0x00FFFFFF) DrawColor(hCloneDC, rcClone, dwFilterColor);
  2303. ::SelectObject(hCloneDC, hOldBitmap);
  2304. ::DeleteDC(hCloneDC);
  2305. ::GdiFlush();
  2306. }
  2307. // Cleanup
  2308. ::SelectObject(hPaintDC, hOldPaintBitmap);
  2309. if (!bUseOffscreenBitmap) ::DeleteObject(hPaintBitmap);
  2310. ::DeleteDC(hPaintDC);
  2311. return hBitmap;
  2312. }
  2313. HBITMAP CRenderEngine::GenerateBitmap(CPaintManagerUI* pManager, CControlUI* pControl, RECT rc, DWORD dwFilterColor)
  2314. {
  2315. if (pManager == NULL || pControl == NULL) return NULL;
  2316. int cx = rc.right - rc.left;
  2317. int cy = rc.bottom - rc.top;
  2318. HDC hPaintDC = ::CreateCompatibleDC(pManager->GetPaintDC());
  2319. HBITMAP hPaintBitmap = ::CreateCompatibleBitmap(pManager->GetPaintDC(), rc.right, rc.bottom);
  2320. ASSERT(hPaintDC);
  2321. ASSERT(hPaintBitmap);
  2322. HBITMAP hOldPaintBitmap = (HBITMAP) ::SelectObject(hPaintDC, hPaintBitmap);
  2323. pControl->Paint(hPaintDC, rc, NULL);
  2324. BITMAPINFO bmi = { 0 };
  2325. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  2326. bmi.bmiHeader.biWidth = cx;
  2327. bmi.bmiHeader.biHeight = cy;
  2328. bmi.bmiHeader.biPlanes = 1;
  2329. bmi.bmiHeader.biBitCount = 32;
  2330. bmi.bmiHeader.biCompression = BI_RGB;
  2331. bmi.bmiHeader.biSizeImage = cx * cy * sizeof(DWORD);
  2332. LPDWORD pDest = NULL;
  2333. HDC hCloneDC = ::CreateCompatibleDC(pManager->GetPaintDC());
  2334. HBITMAP hBitmap = ::CreateDIBSection(pManager->GetPaintDC(), &bmi, DIB_RGB_COLORS, (LPVOID*) &pDest, NULL, 0);
  2335. ASSERT(hCloneDC);
  2336. ASSERT(hBitmap);
  2337. if( hBitmap != NULL )
  2338. {
  2339. HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(hCloneDC, hBitmap);
  2340. ::BitBlt(hCloneDC, 0, 0, cx, cy, hPaintDC, rc.left, rc.top, SRCCOPY);
  2341. RECT rcClone = {0, 0, cx, cy};
  2342. if (dwFilterColor > 0x00FFFFFF) DrawColor(hCloneDC, rcClone, dwFilterColor);
  2343. ::SelectObject(hCloneDC, hOldBitmap);
  2344. ::DeleteDC(hCloneDC);
  2345. ::GdiFlush();
  2346. }
  2347. // Cleanup
  2348. ::SelectObject(hPaintDC, hOldPaintBitmap);
  2349. ::DeleteObject(hPaintBitmap);
  2350. ::DeleteDC(hPaintDC);
  2351. return hBitmap;
  2352. }
  2353. SIZE CRenderEngine::GetTextSize( HDC hDC, CPaintManagerUI* pManager , LPCTSTR pstrText, int iFont, UINT uStyle )
  2354. {
  2355. SIZE size = {0,0};
  2356. ASSERT(::GetObjectType(hDC)==OBJ_DC || ::GetObjectType(hDC)==OBJ_MEMDC);
  2357. if( pstrText == NULL || pManager == NULL ) return size;
  2358. ::SetBkMode(hDC, TRANSPARENT);
  2359. HFONT hOldFont = (HFONT)::SelectObject(hDC, pManager->GetFont(iFont));
  2360. GetTextExtentPoint32(hDC, pstrText, _tcslen(pstrText) , &size);
  2361. ::SelectObject(hDC, hOldFont);
  2362. return size;
  2363. }
  2364. void CRenderEngine::CheckAlphaColor(DWORD& dwColor)
  2365. {
  2366. //RestoreAlphaColor认为0x00000000是真正的透明,其它都是GDI绘制导致的
  2367. //所以在GDI绘制中不能用0xFF000000这个颜色值,现在处理是让它变成RGB(0,0,1)
  2368. //RGB(0,0,1)与RGB(0,0,0)很难分出来
  2369. if((0x00FFFFFF & dwColor) == 0)
  2370. {
  2371. dwColor += 1;
  2372. }
  2373. }
  2374. DWORD CRenderEngine::AdjustColor(DWORD dwColor, short H, short S, short L)
  2375. {
  2376. if( H == 180 && S == 100 && L == 100 ) return dwColor;
  2377. float fH, fS, fL;
  2378. float S1 = S / 100.0f;
  2379. float L1 = L / 100.0f;
  2380. RGBtoHSL(dwColor, &fH, &fS, &fL);
  2381. fH += (H - 180);
  2382. fH = fH > 0 ? fH : fH + 360;
  2383. fS *= S1;
  2384. fL *= L1;
  2385. HSLtoRGB(&dwColor, fH, fS, fL);
  2386. return dwColor;
  2387. }
  2388. HBITMAP CRenderEngine::CreateARGB32Bitmap(HDC hDC, int cx, int cy, BYTE** pBits)
  2389. {
  2390. LPBITMAPINFO lpbiSrc = NULL;
  2391. lpbiSrc = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER)];
  2392. if (lpbiSrc == NULL) return NULL;
  2393. lpbiSrc->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  2394. lpbiSrc->bmiHeader.biWidth = cx;
  2395. lpbiSrc->bmiHeader.biHeight = cy;
  2396. lpbiSrc->bmiHeader.biPlanes = 1;
  2397. lpbiSrc->bmiHeader.biBitCount = 32;
  2398. lpbiSrc->bmiHeader.biCompression = BI_RGB;
  2399. lpbiSrc->bmiHeader.biSizeImage = abs(cx * cy);
  2400. lpbiSrc->bmiHeader.biXPelsPerMeter = 0;
  2401. lpbiSrc->bmiHeader.biYPelsPerMeter = 0;
  2402. lpbiSrc->bmiHeader.biClrUsed = 0;
  2403. lpbiSrc->bmiHeader.biClrImportant = 0;
  2404. HBITMAP hBitmap = CreateDIBSection (hDC, lpbiSrc, DIB_RGB_COLORS, (void **)pBits, NULL, NULL);
  2405. delete [] lpbiSrc;
  2406. return hBitmap;
  2407. }
  2408. void CRenderEngine::AdjustImage(bool bUseHSL, TImageInfo* imageInfo, short H, short S, short L)
  2409. {
  2410. if( imageInfo == NULL || imageInfo->bUseHSL == false || imageInfo->hBitmap == NULL ||
  2411. imageInfo->pBits == NULL || imageInfo->pSrcBits == NULL )
  2412. return;
  2413. if( bUseHSL == false || (H == 180 && S == 100 && L == 100)) {
  2414. ::CopyMemory(imageInfo->pBits, imageInfo->pSrcBits, imageInfo->nX * imageInfo->nY * 4);
  2415. return;
  2416. }
  2417. float fH, fS, fL;
  2418. float S1 = S / 100.0f;
  2419. float L1 = L / 100.0f;
  2420. for( int i = 0; i < imageInfo->nX * imageInfo->nY; i++ ) {
  2421. RGBtoHSL(*(DWORD*)(imageInfo->pSrcBits + i*4), &fH, &fS, &fL);
  2422. fH += (H - 180);
  2423. fH = fH > 0 ? fH : fH + 360;
  2424. fS *= S1;
  2425. fL *= L1;
  2426. HSLtoRGB((DWORD*)(imageInfo->pBits + i*4), fH, fS, fL);
  2427. }
  2428. }
  2429. } // namespace DuiLib