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