UIManager.cpp 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589
  1. #include "StdAfx.h"
  2. #include <zmouse.h>
  3. #pragma warning(push)
  4. #pragma warning(disable:4838 4244)
  5. namespace DuiLib
  6. {
  7. /////////////////////////////////////////////////////////////////////////////////////
  8. //
  9. //
  10. static void GetChildWndRect(HWND hWnd, HWND hChildWnd, RECT& rcChildWnd)
  11. {
  12. ::GetWindowRect(hChildWnd, &rcChildWnd);
  13. POINT pt;
  14. pt.x = rcChildWnd.left;
  15. pt.y = rcChildWnd.top;
  16. ::ScreenToClient(hWnd, &pt);
  17. rcChildWnd.left = pt.x;
  18. rcChildWnd.top = pt.y;
  19. pt.x = rcChildWnd.right;
  20. pt.y = rcChildWnd.bottom;
  21. ::ScreenToClient(hWnd, &pt);
  22. rcChildWnd.right = pt.x;
  23. rcChildWnd.bottom = pt.y;
  24. }
  25. static UINT MapKeyState()
  26. {
  27. UINT uState = 0;
  28. if (::GetKeyState(VK_CONTROL) < 0) uState |= MK_CONTROL;
  29. if (::GetKeyState(VK_LBUTTON) < 0) uState |= MK_LBUTTON;
  30. if (::GetKeyState(VK_RBUTTON) < 0) uState |= MK_RBUTTON;
  31. if (::GetKeyState(VK_SHIFT) < 0) uState |= MK_SHIFT;
  32. if (::GetKeyState(VK_MENU) < 0) uState |= MK_ALT;
  33. return uState;
  34. }
  35. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  36. ///
  37. typedef struct tagFINDTABINFO
  38. {
  39. CControlUI* pFocus;
  40. CControlUI* pLast;
  41. bool bForward;
  42. bool bNextIsIt;
  43. } FINDTABINFO;
  44. typedef struct tagFINDSHORTCUT
  45. {
  46. TCHAR ch;
  47. bool bPickNext;
  48. } FINDSHORTCUT;
  49. typedef struct tagTIMERINFO
  50. {
  51. CControlUI* pSender;
  52. UINT nLocalID;
  53. HWND hWnd;
  54. UINT uWinTimer;
  55. bool bKilled;
  56. } TIMERINFO;
  57. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. ///
  59. tagTImageInfo::tagTImageInfo()
  60. {
  61. pImage = NULL;
  62. hBitmap = NULL;
  63. }
  64. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  65. ///
  66. tagTDrawInfo::tagTDrawInfo()
  67. {
  68. Clear();
  69. }
  70. void tagTDrawInfo::Parse(LPCTSTR pStrImage, LPCTSTR pStrModify, CPaintManagerUI* pManager)
  71. {
  72. // 1、aaa.jpg
  73. // 2、file='aaa.jpg' res='' restype='0' dest='0,0,0,0' source='0,0,0,0' corner='0,0,0,0'
  74. // mask='#FF0000' fade='255' hole='false' xtiled='false' ytiled='false'
  75. sDrawString = pStrImage;
  76. sDrawModify = pStrModify;
  77. sImageName = pStrImage;
  78. CDuiString sItem;
  79. CDuiString sValue;
  80. LPTSTR pstr = NULL;
  81. for (int i = 0; i < 2; ++i)
  82. {
  83. if (i == 1) pStrImage = pStrModify;
  84. if (!pStrImage) continue;
  85. while (*pStrImage != _T('\0'))
  86. {
  87. sItem.Empty();
  88. sValue.Empty();
  89. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  90. while (*pStrImage != _T('\0') && *pStrImage != _T('=') && *pStrImage > _T(' '))
  91. {
  92. LPTSTR pstrTemp = ::CharNext(pStrImage);
  93. while (pStrImage < pstrTemp)
  94. {
  95. sItem += *pStrImage++;
  96. }
  97. }
  98. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  99. if (*pStrImage++ != _T('=')) break;
  100. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  101. if (*pStrImage++ != _T('\'')) break;
  102. while (*pStrImage != _T('\0') && *pStrImage != _T('\''))
  103. {
  104. LPTSTR pstrTemp = ::CharNext(pStrImage);
  105. while (pStrImage < pstrTemp)
  106. {
  107. sValue += *pStrImage++;
  108. }
  109. }
  110. if (*pStrImage++ != _T('\'')) break;
  111. if (!sValue.IsEmpty())
  112. {
  113. if (sItem == _T("file") || sItem == _T("res"))
  114. {
  115. sImageName = sValue;
  116. }
  117. else if (sItem == _T("restype"))
  118. {
  119. sResType = sValue;
  120. }
  121. else if (sItem == _T("dest"))
  122. {
  123. rcDest.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
  124. rcDest.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  125. rcDest.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  126. rcDest.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  127. //if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcDest);
  128. }
  129. else if (sItem == _T("source"))
  130. {
  131. rcSource.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
  132. rcSource.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  133. rcSource.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  134. rcSource.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  135. //if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcSource);
  136. }
  137. else if (sItem == _T("corner"))
  138. {
  139. rcCorner.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
  140. rcCorner.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  141. rcCorner.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  142. rcCorner.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  143. //if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcCorner);
  144. }
  145. else if (sItem == _T("mask"))
  146. {
  147. if (sValue[0] == _T('#')) dwMask = _tcstoul(sValue.GetData() + 1, &pstr, 16);
  148. else dwMask = _tcstoul(sValue.GetData(), &pstr, 16);
  149. }
  150. else if (sItem == _T("fade"))
  151. {
  152. uFade = (UINT)_tcstoul(sValue.GetData(), &pstr, 10);
  153. }
  154. else if (sItem == _T("rotate"))
  155. {
  156. uRotate = (UINT)_tcstoul(sValue.GetData(), &pstr, 10);
  157. bGdiplus = true;
  158. }
  159. else if (sItem == _T("gdiplus"))
  160. {
  161. bGdiplus = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
  162. }
  163. else if (sItem == _T("hole"))
  164. {
  165. bHole = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
  166. }
  167. else if (sItem == _T("xtiled"))
  168. {
  169. bTiledX = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
  170. }
  171. else if (sItem == _T("ytiled"))
  172. {
  173. bTiledY = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
  174. }
  175. else if (sItem == _T("hsl"))
  176. {
  177. bHSL = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
  178. }
  179. else if (sItem == _T("size"))
  180. {
  181. szImage.cx = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
  182. szImage.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  183. }
  184. else if (sItem == _T("align"))
  185. {
  186. sAlign = sValue;
  187. }
  188. else if (sItem == _T("padding"))
  189. {
  190. rcPadding.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
  191. rcPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  192. rcPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  193. rcPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  194. //if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcPadding);
  195. }
  196. }
  197. if (*pStrImage++ != _T(' ')) break;
  198. }
  199. }
  200. // 调整DPI资源
  201. if (pManager != NULL && pManager->GetDPIObj()->GetScale() != 100)
  202. {
  203. CDuiString sScale;
  204. sScale.Format(_T("@%d."), pManager->GetDPIObj()->GetScale());
  205. sImageName.Replace(_T("."), sScale);
  206. }
  207. }
  208. void tagTDrawInfo::Clear()
  209. {
  210. sDrawString.Empty();
  211. sDrawModify.Empty();
  212. sImageName.Empty();
  213. memset(&rcDest, 0, sizeof(RECT));
  214. memset(&rcSource, 0, sizeof(RECT));
  215. memset(&rcCorner, 0, sizeof(RECT));
  216. dwMask = 0;
  217. uFade = 255;
  218. uRotate = 0;
  219. bHole = false;
  220. bTiledX = false;
  221. bTiledY = false;
  222. bHSL = false;
  223. bGdiplus = false;
  224. szImage.cx = szImage.cy = 0;
  225. sAlign.Empty();
  226. memset(&rcPadding, 0, sizeof(RECT));
  227. }
  228. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  229. ///
  230. typedef BOOL(__stdcall* PFUNCUPDATELAYEREDWINDOW)(HWND, HDC, POINT*, SIZE*, HDC, POINT*, COLORREF, BLENDFUNCTION*, DWORD);
  231. PFUNCUPDATELAYEREDWINDOW g_fUpdateLayeredWindow = NULL;
  232. HPEN m_hUpdateRectPen = NULL;
  233. HINSTANCE CPaintManagerUI::m_hResourceInstance = NULL;
  234. CDuiString CPaintManagerUI::m_pStrResourcePath;
  235. CDuiString CPaintManagerUI::m_pStrResourceZip;
  236. CDuiString CPaintManagerUI::m_pStrResourceZipPwd; //Garfield 20160325 带密码zip包解密
  237. HANDLE CPaintManagerUI::m_hResourceZip = NULL;
  238. bool CPaintManagerUI::m_bCachedResourceZip = true;
  239. BYTE* CPaintManagerUI::m_cbZipBuf = nullptr;
  240. int CPaintManagerUI::m_nResType = UILIB_FILE;
  241. TResInfo CPaintManagerUI::m_SharedResInfo;
  242. HINSTANCE CPaintManagerUI::m_hInstance = NULL;
  243. bool CPaintManagerUI::m_bUseHSL = false;
  244. short CPaintManagerUI::m_H = 180;
  245. short CPaintManagerUI::m_S = 100;
  246. short CPaintManagerUI::m_L = 100;
  247. CStdPtrArray CPaintManagerUI::m_aPreMessages;
  248. CStdPtrArray CPaintManagerUI::m_aPlugins;
  249. CPaintManagerUI::CPaintManagerUI() :
  250. m_hWndPaint(NULL),
  251. m_hDcPaint(NULL),
  252. m_hDcOffscreen(NULL),
  253. m_hDcBackground(NULL),
  254. m_bOffscreenPaint(true),
  255. m_hbmpOffscreen(NULL),
  256. m_pOffscreenBits(NULL),
  257. m_hbmpBackground(NULL),
  258. m_pBackgroundBits(NULL),
  259. m_hwndTooltip(NULL),
  260. m_uTimerID(0x1000),
  261. m_pRoot(NULL),
  262. m_pFocus(NULL),
  263. m_pEventHover(NULL),
  264. m_pEventClick(NULL),
  265. m_pEventRClick(NULL),
  266. m_pEventKey(NULL),
  267. m_bFirstLayout(true),
  268. m_bFocusNeeded(false),
  269. m_bUpdateNeeded(false),
  270. m_bMouseTracking(false),
  271. m_bMouseCapture(false),
  272. m_bAsyncNotifyPosted(false),
  273. m_bUsedVirtualWnd(false),
  274. m_bForceUseSharedRes(false),
  275. m_nOpacity(0xFF),
  276. m_bLayered(false),
  277. m_bLayeredChanged(false),
  278. m_bShowUpdateRect(false),
  279. m_bUseGdiplusText(false),
  280. m_trh(0),
  281. m_bDragDrop(false),
  282. m_bDragMode(false),
  283. m_hDragBitmap(NULL),
  284. m_pDPI(NULL),
  285. m_iHoverTime(400UL)
  286. {
  287. if (m_SharedResInfo.m_DefaultFontInfo.sFontName.IsEmpty())
  288. {
  289. m_SharedResInfo.m_dwDefaultDisabledColor = 0xFFA7A6AA;
  290. m_SharedResInfo.m_dwDefaultFontColor = 0xFF000000;
  291. m_SharedResInfo.m_dwDefaultLinkFontColor = 0xFF0000FF;
  292. m_SharedResInfo.m_dwDefaultLinkHoverFontColor = 0xFFD3215F;
  293. m_SharedResInfo.m_dwDefaultSelectedBkColor = 0xFFBAE4FF;
  294. LOGFONT lf = { 0 };
  295. ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
  296. lf.lfCharSet = DEFAULT_CHARSET;
  297. HFONT hDefaultFont = ::CreateFontIndirect(&lf);
  298. m_SharedResInfo.m_DefaultFontInfo.hFont = hDefaultFont;
  299. m_SharedResInfo.m_DefaultFontInfo.sFontName = lf.lfFaceName;
  300. m_SharedResInfo.m_DefaultFontInfo.iSize = -lf.lfHeight;
  301. m_SharedResInfo.m_DefaultFontInfo.bBold = (lf.lfWeight >= FW_BOLD);
  302. m_SharedResInfo.m_DefaultFontInfo.bUnderline = (lf.lfUnderline == TRUE);
  303. m_SharedResInfo.m_DefaultFontInfo.bItalic = (lf.lfItalic == TRUE);
  304. ::ZeroMemory(&m_SharedResInfo.m_DefaultFontInfo.tm, sizeof(m_SharedResInfo.m_DefaultFontInfo.tm));
  305. }
  306. m_ResInfo.m_dwDefaultDisabledColor = m_SharedResInfo.m_dwDefaultDisabledColor;
  307. m_ResInfo.m_dwDefaultFontColor = m_SharedResInfo.m_dwDefaultFontColor;
  308. m_ResInfo.m_dwDefaultLinkFontColor = m_SharedResInfo.m_dwDefaultLinkFontColor;
  309. m_ResInfo.m_dwDefaultLinkHoverFontColor = m_SharedResInfo.m_dwDefaultLinkHoverFontColor;
  310. m_ResInfo.m_dwDefaultSelectedBkColor = m_SharedResInfo.m_dwDefaultSelectedBkColor;
  311. if (m_hUpdateRectPen == NULL)
  312. {
  313. m_hUpdateRectPen = ::CreatePen(PS_SOLID, 1, RGB(220, 0, 0));
  314. // Boot Windows Common Controls (for the ToolTip control)
  315. ::InitCommonControls();
  316. ::LoadLibrary(_T("msimg32.dll"));
  317. }
  318. m_szMinWindow.cx = 0;
  319. m_szMinWindow.cy = 0;
  320. m_szMaxWindow.cx = 0;
  321. m_szMaxWindow.cy = 0;
  322. m_szInitWindowSize.cx = 0;
  323. m_szInitWindowSize.cy = 0;
  324. m_szRoundCorner.cx = m_szRoundCorner.cy = 0;
  325. ::ZeroMemory(&m_rcSizeBox, sizeof(m_rcSizeBox));
  326. ::ZeroMemory(&m_rcCaption, sizeof(m_rcCaption));
  327. ::ZeroMemory(&m_rcLayeredInset, sizeof(m_rcLayeredInset));
  328. ::ZeroMemory(&m_rcLayeredUpdate, sizeof(m_rcLayeredUpdate));
  329. m_ptLastMousePos.x = m_ptLastMousePos.y = -1;
  330. m_pGdiplusStartupInput = new Gdiplus::GdiplusStartupInput;
  331. Gdiplus::GdiplusStartup(&m_gdiplusToken, m_pGdiplusStartupInput, NULL); // 加载GDI接口
  332. CShadowUI::Initialize(m_hInstance);
  333. m_pDragDrop = NULL;
  334. }
  335. CPaintManagerUI::~CPaintManagerUI()
  336. {
  337. // Delete the control-tree structures
  338. for (int i = 0; i < m_aDelayedCleanup.GetSize(); i++) delete static_cast<CControlUI*>(m_aDelayedCleanup[i]);
  339. m_aDelayedCleanup.Resize(0);
  340. for (int i = 0; i < m_aAsyncNotify.GetSize(); i++) delete static_cast<TNotifyUI*>(m_aAsyncNotify[i]);
  341. m_aAsyncNotify.Resize(0);
  342. m_mNameHash.Resize(0);
  343. if (m_pRoot != NULL) delete m_pRoot;
  344. ::DeleteObject(m_ResInfo.m_DefaultFontInfo.hFont);
  345. RemoveAllFonts();
  346. RemoveAllImages();
  347. RemoveAllStyle();
  348. RemoveAllDefaultAttributeList();
  349. RemoveAllWindowCustomAttribute();
  350. RemoveAllOptionGroups();
  351. RemoveAllTimers();
  352. RemoveAllDrawInfos();
  353. if (m_hwndTooltip != NULL)
  354. {
  355. ::DestroyWindow(m_hwndTooltip);
  356. m_hwndTooltip = NULL;
  357. }
  358. if (!m_aFonts.IsEmpty())
  359. {
  360. for (int i = 0; i < m_aFonts.GetSize(); ++i)
  361. {
  362. HANDLE handle = static_cast<HANDLE>(m_aFonts.GetAt(i));
  363. ::RemoveFontMemResourceEx(handle);
  364. }
  365. }
  366. if (m_hDcOffscreen != NULL) ::DeleteDC(m_hDcOffscreen);
  367. if (m_hDcBackground != NULL) ::DeleteDC(m_hDcBackground);
  368. if (m_hbmpOffscreen != NULL) ::DeleteObject(m_hbmpOffscreen);
  369. if (m_hbmpBackground != NULL) ::DeleteObject(m_hbmpBackground);
  370. if (m_hDcPaint != NULL) ::ReleaseDC(m_hWndPaint, m_hDcPaint);
  371. m_aPreMessages.Remove(m_aPreMessages.Find(this));
  372. // 销毁拖拽图片
  373. if (m_hDragBitmap != NULL) ::DeleteObject(m_hDragBitmap);
  374. //卸载GDIPlus
  375. Gdiplus::GdiplusShutdown(m_gdiplusToken);
  376. delete m_pGdiplusStartupInput;
  377. // DPI管理对象
  378. if (m_pDPI != NULL)
  379. {
  380. delete m_pDPI;
  381. m_pDPI = NULL;
  382. }
  383. }
  384. void CPaintManagerUI::Init(HWND hWnd, LPCTSTR pstrName)
  385. {
  386. ASSERT(::IsWindow(hWnd));
  387. m_mNameHash.Resize();
  388. RemoveAllFonts();
  389. RemoveAllImages();
  390. RemoveAllStyle();
  391. RemoveAllDefaultAttributeList();
  392. RemoveAllWindowCustomAttribute();
  393. RemoveAllOptionGroups();
  394. RemoveAllTimers();
  395. m_sName.Empty();
  396. if (pstrName != NULL) m_sName = pstrName;
  397. if (m_hWndPaint != hWnd)
  398. {
  399. m_hWndPaint = hWnd;
  400. m_hDcPaint = ::GetDC(hWnd);
  401. m_aPreMessages.Add(this);
  402. }
  403. }
  404. void CPaintManagerUI::DeletePtr(void* ptr)
  405. {
  406. if (ptr)
  407. {
  408. delete ptr; ptr = NULL;
  409. }
  410. }
  411. HINSTANCE CPaintManagerUI::GetInstance()
  412. {
  413. return m_hInstance;
  414. }
  415. CDuiString CPaintManagerUI::GetInstancePath()
  416. {
  417. if (m_hInstance == NULL) return _T('\0');
  418. TCHAR tszModule[MAX_PATH + 1] = { 0 };
  419. ::GetModuleFileName(m_hInstance, tszModule, MAX_PATH);
  420. CDuiString sInstancePath = tszModule;
  421. int pos = sInstancePath.ReverseFind(_T('\\'));
  422. if (pos >= 0) sInstancePath = sInstancePath.Left(pos + 1);
  423. return sInstancePath;
  424. }
  425. CDuiString CPaintManagerUI::GetCurrentPath()
  426. {
  427. TCHAR tszModule[MAX_PATH + 1] = { 0 };
  428. ::GetCurrentDirectory(MAX_PATH, tszModule);
  429. return tszModule;
  430. }
  431. HINSTANCE CPaintManagerUI::GetResourceDll()
  432. {
  433. if (m_hResourceInstance == NULL) return m_hInstance;
  434. return m_hResourceInstance;
  435. }
  436. const CDuiString& CPaintManagerUI::GetResourcePath()
  437. {
  438. return m_pStrResourcePath;
  439. }
  440. const CDuiString& CPaintManagerUI::GetResourceZip()
  441. {
  442. return m_pStrResourceZip;
  443. }
  444. const CDuiString& CPaintManagerUI::GetResourceZipPwd()
  445. {
  446. return m_pStrResourceZipPwd;
  447. }
  448. bool CPaintManagerUI::IsCachedResourceZip()
  449. {
  450. return m_bCachedResourceZip;
  451. }
  452. HANDLE CPaintManagerUI::GetResourceZipHandle()
  453. {
  454. return m_hResourceZip;
  455. }
  456. void CPaintManagerUI::SetInstance(HINSTANCE hInst)
  457. {
  458. m_hInstance = hInst;
  459. }
  460. void CPaintManagerUI::SetCurrentPath(LPCTSTR pStrPath)
  461. {
  462. ::SetCurrentDirectory(pStrPath);
  463. }
  464. void CPaintManagerUI::SetResourceDll(HINSTANCE hInst)
  465. {
  466. m_hResourceInstance = hInst;
  467. }
  468. void CPaintManagerUI::SetResourcePath(LPCTSTR pStrPath)
  469. {
  470. m_pStrResourcePath = pStrPath;
  471. if (m_pStrResourcePath.IsEmpty()) return;
  472. TCHAR cEnd = m_pStrResourcePath.GetAt(m_pStrResourcePath.GetLength() - 1);
  473. if (cEnd != _T('\\') && cEnd != _T('/')) m_pStrResourcePath += _T('\\');
  474. }
  475. void CPaintManagerUI::SetResourceZip(LPVOID pVoid, unsigned int len, LPCTSTR password)
  476. {
  477. if (m_pStrResourceZip == _T("membuffer")) return;
  478. if (m_bCachedResourceZip && m_hResourceZip != NULL)
  479. {
  480. CloseZip((HZIP)m_hResourceZip);
  481. m_hResourceZip = NULL;
  482. }
  483. m_pStrResourceZip = _T("membuffer");
  484. if (m_cbZipBuf)
  485. {
  486. delete[] m_cbZipBuf;
  487. m_cbZipBuf = nullptr;
  488. }
  489. if (!m_cbZipBuf)
  490. {
  491. m_cbZipBuf = new BYTE[len];
  492. memcpy(m_cbZipBuf, pVoid, len);
  493. }
  494. m_bCachedResourceZip = true;
  495. m_pStrResourceZipPwd = password; //Garfield 20160325 带密码zip包解密
  496. if (m_bCachedResourceZip)
  497. {
  498. #ifdef UNICODE
  499. char* pwd = w2a((wchar_t*)password);
  500. m_hResourceZip = (HANDLE)OpenZip(m_cbZipBuf, len, pwd);
  501. if (pwd)
  502. {
  503. delete[] pwd;
  504. pwd = NULL;
  505. }
  506. #else
  507. m_hResourceZip = (HANDLE)OpenZip(m_cbZipBuf, len, password);
  508. #endif
  509. }
  510. }
  511. void CPaintManagerUI::SetResourceZip(LPCTSTR pStrPath, bool bCachedResourceZip, LPCTSTR password)
  512. {
  513. if (m_pStrResourceZip == pStrPath && m_bCachedResourceZip == bCachedResourceZip) return;
  514. if (m_bCachedResourceZip && m_hResourceZip != NULL)
  515. {
  516. CloseZip((HZIP)m_hResourceZip);
  517. m_hResourceZip = NULL;
  518. }
  519. m_pStrResourceZip = pStrPath;
  520. m_bCachedResourceZip = bCachedResourceZip;
  521. m_pStrResourceZipPwd = password;
  522. if (m_bCachedResourceZip)
  523. {
  524. CDuiString sFile = CPaintManagerUI::GetResourcePath();
  525. sFile += CPaintManagerUI::GetResourceZip();
  526. #ifdef UNICODE
  527. char* pwd = w2a((wchar_t*)password);
  528. m_hResourceZip = (HANDLE)OpenZip(sFile.GetData(), pwd);
  529. if (pwd)
  530. {
  531. delete[] pwd;
  532. pwd = NULL;
  533. }
  534. #else
  535. m_hResourceZip = (HANDLE)OpenZip(sFile.GetData(), password);
  536. #endif
  537. }
  538. }
  539. void CPaintManagerUI::SetResourceType(int nType)
  540. {
  541. m_nResType = nType;
  542. }
  543. int CPaintManagerUI::GetResourceType()
  544. {
  545. return m_nResType;
  546. }
  547. bool CPaintManagerUI::GetHSL(short* H, short* S, short* L)
  548. {
  549. *H = m_H;
  550. *S = m_S;
  551. *L = m_L;
  552. return m_bUseHSL;
  553. }
  554. void CPaintManagerUI::SetHSL(bool bUseHSL, short H, short S, short L)
  555. {
  556. if (m_bUseHSL || m_bUseHSL != bUseHSL)
  557. {
  558. m_bUseHSL = bUseHSL;
  559. if (H == m_H && S == m_S && L == m_L) return;
  560. m_H = CLAMP(H, 0, 360);
  561. m_S = CLAMP(S, 0, 200);
  562. m_L = CLAMP(L, 0, 200);
  563. AdjustSharedImagesHSL();
  564. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  565. {
  566. CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  567. if (pManager != NULL) pManager->AdjustImagesHSL();
  568. }
  569. }
  570. }
  571. void CPaintManagerUI::ReloadSkin()
  572. {
  573. ReloadSharedImages();
  574. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  575. {
  576. CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  577. pManager->ReloadImages();
  578. }
  579. }
  580. CPaintManagerUI* CPaintManagerUI::GetPaintManager(LPCTSTR pstrName)
  581. {
  582. if (pstrName == NULL) return NULL;
  583. CDuiString sName = pstrName;
  584. if (sName.IsEmpty()) return NULL;
  585. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  586. {
  587. CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  588. if (pManager != NULL && sName == pManager->GetName()) return pManager;
  589. }
  590. return NULL;
  591. }
  592. CStdPtrArray* CPaintManagerUI::GetPaintManagers()
  593. {
  594. return &m_aPreMessages;
  595. }
  596. bool CPaintManagerUI::LoadPlugin(LPCTSTR pstrModuleName)
  597. {
  598. ASSERT(!::IsBadStringPtr(pstrModuleName, -1) || pstrModuleName == NULL);
  599. if (pstrModuleName == NULL) return false;
  600. HMODULE hModule = ::LoadLibrary(pstrModuleName);
  601. if (hModule != NULL)
  602. {
  603. LPCREATECONTROL lpCreateControl = (LPCREATECONTROL)::GetProcAddress(hModule, "CreateControl");
  604. if (lpCreateControl != NULL)
  605. {
  606. if (m_aPlugins.Find(lpCreateControl) >= 0) return true;
  607. m_aPlugins.Add(lpCreateControl);
  608. return true;
  609. }
  610. }
  611. return false;
  612. }
  613. CStdPtrArray* CPaintManagerUI::GetPlugins()
  614. {
  615. return &m_aPlugins;
  616. }
  617. HWND CPaintManagerUI::GetPaintWindow() const
  618. {
  619. return m_hWndPaint;
  620. }
  621. HWND CPaintManagerUI::GetTooltipWindow() const
  622. {
  623. return m_hwndTooltip;
  624. }
  625. int CPaintManagerUI::GetHoverTime() const
  626. {
  627. return m_iHoverTime;
  628. }
  629. void CPaintManagerUI::SetHoverTime(int iTime)
  630. {
  631. m_iHoverTime = iTime;
  632. }
  633. LPCTSTR CPaintManagerUI::GetName() const
  634. {
  635. return m_sName;
  636. }
  637. HDC CPaintManagerUI::GetPaintDC() const
  638. {
  639. return m_hDcPaint;
  640. }
  641. POINT CPaintManagerUI::GetMousePos() const
  642. {
  643. return m_ptLastMousePos;
  644. }
  645. SIZE CPaintManagerUI::GetClientSize() const
  646. {
  647. RECT rcClient = { 0 };
  648. ::GetClientRect(m_hWndPaint, &rcClient);
  649. return CDuiSize(rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
  650. }
  651. SIZE CPaintManagerUI::GetInitSize()
  652. {
  653. return m_szInitWindowSize;
  654. }
  655. void CPaintManagerUI::SetInitSize(int cx, int cy)
  656. {
  657. m_szInitWindowSize.cx = cx;
  658. m_szInitWindowSize.cy = cy;
  659. if (m_pRoot == NULL && m_hWndPaint != NULL)
  660. {
  661. ::SetWindowPos(m_hWndPaint, NULL, 0, 0, cx, cy, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
  662. }
  663. }
  664. RECT CPaintManagerUI::GetSizeBox()
  665. {
  666. return GetDPIObj()->Scale(m_rcSizeBox);
  667. }
  668. void CPaintManagerUI::SetSizeBox(RECT& rcSizeBox)
  669. {
  670. m_rcSizeBox = rcSizeBox;
  671. }
  672. RECT CPaintManagerUI::GetCaptionRect()
  673. {
  674. return GetDPIObj()->Scale(m_rcCaption);
  675. }
  676. void CPaintManagerUI::SetCaptionRect(RECT& rcCaption)
  677. {
  678. m_rcCaption = rcCaption;
  679. }
  680. SIZE CPaintManagerUI::GetRoundCorner()
  681. {
  682. return GetDPIObj()->Scale(m_szRoundCorner);
  683. }
  684. void CPaintManagerUI::SetRoundCorner(int cx, int cy)
  685. {
  686. m_szRoundCorner.cx = cx;
  687. m_szRoundCorner.cy = cy;
  688. }
  689. SIZE CPaintManagerUI::GetMinInfo()
  690. {
  691. return GetDPIObj()->Scale(m_szMinWindow);
  692. }
  693. void CPaintManagerUI::SetMinInfo(int cx, int cy)
  694. {
  695. ASSERT(cx >= 0 && cy >= 0);
  696. m_szMinWindow.cx = cx;
  697. m_szMinWindow.cy = cy;
  698. }
  699. SIZE CPaintManagerUI::GetMaxInfo()
  700. {
  701. return GetDPIObj()->Scale(m_szMaxWindow);
  702. }
  703. void CPaintManagerUI::SetMaxInfo(int cx, int cy)
  704. {
  705. ASSERT(cx >= 0 && cy >= 0);
  706. m_szMaxWindow.cx = cx;
  707. m_szMaxWindow.cy = cy;
  708. }
  709. bool CPaintManagerUI::IsShowUpdateRect()
  710. {
  711. return m_bShowUpdateRect;
  712. }
  713. void CPaintManagerUI::SetShowUpdateRect(bool show)
  714. {
  715. m_bShowUpdateRect = show;
  716. }
  717. bool CPaintManagerUI::IsNoActivate()
  718. {
  719. return m_bNoActivate;
  720. }
  721. void CPaintManagerUI::SetNoActivate(bool bNoActivate)
  722. {
  723. m_bNoActivate = bNoActivate;
  724. }
  725. BYTE CPaintManagerUI::GetOpacity() const
  726. {
  727. return m_nOpacity;
  728. }
  729. void CPaintManagerUI::SetOpacity(BYTE nOpacity)
  730. {
  731. m_nOpacity = nOpacity;
  732. if (m_hWndPaint != NULL)
  733. {
  734. typedef BOOL(__stdcall* PFUNCSETLAYEREDWINDOWATTR)(HWND, COLORREF, BYTE, DWORD);
  735. PFUNCSETLAYEREDWINDOWATTR fSetLayeredWindowAttributes = NULL;
  736. HMODULE hUser32 = ::GetModuleHandle(_T("User32.dll"));
  737. if (hUser32)
  738. {
  739. fSetLayeredWindowAttributes =
  740. (PFUNCSETLAYEREDWINDOWATTR)::GetProcAddress(hUser32, "SetLayeredWindowAttributes");
  741. if (fSetLayeredWindowAttributes == NULL) return;
  742. }
  743. DWORD dwStyle = ::GetWindowLong(m_hWndPaint, GWL_EXSTYLE);
  744. DWORD dwNewStyle = dwStyle;
  745. if (nOpacity >= 0 && nOpacity < 256) dwNewStyle |= WS_EX_LAYERED;
  746. else dwNewStyle &= ~WS_EX_LAYERED;
  747. if (dwStyle != dwNewStyle) ::SetWindowLong(m_hWndPaint, GWL_EXSTYLE, dwNewStyle);
  748. fSetLayeredWindowAttributes(m_hWndPaint, 0, nOpacity, LWA_ALPHA);
  749. }
  750. }
  751. bool CPaintManagerUI::IsLayered()
  752. {
  753. return m_bLayered;
  754. }
  755. void CPaintManagerUI::SetLayered(bool bLayered)
  756. {
  757. if (m_hWndPaint != NULL && bLayered != m_bLayered)
  758. {
  759. UINT uStyle = GetWindowStyle(m_hWndPaint);
  760. if ((uStyle & WS_CHILD) != 0) return;
  761. if (g_fUpdateLayeredWindow == NULL)
  762. {
  763. HMODULE hUser32 = ::GetModuleHandle(_T("User32.dll"));
  764. if (hUser32)
  765. {
  766. g_fUpdateLayeredWindow =
  767. (PFUNCUPDATELAYEREDWINDOW)::GetProcAddress(hUser32, "UpdateLayeredWindow");
  768. if (g_fUpdateLayeredWindow == NULL) return;
  769. }
  770. }
  771. m_bLayered = bLayered;
  772. if (m_pRoot != NULL) m_pRoot->NeedUpdate();
  773. Invalidate();
  774. }
  775. }
  776. RECT& CPaintManagerUI::GetLayeredInset()
  777. {
  778. return m_rcLayeredInset;
  779. }
  780. void CPaintManagerUI::SetLayeredInset(RECT& rcLayeredInset)
  781. {
  782. m_rcLayeredInset = rcLayeredInset;
  783. m_bLayeredChanged = true;
  784. Invalidate();
  785. }
  786. BYTE CPaintManagerUI::GetLayeredOpacity()
  787. {
  788. return m_nOpacity;
  789. }
  790. void CPaintManagerUI::SetLayeredOpacity(BYTE nOpacity)
  791. {
  792. m_nOpacity = nOpacity;
  793. m_bLayeredChanged = true;
  794. Invalidate();
  795. }
  796. LPCTSTR CPaintManagerUI::GetLayeredImage()
  797. {
  798. return m_diLayered.sDrawString;
  799. }
  800. void CPaintManagerUI::SetLayeredImage(LPCTSTR pstrImage)
  801. {
  802. m_diLayered.sDrawString = pstrImage;
  803. RECT rcNull = { 0 };
  804. CRenderEngine::DrawImageInfo(NULL, this, rcNull, rcNull, &m_diLayered);
  805. m_bLayeredChanged = true;
  806. Invalidate();
  807. }
  808. CShadowUI* CPaintManagerUI::GetShadow()
  809. {
  810. return &m_shadow;
  811. }
  812. void CPaintManagerUI::SetUseGdiplusText(bool bUse)
  813. {
  814. m_bUseGdiplusText = bUse;
  815. }
  816. bool CPaintManagerUI::IsUseGdiplusText() const
  817. {
  818. return m_bUseGdiplusText;
  819. }
  820. void CPaintManagerUI::SetGdiplusTextRenderingHint(int trh)
  821. {
  822. m_trh = trh;
  823. }
  824. int CPaintManagerUI::GetGdiplusTextRenderingHint() const
  825. {
  826. return m_trh;
  827. }
  828. bool CPaintManagerUI::PreMessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lRes)
  829. {
  830. for (int i = 0; i < m_aPreMessageFilters.GetSize(); i++)
  831. {
  832. bool bHandled = false;
  833. LRESULT lResult = static_cast<IMessageFilterUI*>(m_aPreMessageFilters[i])->MessageHandler(uMsg, wParam, lParam, bHandled);
  834. if (bHandled)
  835. {
  836. lRes = lResult;
  837. return true;
  838. }
  839. }
  840. switch (uMsg)
  841. {
  842. case WM_KEYDOWN:
  843. {
  844. // Tabbing between controls
  845. if (wParam == VK_TAB)
  846. {
  847. if (m_pFocus && m_pFocus->IsVisible() && m_pFocus->IsEnabled() && _tcsstr(m_pFocus->GetClass(), _T("RichEditUI")) != NULL)
  848. {
  849. if (static_cast<CRichEditUI*>(m_pFocus)->IsWantTab()) return false;
  850. }
  851. if (m_pFocus && m_pFocus->IsVisible() && m_pFocus->IsEnabled() && _tcsstr(m_pFocus->GetClass(), _T("WkeWebkitUI")) != NULL)
  852. {
  853. return false;
  854. }
  855. SetNextTabControl(::GetKeyState(VK_SHIFT) >= 0);
  856. return false;
  857. }
  858. }
  859. break;
  860. case WM_SYSCHAR:
  861. {
  862. // Handle ALT-shortcut key-combinations
  863. FINDSHORTCUT fs = { 0 };
  864. fs.ch = toupper((int)wParam);
  865. CControlUI* pControl = m_pRoot->FindControl(__FindControlFromShortcut, &fs, UIFIND_ENABLED | UIFIND_ME_FIRST | UIFIND_TOP_FIRST);
  866. if (pControl != NULL)
  867. {
  868. pControl->SetFocus();
  869. pControl->Activate();
  870. return true;
  871. }
  872. }
  873. break;
  874. case WM_SYSKEYDOWN:
  875. {
  876. if (m_pFocus != NULL)
  877. {
  878. TEventUI event = { 0 };
  879. event.Type = UIEVENT_SYSKEY;
  880. event.chKey = (TCHAR)wParam;
  881. event.ptMouse = m_ptLastMousePos;
  882. event.wKeyState = MapKeyState();
  883. event.dwTimestamp = ::GetTickCount();
  884. m_pFocus->Event(event);
  885. }
  886. }
  887. break;
  888. }
  889. return false;
  890. }
  891. bool CPaintManagerUI::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lRes)
  892. {
  893. if (m_hWndPaint == NULL) return false;
  894. // Cycle through listeners
  895. for (int i = 0; i < m_aMessageFilters.GetSize(); i++)
  896. {
  897. bool bHandled = false;
  898. LRESULT lResult = static_cast<IMessageFilterUI*>(m_aMessageFilters[i])->MessageHandler(uMsg, wParam, lParam, bHandled);
  899. if (bHandled)
  900. {
  901. lRes = lResult;
  902. switch (uMsg)
  903. {
  904. case WM_MOUSEMOVE:
  905. case WM_LBUTTONDOWN:
  906. case WM_LBUTTONDBLCLK:
  907. case WM_LBUTTONUP:
  908. {
  909. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  910. m_ptLastMousePos = pt;
  911. }
  912. break;
  913. case WM_CONTEXTMENU:
  914. case WM_MOUSEWHEEL:
  915. {
  916. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  917. ::ScreenToClient(m_hWndPaint, &pt);
  918. m_ptLastMousePos = pt;
  919. }
  920. break;
  921. }
  922. return true;
  923. }
  924. }
  925. if (m_bLayered)
  926. {
  927. switch (uMsg)
  928. {
  929. case WM_NCACTIVATE:
  930. if (!::IsIconic(m_hWndPaint))
  931. {
  932. lRes = (wParam == 0) ? TRUE : FALSE;
  933. return true;
  934. }
  935. break;
  936. case WM_NCCALCSIZE:
  937. case WM_NCPAINT:
  938. lRes = 0;
  939. return true;
  940. }
  941. }
  942. // Custom handling of events
  943. switch (uMsg)
  944. {
  945. case WM_APP + 1:
  946. {
  947. for (int i = 0; i < m_aDelayedCleanup.GetSize(); i++)
  948. delete static_cast<CControlUI*>(m_aDelayedCleanup[i]);
  949. m_aDelayedCleanup.Empty();
  950. m_bAsyncNotifyPosted = false;
  951. TNotifyUI* pMsg = NULL;
  952. while (pMsg = static_cast<TNotifyUI*>(m_aAsyncNotify.GetAt(0)))
  953. {
  954. m_aAsyncNotify.Remove(0);
  955. if (pMsg->pSender != NULL)
  956. {
  957. if (pMsg->pSender->OnNotify) pMsg->pSender->OnNotify(pMsg);
  958. }
  959. for (int j = 0; j < m_aNotifiers.GetSize(); j++)
  960. {
  961. static_cast<INotifyUI*>(m_aNotifiers[j])->Notify(*pMsg);
  962. }
  963. delete pMsg;
  964. }
  965. }
  966. break;
  967. case WM_CLOSE:
  968. {
  969. // Make sure all matching "closing" events are sent
  970. TEventUI event = { 0 };
  971. event.ptMouse = m_ptLastMousePos;
  972. event.wKeyState = MapKeyState();
  973. event.dwTimestamp = ::GetTickCount();
  974. if (m_pEventHover != NULL)
  975. {
  976. event.Type = UIEVENT_MOUSELEAVE;
  977. event.pSender = m_pEventHover;
  978. m_pEventHover->Event(event);
  979. }
  980. if (m_pEventClick != NULL)
  981. {
  982. event.Type = UIEVENT_BUTTONUP;
  983. event.pSender = m_pEventClick;
  984. m_pEventClick->Event(event);
  985. }
  986. if (m_pEventRClick != NULL)
  987. {
  988. event.Type = UIEVENT_RBUTTONUP;
  989. event.pSender = m_pEventClick;
  990. m_pEventRClick->Event(event);
  991. }
  992. SetFocus(NULL);
  993. if (::GetActiveWindow() == m_hWndPaint)
  994. {
  995. HWND hwndParent = GetWindowOwner(m_hWndPaint);
  996. if (hwndParent != NULL) ::SetFocus(hwndParent);
  997. }
  998. if (m_hwndTooltip != NULL)
  999. {
  1000. ::DestroyWindow(m_hwndTooltip);
  1001. m_hwndTooltip = NULL;
  1002. }
  1003. }
  1004. break;
  1005. case WM_ERASEBKGND:
  1006. {
  1007. // We'll do the painting here...
  1008. lRes = 1;
  1009. }
  1010. return true;
  1011. case WM_PAINT:
  1012. {
  1013. if (m_pRoot == NULL)
  1014. {
  1015. PAINTSTRUCT ps = { 0 };
  1016. ::BeginPaint(m_hWndPaint, &ps);
  1017. CRenderEngine::DrawColor(m_hDcPaint, ps.rcPaint, 0xFF000000);
  1018. ::EndPaint(m_hWndPaint, &ps);
  1019. return true;
  1020. }
  1021. RECT rcClient = { 0 };
  1022. ::GetClientRect(m_hWndPaint, &rcClient);
  1023. RECT rcPaint = { 0 };
  1024. if (!::GetUpdateRect(m_hWndPaint, &rcPaint, FALSE)) return true;
  1025. //if( m_bLayered ) {
  1026. // m_bOffscreenPaint = true;
  1027. // rcPaint = m_rcLayeredUpdate;
  1028. // if( ::IsRectEmpty(&m_rcLayeredUpdate) ) {
  1029. // PAINTSTRUCT ps = { 0 };
  1030. // ::BeginPaint(m_hWndPaint, &ps);
  1031. // ::EndPaint(m_hWndPaint, &ps);
  1032. // return true;
  1033. // }
  1034. // if( rcPaint.right > rcClient.right ) rcPaint.right = rcClient.right;
  1035. // if( rcPaint.bottom > rcClient.bottom ) rcPaint.bottom = rcClient.bottom;
  1036. // ::ZeroMemory(&m_rcLayeredUpdate, sizeof(m_rcLayeredUpdate));
  1037. //}
  1038. //else {
  1039. // if( !::GetUpdateRect(m_hWndPaint, &rcPaint, FALSE) ) return true;
  1040. //}
  1041. // Set focus to first control?
  1042. if (m_bFocusNeeded)
  1043. {
  1044. //修复新窗口自动切换焦点的问题
  1045. //SetNextTabControl();
  1046. m_bFocusNeeded = false;
  1047. }
  1048. SetPainting(true);
  1049. bool bNeedSizeMsg = false;
  1050. DWORD dwWidth = rcClient.right - rcClient.left;
  1051. DWORD dwHeight = rcClient.bottom - rcClient.top;
  1052. SetPainting(true);
  1053. if (m_bUpdateNeeded)
  1054. {
  1055. m_bUpdateNeeded = false;
  1056. if (!::IsRectEmpty(&rcClient) && !::IsIconic(m_hWndPaint))
  1057. {
  1058. if (m_pRoot->IsUpdateNeeded())
  1059. {
  1060. RECT rcRoot = rcClient;
  1061. if (m_hDcOffscreen != NULL) ::DeleteDC(m_hDcOffscreen);
  1062. if (m_hDcBackground != NULL) ::DeleteDC(m_hDcBackground);
  1063. if (m_hbmpOffscreen != NULL) ::DeleteObject(m_hbmpOffscreen);
  1064. if (m_hbmpBackground != NULL) ::DeleteObject(m_hbmpBackground);
  1065. m_hDcOffscreen = NULL;
  1066. m_hDcBackground = NULL;
  1067. m_hbmpOffscreen = NULL;
  1068. m_hbmpBackground = NULL;
  1069. if (m_bLayered)
  1070. {
  1071. rcRoot.left += m_rcLayeredInset.left;
  1072. rcRoot.top += m_rcLayeredInset.top;
  1073. rcRoot.right -= m_rcLayeredInset.right;
  1074. rcRoot.bottom -= m_rcLayeredInset.bottom;
  1075. }
  1076. m_pRoot->SetPos(rcRoot, true);
  1077. bNeedSizeMsg = true;
  1078. }
  1079. else
  1080. {
  1081. CControlUI* pControl = NULL;
  1082. m_aFoundControls.Empty();
  1083. m_pRoot->FindControl(__FindControlsFromUpdate, NULL, UIFIND_VISIBLE | UIFIND_ME_FIRST | UIFIND_UPDATETEST);
  1084. for (int it = 0; it < m_aFoundControls.GetSize(); it++)
  1085. {
  1086. pControl = static_cast<CControlUI*>(m_aFoundControls[it]);
  1087. pControl->SetPos(pControl->GetPos(), true);
  1088. }
  1089. bNeedSizeMsg = true;
  1090. }
  1091. // We'll want to notify the window when it is first initialized
  1092. // with the correct layout. The window form would take the time
  1093. // to submit swipes/animations.
  1094. if (m_bFirstLayout)
  1095. {
  1096. m_bFirstLayout = false;
  1097. SendNotify(m_pRoot, DUI_MSGTYPE_WINDOWINIT, 0, 0, false);
  1098. if (m_bLayered && m_bLayeredChanged)
  1099. {
  1100. Invalidate();
  1101. SetPainting(false);
  1102. return true;
  1103. }
  1104. // 更新阴影窗口显示
  1105. m_shadow.Update(m_hWndPaint);
  1106. }
  1107. }
  1108. }
  1109. else if (m_bLayered && m_bLayeredChanged)
  1110. {
  1111. RECT rcRoot = rcClient;
  1112. if (m_pOffscreenBits) ::ZeroMemory(m_pOffscreenBits, (rcRoot.right - rcRoot.left)
  1113. * (rcRoot.bottom - rcRoot.top) * 4);
  1114. rcRoot.left += m_rcLayeredInset.left;
  1115. rcRoot.top += m_rcLayeredInset.top;
  1116. rcRoot.right -= m_rcLayeredInset.right;
  1117. rcRoot.bottom -= m_rcLayeredInset.bottom;
  1118. m_pRoot->SetPos(rcRoot, true);
  1119. }
  1120. if (m_bLayered)
  1121. {
  1122. DWORD dwExStyle = ::GetWindowLong(m_hWndPaint, GWL_EXSTYLE);
  1123. DWORD dwNewExStyle = dwExStyle | WS_EX_LAYERED;
  1124. if (dwExStyle != dwNewExStyle) ::SetWindowLong(m_hWndPaint, GWL_EXSTYLE, dwNewExStyle);
  1125. m_bOffscreenPaint = true;
  1126. UnionRect(&rcPaint, &rcPaint, &m_rcLayeredUpdate);
  1127. if (rcPaint.right > rcClient.right) rcPaint.right = rcClient.right;
  1128. if (rcPaint.bottom > rcClient.bottom) rcPaint.bottom = rcClient.bottom;
  1129. ::ZeroMemory(&m_rcLayeredUpdate, sizeof(m_rcLayeredUpdate));
  1130. }
  1131. //
  1132. // Render screen
  1133. //
  1134. // Prepare offscreen bitmap
  1135. if (m_bOffscreenPaint && m_hbmpOffscreen == NULL)
  1136. {
  1137. m_hDcOffscreen = ::CreateCompatibleDC(m_hDcPaint);
  1138. m_hbmpOffscreen = CRenderEngine::CreateARGB32Bitmap(m_hDcPaint, dwWidth, dwHeight, (LPBYTE*)&m_pOffscreenBits);
  1139. ASSERT(m_hDcOffscreen);
  1140. ASSERT(m_hbmpOffscreen);
  1141. }
  1142. // Begin Windows paint
  1143. PAINTSTRUCT ps = { 0 };
  1144. ::BeginPaint(m_hWndPaint, &ps);
  1145. if (m_bOffscreenPaint)
  1146. {
  1147. HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(m_hDcOffscreen, m_hbmpOffscreen);
  1148. int iSaveDC = ::SaveDC(m_hDcOffscreen);
  1149. if (m_bLayered)
  1150. {
  1151. for (LONG y = rcClient.bottom - rcPaint.bottom; y < rcClient.bottom - rcPaint.top; ++y)
  1152. {
  1153. for (LONG x = rcPaint.left; x < rcPaint.right; ++x)
  1154. {
  1155. int i = (y * dwWidth + x) * 4;
  1156. *(DWORD*)(&m_pOffscreenBits[i]) = 0;
  1157. }
  1158. }
  1159. }
  1160. m_pRoot->Paint(m_hDcOffscreen, rcPaint, NULL);
  1161. if (m_bLayered)
  1162. {
  1163. for (int i = 0; i < m_aNativeWindow.GetSize(); )
  1164. {
  1165. HWND hChildWnd = static_cast<HWND>(m_aNativeWindow[i]);
  1166. if (!::IsWindow(hChildWnd))
  1167. {
  1168. m_aNativeWindow.Remove(i);
  1169. m_aNativeWindowControl.Remove(i);
  1170. continue;
  1171. }
  1172. ++i;
  1173. if (!::IsWindowVisible(hChildWnd)) continue;
  1174. RECT rcChildWnd = GetNativeWindowRect(hChildWnd);
  1175. RECT rcTemp = { 0 };
  1176. if (!::IntersectRect(&rcTemp, &rcPaint, &rcChildWnd)) continue;
  1177. COLORREF* pChildBitmapBits = NULL;
  1178. HDC hChildMemDC = ::CreateCompatibleDC(m_hDcOffscreen);
  1179. HBITMAP hChildBitmap = CRenderEngine::CreateARGB32Bitmap(hChildMemDC, rcChildWnd.right - rcChildWnd.left, rcChildWnd.bottom - rcChildWnd.top, (BYTE**)&pChildBitmapBits);
  1180. ::ZeroMemory(pChildBitmapBits, (rcChildWnd.right - rcChildWnd.left) * (rcChildWnd.bottom - rcChildWnd.top) * 4);
  1181. HBITMAP hOldChildBitmap = (HBITMAP) ::SelectObject(hChildMemDC, hChildBitmap);
  1182. ::SendMessage(hChildWnd, WM_PRINT, (WPARAM)hChildMemDC, (LPARAM)(PRF_CHECKVISIBLE | PRF_CHILDREN | PRF_CLIENT | PRF_OWNED));
  1183. COLORREF* pChildBitmapBit;
  1184. for (LONG y = 0; y < rcChildWnd.bottom - rcChildWnd.top; y++)
  1185. {
  1186. for (LONG x = 0; x < rcChildWnd.right - rcChildWnd.left; x++)
  1187. {
  1188. pChildBitmapBit = pChildBitmapBits + y * (rcChildWnd.right - rcChildWnd.left) + x;
  1189. if (*pChildBitmapBit != 0x00000000) *pChildBitmapBit |= 0xff000000;
  1190. }
  1191. }
  1192. ::BitBlt(m_hDcOffscreen, rcChildWnd.left, rcChildWnd.top, rcChildWnd.right - rcChildWnd.left,
  1193. rcChildWnd.bottom - rcChildWnd.top, hChildMemDC, 0, 0, SRCCOPY);
  1194. ::SelectObject(hChildMemDC, hOldChildBitmap);
  1195. ::DeleteObject(hChildBitmap);
  1196. ::DeleteDC(hChildMemDC);
  1197. }
  1198. }
  1199. for (int i = 0; i < m_aPostPaintControls.GetSize(); i++)
  1200. {
  1201. CControlUI* pPostPaintControl = static_cast<CControlUI*>(m_aPostPaintControls[i]);
  1202. pPostPaintControl->DoPostPaint(m_hDcOffscreen, rcPaint);
  1203. }
  1204. ::RestoreDC(m_hDcOffscreen, iSaveDC);
  1205. if (m_bLayered)
  1206. {
  1207. RECT rcWnd = { 0 };
  1208. ::GetWindowRect(m_hWndPaint, &rcWnd);
  1209. if (!m_diLayered.sDrawString.IsEmpty())
  1210. {
  1211. DWORD dwWidth = rcClient.right - rcClient.left;
  1212. DWORD dwHeight = rcClient.bottom - rcClient.top;
  1213. RECT rcLayeredClient = rcClient;
  1214. rcLayeredClient.left += m_rcLayeredInset.left;
  1215. rcLayeredClient.top += m_rcLayeredInset.top;
  1216. rcLayeredClient.right -= m_rcLayeredInset.right;
  1217. rcLayeredClient.bottom -= m_rcLayeredInset.bottom;
  1218. COLORREF* pOffscreenBits = (COLORREF*)m_pOffscreenBits;
  1219. COLORREF* pBackgroundBits = m_pBackgroundBits;
  1220. BYTE A = 0;
  1221. BYTE R = 0;
  1222. BYTE G = 0;
  1223. BYTE B = 0;
  1224. if (!m_diLayered.sDrawString.IsEmpty())
  1225. {
  1226. if (m_hbmpBackground == NULL)
  1227. {
  1228. m_hDcBackground = ::CreateCompatibleDC(m_hDcPaint);
  1229. m_hbmpBackground = CRenderEngine::CreateARGB32Bitmap(m_hDcPaint, dwWidth, dwHeight, (BYTE**)&m_pBackgroundBits);
  1230. ::ZeroMemory(m_pBackgroundBits, dwWidth * dwHeight * 4);
  1231. ::SelectObject(m_hDcBackground, m_hbmpBackground);
  1232. CRenderClip clip;
  1233. CRenderClip::GenerateClip(m_hDcBackground, rcLayeredClient, clip);
  1234. CRenderEngine::DrawImageInfo(m_hDcBackground, this, rcLayeredClient, rcLayeredClient, &m_diLayered);
  1235. }
  1236. else if (m_bLayeredChanged)
  1237. {
  1238. ::ZeroMemory(m_pBackgroundBits, dwWidth * dwHeight * 4);
  1239. CRenderClip clip;
  1240. CRenderClip::GenerateClip(m_hDcBackground, rcLayeredClient, clip);
  1241. CRenderEngine::DrawImageInfo(m_hDcBackground, this, rcLayeredClient, rcLayeredClient, &m_diLayered);
  1242. }
  1243. for (LONG y = rcClient.bottom - rcPaint.bottom; y < rcClient.bottom - rcPaint.top; ++y)
  1244. {
  1245. for (LONG x = rcPaint.left; x < rcPaint.right; ++x)
  1246. {
  1247. pOffscreenBits = (COLORREF*)(m_pOffscreenBits + y * dwWidth + x);
  1248. pBackgroundBits = m_pBackgroundBits + y * dwWidth + x;
  1249. A = (BYTE)((*pBackgroundBits) >> 24);
  1250. R = (BYTE)((*pOffscreenBits) >> 16) * A / 255;
  1251. G = (BYTE)((*pOffscreenBits) >> 8) * A / 255;
  1252. B = (BYTE)(*pOffscreenBits) * A / 255;
  1253. *pOffscreenBits = RGB(B, G, R) + ((DWORD)A << 24);
  1254. }
  1255. }
  1256. }
  1257. }
  1258. else
  1259. {
  1260. for (LONG y = rcClient.bottom - rcPaint.bottom; y < rcClient.bottom - rcPaint.top; ++y)
  1261. {
  1262. for (LONG x = rcPaint.left; x < rcPaint.right; ++x)
  1263. {
  1264. int i = (y * dwWidth + x) * 4;
  1265. if ((m_pOffscreenBits[i + 3] == 0) && (m_pOffscreenBits[i + 0] != 0 || m_pOffscreenBits[i + 1] != 0 || m_pOffscreenBits[i + 2] != 0))
  1266. m_pOffscreenBits[i + 3] = 255;
  1267. }
  1268. }
  1269. }
  1270. BLENDFUNCTION bf = { AC_SRC_OVER, 0, m_nOpacity, AC_SRC_ALPHA };
  1271. POINT ptPos = { rcWnd.left, rcWnd.top };
  1272. SIZE sizeWnd = { dwWidth, dwHeight };
  1273. POINT ptSrc = { 0, 0 };
  1274. g_fUpdateLayeredWindow(m_hWndPaint, m_hDcPaint, &ptPos, &sizeWnd, m_hDcOffscreen, &ptSrc, 0, &bf, ULW_ALPHA);
  1275. }
  1276. else
  1277. {
  1278. ::BitBlt(m_hDcPaint, rcPaint.left, rcPaint.top, rcPaint.right - rcPaint.left, rcPaint.bottom - rcPaint.top, m_hDcOffscreen, rcPaint.left, rcPaint.top, SRCCOPY);
  1279. }
  1280. ::SelectObject(m_hDcOffscreen, hOldBitmap);
  1281. if (m_bShowUpdateRect && !m_bLayered)
  1282. {
  1283. HPEN hOldPen = (HPEN)::SelectObject(m_hDcPaint, m_hUpdateRectPen);
  1284. ::SelectObject(m_hDcPaint, ::GetStockObject(HOLLOW_BRUSH));
  1285. ::Rectangle(m_hDcPaint, rcPaint.left, rcPaint.top, rcPaint.right, rcPaint.bottom);
  1286. ::SelectObject(m_hDcPaint, hOldPen);
  1287. }
  1288. }
  1289. else
  1290. {
  1291. // A standard paint job
  1292. int iSaveDC = ::SaveDC(m_hDcPaint);
  1293. m_pRoot->Paint(m_hDcPaint, rcPaint, NULL);
  1294. for (int i = 0; i < m_aPostPaintControls.GetSize(); i++)
  1295. {
  1296. CControlUI* pPostPaintControl = static_cast<CControlUI*>(m_aPostPaintControls[i]);
  1297. pPostPaintControl->DoPostPaint(m_hDcPaint, rcPaint);
  1298. }
  1299. ::RestoreDC(m_hDcPaint, iSaveDC);
  1300. }
  1301. // All Done!
  1302. ::EndPaint(m_hWndPaint, &ps);
  1303. // 绘制结束
  1304. SetPainting(false);
  1305. m_bLayeredChanged = false;
  1306. if (m_bUpdateNeeded) Invalidate();
  1307. // 发送窗口大小改变消息
  1308. if (bNeedSizeMsg)
  1309. {
  1310. this->SendNotify(m_pRoot, DUI_MSGTYPE_WINDOWSIZE, 0, 0, true);
  1311. }
  1312. return true;
  1313. }
  1314. case WM_PRINTCLIENT:
  1315. {
  1316. if (m_pRoot == NULL) break;
  1317. RECT rcClient;
  1318. ::GetClientRect(m_hWndPaint, &rcClient);
  1319. HDC hDC = (HDC)wParam;
  1320. int save = ::SaveDC(hDC);
  1321. m_pRoot->Paint(hDC, rcClient, NULL);
  1322. if ((lParam & PRF_CHILDREN) != 0)
  1323. {
  1324. HWND hWndChild = ::GetWindow(m_hWndPaint, GW_CHILD);
  1325. while (hWndChild != NULL)
  1326. {
  1327. RECT rcPos = { 0 };
  1328. ::GetWindowRect(hWndChild, &rcPos);
  1329. ::MapWindowPoints(HWND_DESKTOP, m_hWndPaint, reinterpret_cast<LPPOINT>(&rcPos), 2);
  1330. ::SetWindowOrgEx(hDC, -rcPos.left, -rcPos.top, NULL);
  1331. ::SendMessage(hWndChild, WM_PRINT, wParam, lParam | PRF_NONCLIENT);
  1332. hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT);
  1333. }
  1334. }
  1335. ::RestoreDC(hDC, save);
  1336. }
  1337. break;
  1338. case WM_GETMINMAXINFO:
  1339. {
  1340. MONITORINFO Monitor = {};
  1341. Monitor.cbSize = sizeof(Monitor);
  1342. ::GetMonitorInfo(::MonitorFromWindow(m_hWndPaint, MONITOR_DEFAULTTOPRIMARY), &Monitor);
  1343. RECT rcWork = Monitor.rcWork;
  1344. if (Monitor.dwFlags != MONITORINFOF_PRIMARY)
  1345. {
  1346. ::OffsetRect(&rcWork, -rcWork.left, -rcWork.top);
  1347. }
  1348. LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
  1349. if (m_szMinWindow.cx > 0) lpMMI->ptMinTrackSize.x = m_szMinWindow.cx;
  1350. if (m_szMinWindow.cy > 0) lpMMI->ptMinTrackSize.y = m_szMinWindow.cy;
  1351. if (m_szMaxWindow.cx > 0) lpMMI->ptMaxTrackSize.x = m_szMaxWindow.cx;
  1352. if (m_szMaxWindow.cy > 0) lpMMI->ptMaxTrackSize.y = m_szMaxWindow.cy;
  1353. if (m_szMaxWindow.cx > 0) lpMMI->ptMaxSize.x = m_szMaxWindow.cx;
  1354. if (m_szMaxWindow.cy > 0) lpMMI->ptMaxSize.y = m_szMaxWindow.cy;
  1355. }
  1356. break;
  1357. case WM_SIZE:
  1358. {
  1359. if (m_pFocus != NULL)
  1360. {
  1361. TEventUI event = { 0 };
  1362. event.Type = UIEVENT_WINDOWSIZE;
  1363. event.pSender = m_pFocus;
  1364. event.dwTimestamp = ::GetTickCount();
  1365. m_pFocus->Event(event);
  1366. }
  1367. if (m_pRoot != NULL) m_pRoot->NeedUpdate();
  1368. }
  1369. return true;
  1370. case WM_TIMER:
  1371. {
  1372. for (int i = 0; i < m_aTimers.GetSize(); i++)
  1373. {
  1374. const TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
  1375. if (pTimer->hWnd == m_hWndPaint &&
  1376. pTimer->uWinTimer == LOWORD(wParam) &&
  1377. pTimer->bKilled == false)
  1378. {
  1379. TEventUI event = { 0 };
  1380. event.Type = UIEVENT_TIMER;
  1381. event.pSender = pTimer->pSender;
  1382. event.dwTimestamp = ::GetTickCount();
  1383. event.ptMouse = m_ptLastMousePos;
  1384. event.wKeyState = MapKeyState();
  1385. event.wParam = pTimer->nLocalID;
  1386. event.lParam = lParam;
  1387. pTimer->pSender->Event(event);
  1388. break;
  1389. }
  1390. }
  1391. }
  1392. break;
  1393. case WM_MOUSEHOVER:
  1394. {
  1395. m_bMouseTracking = false;
  1396. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1397. CControlUI* pHover = FindControl(pt);
  1398. if (pHover == NULL) break;
  1399. // Generate mouse hover event
  1400. if (m_pEventHover != NULL)
  1401. {
  1402. TEventUI event = { 0 };
  1403. event.Type = UIEVENT_MOUSEHOVER;
  1404. event.pSender = m_pEventHover;
  1405. event.wParam = wParam;
  1406. event.lParam = lParam;
  1407. event.dwTimestamp = ::GetTickCount();
  1408. event.ptMouse = pt;
  1409. event.wKeyState = MapKeyState();
  1410. m_pEventHover->Event(event);
  1411. }
  1412. // Create tooltip information
  1413. CDuiString sToolTip = pHover->GetToolTip();
  1414. if (sToolTip.IsEmpty()) return true;
  1415. ::ZeroMemory(&m_ToolTip, sizeof(TOOLINFO));
  1416. m_ToolTip.cbSize = sizeof(TOOLINFO);
  1417. m_ToolTip.uFlags = TTF_IDISHWND;
  1418. m_ToolTip.hwnd = m_hWndPaint;
  1419. m_ToolTip.uId = (UINT_PTR)m_hWndPaint;
  1420. m_ToolTip.hinst = m_hInstance;
  1421. m_ToolTip.lpszText = const_cast<LPTSTR>((LPCTSTR)sToolTip);
  1422. m_ToolTip.rect = pHover->GetPos();
  1423. if (m_hwndTooltip == NULL)
  1424. {
  1425. m_hwndTooltip = ::CreateWindowEx(0, TOOLTIPS_CLASS, NULL, WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, m_hWndPaint, NULL, m_hInstance, NULL);
  1426. ::SendMessage(m_hwndTooltip, TTM_ADDTOOL, 0, (LPARAM)&m_ToolTip);
  1427. ::SendMessage(m_hwndTooltip, TTM_SETMAXTIPWIDTH, 0, pHover->GetToolTipWidth());
  1428. }
  1429. if (!::IsWindowVisible(m_hwndTooltip))
  1430. {
  1431. ::SendMessage(m_hwndTooltip, TTM_SETTOOLINFO, 0, (LPARAM)&m_ToolTip);
  1432. ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&m_ToolTip);
  1433. }
  1434. }
  1435. return true;
  1436. case WM_MOUSELEAVE:
  1437. {
  1438. if (m_hwndTooltip != NULL) ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ToolTip);
  1439. if (m_bMouseTracking)
  1440. {
  1441. POINT pt = { 0 };
  1442. RECT rcWnd = { 0 };
  1443. ::GetCursorPos(&pt);
  1444. ::GetWindowRect(m_hWndPaint, &rcWnd);
  1445. if (!::IsIconic(m_hWndPaint) && ::GetActiveWindow() == m_hWndPaint && ::PtInRect(&rcWnd, pt))
  1446. {
  1447. if (::SendMessage(m_hWndPaint, WM_NCHITTEST, 0, MAKELPARAM(pt.x, pt.y)) == HTCLIENT)
  1448. {
  1449. ::ScreenToClient(m_hWndPaint, &pt);
  1450. ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, MAKELPARAM(pt.x, pt.y));
  1451. }
  1452. else
  1453. ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, (LPARAM)-1);
  1454. }
  1455. else
  1456. ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, (LPARAM)-1);
  1457. }
  1458. m_bMouseTracking = false;
  1459. }
  1460. break;
  1461. case WM_MOUSEMOVE:
  1462. {
  1463. // Start tracking this entire window again...
  1464. if (!m_bMouseTracking)
  1465. {
  1466. TRACKMOUSEEVENT tme = { 0 };
  1467. tme.cbSize = sizeof(TRACKMOUSEEVENT);
  1468. tme.dwFlags = TME_HOVER | TME_LEAVE;
  1469. tme.hwndTrack = m_hWndPaint;
  1470. tme.dwHoverTime = m_hwndTooltip == NULL ? m_iHoverTime : (DWORD) ::SendMessage(m_hwndTooltip, TTM_GETDELAYTIME, TTDT_INITIAL, 0L);
  1471. _TrackMouseEvent(&tme);
  1472. m_bMouseTracking = true;
  1473. }
  1474. // Generate the appropriate mouse messages
  1475. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1476. // 是否移动
  1477. bool bNeedDrag = true;
  1478. if (m_ptLastMousePos.x == pt.x && m_ptLastMousePos.y == pt.y)
  1479. {
  1480. bNeedDrag = false;
  1481. }
  1482. // 记录鼠标位置
  1483. m_ptLastMousePos = pt;
  1484. CControlUI* pNewHover = FindControl(pt);
  1485. if (pNewHover != NULL && pNewHover->GetManager() != this) break;
  1486. // 拖拽事件
  1487. if (bNeedDrag && m_bDragMode && wParam == MK_LBUTTON)
  1488. {
  1489. // 释放Capture
  1490. ::ReleaseCapture();
  1491. // 接口
  1492. if (m_pDragDrop != NULL && m_pDragDrop->OnDragDrop(m_pEventClick))
  1493. {
  1494. m_bDragMode = false;
  1495. break;
  1496. }
  1497. CIDropSource* pdsrc = new CIDropSource;
  1498. if (pdsrc == NULL) return 0;
  1499. pdsrc->AddRef();
  1500. CIDataObject* pdobj = new CIDataObject(pdsrc);
  1501. if (pdobj == NULL) return 0;
  1502. pdobj->AddRef();
  1503. FORMATETC fmtetc = { 0 };
  1504. STGMEDIUM medium = { 0 };
  1505. fmtetc.dwAspect = DVASPECT_CONTENT;
  1506. fmtetc.lindex = -1;
  1507. fmtetc.cfFormat = CF_BITMAP;
  1508. fmtetc.tymed = TYMED_GDI;
  1509. //////////////////////////////////////
  1510. HBITMAP hBitmap = (HBITMAP)OleDuplicateData(m_hDragBitmap, fmtetc.cfFormat, NULL);
  1511. medium.hBitmap = hBitmap;
  1512. pdobj->SetData(&fmtetc, &medium, FALSE);
  1513. //////////////////////////////////////
  1514. BITMAP bmap;
  1515. GetObject(hBitmap, sizeof(BITMAP), &bmap);
  1516. RECT rc = { 0, 0, bmap.bmWidth, bmap.bmHeight };
  1517. fmtetc.cfFormat = CF_ENHMETAFILE;
  1518. fmtetc.tymed = TYMED_ENHMF;
  1519. HDC hMetaDC = CreateEnhMetaFile(m_hDcPaint, NULL, NULL, NULL);
  1520. HDC hdcMem = CreateCompatibleDC(m_hDcPaint);
  1521. HGDIOBJ hOldBmp = ::SelectObject(hdcMem, hBitmap);
  1522. ::BitBlt(hMetaDC, 0, 0, rc.right, rc.bottom, hdcMem, 0, 0, SRCCOPY);
  1523. ::SelectObject(hdcMem, hOldBmp);
  1524. medium.hEnhMetaFile = CloseEnhMetaFile(hMetaDC);
  1525. DeleteDC(hdcMem);
  1526. medium.tymed = TYMED_ENHMF;
  1527. pdobj->SetData(&fmtetc, &medium, TRUE);
  1528. //////////////////////////////////////
  1529. CDragSourceHelper dragSrcHelper;
  1530. POINT ptDrag = { 0 };
  1531. ptDrag.x = bmap.bmWidth / 2;
  1532. ptDrag.y = bmap.bmHeight / 2;
  1533. dragSrcHelper.InitializeFromBitmap(hBitmap, ptDrag, rc, pdobj); //will own the bmp
  1534. DWORD dwEffect;
  1535. HRESULT hr = ::DoDragDrop(pdobj, pdsrc, DROPEFFECT_COPY | DROPEFFECT_MOVE, &dwEffect);
  1536. if (dwEffect) pdsrc->Release();
  1537. else delete pdsrc;
  1538. pdobj->Release();
  1539. m_bDragMode = false;
  1540. break;
  1541. }
  1542. TEventUI event = { 0 };
  1543. event.ptMouse = pt;
  1544. event.wParam = wParam;
  1545. event.lParam = lParam;
  1546. event.dwTimestamp = ::GetTickCount();
  1547. event.wKeyState = MapKeyState();
  1548. if (!IsCaptured())
  1549. {
  1550. pNewHover = FindControl(pt);
  1551. if (pNewHover != NULL && pNewHover->GetManager() != this) break;
  1552. if (pNewHover != m_pEventHover && m_pEventHover != NULL)
  1553. {
  1554. event.Type = UIEVENT_MOUSELEAVE;
  1555. event.pSender = m_pEventHover;
  1556. CStdPtrArray aNeedMouseLeaveNeeded(m_aNeedMouseLeaveNeeded.GetSize());
  1557. aNeedMouseLeaveNeeded.Resize(m_aNeedMouseLeaveNeeded.GetSize());
  1558. ::CopyMemory(aNeedMouseLeaveNeeded.GetData(), m_aNeedMouseLeaveNeeded.GetData(), m_aNeedMouseLeaveNeeded.GetSize() * sizeof(LPVOID));
  1559. for (int i = 0; i < aNeedMouseLeaveNeeded.GetSize(); i++)
  1560. {
  1561. static_cast<CControlUI*>(aNeedMouseLeaveNeeded[i])->Event(event);
  1562. }
  1563. m_pEventHover->Event(event);
  1564. m_pEventHover = NULL;
  1565. if (m_hwndTooltip != NULL) ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ToolTip);
  1566. }
  1567. if (pNewHover != m_pEventHover && pNewHover != NULL)
  1568. {
  1569. event.Type = UIEVENT_MOUSEENTER;
  1570. event.pSender = pNewHover;
  1571. pNewHover->Event(event);
  1572. m_pEventHover = pNewHover;
  1573. }
  1574. }
  1575. if (m_pEventClick != NULL)
  1576. {
  1577. event.Type = UIEVENT_MOUSEMOVE;
  1578. event.pSender = m_pEventClick;
  1579. m_pEventClick->Event(event);
  1580. }
  1581. else if (pNewHover != NULL)
  1582. {
  1583. event.Type = UIEVENT_MOUSEMOVE;
  1584. event.pSender = pNewHover;
  1585. pNewHover->Event(event);
  1586. }
  1587. }
  1588. break;
  1589. case WM_LBUTTONDOWN:
  1590. {
  1591. // We alway set focus back to our app (this helps
  1592. // when Win32 child windows are placed on the dialog
  1593. // and we need to remove them on focus change).
  1594. if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
  1595. if (m_pRoot == NULL) break;
  1596. // 查找控件
  1597. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1598. m_ptLastMousePos = pt;
  1599. CControlUI* pControl = FindControl(pt);
  1600. if (pControl == NULL) break;
  1601. if (pControl->GetManager() != this) break;
  1602. // 准备拖拽
  1603. if (m_bDragDrop && pControl->IsDragEnabled())
  1604. {
  1605. m_bDragMode = true;
  1606. if (m_hDragBitmap != NULL)
  1607. {
  1608. ::DeleteObject(m_hDragBitmap);
  1609. m_hDragBitmap = NULL;
  1610. }
  1611. m_hDragBitmap = CRenderEngine::GenerateBitmap(this, pControl, pControl->GetPos());
  1612. }
  1613. // 开启捕获
  1614. SetCapture();
  1615. // 事件处理
  1616. m_pEventClick = pControl;
  1617. pControl->SetFocus();
  1618. TEventUI event = { 0 };
  1619. event.Type = UIEVENT_BUTTONDOWN;
  1620. event.pSender = pControl;
  1621. event.wParam = wParam;
  1622. event.lParam = lParam;
  1623. event.ptMouse = pt;
  1624. event.wKeyState = (WORD)wParam;
  1625. event.dwTimestamp = ::GetTickCount();
  1626. pControl->Event(event);
  1627. }
  1628. break;
  1629. case WM_LBUTTONDBLCLK:
  1630. {
  1631. if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
  1632. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1633. m_ptLastMousePos = pt;
  1634. CControlUI* pControl = FindControl(pt);
  1635. if (pControl == NULL) break;
  1636. if (pControl->GetManager() != this) break;
  1637. //SetCapture();
  1638. TEventUI event = { 0 };
  1639. event.Type = UIEVENT_DBLCLICK;
  1640. event.pSender = pControl;
  1641. event.ptMouse = pt;
  1642. event.wParam = wParam;
  1643. event.lParam = lParam;
  1644. event.wKeyState = (WORD)wParam;
  1645. event.dwTimestamp = ::GetTickCount();
  1646. pControl->Event(event);
  1647. m_pEventClick = pControl;
  1648. }
  1649. break;
  1650. case WM_LBUTTONUP:
  1651. {
  1652. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1653. m_ptLastMousePos = pt;
  1654. if (m_pEventClick == NULL) break;
  1655. ReleaseCapture();
  1656. TEventUI event = { 0 };
  1657. event.Type = UIEVENT_BUTTONUP;
  1658. event.pSender = m_pEventClick;
  1659. event.wParam = wParam;
  1660. event.lParam = lParam;
  1661. event.ptMouse = pt;
  1662. event.wKeyState = (WORD)wParam;
  1663. event.dwTimestamp = ::GetTickCount();
  1664. CControlUI* pClick = m_pEventClick;
  1665. m_pEventClick = NULL;
  1666. pClick->Event(event);
  1667. }
  1668. break;
  1669. case WM_RBUTTONDOWN:
  1670. {
  1671. if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
  1672. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1673. m_ptLastMousePos = pt;
  1674. CControlUI* pControl = FindControl(pt);
  1675. if (pControl == NULL) break;
  1676. if (pControl->GetManager() != this) break;
  1677. pControl->SetFocus();
  1678. SetCapture();
  1679. TEventUI event = { 0 };
  1680. event.Type = UIEVENT_RBUTTONDOWN;
  1681. event.pSender = pControl;
  1682. event.wParam = wParam;
  1683. event.lParam = lParam;
  1684. event.ptMouse = pt;
  1685. event.wKeyState = (WORD)wParam;
  1686. event.dwTimestamp = ::GetTickCount();
  1687. pControl->Event(event);
  1688. m_pEventRClick = pControl;
  1689. }
  1690. break;
  1691. case WM_RBUTTONUP:
  1692. {
  1693. if (m_bMouseCapture) ReleaseCapture();
  1694. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1695. m_ptLastMousePos = pt;
  1696. m_pEventRClick = FindControl(pt);
  1697. if (m_pEventRClick == NULL) break;
  1698. TEventUI event = { 0 };
  1699. event.Type = UIEVENT_RBUTTONUP;
  1700. event.pSender = m_pEventRClick;
  1701. event.wParam = wParam;
  1702. event.lParam = lParam;
  1703. event.ptMouse = pt;
  1704. event.wKeyState = (WORD)wParam;
  1705. event.dwTimestamp = ::GetTickCount();
  1706. m_pEventRClick->Event(event);
  1707. }
  1708. break;
  1709. case WM_MBUTTONDOWN:
  1710. {
  1711. if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
  1712. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1713. m_ptLastMousePos = pt;
  1714. CControlUI* pControl = FindControl(pt);
  1715. if (pControl == NULL) break;
  1716. if (pControl->GetManager() != this) break;
  1717. pControl->SetFocus();
  1718. SetCapture();
  1719. TEventUI event = { 0 };
  1720. event.Type = UIEVENT_MBUTTONDOWN;
  1721. event.pSender = pControl;
  1722. event.wParam = wParam;
  1723. event.lParam = lParam;
  1724. event.ptMouse = pt;
  1725. event.wKeyState = (WORD)wParam;
  1726. event.dwTimestamp = ::GetTickCount();
  1727. pControl->Event(event);
  1728. m_pEventClick = pControl;
  1729. }
  1730. break;
  1731. case WM_MBUTTONUP:
  1732. {
  1733. if (m_bMouseCapture) ReleaseCapture();
  1734. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1735. m_ptLastMousePos = pt;
  1736. m_pEventClick = FindControl(pt);
  1737. if (m_pEventClick == NULL) break;
  1738. TEventUI event = { 0 };
  1739. event.Type = UIEVENT_MBUTTONUP;
  1740. event.pSender = m_pEventClick;
  1741. event.wParam = wParam;
  1742. event.lParam = lParam;
  1743. event.ptMouse = pt;
  1744. event.wKeyState = (WORD)wParam;
  1745. event.dwTimestamp = ::GetTickCount();
  1746. m_pEventClick->Event(event);
  1747. }
  1748. break;
  1749. case WM_CONTEXTMENU:
  1750. {
  1751. if (m_pRoot == NULL) break;
  1752. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1753. ::ScreenToClient(m_hWndPaint, &pt);
  1754. m_ptLastMousePos = pt;
  1755. if (m_pEventRClick == NULL) break;
  1756. ReleaseCapture();
  1757. TEventUI event = { 0 };
  1758. event.Type = UIEVENT_CONTEXTMENU;
  1759. event.pSender = m_pEventRClick;
  1760. event.wParam = wParam;
  1761. event.lParam = lParam;
  1762. event.ptMouse = pt;
  1763. event.wKeyState = (WORD)wParam;
  1764. event.lParam = (LPARAM)m_pEventRClick;
  1765. event.dwTimestamp = ::GetTickCount();
  1766. m_pEventRClick->Event(event);
  1767. m_pEventRClick = NULL;
  1768. }
  1769. break;
  1770. case WM_MOUSEWHEEL:
  1771. {
  1772. if (m_pRoot == NULL) break;
  1773. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1774. ::ScreenToClient(m_hWndPaint, &pt);
  1775. m_ptLastMousePos = pt;
  1776. CControlUI* pControl = FindControl(pt);
  1777. if (pControl == NULL) break;
  1778. if (pControl->GetManager() != this) break;
  1779. int zDelta = (int)(short)HIWORD(wParam);
  1780. TEventUI event = { 0 };
  1781. event.Type = UIEVENT_SCROLLWHEEL;
  1782. event.pSender = pControl;
  1783. event.wParam = MAKEWPARAM(zDelta < 0 ? SB_LINEDOWN : SB_LINEUP, zDelta);
  1784. event.lParam = lParam;
  1785. event.ptMouse = pt;
  1786. event.wKeyState = MapKeyState();
  1787. event.dwTimestamp = ::GetTickCount();
  1788. pControl->Event(event);
  1789. // Let's make sure that the scroll item below the cursor is the same as before...
  1790. ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, (LPARAM)MAKELPARAM(m_ptLastMousePos.x, m_ptLastMousePos.y));
  1791. }
  1792. break;
  1793. case WM_CHAR:
  1794. {
  1795. if (m_pRoot == NULL) break;
  1796. if (m_pFocus == NULL) break;
  1797. TEventUI event = { 0 };
  1798. event.Type = UIEVENT_CHAR;
  1799. event.pSender = m_pFocus;
  1800. event.wParam = wParam;
  1801. event.lParam = lParam;
  1802. event.chKey = (TCHAR)wParam;
  1803. event.ptMouse = m_ptLastMousePos;
  1804. event.wKeyState = MapKeyState();
  1805. event.dwTimestamp = ::GetTickCount();
  1806. m_pFocus->Event(event);
  1807. }
  1808. break;
  1809. case WM_KEYDOWN:
  1810. {
  1811. if (m_pRoot == NULL) break;
  1812. if (m_pFocus == NULL) break;
  1813. TEventUI event = { 0 };
  1814. event.Type = UIEVENT_KEYDOWN;
  1815. event.pSender = m_pFocus;
  1816. event.wParam = wParam;
  1817. event.lParam = lParam;
  1818. event.chKey = (TCHAR)wParam;
  1819. event.ptMouse = m_ptLastMousePos;
  1820. event.wKeyState = MapKeyState();
  1821. event.dwTimestamp = ::GetTickCount();
  1822. m_pFocus->Event(event);
  1823. m_pEventKey = m_pFocus;
  1824. }
  1825. break;
  1826. case WM_KEYUP:
  1827. {
  1828. if (m_pRoot == NULL) break;
  1829. if (m_pEventKey == NULL) break;
  1830. TEventUI event = { 0 };
  1831. event.Type = UIEVENT_KEYUP;
  1832. event.pSender = m_pEventKey;
  1833. event.wParam = wParam;
  1834. event.lParam = lParam;
  1835. event.chKey = (TCHAR)wParam;
  1836. event.ptMouse = m_ptLastMousePos;
  1837. event.wKeyState = MapKeyState();
  1838. event.dwTimestamp = ::GetTickCount();
  1839. m_pEventKey->Event(event);
  1840. m_pEventKey = NULL;
  1841. }
  1842. break;
  1843. case WM_SETCURSOR:
  1844. {
  1845. if (m_pRoot == NULL) break;
  1846. if (LOWORD(lParam) != HTCLIENT) break;
  1847. if (m_bMouseCapture) return true;
  1848. POINT pt = { 0 };
  1849. ::GetCursorPos(&pt);
  1850. ::ScreenToClient(m_hWndPaint, &pt);
  1851. CControlUI* pControl = FindControl(pt);
  1852. if (pControl == NULL) break;
  1853. if ((pControl->GetControlFlags() & UIFLAG_SETCURSOR) == 0) break;
  1854. TEventUI event = { 0 };
  1855. event.Type = UIEVENT_SETCURSOR;
  1856. event.pSender = pControl;
  1857. event.wParam = wParam;
  1858. event.lParam = lParam;
  1859. event.ptMouse = pt;
  1860. event.wKeyState = MapKeyState();
  1861. event.dwTimestamp = ::GetTickCount();
  1862. pControl->Event(event);
  1863. }
  1864. return true;
  1865. case WM_SETFOCUS:
  1866. {
  1867. if (m_pFocus != NULL)
  1868. {
  1869. TEventUI event = { 0 };
  1870. event.Type = UIEVENT_SETFOCUS;
  1871. event.wParam = wParam;
  1872. event.lParam = lParam;
  1873. event.pSender = m_pFocus;
  1874. event.dwTimestamp = ::GetTickCount();
  1875. m_pFocus->Event(event);
  1876. }
  1877. break;
  1878. }
  1879. case WM_KILLFOCUS:
  1880. {
  1881. if (IsCaptured()) ReleaseCapture();
  1882. break;
  1883. }
  1884. case WM_NOTIFY:
  1885. {
  1886. if (lParam == 0) break;
  1887. LPNMHDR lpNMHDR = (LPNMHDR)lParam;
  1888. if (lpNMHDR != NULL) lRes = ::SendMessage(lpNMHDR->hwndFrom, OCM__BASE + uMsg, wParam, lParam);
  1889. return true;
  1890. }
  1891. break;
  1892. case WM_COMMAND:
  1893. {
  1894. if (lParam == 0) break;
  1895. HWND hWndChild = (HWND)lParam;
  1896. lRes = ::SendMessage(hWndChild, OCM__BASE + uMsg, wParam, lParam);
  1897. if (lRes != 0) return true;
  1898. }
  1899. break;
  1900. case WM_CTLCOLOREDIT:
  1901. case WM_CTLCOLORSTATIC:
  1902. {
  1903. // Refer To: http://msdn.microsoft.com/en-us/library/bb761691(v=vs.85).aspx
  1904. // Read-only or disabled edit controls do not send the WM_CTLCOLOREDIT message; instead, they send the WM_CTLCOLORSTATIC message.
  1905. if (lParam == 0) break;
  1906. HWND hWndChild = (HWND)lParam;
  1907. lRes = ::SendMessage(hWndChild, OCM__BASE + uMsg, wParam, lParam);
  1908. if (lRes != 0) return true;
  1909. }
  1910. break;
  1911. default:
  1912. break;
  1913. }
  1914. return false;
  1915. }
  1916. bool CPaintManagerUI::IsUpdateNeeded() const
  1917. {
  1918. return m_bUpdateNeeded;
  1919. }
  1920. void CPaintManagerUI::NeedUpdate()
  1921. {
  1922. m_bUpdateNeeded = true;
  1923. }
  1924. void CPaintManagerUI::Invalidate()
  1925. {
  1926. RECT rcClient = { 0 };
  1927. ::GetClientRect(m_hWndPaint, &rcClient);
  1928. ::UnionRect(&m_rcLayeredUpdate, &m_rcLayeredUpdate, &rcClient);
  1929. ::InvalidateRect(m_hWndPaint, NULL, FALSE);
  1930. }
  1931. void CPaintManagerUI::Invalidate(RECT& rcItem)
  1932. {
  1933. if (rcItem.left < 0) rcItem.left = 0;
  1934. if (rcItem.top < 0) rcItem.top = 0;
  1935. if (rcItem.right < rcItem.left) rcItem.right = rcItem.left;
  1936. if (rcItem.bottom < rcItem.top) rcItem.bottom = rcItem.top;
  1937. ::UnionRect(&m_rcLayeredUpdate, &m_rcLayeredUpdate, &rcItem);
  1938. ::InvalidateRect(m_hWndPaint, &rcItem, FALSE);
  1939. }
  1940. bool CPaintManagerUI::IsValid()
  1941. {
  1942. return m_hWndPaint != NULL && m_pRoot != NULL;
  1943. }
  1944. bool CPaintManagerUI::AttachDialog(CControlUI* pControl)
  1945. {
  1946. ASSERT(::IsWindow(m_hWndPaint));
  1947. // 创建阴影窗口
  1948. m_shadow.Create(this);
  1949. // Reset any previous attachment
  1950. SetFocus(NULL);
  1951. m_pEventKey = NULL;
  1952. m_pEventHover = NULL;
  1953. m_pEventClick = NULL;
  1954. m_pEventRClick = NULL;
  1955. // Remove the existing control-tree. We might have gotten inside this function as
  1956. // a result of an event fired or similar, so we cannot just delete the objects and
  1957. // pull the internal memory of the calling code. We'll delay the cleanup.
  1958. if (m_pRoot != NULL)
  1959. {
  1960. m_aPostPaintControls.Empty();
  1961. AddDelayedCleanup(m_pRoot);
  1962. }
  1963. // Set the dialog root element
  1964. m_pRoot = pControl;
  1965. // Go ahead...
  1966. m_bUpdateNeeded = true;
  1967. m_bFirstLayout = true;
  1968. m_bFocusNeeded = true;
  1969. // Initiate all control
  1970. return InitControls(pControl);
  1971. }
  1972. bool CPaintManagerUI::InitControls(CControlUI* pControl, CControlUI* pParent /*= NULL*/)
  1973. {
  1974. ASSERT(pControl);
  1975. if (pControl == NULL) return false;
  1976. pControl->SetManager(this, pParent != NULL ? pParent : pControl->GetParent(), true);
  1977. pControl->FindControl(__FindControlFromNameHash, this, UIFIND_ALL);
  1978. return true;
  1979. }
  1980. void CPaintManagerUI::ReapObjects(CControlUI* pControl)
  1981. {
  1982. if (pControl == m_pEventKey) m_pEventKey = NULL;
  1983. if (pControl == m_pEventHover) m_pEventHover = NULL;
  1984. if (pControl == m_pEventClick) m_pEventClick = NULL;
  1985. if (pControl == m_pEventRClick) m_pEventRClick = NULL;
  1986. if (pControl == m_pFocus) m_pFocus = NULL;
  1987. KillTimer(pControl);
  1988. const CDuiString& sName = pControl->GetName();
  1989. if (!sName.IsEmpty())
  1990. {
  1991. if (pControl == FindControl(sName)) m_mNameHash.Remove(sName);
  1992. }
  1993. for (int i = 0; i < m_aAsyncNotify.GetSize(); i++)
  1994. {
  1995. TNotifyUI* pMsg = static_cast<TNotifyUI*>(m_aAsyncNotify[i]);
  1996. if (pMsg->pSender == pControl) pMsg->pSender = NULL;
  1997. }
  1998. }
  1999. bool CPaintManagerUI::AddOptionGroup(LPCTSTR pStrGroupName, CControlUI* pControl)
  2000. {
  2001. LPVOID lp = m_mOptionGroup.Find(pStrGroupName);
  2002. if (lp)
  2003. {
  2004. CStdPtrArray* aOptionGroup = static_cast<CStdPtrArray*>(lp);
  2005. for (int i = 0; i < aOptionGroup->GetSize(); i++)
  2006. {
  2007. if (static_cast<CControlUI*>(aOptionGroup->GetAt(i)) == pControl)
  2008. {
  2009. return false;
  2010. }
  2011. }
  2012. aOptionGroup->Add(pControl);
  2013. }
  2014. else
  2015. {
  2016. CStdPtrArray* aOptionGroup = new CStdPtrArray(6);
  2017. aOptionGroup->Add(pControl);
  2018. m_mOptionGroup.Insert(pStrGroupName, aOptionGroup);
  2019. }
  2020. return true;
  2021. }
  2022. CStdPtrArray* CPaintManagerUI::GetOptionGroup(LPCTSTR pStrGroupName)
  2023. {
  2024. LPVOID lp = m_mOptionGroup.Find(pStrGroupName);
  2025. if (lp) return static_cast<CStdPtrArray*>(lp);
  2026. return NULL;
  2027. }
  2028. void CPaintManagerUI::RemoveOptionGroup(LPCTSTR pStrGroupName, CControlUI* pControl)
  2029. {
  2030. LPVOID lp = m_mOptionGroup.Find(pStrGroupName);
  2031. if (lp)
  2032. {
  2033. CStdPtrArray* aOptionGroup = static_cast<CStdPtrArray*>(lp);
  2034. if (aOptionGroup == NULL) return;
  2035. for (int i = 0; i < aOptionGroup->GetSize(); i++)
  2036. {
  2037. if (static_cast<CControlUI*>(aOptionGroup->GetAt(i)) == pControl)
  2038. {
  2039. aOptionGroup->Remove(i);
  2040. break;
  2041. }
  2042. }
  2043. if (aOptionGroup->IsEmpty())
  2044. {
  2045. delete aOptionGroup;
  2046. m_mOptionGroup.Remove(pStrGroupName);
  2047. }
  2048. }
  2049. }
  2050. void CPaintManagerUI::RemoveAllOptionGroups()
  2051. {
  2052. CStdPtrArray* aOptionGroup;
  2053. for (int i = 0; i < m_mOptionGroup.GetSize(); i++)
  2054. {
  2055. if (LPCTSTR key = m_mOptionGroup.GetAt(i))
  2056. {
  2057. aOptionGroup = static_cast<CStdPtrArray*>(m_mOptionGroup.Find(key));
  2058. delete aOptionGroup;
  2059. }
  2060. }
  2061. m_mOptionGroup.RemoveAll();
  2062. }
  2063. void CPaintManagerUI::MessageLoop()
  2064. {
  2065. MSG msg = { 0 };
  2066. while (::GetMessage(&msg, NULL, 0, 0))
  2067. {
  2068. if (!CPaintManagerUI::TranslateMessage(&msg))
  2069. {
  2070. ::TranslateMessage(&msg);
  2071. try
  2072. {
  2073. ::DispatchMessage(&msg);
  2074. }
  2075. catch (...)
  2076. {
  2077. DUITRACE(_T("EXCEPTION: %s(%d)\n"), __FILET__, __LINE__);
  2078. #ifdef _DEBUG
  2079. throw "CPaintManagerUI::MessageLoop";
  2080. #endif
  2081. }
  2082. }
  2083. }
  2084. }
  2085. void CPaintManagerUI::Term()
  2086. {
  2087. // 销毁资源管理器
  2088. CResourceManager::GetInstance()->Release();
  2089. CControlFactory::GetInstance()->Release();
  2090. // 清理共享资源
  2091. // 图片
  2092. TImageInfo* data;
  2093. for (int i = 0; i < m_SharedResInfo.m_ImageHash.GetSize(); i++)
  2094. {
  2095. if (LPCTSTR key = m_SharedResInfo.m_ImageHash.GetAt(i))
  2096. {
  2097. data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(key, false));
  2098. if (data)
  2099. {
  2100. CRenderEngine::FreeImage(data);
  2101. data = NULL;
  2102. }
  2103. }
  2104. }
  2105. m_SharedResInfo.m_ImageHash.RemoveAll();
  2106. // 字体
  2107. TFontInfo* pFontInfo;
  2108. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  2109. {
  2110. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  2111. {
  2112. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key, false));
  2113. if (pFontInfo)
  2114. {
  2115. ::DeleteObject(pFontInfo->hFont);
  2116. delete pFontInfo;
  2117. pFontInfo = NULL;
  2118. }
  2119. }
  2120. }
  2121. m_SharedResInfo.m_CustomFonts.RemoveAll();
  2122. // 默认字体
  2123. if (m_SharedResInfo.m_DefaultFontInfo.hFont != NULL)
  2124. {
  2125. ::DeleteObject(m_SharedResInfo.m_DefaultFontInfo.hFont);
  2126. }
  2127. // 样式
  2128. CDuiString* pStyle;
  2129. for (int i = 0; i < m_SharedResInfo.m_StyleHash.GetSize(); i++)
  2130. {
  2131. if (LPCTSTR key = m_SharedResInfo.m_StyleHash.GetAt(i))
  2132. {
  2133. pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(key, false));
  2134. if (pStyle)
  2135. {
  2136. delete pStyle;
  2137. pStyle = NULL;
  2138. }
  2139. }
  2140. }
  2141. m_SharedResInfo.m_StyleHash.RemoveAll();
  2142. // 样式
  2143. CDuiString* pAttr;
  2144. for (int i = 0; i < m_SharedResInfo.m_AttrHash.GetSize(); i++)
  2145. {
  2146. if (LPCTSTR key = m_SharedResInfo.m_AttrHash.GetAt(i))
  2147. {
  2148. pAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Find(key, false));
  2149. if (pAttr)
  2150. {
  2151. delete pAttr;
  2152. pAttr = NULL;
  2153. }
  2154. }
  2155. }
  2156. m_SharedResInfo.m_AttrHash.RemoveAll();
  2157. // 关闭ZIP
  2158. if (m_bCachedResourceZip && m_hResourceZip != NULL)
  2159. {
  2160. CloseZip((HZIP)m_hResourceZip);
  2161. m_hResourceZip = NULL;
  2162. }
  2163. if (m_cbZipBuf)
  2164. {
  2165. delete[] m_cbZipBuf;
  2166. m_cbZipBuf = nullptr;
  2167. }
  2168. }
  2169. CDPI* DuiLib::CPaintManagerUI::GetDPIObj()
  2170. {
  2171. if (m_pDPI == NULL)
  2172. {
  2173. m_pDPI = new CDPI;
  2174. }
  2175. return m_pDPI;
  2176. }
  2177. void DuiLib::CPaintManagerUI::SetDPI(int iDPI)
  2178. {
  2179. int scale1 = GetDPIObj()->GetScale();
  2180. GetDPIObj()->SetScale(iDPI);
  2181. int scale2 = GetDPIObj()->GetScale();
  2182. ResetDPIAssets();
  2183. RECT rcWnd = { 0 };
  2184. ::GetWindowRect(GetPaintWindow(), &rcWnd);
  2185. RECT* prcNewWindow = &rcWnd;
  2186. if (!::IsZoomed(GetPaintWindow()))
  2187. {
  2188. RECT rc = rcWnd;
  2189. rc.right = rcWnd.left + (rcWnd.right - rcWnd.left) * scale2 / scale1;
  2190. rc.bottom = rcWnd.top + (rcWnd.bottom - rcWnd.top) * scale2 / scale1;
  2191. prcNewWindow = &rc;
  2192. }
  2193. SetWindowPos(GetPaintWindow(), NULL, prcNewWindow->left, prcNewWindow->top, prcNewWindow->right - prcNewWindow->left, prcNewWindow->bottom - prcNewWindow->top, SWP_NOZORDER | SWP_NOACTIVATE);
  2194. if (GetRoot() != NULL) GetRoot()->NeedUpdate();
  2195. ::PostMessage(GetPaintWindow(), UIMSG_SET_DPI, 0, 0);
  2196. }
  2197. void DuiLib::CPaintManagerUI::SetAllDPI(int iDPI)
  2198. {
  2199. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  2200. {
  2201. CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  2202. pManager->SetDPI(iDPI);
  2203. }
  2204. }
  2205. void DuiLib::CPaintManagerUI::ResetDPIAssets()
  2206. {
  2207. RemoveAllDrawInfos();
  2208. RemoveAllImages();;
  2209. for (int it = 0; it < m_ResInfo.m_CustomFonts.GetSize(); it++)
  2210. {
  2211. TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(m_ResInfo.m_CustomFonts[it]));
  2212. RebuildFont(pFontInfo);
  2213. }
  2214. RebuildFont(&m_ResInfo.m_DefaultFontInfo);
  2215. for (int it = 0; it < m_SharedResInfo.m_CustomFonts.GetSize(); it++)
  2216. {
  2217. TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(m_SharedResInfo.m_CustomFonts[it]));
  2218. RebuildFont(pFontInfo);
  2219. }
  2220. RebuildFont(&m_SharedResInfo.m_DefaultFontInfo);
  2221. CStdPtrArray* richEditList = FindSubControlsByClass(GetRoot(), _T("RichEditUI"));
  2222. for (int i = 0; i < richEditList->GetSize(); i++)
  2223. {
  2224. CRichEditUI* pT = static_cast<CRichEditUI*>((*richEditList)[i]);
  2225. pT->SetFont(pT->GetFont());
  2226. }
  2227. }
  2228. void DuiLib::CPaintManagerUI::RebuildFont(TFontInfo* pFontInfo)
  2229. {
  2230. ::DeleteObject(pFontInfo->hFont);
  2231. LOGFONT lf = { 0 };
  2232. ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
  2233. _tcsncpy(lf.lfFaceName, pFontInfo->sFontName, LF_FACESIZE);
  2234. lf.lfCharSet = DEFAULT_CHARSET;
  2235. lf.lfHeight = -GetDPIObj()->Scale(pFontInfo->iSize);
  2236. lf.lfQuality = CLEARTYPE_QUALITY;
  2237. if (pFontInfo->bBold) lf.lfWeight += FW_BOLD;
  2238. if (pFontInfo->bUnderline) lf.lfUnderline = TRUE;
  2239. if (pFontInfo->bItalic) lf.lfItalic = TRUE;
  2240. HFONT hFont = ::CreateFontIndirect(&lf);
  2241. pFontInfo->hFont = hFont;
  2242. ::ZeroMemory(&(pFontInfo->tm), sizeof(pFontInfo->tm));
  2243. if (m_hDcPaint)
  2244. {
  2245. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
  2246. ::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
  2247. ::SelectObject(m_hDcPaint, hOldFont);
  2248. }
  2249. }
  2250. CControlUI* CPaintManagerUI::GetFocus() const
  2251. {
  2252. return m_pFocus;
  2253. }
  2254. void CPaintManagerUI::SetFocus(CControlUI* pControl)
  2255. {
  2256. // Paint manager window has focus?
  2257. HWND hFocusWnd = ::GetFocus();
  2258. if (!m_bNoActivate && hFocusWnd != m_hWndPaint && pControl != m_pFocus) ::SetFocus(m_hWndPaint);
  2259. // Already has focus?
  2260. if (pControl == m_pFocus) return;
  2261. // Remove focus from old control
  2262. if (m_pFocus != NULL)
  2263. {
  2264. TEventUI event = { 0 };
  2265. event.Type = UIEVENT_KILLFOCUS;
  2266. event.pSender = pControl;
  2267. event.dwTimestamp = ::GetTickCount();
  2268. m_pFocus->Event(event);
  2269. SendNotify(m_pFocus, DUI_MSGTYPE_KILLFOCUS);
  2270. m_pFocus = NULL;
  2271. }
  2272. if (pControl == NULL) return;
  2273. // Set focus to new control
  2274. if (pControl != NULL
  2275. && pControl->GetManager() == this
  2276. && pControl->IsVisible()
  2277. && pControl->IsEnabled())
  2278. {
  2279. m_pFocus = pControl;
  2280. TEventUI event = { 0 };
  2281. event.Type = UIEVENT_SETFOCUS;
  2282. event.pSender = pControl;
  2283. event.dwTimestamp = ::GetTickCount();
  2284. m_pFocus->Event(event);
  2285. SendNotify(m_pFocus, DUI_MSGTYPE_SETFOCUS);
  2286. }
  2287. }
  2288. void CPaintManagerUI::SetFocusNeeded(CControlUI* pControl)
  2289. {
  2290. if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
  2291. if (pControl == NULL) return;
  2292. if (m_pFocus != NULL)
  2293. {
  2294. TEventUI event = { 0 };
  2295. event.Type = UIEVENT_KILLFOCUS;
  2296. event.pSender = pControl;
  2297. event.dwTimestamp = ::GetTickCount();
  2298. m_pFocus->Event(event);
  2299. SendNotify(m_pFocus, DUI_MSGTYPE_KILLFOCUS);
  2300. m_pFocus = NULL;
  2301. }
  2302. FINDTABINFO info = { 0 };
  2303. info.pFocus = pControl;
  2304. info.bForward = false;
  2305. m_pFocus = m_pRoot->FindControl(__FindControlFromTab, &info, UIFIND_VISIBLE | UIFIND_ENABLED | UIFIND_ME_FIRST);
  2306. m_bFocusNeeded = true;
  2307. if (m_pRoot != NULL) m_pRoot->NeedUpdate();
  2308. }
  2309. bool CPaintManagerUI::SetTimer(CControlUI* pControl, UINT nTimerID, UINT uElapse)
  2310. {
  2311. ASSERT(pControl != NULL);
  2312. ASSERT(uElapse > 0);
  2313. for (int i = 0; i < m_aTimers.GetSize(); i++)
  2314. {
  2315. TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
  2316. if (pTimer->pSender == pControl
  2317. && pTimer->hWnd == m_hWndPaint
  2318. && pTimer->nLocalID == nTimerID)
  2319. {
  2320. if (pTimer->bKilled == true)
  2321. {
  2322. if (::SetTimer(m_hWndPaint, pTimer->uWinTimer, uElapse, NULL))
  2323. {
  2324. pTimer->bKilled = false;
  2325. return true;
  2326. }
  2327. return false;
  2328. }
  2329. return false;
  2330. }
  2331. }
  2332. m_uTimerID = (++m_uTimerID) % 0xF0; //0xf1-0xfe特殊用途
  2333. if (!::SetTimer(m_hWndPaint, m_uTimerID, uElapse, NULL)) return FALSE;
  2334. TIMERINFO* pTimer = new TIMERINFO;
  2335. if (pTimer == NULL) return FALSE;
  2336. pTimer->hWnd = m_hWndPaint;
  2337. pTimer->pSender = pControl;
  2338. pTimer->nLocalID = nTimerID;
  2339. pTimer->uWinTimer = m_uTimerID;
  2340. pTimer->bKilled = false;
  2341. return m_aTimers.Add(pTimer);
  2342. }
  2343. bool CPaintManagerUI::KillTimer(CControlUI* pControl, UINT nTimerID)
  2344. {
  2345. ASSERT(pControl != NULL);
  2346. for (int i = 0; i < m_aTimers.GetSize(); i++)
  2347. {
  2348. TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
  2349. if (pTimer->pSender == pControl
  2350. && pTimer->hWnd == m_hWndPaint
  2351. && pTimer->nLocalID == nTimerID)
  2352. {
  2353. if (pTimer->bKilled == false)
  2354. {
  2355. if (::IsWindow(m_hWndPaint)) ::KillTimer(pTimer->hWnd, pTimer->uWinTimer);
  2356. pTimer->bKilled = true;
  2357. return true;
  2358. }
  2359. }
  2360. }
  2361. return false;
  2362. }
  2363. void CPaintManagerUI::KillTimer(CControlUI* pControl)
  2364. {
  2365. ASSERT(pControl != NULL);
  2366. int count = m_aTimers.GetSize();
  2367. for (int i = 0, j = 0; i < count; i++)
  2368. {
  2369. TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i - j]);
  2370. if (pTimer->pSender == pControl && pTimer->hWnd == m_hWndPaint)
  2371. {
  2372. if (pTimer->bKilled == false) ::KillTimer(pTimer->hWnd, pTimer->uWinTimer);
  2373. delete pTimer;
  2374. m_aTimers.Remove(i - j);
  2375. j++;
  2376. }
  2377. }
  2378. }
  2379. void CPaintManagerUI::RemoveAllTimers()
  2380. {
  2381. for (int i = 0; i < m_aTimers.GetSize(); i++)
  2382. {
  2383. TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
  2384. if (pTimer->hWnd == m_hWndPaint)
  2385. {
  2386. if (pTimer->bKilled == false)
  2387. {
  2388. if (::IsWindow(m_hWndPaint)) ::KillTimer(m_hWndPaint, pTimer->uWinTimer);
  2389. }
  2390. delete pTimer;
  2391. }
  2392. }
  2393. m_aTimers.Empty();
  2394. }
  2395. void CPaintManagerUI::SetCapture()
  2396. {
  2397. ::SetCapture(m_hWndPaint);
  2398. m_bMouseCapture = true;
  2399. }
  2400. void CPaintManagerUI::ReleaseCapture()
  2401. {
  2402. ::ReleaseCapture();
  2403. m_bMouseCapture = false;
  2404. m_bDragMode = false;
  2405. }
  2406. bool CPaintManagerUI::IsCaptured()
  2407. {
  2408. return m_bMouseCapture;
  2409. }
  2410. bool CPaintManagerUI::IsPainting()
  2411. {
  2412. return m_bIsPainting;
  2413. }
  2414. void CPaintManagerUI::SetPainting(bool bIsPainting)
  2415. {
  2416. m_bIsPainting = bIsPainting;
  2417. }
  2418. bool CPaintManagerUI::SetNextTabControl(bool bForward)
  2419. {
  2420. // If we're in the process of restructuring the layout we can delay the
  2421. // focus calulation until the next repaint.
  2422. if (m_bUpdateNeeded && bForward)
  2423. {
  2424. m_bFocusNeeded = true;
  2425. ::InvalidateRect(m_hWndPaint, NULL, FALSE);
  2426. return true;
  2427. }
  2428. // Find next/previous tabbable control
  2429. FINDTABINFO info1 = { 0 };
  2430. info1.pFocus = m_pFocus;
  2431. info1.bForward = bForward;
  2432. CControlUI* pControl = m_pRoot->FindControl(__FindControlFromTab, &info1, UIFIND_VISIBLE | UIFIND_ENABLED | UIFIND_ME_FIRST);
  2433. if (pControl == NULL)
  2434. {
  2435. if (bForward)
  2436. {
  2437. // Wrap around
  2438. FINDTABINFO info2 = { 0 };
  2439. info2.pFocus = bForward ? NULL : info1.pLast;
  2440. info2.bForward = bForward;
  2441. pControl = m_pRoot->FindControl(__FindControlFromTab, &info2, UIFIND_VISIBLE | UIFIND_ENABLED | UIFIND_ME_FIRST);
  2442. }
  2443. else
  2444. {
  2445. pControl = info1.pLast;
  2446. }
  2447. }
  2448. if (pControl != NULL) SetFocus(pControl);
  2449. m_bFocusNeeded = false;
  2450. return true;
  2451. }
  2452. bool CPaintManagerUI::AddNotifier(INotifyUI* pNotifier)
  2453. {
  2454. ASSERT(m_aNotifiers.Find(pNotifier) < 0);
  2455. return m_aNotifiers.Add(pNotifier);
  2456. }
  2457. bool CPaintManagerUI::RemoveNotifier(INotifyUI* pNotifier)
  2458. {
  2459. for (int i = 0; i < m_aNotifiers.GetSize(); i++)
  2460. {
  2461. if (static_cast<INotifyUI*>(m_aNotifiers[i]) == pNotifier)
  2462. {
  2463. return m_aNotifiers.Remove(i);
  2464. }
  2465. }
  2466. return false;
  2467. }
  2468. bool CPaintManagerUI::AddPreMessageFilter(IMessageFilterUI* pFilter)
  2469. {
  2470. ASSERT(m_aPreMessageFilters.Find(pFilter) < 0);
  2471. return m_aPreMessageFilters.Add(pFilter);
  2472. }
  2473. bool CPaintManagerUI::RemovePreMessageFilter(IMessageFilterUI* pFilter)
  2474. {
  2475. for (int i = 0; i < m_aPreMessageFilters.GetSize(); i++)
  2476. {
  2477. if (static_cast<IMessageFilterUI*>(m_aPreMessageFilters[i]) == pFilter)
  2478. {
  2479. return m_aPreMessageFilters.Remove(i);
  2480. }
  2481. }
  2482. return false;
  2483. }
  2484. bool CPaintManagerUI::AddMessageFilter(IMessageFilterUI* pFilter)
  2485. {
  2486. ASSERT(m_aMessageFilters.Find(pFilter) < 0);
  2487. return m_aMessageFilters.Add(pFilter);
  2488. }
  2489. bool CPaintManagerUI::RemoveMessageFilter(IMessageFilterUI* pFilter)
  2490. {
  2491. for (int i = 0; i < m_aMessageFilters.GetSize(); i++)
  2492. {
  2493. if (static_cast<IMessageFilterUI*>(m_aMessageFilters[i]) == pFilter)
  2494. {
  2495. return m_aMessageFilters.Remove(i);
  2496. }
  2497. }
  2498. return false;
  2499. }
  2500. int CPaintManagerUI::GetPostPaintCount() const
  2501. {
  2502. return m_aPostPaintControls.GetSize();
  2503. }
  2504. bool CPaintManagerUI::IsPostPaint(CControlUI* pControl)
  2505. {
  2506. return m_aPostPaintControls.Find(pControl) >= 0;
  2507. }
  2508. bool CPaintManagerUI::AddPostPaint(CControlUI* pControl)
  2509. {
  2510. ASSERT(m_aPostPaintControls.Find(pControl) < 0);
  2511. return m_aPostPaintControls.Add(pControl);
  2512. }
  2513. bool CPaintManagerUI::RemovePostPaint(CControlUI* pControl)
  2514. {
  2515. for (int i = 0; i < m_aPostPaintControls.GetSize(); i++)
  2516. {
  2517. if (static_cast<CControlUI*>(m_aPostPaintControls[i]) == pControl)
  2518. {
  2519. return m_aPostPaintControls.Remove(i);
  2520. }
  2521. }
  2522. return false;
  2523. }
  2524. bool CPaintManagerUI::SetPostPaintIndex(CControlUI* pControl, int iIndex)
  2525. {
  2526. RemovePostPaint(pControl);
  2527. return m_aPostPaintControls.InsertAt(iIndex, pControl);
  2528. }
  2529. int CPaintManagerUI::GetNativeWindowCount() const
  2530. {
  2531. return m_aNativeWindow.GetSize();
  2532. }
  2533. bool CPaintManagerUI::AddNativeWindow(CControlUI* pControl, HWND hChildWnd)
  2534. {
  2535. if (pControl == NULL || hChildWnd == NULL) return false;
  2536. RECT rcChildWnd = GetNativeWindowRect(hChildWnd);
  2537. Invalidate(rcChildWnd);
  2538. if (m_aNativeWindow.Find(hChildWnd) >= 0) return false;
  2539. if (m_aNativeWindow.Add(hChildWnd))
  2540. {
  2541. m_aNativeWindowControl.Add(pControl);
  2542. return true;
  2543. }
  2544. return false;
  2545. }
  2546. bool CPaintManagerUI::RemoveNativeWindow(HWND hChildWnd)
  2547. {
  2548. for (int i = 0; i < m_aNativeWindow.GetSize(); i++)
  2549. {
  2550. if (static_cast<HWND>(m_aNativeWindow[i]) == hChildWnd)
  2551. {
  2552. if (m_aNativeWindow.Remove(i))
  2553. {
  2554. m_aNativeWindowControl.Remove(i);
  2555. return true;
  2556. }
  2557. return false;
  2558. }
  2559. }
  2560. return false;
  2561. }
  2562. RECT CPaintManagerUI::GetNativeWindowRect(HWND hChildWnd)
  2563. {
  2564. RECT rcChildWnd;
  2565. ::GetWindowRect(hChildWnd, &rcChildWnd);
  2566. ::ScreenToClient(m_hWndPaint, (LPPOINT)(&rcChildWnd));
  2567. ::ScreenToClient(m_hWndPaint, (LPPOINT)(&rcChildWnd) + 1);
  2568. return rcChildWnd;
  2569. }
  2570. void CPaintManagerUI::AddDelayedCleanup(CControlUI* pControl)
  2571. {
  2572. if (pControl == NULL) return;
  2573. pControl->SetManager(this, NULL, false);
  2574. m_aDelayedCleanup.Add(pControl);
  2575. PostAsyncNotify();
  2576. }
  2577. void CPaintManagerUI::AddMouseLeaveNeeded(CControlUI* pControl)
  2578. {
  2579. if (pControl == NULL) return;
  2580. for (int i = 0; i < m_aNeedMouseLeaveNeeded.GetSize(); i++)
  2581. {
  2582. if (static_cast<CControlUI*>(m_aNeedMouseLeaveNeeded[i]) == pControl)
  2583. {
  2584. return;
  2585. }
  2586. }
  2587. m_aNeedMouseLeaveNeeded.Add(pControl);
  2588. }
  2589. bool CPaintManagerUI::RemoveMouseLeaveNeeded(CControlUI* pControl)
  2590. {
  2591. if (pControl == NULL) return false;
  2592. for (int i = 0; i < m_aNeedMouseLeaveNeeded.GetSize(); i++)
  2593. {
  2594. if (static_cast<CControlUI*>(m_aNeedMouseLeaveNeeded[i]) == pControl)
  2595. {
  2596. return m_aNeedMouseLeaveNeeded.Remove(i);
  2597. }
  2598. }
  2599. return false;
  2600. }
  2601. void CPaintManagerUI::SendNotify(CControlUI* pControl, LPCTSTR pstrMessage, WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/, bool bAsync /*= false*/)
  2602. {
  2603. TNotifyUI Msg;
  2604. Msg.pSender = pControl;
  2605. Msg.sType = pstrMessage;
  2606. Msg.wParam = wParam;
  2607. Msg.lParam = lParam;
  2608. SendNotify(Msg, bAsync);
  2609. }
  2610. void CPaintManagerUI::SendNotify(TNotifyUI& Msg, bool bAsync /*= false*/)
  2611. {
  2612. Msg.ptMouse = m_ptLastMousePos;
  2613. Msg.dwTimestamp = ::GetTickCount();
  2614. if (m_bUsedVirtualWnd)
  2615. {
  2616. Msg.sVirtualWnd = Msg.pSender->GetVirtualWnd();
  2617. }
  2618. if (!bAsync)
  2619. {
  2620. // Send to all listeners
  2621. if (Msg.pSender != NULL)
  2622. {
  2623. if (Msg.pSender->OnNotify) Msg.pSender->OnNotify(&Msg);
  2624. }
  2625. for (int i = 0; i < m_aNotifiers.GetSize(); i++)
  2626. {
  2627. static_cast<INotifyUI*>(m_aNotifiers[i])->Notify(Msg);
  2628. }
  2629. }
  2630. else
  2631. {
  2632. TNotifyUI* pMsg = new TNotifyUI;
  2633. pMsg->sVirtualWnd = Msg.sVirtualWnd;
  2634. pMsg->pSender = Msg.pSender;
  2635. pMsg->sType = Msg.sType;
  2636. pMsg->wParam = Msg.wParam;
  2637. pMsg->lParam = Msg.lParam;
  2638. pMsg->ptMouse = Msg.ptMouse;
  2639. pMsg->dwTimestamp = Msg.dwTimestamp;
  2640. m_aAsyncNotify.Add(pMsg);
  2641. PostAsyncNotify();
  2642. }
  2643. }
  2644. bool CPaintManagerUI::IsForceUseSharedRes() const
  2645. {
  2646. return m_bForceUseSharedRes;
  2647. }
  2648. void CPaintManagerUI::SetForceUseSharedRes(bool bForce)
  2649. {
  2650. m_bForceUseSharedRes = bForce;
  2651. }
  2652. DWORD CPaintManagerUI::GetDefaultDisabledColor() const
  2653. {
  2654. return m_ResInfo.m_dwDefaultDisabledColor;
  2655. }
  2656. void CPaintManagerUI::SetDefaultDisabledColor(DWORD dwColor, bool bShared)
  2657. {
  2658. if (bShared)
  2659. {
  2660. if (m_ResInfo.m_dwDefaultDisabledColor == m_SharedResInfo.m_dwDefaultDisabledColor)
  2661. m_ResInfo.m_dwDefaultDisabledColor = dwColor;
  2662. m_SharedResInfo.m_dwDefaultDisabledColor = dwColor;
  2663. }
  2664. else
  2665. {
  2666. m_ResInfo.m_dwDefaultDisabledColor = dwColor;
  2667. }
  2668. }
  2669. DWORD CPaintManagerUI::GetDefaultFontColor() const
  2670. {
  2671. return m_ResInfo.m_dwDefaultFontColor;
  2672. }
  2673. void CPaintManagerUI::SetDefaultFontColor(DWORD dwColor, bool bShared)
  2674. {
  2675. if (bShared)
  2676. {
  2677. if (m_ResInfo.m_dwDefaultFontColor == m_SharedResInfo.m_dwDefaultFontColor)
  2678. m_ResInfo.m_dwDefaultFontColor = dwColor;
  2679. m_SharedResInfo.m_dwDefaultFontColor = dwColor;
  2680. }
  2681. else
  2682. {
  2683. m_ResInfo.m_dwDefaultFontColor = dwColor;
  2684. }
  2685. }
  2686. DWORD CPaintManagerUI::GetDefaultLinkFontColor() const
  2687. {
  2688. return m_ResInfo.m_dwDefaultLinkFontColor;
  2689. }
  2690. void CPaintManagerUI::SetDefaultLinkFontColor(DWORD dwColor, bool bShared)
  2691. {
  2692. if (bShared)
  2693. {
  2694. if (m_ResInfo.m_dwDefaultLinkFontColor == m_SharedResInfo.m_dwDefaultLinkFontColor)
  2695. m_ResInfo.m_dwDefaultLinkFontColor = dwColor;
  2696. m_SharedResInfo.m_dwDefaultLinkFontColor = dwColor;
  2697. }
  2698. else
  2699. {
  2700. m_ResInfo.m_dwDefaultLinkFontColor = dwColor;
  2701. }
  2702. }
  2703. DWORD CPaintManagerUI::GetDefaultLinkHoverFontColor() const
  2704. {
  2705. return m_ResInfo.m_dwDefaultLinkHoverFontColor;
  2706. }
  2707. void CPaintManagerUI::SetDefaultLinkHoverFontColor(DWORD dwColor, bool bShared)
  2708. {
  2709. if (bShared)
  2710. {
  2711. if (m_ResInfo.m_dwDefaultLinkHoverFontColor == m_SharedResInfo.m_dwDefaultLinkHoverFontColor)
  2712. m_ResInfo.m_dwDefaultLinkHoverFontColor = dwColor;
  2713. m_SharedResInfo.m_dwDefaultLinkHoverFontColor = dwColor;
  2714. }
  2715. else
  2716. {
  2717. m_ResInfo.m_dwDefaultLinkHoverFontColor = dwColor;
  2718. }
  2719. }
  2720. DWORD CPaintManagerUI::GetDefaultSelectedBkColor() const
  2721. {
  2722. return m_ResInfo.m_dwDefaultSelectedBkColor;
  2723. }
  2724. void CPaintManagerUI::SetDefaultSelectedBkColor(DWORD dwColor, bool bShared)
  2725. {
  2726. if (bShared)
  2727. {
  2728. if (m_ResInfo.m_dwDefaultSelectedBkColor == m_SharedResInfo.m_dwDefaultSelectedBkColor)
  2729. m_ResInfo.m_dwDefaultSelectedBkColor = dwColor;
  2730. m_SharedResInfo.m_dwDefaultSelectedBkColor = dwColor;
  2731. }
  2732. else
  2733. {
  2734. m_ResInfo.m_dwDefaultSelectedBkColor = dwColor;
  2735. }
  2736. }
  2737. TFontInfo* CPaintManagerUI::GetDefaultFontInfo()
  2738. {
  2739. if (m_ResInfo.m_DefaultFontInfo.sFontName.IsEmpty())
  2740. {
  2741. if (m_SharedResInfo.m_DefaultFontInfo.tm.tmHeight == 0)
  2742. {
  2743. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, m_SharedResInfo.m_DefaultFontInfo.hFont);
  2744. ::GetTextMetrics(m_hDcPaint, &m_SharedResInfo.m_DefaultFontInfo.tm);
  2745. ::SelectObject(m_hDcPaint, hOldFont);
  2746. }
  2747. return &m_SharedResInfo.m_DefaultFontInfo;
  2748. }
  2749. else
  2750. {
  2751. if (m_ResInfo.m_DefaultFontInfo.tm.tmHeight == 0)
  2752. {
  2753. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, m_ResInfo.m_DefaultFontInfo.hFont);
  2754. ::GetTextMetrics(m_hDcPaint, &m_ResInfo.m_DefaultFontInfo.tm);
  2755. ::SelectObject(m_hDcPaint, hOldFont);
  2756. }
  2757. return &m_ResInfo.m_DefaultFontInfo;
  2758. }
  2759. }
  2760. void CPaintManagerUI::SetDefaultFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared)
  2761. {
  2762. LOGFONT lf = { 0 };
  2763. ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
  2764. if (lstrlen(pStrFontName) > 0)
  2765. {
  2766. TCHAR szFaceName[32] = { 0 };//_T("@");
  2767. _tcsncat(szFaceName, pStrFontName, LF_FACESIZE);
  2768. _tcsncpy(lf.lfFaceName, szFaceName, LF_FACESIZE);
  2769. }
  2770. lf.lfCharSet = DEFAULT_CHARSET;
  2771. lf.lfHeight = -GetDPIObj()->Scale(nSize);;
  2772. if (bBold) lf.lfWeight += FW_BOLD;
  2773. if (bUnderline) lf.lfUnderline = TRUE;
  2774. if (bItalic) lf.lfItalic = TRUE;
  2775. if (bStrikeout) lf.lfStrikeOut = TRUE;
  2776. HFONT hFont = ::CreateFontIndirect(&lf);
  2777. if (hFont == NULL) return;
  2778. if (bShared)
  2779. {
  2780. ::DeleteObject(m_SharedResInfo.m_DefaultFontInfo.hFont);
  2781. m_SharedResInfo.m_DefaultFontInfo.hFont = hFont;
  2782. m_SharedResInfo.m_DefaultFontInfo.sFontName = lf.lfFaceName;
  2783. m_SharedResInfo.m_DefaultFontInfo.iSize = nSize;
  2784. m_SharedResInfo.m_DefaultFontInfo.bBold = bBold;
  2785. m_SharedResInfo.m_DefaultFontInfo.bUnderline = bUnderline;
  2786. m_SharedResInfo.m_DefaultFontInfo.bItalic = bItalic;
  2787. m_SharedResInfo.m_DefaultFontInfo.bStrikeout = bStrikeout;
  2788. ::ZeroMemory(&m_SharedResInfo.m_DefaultFontInfo.tm, sizeof(m_SharedResInfo.m_DefaultFontInfo.tm));
  2789. if (m_hDcPaint)
  2790. {
  2791. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
  2792. ::GetTextMetrics(m_hDcPaint, &m_SharedResInfo.m_DefaultFontInfo.tm);
  2793. ::SelectObject(m_hDcPaint, hOldFont);
  2794. }
  2795. }
  2796. else
  2797. {
  2798. ::DeleteObject(m_ResInfo.m_DefaultFontInfo.hFont);
  2799. m_ResInfo.m_DefaultFontInfo.hFont = hFont;
  2800. m_ResInfo.m_DefaultFontInfo.sFontName = lf.lfFaceName;
  2801. m_ResInfo.m_DefaultFontInfo.iSize = nSize;
  2802. m_ResInfo.m_DefaultFontInfo.bBold = bBold;
  2803. m_ResInfo.m_DefaultFontInfo.bUnderline = bUnderline;
  2804. m_ResInfo.m_DefaultFontInfo.bItalic = bItalic;
  2805. m_ResInfo.m_DefaultFontInfo.bStrikeout = bStrikeout;
  2806. ::ZeroMemory(&m_ResInfo.m_DefaultFontInfo.tm, sizeof(m_ResInfo.m_DefaultFontInfo.tm));
  2807. if (m_hDcPaint)
  2808. {
  2809. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
  2810. ::GetTextMetrics(m_hDcPaint, &m_ResInfo.m_DefaultFontInfo.tm);
  2811. ::SelectObject(m_hDcPaint, hOldFont);
  2812. }
  2813. }
  2814. }
  2815. DWORD CPaintManagerUI::GetCustomFontCount(bool bShared) const
  2816. {
  2817. if (bShared)
  2818. return m_SharedResInfo.m_CustomFonts.GetSize();
  2819. else
  2820. return m_ResInfo.m_CustomFonts.GetSize();
  2821. }
  2822. HFONT CPaintManagerUI::AddFont(int id, LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared)
  2823. {
  2824. LOGFONT lf = { 0 };
  2825. ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
  2826. if (lstrlen(pStrFontName) > 0)
  2827. {
  2828. TCHAR szFaceName[32] = { 0 };//_T("@");
  2829. _tcsncat(szFaceName, pStrFontName, LF_FACESIZE);
  2830. _tcsncpy(lf.lfFaceName, szFaceName, LF_FACESIZE);
  2831. }
  2832. lf.lfCharSet = DEFAULT_CHARSET;
  2833. lf.lfHeight = -GetDPIObj()->Scale(nSize);
  2834. if (bBold) lf.lfWeight = FW_BOLD;
  2835. if (bUnderline) lf.lfUnderline = TRUE;
  2836. if (bItalic) lf.lfItalic = TRUE;
  2837. if (bStrikeout) lf.lfStrikeOut = TRUE;
  2838. HFONT hFont = ::CreateFontIndirect(&lf);
  2839. if (hFont == NULL) return NULL;
  2840. TFontInfo* pFontInfo = new TFontInfo;
  2841. if (!pFontInfo) return false;
  2842. ::ZeroMemory(pFontInfo, sizeof(TFontInfo));
  2843. pFontInfo->hFont = hFont;
  2844. pFontInfo->sFontName = lf.lfFaceName;
  2845. pFontInfo->iSize = nSize;
  2846. pFontInfo->bBold = bBold;
  2847. pFontInfo->bUnderline = bUnderline;
  2848. pFontInfo->bItalic = bItalic;
  2849. pFontInfo->bStrikeout = bStrikeout;
  2850. if (m_hDcPaint)
  2851. {
  2852. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
  2853. ::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
  2854. ::SelectObject(m_hDcPaint, hOldFont);
  2855. }
  2856. TCHAR idBuffer[16];
  2857. ::ZeroMemory(idBuffer, sizeof(idBuffer));
  2858. _itot(id, idBuffer, 10);
  2859. if (bShared || m_bForceUseSharedRes)
  2860. {
  2861. TFontInfo* pOldFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(idBuffer));
  2862. if (pOldFontInfo)
  2863. {
  2864. ::DeleteObject(pOldFontInfo->hFont);
  2865. delete pOldFontInfo;
  2866. m_SharedResInfo.m_CustomFonts.Remove(idBuffer);
  2867. }
  2868. if (!m_SharedResInfo.m_CustomFonts.Insert(idBuffer, pFontInfo))
  2869. {
  2870. ::DeleteObject(hFont);
  2871. delete pFontInfo;
  2872. return NULL;
  2873. }
  2874. }
  2875. else
  2876. {
  2877. TFontInfo* pOldFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(idBuffer));
  2878. if (pOldFontInfo)
  2879. {
  2880. ::DeleteObject(pOldFontInfo->hFont);
  2881. delete pOldFontInfo;
  2882. m_ResInfo.m_CustomFonts.Remove(idBuffer);
  2883. }
  2884. if (!m_ResInfo.m_CustomFonts.Insert(idBuffer, pFontInfo))
  2885. {
  2886. ::DeleteObject(hFont);
  2887. delete pFontInfo;
  2888. return NULL;
  2889. }
  2890. }
  2891. return hFont;
  2892. }
  2893. void CPaintManagerUI::AddFontArray(LPCTSTR pstrPath)
  2894. {
  2895. LPBYTE pData = NULL;
  2896. DWORD dwSize = 0;
  2897. do
  2898. {
  2899. CDuiString sFile = CPaintManagerUI::GetResourcePath();
  2900. if (CPaintManagerUI::GetResourceZip().IsEmpty())
  2901. {
  2902. sFile += pstrPath;
  2903. HANDLE hFile = ::CreateFile(sFile.GetData(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
  2904. FILE_ATTRIBUTE_NORMAL, NULL);
  2905. if (hFile == INVALID_HANDLE_VALUE) break;
  2906. dwSize = ::GetFileSize(hFile, NULL);
  2907. if (dwSize == 0) break;
  2908. DWORD dwRead = 0;
  2909. pData = new BYTE[dwSize];
  2910. ::ReadFile(hFile, pData, dwSize, &dwRead, NULL);
  2911. ::CloseHandle(hFile);
  2912. if (dwRead != dwSize)
  2913. {
  2914. delete[] pData;
  2915. pData = NULL;
  2916. break;
  2917. }
  2918. }
  2919. else
  2920. {
  2921. sFile += CPaintManagerUI::GetResourceZip();
  2922. HZIP hz = NULL;
  2923. if (CPaintManagerUI::IsCachedResourceZip()) hz = (HZIP)CPaintManagerUI::GetResourceZipHandle();
  2924. else
  2925. {
  2926. CDuiString sFilePwd = CPaintManagerUI::GetResourceZipPwd();
  2927. #ifdef UNICODE
  2928. char* pwd = w2a((wchar_t*)sFilePwd.GetData());
  2929. hz = OpenZip(sFile.GetData(), pwd);
  2930. if (pwd) delete[] pwd;
  2931. #else
  2932. hz = OpenZip(sFile.GetData(), sFilePwd.GetData());
  2933. #endif
  2934. }
  2935. if (hz == NULL) break;
  2936. ZIPENTRY ze;
  2937. int i = 0;
  2938. CDuiString key = pstrPath;
  2939. key.Replace(_T("\\"), _T("/"));
  2940. if (FindZipItem(hz, key, true, &i, &ze) != 0) break;
  2941. dwSize = ze.unc_size;
  2942. if (dwSize == 0) break;
  2943. pData = new BYTE[dwSize];
  2944. int res = UnzipItem(hz, i, pData, dwSize);
  2945. if (res != 0x00000000 && res != 0x00000600)
  2946. {
  2947. delete[] pData;
  2948. pData = NULL;
  2949. if (!CPaintManagerUI::IsCachedResourceZip()) CloseZip(hz);
  2950. break;
  2951. }
  2952. if (!CPaintManagerUI::IsCachedResourceZip()) CloseZip(hz);
  2953. }
  2954. } while (0);
  2955. while (!pData)
  2956. {
  2957. //读不到图片, 则直接去读取bitmap.m_lpstr指向的路径
  2958. HANDLE hFile = ::CreateFile(pstrPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  2959. if (hFile == INVALID_HANDLE_VALUE) break;
  2960. dwSize = ::GetFileSize(hFile, NULL);
  2961. if (dwSize == 0) break;
  2962. DWORD dwRead = 0;
  2963. pData = new BYTE[dwSize];
  2964. ::ReadFile(hFile, pData, dwSize, &dwRead, NULL);
  2965. ::CloseHandle(hFile);
  2966. if (dwRead != dwSize)
  2967. {
  2968. delete[] pData;
  2969. pData = NULL;
  2970. }
  2971. break;
  2972. }
  2973. DWORD nFonts;
  2974. HANDLE hFont = ::AddFontMemResourceEx(pData, dwSize, NULL, &nFonts);
  2975. delete[] pData;
  2976. pData = NULL;
  2977. m_aFonts.Add(hFont);
  2978. }
  2979. HFONT CPaintManagerUI::GetFont(int id)
  2980. {
  2981. if (id < 0) return GetDefaultFontInfo()->hFont;
  2982. TCHAR idBuffer[16];
  2983. ::ZeroMemory(idBuffer, sizeof(idBuffer));
  2984. _itot(id, idBuffer, 10);
  2985. TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(idBuffer));
  2986. if (!pFontInfo) pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(idBuffer));
  2987. if (!pFontInfo) return GetDefaultFontInfo()->hFont;
  2988. return pFontInfo->hFont;
  2989. }
  2990. HFONT CPaintManagerUI::GetFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout)
  2991. {
  2992. TFontInfo* pFontInfo = NULL;
  2993. for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
  2994. {
  2995. if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
  2996. {
  2997. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
  2998. if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
  2999. pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
  3000. && pFontInfo->bStrikeout == bStrikeout)
  3001. return pFontInfo->hFont;
  3002. }
  3003. }
  3004. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  3005. {
  3006. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  3007. {
  3008. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
  3009. if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
  3010. pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
  3011. && pFontInfo->bStrikeout == bStrikeout)
  3012. return pFontInfo->hFont;
  3013. }
  3014. }
  3015. return NULL;
  3016. }
  3017. int CPaintManagerUI::GetFontIndex(HFONT hFont, bool bShared)
  3018. {
  3019. TFontInfo* pFontInfo = NULL;
  3020. if (bShared)
  3021. {
  3022. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  3023. {
  3024. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  3025. {
  3026. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
  3027. if (pFontInfo && pFontInfo->hFont == hFont) return _ttoi(key);
  3028. }
  3029. }
  3030. }
  3031. else
  3032. {
  3033. for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
  3034. {
  3035. if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
  3036. {
  3037. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
  3038. if (pFontInfo && pFontInfo->hFont == hFont) return _ttoi(key);
  3039. }
  3040. }
  3041. }
  3042. return -1;
  3043. }
  3044. int CPaintManagerUI::GetFontIndex(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared)
  3045. {
  3046. TFontInfo* pFontInfo = NULL;
  3047. if (bShared)
  3048. {
  3049. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  3050. {
  3051. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  3052. {
  3053. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
  3054. if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
  3055. pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
  3056. && pFontInfo->bStrikeout == bStrikeout)
  3057. return _ttoi(key);
  3058. }
  3059. }
  3060. }
  3061. else
  3062. {
  3063. for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
  3064. {
  3065. if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
  3066. {
  3067. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
  3068. if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
  3069. pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
  3070. && pFontInfo->bStrikeout == bStrikeout)
  3071. return _ttoi(key);
  3072. }
  3073. }
  3074. }
  3075. return -1;
  3076. }
  3077. void CPaintManagerUI::RemoveFont(HFONT hFont, bool bShared)
  3078. {
  3079. TFontInfo* pFontInfo = NULL;
  3080. if (bShared)
  3081. {
  3082. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  3083. {
  3084. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  3085. {
  3086. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
  3087. if (pFontInfo && pFontInfo->hFont == hFont)
  3088. {
  3089. ::DeleteObject(pFontInfo->hFont);
  3090. delete pFontInfo;
  3091. m_SharedResInfo.m_CustomFonts.Remove(key);
  3092. return;
  3093. }
  3094. }
  3095. }
  3096. }
  3097. else
  3098. {
  3099. for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
  3100. {
  3101. if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
  3102. {
  3103. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
  3104. if (pFontInfo && pFontInfo->hFont == hFont)
  3105. {
  3106. ::DeleteObject(pFontInfo->hFont);
  3107. delete pFontInfo;
  3108. m_ResInfo.m_CustomFonts.Remove(key);
  3109. return;
  3110. }
  3111. }
  3112. }
  3113. }
  3114. }
  3115. void CPaintManagerUI::RemoveFont(int id, bool bShared)
  3116. {
  3117. TCHAR idBuffer[16];
  3118. ::ZeroMemory(idBuffer, sizeof(idBuffer));
  3119. _itot(id, idBuffer, 10);
  3120. TFontInfo* pFontInfo = NULL;
  3121. if (bShared)
  3122. {
  3123. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(idBuffer));
  3124. if (pFontInfo)
  3125. {
  3126. ::DeleteObject(pFontInfo->hFont);
  3127. delete pFontInfo;
  3128. m_SharedResInfo.m_CustomFonts.Remove(idBuffer);
  3129. }
  3130. }
  3131. else
  3132. {
  3133. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(idBuffer));
  3134. if (pFontInfo)
  3135. {
  3136. ::DeleteObject(pFontInfo->hFont);
  3137. delete pFontInfo;
  3138. m_ResInfo.m_CustomFonts.Remove(idBuffer);
  3139. }
  3140. }
  3141. }
  3142. void CPaintManagerUI::RemoveAllFonts(bool bShared)
  3143. {
  3144. TFontInfo* pFontInfo;
  3145. if (bShared)
  3146. {
  3147. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  3148. {
  3149. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  3150. {
  3151. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key, false));
  3152. if (pFontInfo)
  3153. {
  3154. ::DeleteObject(pFontInfo->hFont);
  3155. delete pFontInfo;
  3156. }
  3157. }
  3158. }
  3159. m_SharedResInfo.m_CustomFonts.RemoveAll();
  3160. }
  3161. else
  3162. {
  3163. for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
  3164. {
  3165. if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
  3166. {
  3167. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key, false));
  3168. if (pFontInfo)
  3169. {
  3170. ::DeleteObject(pFontInfo->hFont);
  3171. delete pFontInfo;
  3172. }
  3173. }
  3174. }
  3175. m_ResInfo.m_CustomFonts.RemoveAll();
  3176. }
  3177. }
  3178. TFontInfo* CPaintManagerUI::GetFontInfo(int id)
  3179. {
  3180. if (id < 0) return GetDefaultFontInfo();
  3181. TCHAR idBuffer[16];
  3182. ::ZeroMemory(idBuffer, sizeof(idBuffer));
  3183. _itot(id, idBuffer, 10);
  3184. TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(idBuffer));
  3185. if (!pFontInfo) pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(idBuffer));
  3186. if (!pFontInfo) pFontInfo = GetDefaultFontInfo();
  3187. if (pFontInfo->tm.tmHeight == 0)
  3188. {
  3189. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, pFontInfo->hFont);
  3190. ::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
  3191. ::SelectObject(m_hDcPaint, hOldFont);
  3192. }
  3193. return pFontInfo;
  3194. }
  3195. TFontInfo* CPaintManagerUI::GetFontInfo(HFONT hFont)
  3196. {
  3197. TFontInfo* pFontInfo = NULL;
  3198. for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
  3199. {
  3200. if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
  3201. {
  3202. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
  3203. if (pFontInfo && pFontInfo->hFont == hFont) break;
  3204. }
  3205. }
  3206. if (!pFontInfo)
  3207. {
  3208. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  3209. {
  3210. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  3211. {
  3212. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
  3213. if (pFontInfo && pFontInfo->hFont == hFont) break;
  3214. }
  3215. }
  3216. }
  3217. if (!pFontInfo) pFontInfo = GetDefaultFontInfo();
  3218. if (pFontInfo->tm.tmHeight == 0)
  3219. {
  3220. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, pFontInfo->hFont);
  3221. ::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
  3222. ::SelectObject(m_hDcPaint, hOldFont);
  3223. }
  3224. return pFontInfo;
  3225. }
  3226. const TImageInfo* CPaintManagerUI::GetImage(LPCTSTR bitmap)
  3227. {
  3228. TImageInfo* data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
  3229. if (!data) data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
  3230. return data;
  3231. }
  3232. const TImageInfo* CPaintManagerUI::GetImageEx(LPCTSTR bitmap, LPCTSTR type, DWORD mask, bool bUseHSL, bool bGdiplus, HINSTANCE instance)
  3233. {
  3234. const TImageInfo* data = GetImage(bitmap);
  3235. if (!data)
  3236. {
  3237. if (AddImage(bitmap, type, mask, bUseHSL, bGdiplus, false, instance))
  3238. {
  3239. if (m_bForceUseSharedRes) data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
  3240. else data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
  3241. }
  3242. }
  3243. return data;
  3244. }
  3245. const TImageInfo* CPaintManagerUI::AddImage(LPCTSTR bitmap, LPCTSTR type, DWORD mask, bool bUseHSL, bool bGdiplus, bool bShared, HINSTANCE instance)
  3246. {
  3247. if (bitmap == NULL || bitmap[0] == _T('\0')) return NULL;
  3248. TImageInfo* data = NULL;
  3249. if (type != NULL && lstrlen(type) > 0)
  3250. {
  3251. if (isdigit(*bitmap))
  3252. {
  3253. LPTSTR pstr = NULL;
  3254. int iIndex = _tcstol(bitmap, &pstr, 10);
  3255. data = bGdiplus ? CRenderEngine::GdiplusLoadImage(iIndex, type, mask, instance) : CRenderEngine::LoadImage(iIndex, type, mask, instance);
  3256. }
  3257. }
  3258. else
  3259. {
  3260. data = bGdiplus ? CRenderEngine::GdiplusLoadImage(bitmap, NULL, mask, instance) : CRenderEngine::LoadImage(bitmap, NULL, mask, instance);
  3261. if (!data)
  3262. {
  3263. CDuiString sImageName = bitmap;
  3264. int iAtIdx = sImageName.ReverseFind(_T('@'));
  3265. int iDotIdx = sImageName.ReverseFind(_T('.'));
  3266. if (iAtIdx != -1 && iDotIdx != -1)
  3267. {
  3268. CDuiString sExe = sImageName.Mid(iDotIdx);
  3269. sImageName = sImageName.Left(iAtIdx) + sExe;
  3270. data = bGdiplus ? CRenderEngine::GdiplusLoadImage(sImageName.GetData(), NULL, mask, instance) : CRenderEngine::LoadImage(sImageName.GetData(), NULL, mask, instance);
  3271. }
  3272. }
  3273. }
  3274. if (data == NULL)
  3275. {
  3276. return NULL;
  3277. }
  3278. data->bUseHSL = bUseHSL;
  3279. if (type != NULL) data->sResType = type;
  3280. data->dwMask = mask;
  3281. if (data->bUseHSL)
  3282. {
  3283. data->pSrcBits = new BYTE[data->nX * data->nY * 4];
  3284. ::CopyMemory(data->pSrcBits, data->pBits, data->nX * data->nY * 4);
  3285. }
  3286. else data->pSrcBits = NULL;
  3287. if (m_bUseHSL) CRenderEngine::AdjustImage(true, data, m_H, m_S, m_L);
  3288. if (data)
  3289. {
  3290. if (bShared || m_bForceUseSharedRes)
  3291. {
  3292. TImageInfo* pOldImageInfo = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
  3293. if (pOldImageInfo)
  3294. {
  3295. CRenderEngine::FreeImage(pOldImageInfo);
  3296. m_SharedResInfo.m_ImageHash.Remove(bitmap);
  3297. }
  3298. if (!m_SharedResInfo.m_ImageHash.Insert(bitmap, data))
  3299. {
  3300. CRenderEngine::FreeImage(data);
  3301. data = NULL;
  3302. }
  3303. }
  3304. else
  3305. {
  3306. TImageInfo* pOldImageInfo = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
  3307. if (pOldImageInfo)
  3308. {
  3309. CRenderEngine::FreeImage(pOldImageInfo);
  3310. m_ResInfo.m_ImageHash.Remove(bitmap);
  3311. }
  3312. if (!m_ResInfo.m_ImageHash.Insert(bitmap, data))
  3313. {
  3314. CRenderEngine::FreeImage(data);
  3315. data = NULL;
  3316. }
  3317. }
  3318. }
  3319. return data;
  3320. }
  3321. const TImageInfo* CPaintManagerUI::AddImage(LPCTSTR bitmap, HBITMAP hBitmap, int iWidth, int iHeight, bool bAlpha, bool bShared)
  3322. {
  3323. // 因无法确定外部HBITMAP格式,不能使用hsl调整
  3324. if (bitmap == NULL || bitmap[0] == _T('\0')) return NULL;
  3325. if (hBitmap == NULL || iWidth <= 0 || iHeight <= 0) return NULL;
  3326. TImageInfo* data = new TImageInfo;
  3327. data->pBits = NULL;
  3328. data->pSrcBits = NULL;
  3329. data->hBitmap = hBitmap;
  3330. data->pBits = NULL;
  3331. data->nX = iWidth;
  3332. data->nY = iHeight;
  3333. data->bAlpha = bAlpha;
  3334. data->bUseHSL = false;
  3335. data->pSrcBits = NULL;
  3336. data->dwMask = 0;
  3337. if (bShared || m_bForceUseSharedRes)
  3338. {
  3339. if (!m_SharedResInfo.m_ImageHash.Insert(bitmap, data))
  3340. {
  3341. CRenderEngine::FreeImage(data);
  3342. data = NULL;
  3343. }
  3344. }
  3345. else
  3346. {
  3347. if (!m_ResInfo.m_ImageHash.Insert(bitmap, data))
  3348. {
  3349. CRenderEngine::FreeImage(data);
  3350. data = NULL;
  3351. }
  3352. }
  3353. return data;
  3354. }
  3355. void CPaintManagerUI::RemoveImage(LPCTSTR bitmap, bool bShared)
  3356. {
  3357. TImageInfo* data = NULL;
  3358. if (bShared)
  3359. {
  3360. data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
  3361. if (data)
  3362. {
  3363. CRenderEngine::FreeImage(data);
  3364. m_SharedResInfo.m_ImageHash.Remove(bitmap);
  3365. }
  3366. }
  3367. else
  3368. {
  3369. data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
  3370. if (data)
  3371. {
  3372. CRenderEngine::FreeImage(data);
  3373. m_ResInfo.m_ImageHash.Remove(bitmap);
  3374. }
  3375. }
  3376. }
  3377. void CPaintManagerUI::RemoveAllImages(bool bShared)
  3378. {
  3379. if (bShared)
  3380. {
  3381. TImageInfo* data;
  3382. for (int i = 0; i < m_SharedResInfo.m_ImageHash.GetSize(); i++)
  3383. {
  3384. if (LPCTSTR key = m_SharedResInfo.m_ImageHash.GetAt(i))
  3385. {
  3386. data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(key, false));
  3387. if (data)
  3388. {
  3389. CRenderEngine::FreeImage(data);
  3390. }
  3391. }
  3392. }
  3393. m_SharedResInfo.m_ImageHash.RemoveAll();
  3394. }
  3395. else
  3396. {
  3397. TImageInfo* data;
  3398. for (int i = 0; i < m_ResInfo.m_ImageHash.GetSize(); i++)
  3399. {
  3400. if (LPCTSTR key = m_ResInfo.m_ImageHash.GetAt(i))
  3401. {
  3402. data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(key, false));
  3403. if (data)
  3404. {
  3405. CRenderEngine::FreeImage(data);
  3406. }
  3407. }
  3408. }
  3409. m_ResInfo.m_ImageHash.RemoveAll();
  3410. }
  3411. }
  3412. void CPaintManagerUI::AdjustSharedImagesHSL()
  3413. {
  3414. TImageInfo* data;
  3415. for (int i = 0; i < m_SharedResInfo.m_ImageHash.GetSize(); i++)
  3416. {
  3417. if (LPCTSTR key = m_SharedResInfo.m_ImageHash.GetAt(i))
  3418. {
  3419. data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(key));
  3420. if (data && data->bUseHSL)
  3421. {
  3422. CRenderEngine::AdjustImage(m_bUseHSL, data, m_H, m_S, m_L);
  3423. }
  3424. }
  3425. }
  3426. }
  3427. void CPaintManagerUI::AdjustImagesHSL()
  3428. {
  3429. TImageInfo* data;
  3430. for (int i = 0; i < m_ResInfo.m_ImageHash.GetSize(); i++)
  3431. {
  3432. if (LPCTSTR key = m_ResInfo.m_ImageHash.GetAt(i))
  3433. {
  3434. data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(key));
  3435. if (data && data->bUseHSL)
  3436. {
  3437. CRenderEngine::AdjustImage(m_bUseHSL, data, m_H, m_S, m_L);
  3438. }
  3439. }
  3440. }
  3441. Invalidate();
  3442. }
  3443. void CPaintManagerUI::PostAsyncNotify()
  3444. {
  3445. if (!m_bAsyncNotifyPosted)
  3446. {
  3447. ::PostMessage(m_hWndPaint, WM_APP + 1, 0, 0L);
  3448. m_bAsyncNotifyPosted = true;
  3449. }
  3450. }
  3451. void CPaintManagerUI::ReloadSharedImages()
  3452. {
  3453. TImageInfo* data = NULL;
  3454. TImageInfo* pNewData = NULL;
  3455. for (int i = 0; i < m_SharedResInfo.m_ImageHash.GetSize(); i++)
  3456. {
  3457. if (LPCTSTR bitmap = m_SharedResInfo.m_ImageHash.GetAt(i))
  3458. {
  3459. data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
  3460. if (data != NULL)
  3461. {
  3462. if (!data->sResType.IsEmpty())
  3463. {
  3464. if (isdigit(*bitmap))
  3465. {
  3466. LPTSTR pstr = NULL;
  3467. int iIndex = _tcstol(bitmap, &pstr, 10);
  3468. pNewData = CRenderEngine::LoadImage(iIndex, data->sResType.GetData(), data->dwMask);
  3469. }
  3470. }
  3471. else
  3472. {
  3473. pNewData = CRenderEngine::LoadImage(bitmap, NULL, data->dwMask);
  3474. }
  3475. if (pNewData == NULL) continue;
  3476. CRenderEngine::FreeImage(data, false);
  3477. data->hBitmap = pNewData->hBitmap;
  3478. data->pImage = pNewData->pImage;
  3479. data->pBits = pNewData->pBits;
  3480. data->nX = pNewData->nX;
  3481. data->nY = pNewData->nY;
  3482. data->bAlpha = pNewData->bAlpha;
  3483. data->pSrcBits = NULL;
  3484. if (data->bUseHSL)
  3485. {
  3486. data->pSrcBits = new BYTE[data->nX * data->nY * 4];
  3487. ::CopyMemory(data->pSrcBits, data->pBits, data->nX * data->nY * 4);
  3488. }
  3489. else data->pSrcBits = NULL;
  3490. if (m_bUseHSL) CRenderEngine::AdjustImage(true, data, m_H, m_S, m_L);
  3491. delete pNewData;
  3492. }
  3493. }
  3494. }
  3495. }
  3496. void CPaintManagerUI::ReloadImages()
  3497. {
  3498. RemoveAllDrawInfos();
  3499. TImageInfo* data = NULL;
  3500. TImageInfo* pNewData = NULL;
  3501. for (int i = 0; i < m_ResInfo.m_ImageHash.GetSize(); i++)
  3502. {
  3503. if (LPCTSTR bitmap = m_ResInfo.m_ImageHash.GetAt(i))
  3504. {
  3505. data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
  3506. if (data != NULL)
  3507. {
  3508. if (!data->sResType.IsEmpty())
  3509. {
  3510. if (isdigit(*bitmap))
  3511. {
  3512. LPTSTR pstr = NULL;
  3513. int iIndex = _tcstol(bitmap, &pstr, 10);
  3514. pNewData = CRenderEngine::LoadImage(iIndex, data->sResType.GetData(), data->dwMask);
  3515. }
  3516. }
  3517. else
  3518. {
  3519. pNewData = CRenderEngine::LoadImage(bitmap, NULL, data->dwMask);
  3520. }
  3521. CRenderEngine::FreeImage(data, false);
  3522. if (pNewData == NULL)
  3523. {
  3524. m_ResInfo.m_ImageHash.Remove(bitmap);
  3525. continue;
  3526. }
  3527. data->hBitmap = pNewData->hBitmap;
  3528. data->pBits = pNewData->pBits;
  3529. data->nX = pNewData->nX;
  3530. data->nY = pNewData->nY;
  3531. data->bAlpha = pNewData->bAlpha;
  3532. data->pSrcBits = NULL;
  3533. if (data->bUseHSL)
  3534. {
  3535. data->pSrcBits = new BYTE[data->nX * data->nY * 4];
  3536. ::CopyMemory(data->pSrcBits, data->pBits, data->nX * data->nY * 4);
  3537. }
  3538. else data->pSrcBits = NULL;
  3539. if (m_bUseHSL) CRenderEngine::AdjustImage(true, data, m_H, m_S, m_L);
  3540. delete pNewData;
  3541. }
  3542. }
  3543. }
  3544. if (m_pRoot) m_pRoot->Invalidate();
  3545. }
  3546. const TDrawInfo* CPaintManagerUI::GetDrawInfo(LPCTSTR pStrImage, LPCTSTR pStrModify)
  3547. {
  3548. CDuiString sKey;
  3549. sKey.Format(_T("%s%s"), pStrImage == NULL ? _T("") : pStrImage, pStrModify == NULL ? _T("") : pStrModify);
  3550. TDrawInfo* pDrawInfo = static_cast<TDrawInfo*>(m_ResInfo.m_DrawInfoHash.Find(sKey));
  3551. if (pDrawInfo == NULL && !sKey.IsEmpty())
  3552. {
  3553. pDrawInfo = new TDrawInfo();
  3554. pDrawInfo->Parse(pStrImage, pStrModify, this);
  3555. m_ResInfo.m_DrawInfoHash.Insert(sKey, pDrawInfo);
  3556. }
  3557. return pDrawInfo;
  3558. }
  3559. void CPaintManagerUI::RemoveDrawInfo(LPCTSTR pStrImage, LPCTSTR pStrModify)
  3560. {
  3561. CDuiString sKey;
  3562. sKey.Format(_T("%s%s"), pStrImage == NULL ? _T("") : pStrImage, pStrModify == NULL ? _T("") : pStrModify);
  3563. TDrawInfo* pDrawInfo = static_cast<TDrawInfo*>(m_ResInfo.m_DrawInfoHash.Find(sKey));
  3564. if (pDrawInfo != NULL)
  3565. {
  3566. m_ResInfo.m_DrawInfoHash.Remove(sKey);
  3567. delete pDrawInfo;
  3568. pDrawInfo = NULL;
  3569. }
  3570. }
  3571. void CPaintManagerUI::RemoveAllDrawInfos()
  3572. {
  3573. TDrawInfo* pDrawInfo = NULL;
  3574. for (int i = 0; i < m_ResInfo.m_DrawInfoHash.GetSize(); i++)
  3575. {
  3576. LPCTSTR key = m_ResInfo.m_DrawInfoHash.GetAt(i);
  3577. if (key != NULL)
  3578. {
  3579. pDrawInfo = static_cast<TDrawInfo*>(m_ResInfo.m_DrawInfoHash.Find(key, false));
  3580. if (pDrawInfo)
  3581. {
  3582. delete pDrawInfo;
  3583. pDrawInfo = NULL;
  3584. }
  3585. }
  3586. }
  3587. m_ResInfo.m_DrawInfoHash.RemoveAll();
  3588. }
  3589. void CPaintManagerUI::AddDefaultAttributeList(LPCTSTR pStrControlName, LPCTSTR pStrControlAttrList, bool bShared)
  3590. {
  3591. if (bShared || m_bForceUseSharedRes)
  3592. {
  3593. CDuiString* pDefaultAttr = new CDuiString(pStrControlAttrList);
  3594. if (pDefaultAttr != NULL)
  3595. {
  3596. CDuiString* pOldDefaultAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Set(pStrControlName, (LPVOID)pDefaultAttr));
  3597. if (pOldDefaultAttr) delete pOldDefaultAttr;
  3598. }
  3599. }
  3600. else
  3601. {
  3602. CDuiString* pDefaultAttr = new CDuiString(pStrControlAttrList);
  3603. if (pDefaultAttr != NULL)
  3604. {
  3605. CDuiString* pOldDefaultAttr = static_cast<CDuiString*>(m_ResInfo.m_AttrHash.Set(pStrControlName, (LPVOID)pDefaultAttr));
  3606. if (pOldDefaultAttr) delete pOldDefaultAttr;
  3607. }
  3608. }
  3609. }
  3610. LPCTSTR CPaintManagerUI::GetDefaultAttributeList(LPCTSTR pStrControlName) const
  3611. {
  3612. CDuiString* pDefaultAttr = static_cast<CDuiString*>(m_ResInfo.m_AttrHash.Find(pStrControlName));
  3613. if (!pDefaultAttr) pDefaultAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Find(pStrControlName));
  3614. if (pDefaultAttr) return pDefaultAttr->GetData();
  3615. return NULL;
  3616. }
  3617. bool CPaintManagerUI::RemoveDefaultAttributeList(LPCTSTR pStrControlName, bool bShared)
  3618. {
  3619. if (bShared)
  3620. {
  3621. CDuiString* pDefaultAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Find(pStrControlName));
  3622. if (!pDefaultAttr) return false;
  3623. delete pDefaultAttr;
  3624. return m_SharedResInfo.m_AttrHash.Remove(pStrControlName);
  3625. }
  3626. else
  3627. {
  3628. CDuiString* pDefaultAttr = static_cast<CDuiString*>(m_ResInfo.m_AttrHash.Find(pStrControlName));
  3629. if (!pDefaultAttr) return false;
  3630. delete pDefaultAttr;
  3631. return m_ResInfo.m_AttrHash.Remove(pStrControlName);
  3632. }
  3633. }
  3634. void CPaintManagerUI::RemoveAllDefaultAttributeList(bool bShared)
  3635. {
  3636. if (bShared)
  3637. {
  3638. CDuiString* pDefaultAttr;
  3639. for (int i = 0; i < m_SharedResInfo.m_AttrHash.GetSize(); i++)
  3640. {
  3641. if (LPCTSTR key = m_SharedResInfo.m_AttrHash.GetAt(i))
  3642. {
  3643. pDefaultAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Find(key));
  3644. if (pDefaultAttr) delete pDefaultAttr;
  3645. }
  3646. }
  3647. m_SharedResInfo.m_AttrHash.RemoveAll();
  3648. }
  3649. else
  3650. {
  3651. CDuiString* pDefaultAttr;
  3652. for (int i = 0; i < m_ResInfo.m_AttrHash.GetSize(); i++)
  3653. {
  3654. if (LPCTSTR key = m_ResInfo.m_AttrHash.GetAt(i))
  3655. {
  3656. pDefaultAttr = static_cast<CDuiString*>(m_ResInfo.m_AttrHash.Find(key));
  3657. if (pDefaultAttr) delete pDefaultAttr;
  3658. }
  3659. }
  3660. m_ResInfo.m_AttrHash.RemoveAll();
  3661. }
  3662. }
  3663. void CPaintManagerUI::AddWindowCustomAttribute(LPCTSTR pstrName, LPCTSTR pstrAttr)
  3664. {
  3665. if (pstrName == NULL || pstrName[0] == _T('\0') || pstrAttr == NULL || pstrAttr[0] == _T('\0')) return;
  3666. CDuiString* pCostomAttr = new CDuiString(pstrAttr);
  3667. if (pCostomAttr != NULL)
  3668. {
  3669. if (m_mWindowCustomAttrHash.Find(pstrName) == NULL)
  3670. m_mWindowCustomAttrHash.Set(pstrName, (LPVOID)pCostomAttr);
  3671. else
  3672. delete pCostomAttr;
  3673. }
  3674. }
  3675. LPCTSTR CPaintManagerUI::GetWindowCustomAttribute(LPCTSTR pstrName) const
  3676. {
  3677. if (pstrName == NULL || pstrName[0] == _T('\0')) return NULL;
  3678. CDuiString* pCostomAttr = static_cast<CDuiString*>(m_mWindowCustomAttrHash.Find(pstrName));
  3679. if (pCostomAttr) return pCostomAttr->GetData();
  3680. return NULL;
  3681. }
  3682. bool CPaintManagerUI::RemoveWindowCustomAttribute(LPCTSTR pstrName)
  3683. {
  3684. if (pstrName == NULL || pstrName[0] == _T('\0')) return NULL;
  3685. CDuiString* pCostomAttr = static_cast<CDuiString*>(m_mWindowCustomAttrHash.Find(pstrName));
  3686. if (!pCostomAttr) return false;
  3687. delete pCostomAttr;
  3688. return m_mWindowCustomAttrHash.Remove(pstrName);
  3689. }
  3690. void CPaintManagerUI::RemoveAllWindowCustomAttribute()
  3691. {
  3692. CDuiString* pCostomAttr;
  3693. for (int i = 0; i < m_mWindowCustomAttrHash.GetSize(); i++)
  3694. {
  3695. if (LPCTSTR key = m_mWindowCustomAttrHash.GetAt(i))
  3696. {
  3697. pCostomAttr = static_cast<CDuiString*>(m_mWindowCustomAttrHash.Find(key));
  3698. delete pCostomAttr;
  3699. }
  3700. }
  3701. m_mWindowCustomAttrHash.Resize();
  3702. }
  3703. CControlUI* CPaintManagerUI::GetRoot() const
  3704. {
  3705. ASSERT(m_pRoot);
  3706. return m_pRoot;
  3707. }
  3708. CControlUI* CPaintManagerUI::FindControl(POINT pt) const
  3709. {
  3710. ASSERT(m_pRoot);
  3711. return m_pRoot->FindControl(__FindControlFromPoint, &pt, UIFIND_VISIBLE | UIFIND_HITTEST | UIFIND_TOP_FIRST);
  3712. }
  3713. CControlUI* CPaintManagerUI::FindControl(LPCTSTR pstrName) const
  3714. {
  3715. ASSERT(m_pRoot);
  3716. return static_cast<CControlUI*>(m_mNameHash.Find(pstrName));
  3717. }
  3718. CControlUI* CPaintManagerUI::FindSubControlByPoint(CControlUI* pParent, POINT pt) const
  3719. {
  3720. if (pParent == NULL) pParent = GetRoot();
  3721. ASSERT(pParent);
  3722. return pParent->FindControl(__FindControlFromPoint, &pt, UIFIND_VISIBLE | UIFIND_HITTEST | UIFIND_TOP_FIRST);
  3723. }
  3724. CControlUI* CPaintManagerUI::FindSubControlByName(CControlUI* pParent, LPCTSTR pstrName) const
  3725. {
  3726. if (pParent == NULL) pParent = GetRoot();
  3727. ASSERT(pParent);
  3728. return pParent->FindControl(__FindControlFromName, (LPVOID)pstrName, UIFIND_ALL);
  3729. }
  3730. CControlUI* CPaintManagerUI::FindSubControlByClass(CControlUI* pParent, LPCTSTR pstrClass, int iIndex)
  3731. {
  3732. if (pParent == NULL) pParent = GetRoot();
  3733. ASSERT(pParent);
  3734. m_aFoundControls.Resize(iIndex + 1);
  3735. return pParent->FindControl(__FindControlFromClass, (LPVOID)pstrClass, UIFIND_ALL);
  3736. }
  3737. CStdPtrArray* CPaintManagerUI::FindSubControlsByClass(CControlUI* pParent, LPCTSTR pstrClass)
  3738. {
  3739. if (pParent == NULL) pParent = GetRoot();
  3740. ASSERT(pParent);
  3741. m_aFoundControls.Empty();
  3742. pParent->FindControl(__FindControlsFromClass, (LPVOID)pstrClass, UIFIND_ALL);
  3743. return &m_aFoundControls;
  3744. }
  3745. CStdPtrArray* CPaintManagerUI::GetFoundControls()
  3746. {
  3747. return &m_aFoundControls;
  3748. }
  3749. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromNameHash(CControlUI* pThis, LPVOID pData)
  3750. {
  3751. CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(pData);
  3752. const CDuiString& sName = pThis->GetName();
  3753. if (sName.IsEmpty()) return NULL;
  3754. // Add this control to the hash list
  3755. pManager->m_mNameHash.Set(sName, pThis);
  3756. return NULL; // Attempt to add all controls
  3757. }
  3758. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromCount(CControlUI* /*pThis*/, LPVOID pData)
  3759. {
  3760. int* pnCount = static_cast<int*>(pData);
  3761. (*pnCount)++;
  3762. return NULL; // Count all controls
  3763. }
  3764. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromPoint(CControlUI* pThis, LPVOID pData)
  3765. {
  3766. LPPOINT pPoint = static_cast<LPPOINT>(pData);
  3767. return ::PtInRect(&pThis->GetPos(), *pPoint) ? pThis : NULL;
  3768. }
  3769. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromTab(CControlUI* pThis, LPVOID pData)
  3770. {
  3771. FINDTABINFO* pInfo = static_cast<FINDTABINFO*>(pData);
  3772. if (pInfo->pFocus == pThis)
  3773. {
  3774. if (pInfo->bForward) pInfo->bNextIsIt = true;
  3775. return pInfo->bForward ? NULL : pInfo->pLast;
  3776. }
  3777. if ((pThis->GetControlFlags() & UIFLAG_TABSTOP) == 0) return NULL;
  3778. pInfo->pLast = pThis;
  3779. if (pInfo->bNextIsIt) return pThis;
  3780. if (pInfo->pFocus == NULL) return pThis;
  3781. return NULL; // Examine all controls
  3782. }
  3783. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromShortcut(CControlUI* pThis, LPVOID pData)
  3784. {
  3785. if (!pThis->IsVisible()) return NULL;
  3786. FINDSHORTCUT* pFS = static_cast<FINDSHORTCUT*>(pData);
  3787. if (pFS->ch == toupper(pThis->GetShortcut())) pFS->bPickNext = true;
  3788. if (_tcsstr(pThis->GetClass(), _T("LabelUI")) != NULL) return NULL; // Labels never get focus!
  3789. return pFS->bPickNext ? pThis : NULL;
  3790. }
  3791. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromName(CControlUI* pThis, LPVOID pData)
  3792. {
  3793. LPCTSTR pstrName = static_cast<LPCTSTR>(pData);
  3794. const CDuiString& sName = pThis->GetName();
  3795. if (sName.IsEmpty()) return NULL;
  3796. return (_tcsicmp(sName, pstrName) == 0) ? pThis : NULL;
  3797. }
  3798. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromClass(CControlUI* pThis, LPVOID pData)
  3799. {
  3800. LPCTSTR pstrType = static_cast<LPCTSTR>(pData);
  3801. LPCTSTR pType = pThis->GetClass();
  3802. CStdPtrArray* pFoundControls = pThis->GetManager()->GetFoundControls();
  3803. if (_tcscmp(pstrType, _T("*")) == 0 || _tcscmp(pstrType, pType) == 0)
  3804. {
  3805. int iIndex = -1;
  3806. while (pFoundControls->GetAt(++iIndex) != NULL);
  3807. if (iIndex < pFoundControls->GetSize()) pFoundControls->SetAt(iIndex, pThis);
  3808. }
  3809. if (pFoundControls->GetAt(pFoundControls->GetSize() - 1) != NULL) return pThis;
  3810. return NULL;
  3811. }
  3812. CControlUI* CALLBACK CPaintManagerUI::__FindControlsFromClass(CControlUI* pThis, LPVOID pData)
  3813. {
  3814. LPCTSTR pstrType = static_cast<LPCTSTR>(pData);
  3815. LPCTSTR pType = pThis->GetClass();
  3816. if (_tcscmp(pstrType, _T("*")) == 0 || _tcscmp(pstrType, pType) == 0)
  3817. pThis->GetManager()->GetFoundControls()->Add((LPVOID)pThis);
  3818. return NULL;
  3819. }
  3820. CControlUI* CALLBACK CPaintManagerUI::__FindControlsFromUpdate(CControlUI* pThis, LPVOID pData)
  3821. {
  3822. if (pThis->IsUpdateNeeded())
  3823. {
  3824. pThis->GetManager()->GetFoundControls()->Add((LPVOID)pThis);
  3825. return pThis;
  3826. }
  3827. return NULL;
  3828. }
  3829. bool CPaintManagerUI::TranslateAccelerator(LPMSG pMsg)
  3830. {
  3831. for (int i = 0; i < m_aTranslateAccelerator.GetSize(); i++)
  3832. {
  3833. LRESULT lResult = static_cast<ITranslateAccelerator*>(m_aTranslateAccelerator[i])->TranslateAccelerator(pMsg);
  3834. if (lResult == S_OK) return true;
  3835. }
  3836. return false;
  3837. }
  3838. bool CPaintManagerUI::TranslateMessage(const LPMSG pMsg)
  3839. {
  3840. // Pretranslate Message takes care of system-wide messages, such as
  3841. // tabbing and shortcut key-combos. We'll look for all messages for
  3842. // each window and any child control attached.
  3843. UINT uStyle = GetWindowStyle(pMsg->hwnd);
  3844. UINT uChildRes = uStyle & WS_CHILD;
  3845. LRESULT lRes = 0;
  3846. if (uChildRes != 0)
  3847. {
  3848. HWND hWndParent = ::GetParent(pMsg->hwnd);
  3849. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  3850. {
  3851. CPaintManagerUI* pT = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  3852. HWND hTempParent = hWndParent;
  3853. while (hTempParent)
  3854. {
  3855. if (pMsg->hwnd == pT->GetPaintWindow() || hTempParent == pT->GetPaintWindow())
  3856. {
  3857. if (pT->TranslateAccelerator(pMsg))
  3858. return true;
  3859. pT->PreMessageHandler(pMsg->message, pMsg->wParam, pMsg->lParam, lRes);
  3860. }
  3861. hTempParent = GetParent(hTempParent);
  3862. }
  3863. }
  3864. }
  3865. else
  3866. {
  3867. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  3868. {
  3869. CPaintManagerUI* pT = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  3870. if (pMsg->hwnd == pT->GetPaintWindow())
  3871. {
  3872. if (pT->TranslateAccelerator(pMsg))
  3873. return true;
  3874. if (pT->PreMessageHandler(pMsg->message, pMsg->wParam, pMsg->lParam, lRes))
  3875. return true;
  3876. return false;
  3877. }
  3878. }
  3879. }
  3880. return false;
  3881. }
  3882. bool CPaintManagerUI::AddTranslateAccelerator(ITranslateAccelerator* pTranslateAccelerator)
  3883. {
  3884. ASSERT(m_aTranslateAccelerator.Find(pTranslateAccelerator) < 0);
  3885. return m_aTranslateAccelerator.Add(pTranslateAccelerator);
  3886. }
  3887. bool CPaintManagerUI::RemoveTranslateAccelerator(ITranslateAccelerator* pTranslateAccelerator)
  3888. {
  3889. for (int i = 0; i < m_aTranslateAccelerator.GetSize(); i++)
  3890. {
  3891. if (static_cast<ITranslateAccelerator*>(m_aTranslateAccelerator[i]) == pTranslateAccelerator)
  3892. {
  3893. return m_aTranslateAccelerator.Remove(i);
  3894. }
  3895. }
  3896. return false;
  3897. }
  3898. void CPaintManagerUI::UsedVirtualWnd(bool bUsed)
  3899. {
  3900. m_bUsedVirtualWnd = bUsed;
  3901. }
  3902. // 样式管理
  3903. void CPaintManagerUI::AddStyle(LPCTSTR pName, LPCTSTR pDeclarationList, bool bShared)
  3904. {
  3905. CDuiString* pStyle = new CDuiString(pDeclarationList);
  3906. if (bShared || m_bForceUseSharedRes)
  3907. {
  3908. if (!m_SharedResInfo.m_StyleHash.Insert(pName, pStyle))
  3909. {
  3910. delete pStyle;
  3911. }
  3912. }
  3913. else
  3914. {
  3915. if (!m_ResInfo.m_StyleHash.Insert(pName, pStyle))
  3916. {
  3917. delete pStyle;
  3918. }
  3919. }
  3920. }
  3921. LPCTSTR CPaintManagerUI::GetStyle(LPCTSTR pName) const
  3922. {
  3923. CDuiString* pStyle = static_cast<CDuiString*>(m_ResInfo.m_StyleHash.Find(pName));
  3924. if (!pStyle) pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(pName));
  3925. if (pStyle) return pStyle->GetData();
  3926. else return NULL;
  3927. }
  3928. BOOL CPaintManagerUI::RemoveStyle(LPCTSTR pName, bool bShared)
  3929. {
  3930. CDuiString* pStyle = NULL;
  3931. if (bShared)
  3932. {
  3933. pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(pName));
  3934. if (pStyle)
  3935. {
  3936. delete pStyle;
  3937. m_SharedResInfo.m_StyleHash.Remove(pName);
  3938. }
  3939. }
  3940. else
  3941. {
  3942. pStyle = static_cast<CDuiString*>(m_ResInfo.m_StyleHash.Find(pName));
  3943. if (pStyle)
  3944. {
  3945. delete pStyle;
  3946. m_ResInfo.m_StyleHash.Remove(pName);
  3947. }
  3948. }
  3949. return true;
  3950. }
  3951. const CStdStringPtrMap& CPaintManagerUI::GetStyles(bool bShared) const
  3952. {
  3953. if (bShared) return m_SharedResInfo.m_StyleHash;
  3954. else return m_ResInfo.m_StyleHash;
  3955. }
  3956. void CPaintManagerUI::RemoveAllStyle(bool bShared)
  3957. {
  3958. if (bShared)
  3959. {
  3960. CDuiString* pStyle;
  3961. for (int i = 0; i < m_SharedResInfo.m_StyleHash.GetSize(); i++)
  3962. {
  3963. if (LPCTSTR key = m_SharedResInfo.m_StyleHash.GetAt(i))
  3964. {
  3965. pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(key));
  3966. delete pStyle;
  3967. }
  3968. }
  3969. m_SharedResInfo.m_StyleHash.RemoveAll();
  3970. }
  3971. else
  3972. {
  3973. CDuiString* pStyle;
  3974. for (int i = 0; i < m_ResInfo.m_StyleHash.GetSize(); i++)
  3975. {
  3976. if (LPCTSTR key = m_ResInfo.m_StyleHash.GetAt(i))
  3977. {
  3978. pStyle = static_cast<CDuiString*>(m_ResInfo.m_StyleHash.Find(key));
  3979. delete pStyle;
  3980. }
  3981. }
  3982. m_ResInfo.m_StyleHash.RemoveAll();
  3983. }
  3984. }
  3985. const TImageInfo* CPaintManagerUI::GetImageString(LPCTSTR pStrImage, LPCTSTR pStrModify)
  3986. {
  3987. CDuiString sImageName = pStrImage;
  3988. CDuiString sImageResType = _T("");
  3989. DWORD dwMask = 0;
  3990. CDuiString sItem;
  3991. CDuiString sValue;
  3992. LPTSTR pstr = NULL;
  3993. for (int i = 0; i < 2; ++i)
  3994. {
  3995. if (i == 1)
  3996. pStrImage = pStrModify;
  3997. if (!pStrImage) continue;
  3998. while (*pStrImage != _T('\0'))
  3999. {
  4000. sItem.Empty();
  4001. sValue.Empty();
  4002. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  4003. while (*pStrImage != _T('\0') && *pStrImage != _T('=') && *pStrImage > _T(' '))
  4004. {
  4005. LPTSTR pstrTemp = ::CharNext(pStrImage);
  4006. while (pStrImage < pstrTemp)
  4007. {
  4008. sItem += *pStrImage++;
  4009. }
  4010. }
  4011. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  4012. if (*pStrImage++ != _T('=')) break;
  4013. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  4014. if (*pStrImage++ != _T('\'')) break;
  4015. while (*pStrImage != _T('\0') && *pStrImage != _T('\''))
  4016. {
  4017. LPTSTR pstrTemp = ::CharNext(pStrImage);
  4018. while (pStrImage < pstrTemp)
  4019. {
  4020. sValue += *pStrImage++;
  4021. }
  4022. }
  4023. if (*pStrImage++ != _T('\'')) break;
  4024. if (!sValue.IsEmpty())
  4025. {
  4026. if (sItem == _T("file") || sItem == _T("res"))
  4027. {
  4028. sImageName = sValue;
  4029. }
  4030. else if (sItem == _T("restype"))
  4031. {
  4032. sImageResType = sValue;
  4033. }
  4034. else if (sItem == _T("mask"))
  4035. {
  4036. if (sValue[0] == _T('#')) dwMask = _tcstoul(sValue.GetData() + 1, &pstr, 16);
  4037. else dwMask = _tcstoul(sValue.GetData(), &pstr, 16);
  4038. }
  4039. }
  4040. if (*pStrImage++ != _T(' ')) break;
  4041. }
  4042. }
  4043. return GetImageEx(sImageName, sImageResType, dwMask);
  4044. }
  4045. bool CPaintManagerUI::EnableDragDrop(bool bEnable)
  4046. {
  4047. if (m_bDragDrop == bEnable) return false;
  4048. m_bDragDrop = bEnable;
  4049. if (bEnable)
  4050. {
  4051. AddRef();
  4052. if (FAILED(RegisterDragDrop(m_hWndPaint, this)))
  4053. {
  4054. return false;
  4055. }
  4056. SetTargetWnd(m_hWndPaint);
  4057. FORMATETC ftetc = { 0 };
  4058. ftetc.cfFormat = CF_BITMAP;
  4059. ftetc.dwAspect = DVASPECT_CONTENT;
  4060. ftetc.lindex = -1;
  4061. ftetc.tymed = TYMED_GDI;
  4062. AddSuportedFormat(ftetc);
  4063. ftetc.cfFormat = CF_DIB;
  4064. ftetc.tymed = TYMED_HGLOBAL;
  4065. AddSuportedFormat(ftetc);
  4066. ftetc.cfFormat = CF_HDROP;
  4067. ftetc.tymed = TYMED_HGLOBAL;
  4068. AddSuportedFormat(ftetc);
  4069. ftetc.cfFormat = CF_ENHMETAFILE;
  4070. ftetc.tymed = TYMED_ENHMF;
  4071. AddSuportedFormat(ftetc);
  4072. }
  4073. else
  4074. {
  4075. Release();
  4076. if (FAILED(RevokeDragDrop(m_hWndPaint)))
  4077. {
  4078. return false;
  4079. }
  4080. }
  4081. return true;
  4082. }
  4083. void CPaintManagerUI::SetDragDrop(IDragDropUI* pDragDrop)
  4084. {
  4085. m_pDragDrop = pDragDrop;
  4086. }
  4087. static WORD DIBNumColors(void* pv)
  4088. {
  4089. int bits;
  4090. LPBITMAPINFOHEADER lpbi;
  4091. LPBITMAPCOREHEADER lpbc;
  4092. lpbi = ((LPBITMAPINFOHEADER)pv);
  4093. lpbc = ((LPBITMAPCOREHEADER)pv);
  4094. /* With the BITMAPINFO format headers, the size of the palette
  4095. * is in biClrUsed, whereas in the BITMAPCORE - style headers, it
  4096. * is dependent on the bits per pixel ( = 2 raised to the power of
  4097. * bits/pixel).
  4098. */
  4099. if (lpbi->biSize != sizeof(BITMAPCOREHEADER))
  4100. {
  4101. if (lpbi->biClrUsed != 0)
  4102. return (WORD)lpbi->biClrUsed;
  4103. bits = lpbi->biBitCount;
  4104. }
  4105. else
  4106. bits = lpbc->bcBitCount;
  4107. switch (bits)
  4108. {
  4109. case 1:
  4110. return 2;
  4111. case 4:
  4112. return 16;
  4113. case 8:
  4114. return 256;
  4115. default:
  4116. /* A 24 bitcount DIB has no color table */
  4117. return 0;
  4118. }
  4119. }
  4120. //code taken from SEEDIB MSDN sample
  4121. static WORD ColorTableSize(LPVOID lpv)
  4122. {
  4123. LPBITMAPINFOHEADER lpbih = (LPBITMAPINFOHEADER)lpv;
  4124. if (lpbih->biSize != sizeof(BITMAPCOREHEADER))
  4125. {
  4126. if (((LPBITMAPINFOHEADER)(lpbih))->biCompression == BI_BITFIELDS)
  4127. /* Remember that 16/32bpp dibs can still have a color table */
  4128. return (sizeof(DWORD) * 3) + (DIBNumColors(lpbih) * sizeof(RGBQUAD));
  4129. else
  4130. return (WORD)(DIBNumColors(lpbih) * sizeof(RGBQUAD));
  4131. }
  4132. else
  4133. return (WORD)(DIBNumColors(lpbih) * sizeof(RGBTRIPLE));
  4134. }
  4135. bool CPaintManagerUI::OnDrop(FORMATETC* pFmtEtc, STGMEDIUM& medium, DWORD* pdwEffect)
  4136. {
  4137. POINT ptMouse = { 0 };
  4138. GetCursorPos(&ptMouse);
  4139. ::SendMessage(m_hTargetWnd, WM_LBUTTONUP, NULL, MAKELPARAM(ptMouse.x, ptMouse.y));
  4140. if (pFmtEtc->cfFormat == CF_DIB && medium.tymed == TYMED_HGLOBAL)
  4141. {
  4142. if (medium.hGlobal != NULL)
  4143. {
  4144. LPBITMAPINFOHEADER lpbi = (BITMAPINFOHEADER*)GlobalLock(medium.hGlobal);
  4145. if (lpbi != NULL)
  4146. {
  4147. HBITMAP hbm = NULL;
  4148. HDC hdc = GetDC(NULL);
  4149. if (hdc != NULL)
  4150. {
  4151. int i = ((BITMAPFILEHEADER*)lpbi)->bfOffBits;
  4152. hbm = CreateDIBitmap(hdc, (LPBITMAPINFOHEADER)lpbi,
  4153. (LONG)CBM_INIT,
  4154. (LPSTR)lpbi + lpbi->biSize + ColorTableSize(lpbi),
  4155. (LPBITMAPINFO)lpbi, DIB_RGB_COLORS);
  4156. ::ReleaseDC(NULL, hdc);
  4157. }
  4158. GlobalUnlock(medium.hGlobal);
  4159. if (hbm != NULL)
  4160. hbm = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbm);
  4161. if (hbm != NULL)
  4162. DeleteObject(hbm);
  4163. return true; //release the medium
  4164. }
  4165. }
  4166. }
  4167. if (pFmtEtc->cfFormat == CF_BITMAP && medium.tymed == TYMED_GDI)
  4168. {
  4169. if (medium.hBitmap != NULL)
  4170. {
  4171. HBITMAP hBmp = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)medium.hBitmap);
  4172. if (hBmp != NULL)
  4173. DeleteObject(hBmp);
  4174. return false; //don't free the bitmap
  4175. }
  4176. }
  4177. if (pFmtEtc->cfFormat == CF_ENHMETAFILE && medium.tymed == TYMED_ENHMF)
  4178. {
  4179. ENHMETAHEADER emh;
  4180. GetEnhMetaFileHeader(medium.hEnhMetaFile, sizeof(ENHMETAHEADER), &emh);
  4181. RECT rc;//={0,0,EnhMetaHdr.rclBounds.right-EnhMetaHdr.rclBounds.left, EnhMetaHdr.rclBounds.bottom-EnhMetaHdr.rclBounds.top};
  4182. HDC hDC = GetDC(m_hTargetWnd);
  4183. //start code: taken from ENHMETA.EXE MSDN Sample
  4184. //*ALSO NEED to GET the pallete (select and RealizePalette it, but i was too lazy*
  4185. // Get the characteristics of the output device
  4186. float PixelsX = (float)GetDeviceCaps(hDC, HORZRES);
  4187. float PixelsY = (float)GetDeviceCaps(hDC, VERTRES);
  4188. float MMX = (float)GetDeviceCaps(hDC, HORZSIZE);
  4189. float MMY = (float)GetDeviceCaps(hDC, VERTSIZE);
  4190. // Calculate the rect in which to draw the metafile based on the
  4191. // intended size and the current output device resolution
  4192. // Remember that the intended size is given in 0.01mm units, so
  4193. // convert those to device units on the target device
  4194. rc.top = (int)((float)(emh.rclFrame.top) * PixelsY / (MMY * 100.0f));
  4195. rc.left = (int)((float)(emh.rclFrame.left) * PixelsX / (MMX * 100.0f));
  4196. rc.right = (int)((float)(emh.rclFrame.right) * PixelsX / (MMX * 100.0f));
  4197. rc.bottom = (int)((float)(emh.rclFrame.bottom) * PixelsY / (MMY * 100.0f));
  4198. //end code: taken from ENHMETA.EXE MSDN Sample
  4199. HDC hdcMem = CreateCompatibleDC(hDC);
  4200. HGDIOBJ hBmpMem = CreateCompatibleBitmap(hDC, emh.rclBounds.right, emh.rclBounds.bottom);
  4201. HGDIOBJ hOldBmp = ::SelectObject(hdcMem, hBmpMem);
  4202. PlayEnhMetaFile(hdcMem, medium.hEnhMetaFile, &rc);
  4203. HBITMAP hBmp = (HBITMAP)::SelectObject(hdcMem, hOldBmp);
  4204. DeleteDC(hdcMem);
  4205. ReleaseDC(m_hTargetWnd, hDC);
  4206. hBmp = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBmp);
  4207. if (hBmp != NULL)
  4208. DeleteObject(hBmp);
  4209. return true;
  4210. }
  4211. if (pFmtEtc->cfFormat == CF_HDROP && medium.tymed == TYMED_HGLOBAL)
  4212. {
  4213. HDROP hDrop = (HDROP)GlobalLock(medium.hGlobal);
  4214. if (hDrop != NULL)
  4215. {
  4216. TCHAR szFileName[MAX_PATH];
  4217. UINT cFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
  4218. if (cFiles > 0)
  4219. {
  4220. DragQueryFile(hDrop, 0, szFileName, sizeof(szFileName));
  4221. HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
  4222. if (hBitmap)
  4223. {
  4224. HBITMAP hBmp = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
  4225. if (hBmp != NULL)
  4226. DeleteObject(hBmp);
  4227. }
  4228. }
  4229. }
  4230. GlobalUnlock(medium.hGlobal);
  4231. }
  4232. return true; //let base free the medium
  4233. }
  4234. } // namespace DuiLib
  4235. #pragma warning(pop)