UIManager.cpp 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587
  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. SetNextTabControl();
  1045. }
  1046. SetPainting(true);
  1047. bool bNeedSizeMsg = false;
  1048. DWORD dwWidth = rcClient.right - rcClient.left;
  1049. DWORD dwHeight = rcClient.bottom - rcClient.top;
  1050. SetPainting(true);
  1051. if (m_bUpdateNeeded)
  1052. {
  1053. m_bUpdateNeeded = false;
  1054. if (!::IsRectEmpty(&rcClient) && !::IsIconic(m_hWndPaint))
  1055. {
  1056. if (m_pRoot->IsUpdateNeeded())
  1057. {
  1058. RECT rcRoot = rcClient;
  1059. if (m_hDcOffscreen != NULL) ::DeleteDC(m_hDcOffscreen);
  1060. if (m_hDcBackground != NULL) ::DeleteDC(m_hDcBackground);
  1061. if (m_hbmpOffscreen != NULL) ::DeleteObject(m_hbmpOffscreen);
  1062. if (m_hbmpBackground != NULL) ::DeleteObject(m_hbmpBackground);
  1063. m_hDcOffscreen = NULL;
  1064. m_hDcBackground = NULL;
  1065. m_hbmpOffscreen = NULL;
  1066. m_hbmpBackground = NULL;
  1067. if (m_bLayered)
  1068. {
  1069. rcRoot.left += m_rcLayeredInset.left;
  1070. rcRoot.top += m_rcLayeredInset.top;
  1071. rcRoot.right -= m_rcLayeredInset.right;
  1072. rcRoot.bottom -= m_rcLayeredInset.bottom;
  1073. }
  1074. m_pRoot->SetPos(rcRoot, true);
  1075. bNeedSizeMsg = true;
  1076. }
  1077. else
  1078. {
  1079. CControlUI* pControl = NULL;
  1080. m_aFoundControls.Empty();
  1081. m_pRoot->FindControl(__FindControlsFromUpdate, NULL, UIFIND_VISIBLE | UIFIND_ME_FIRST | UIFIND_UPDATETEST);
  1082. for (int it = 0; it < m_aFoundControls.GetSize(); it++)
  1083. {
  1084. pControl = static_cast<CControlUI*>(m_aFoundControls[it]);
  1085. pControl->SetPos(pControl->GetPos(), true);
  1086. }
  1087. bNeedSizeMsg = true;
  1088. }
  1089. // We'll want to notify the window when it is first initialized
  1090. // with the correct layout. The window form would take the time
  1091. // to submit swipes/animations.
  1092. if (m_bFirstLayout)
  1093. {
  1094. m_bFirstLayout = false;
  1095. SendNotify(m_pRoot, DUI_MSGTYPE_WINDOWINIT, 0, 0, false);
  1096. if (m_bLayered && m_bLayeredChanged)
  1097. {
  1098. Invalidate();
  1099. SetPainting(false);
  1100. return true;
  1101. }
  1102. // 更新阴影窗口显示
  1103. m_shadow.Update(m_hWndPaint);
  1104. }
  1105. }
  1106. }
  1107. else if (m_bLayered && m_bLayeredChanged)
  1108. {
  1109. RECT rcRoot = rcClient;
  1110. if (m_pOffscreenBits) ::ZeroMemory(m_pOffscreenBits, (rcRoot.right - rcRoot.left)
  1111. * (rcRoot.bottom - rcRoot.top) * 4);
  1112. rcRoot.left += m_rcLayeredInset.left;
  1113. rcRoot.top += m_rcLayeredInset.top;
  1114. rcRoot.right -= m_rcLayeredInset.right;
  1115. rcRoot.bottom -= m_rcLayeredInset.bottom;
  1116. m_pRoot->SetPos(rcRoot, true);
  1117. }
  1118. if (m_bLayered)
  1119. {
  1120. DWORD dwExStyle = ::GetWindowLong(m_hWndPaint, GWL_EXSTYLE);
  1121. DWORD dwNewExStyle = dwExStyle | WS_EX_LAYERED;
  1122. if (dwExStyle != dwNewExStyle) ::SetWindowLong(m_hWndPaint, GWL_EXSTYLE, dwNewExStyle);
  1123. m_bOffscreenPaint = true;
  1124. UnionRect(&rcPaint, &rcPaint, &m_rcLayeredUpdate);
  1125. if (rcPaint.right > rcClient.right) rcPaint.right = rcClient.right;
  1126. if (rcPaint.bottom > rcClient.bottom) rcPaint.bottom = rcClient.bottom;
  1127. ::ZeroMemory(&m_rcLayeredUpdate, sizeof(m_rcLayeredUpdate));
  1128. }
  1129. //
  1130. // Render screen
  1131. //
  1132. // Prepare offscreen bitmap
  1133. if (m_bOffscreenPaint && m_hbmpOffscreen == NULL)
  1134. {
  1135. m_hDcOffscreen = ::CreateCompatibleDC(m_hDcPaint);
  1136. m_hbmpOffscreen = CRenderEngine::CreateARGB32Bitmap(m_hDcPaint, dwWidth, dwHeight, (LPBYTE*)&m_pOffscreenBits);
  1137. ASSERT(m_hDcOffscreen);
  1138. ASSERT(m_hbmpOffscreen);
  1139. }
  1140. // Begin Windows paint
  1141. PAINTSTRUCT ps = { 0 };
  1142. ::BeginPaint(m_hWndPaint, &ps);
  1143. if (m_bOffscreenPaint)
  1144. {
  1145. HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(m_hDcOffscreen, m_hbmpOffscreen);
  1146. int iSaveDC = ::SaveDC(m_hDcOffscreen);
  1147. if (m_bLayered)
  1148. {
  1149. for (LONG y = rcClient.bottom - rcPaint.bottom; y < rcClient.bottom - rcPaint.top; ++y)
  1150. {
  1151. for (LONG x = rcPaint.left; x < rcPaint.right; ++x)
  1152. {
  1153. int i = (y * dwWidth + x) * 4;
  1154. *(DWORD*)(&m_pOffscreenBits[i]) = 0;
  1155. }
  1156. }
  1157. }
  1158. m_pRoot->Paint(m_hDcOffscreen, rcPaint, NULL);
  1159. if (m_bLayered)
  1160. {
  1161. for (int i = 0; i < m_aNativeWindow.GetSize(); )
  1162. {
  1163. HWND hChildWnd = static_cast<HWND>(m_aNativeWindow[i]);
  1164. if (!::IsWindow(hChildWnd))
  1165. {
  1166. m_aNativeWindow.Remove(i);
  1167. m_aNativeWindowControl.Remove(i);
  1168. continue;
  1169. }
  1170. ++i;
  1171. if (!::IsWindowVisible(hChildWnd)) continue;
  1172. RECT rcChildWnd = GetNativeWindowRect(hChildWnd);
  1173. RECT rcTemp = { 0 };
  1174. if (!::IntersectRect(&rcTemp, &rcPaint, &rcChildWnd)) continue;
  1175. COLORREF* pChildBitmapBits = NULL;
  1176. HDC hChildMemDC = ::CreateCompatibleDC(m_hDcOffscreen);
  1177. HBITMAP hChildBitmap = CRenderEngine::CreateARGB32Bitmap(hChildMemDC, rcChildWnd.right - rcChildWnd.left, rcChildWnd.bottom - rcChildWnd.top, (BYTE**)&pChildBitmapBits);
  1178. ::ZeroMemory(pChildBitmapBits, (rcChildWnd.right - rcChildWnd.left) * (rcChildWnd.bottom - rcChildWnd.top) * 4);
  1179. HBITMAP hOldChildBitmap = (HBITMAP) ::SelectObject(hChildMemDC, hChildBitmap);
  1180. ::SendMessage(hChildWnd, WM_PRINT, (WPARAM)hChildMemDC, (LPARAM)(PRF_CHECKVISIBLE | PRF_CHILDREN | PRF_CLIENT | PRF_OWNED));
  1181. COLORREF* pChildBitmapBit;
  1182. for (LONG y = 0; y < rcChildWnd.bottom - rcChildWnd.top; y++)
  1183. {
  1184. for (LONG x = 0; x < rcChildWnd.right - rcChildWnd.left; x++)
  1185. {
  1186. pChildBitmapBit = pChildBitmapBits + y * (rcChildWnd.right - rcChildWnd.left) + x;
  1187. if (*pChildBitmapBit != 0x00000000) *pChildBitmapBit |= 0xff000000;
  1188. }
  1189. }
  1190. ::BitBlt(m_hDcOffscreen, rcChildWnd.left, rcChildWnd.top, rcChildWnd.right - rcChildWnd.left,
  1191. rcChildWnd.bottom - rcChildWnd.top, hChildMemDC, 0, 0, SRCCOPY);
  1192. ::SelectObject(hChildMemDC, hOldChildBitmap);
  1193. ::DeleteObject(hChildBitmap);
  1194. ::DeleteDC(hChildMemDC);
  1195. }
  1196. }
  1197. for (int i = 0; i < m_aPostPaintControls.GetSize(); i++)
  1198. {
  1199. CControlUI* pPostPaintControl = static_cast<CControlUI*>(m_aPostPaintControls[i]);
  1200. pPostPaintControl->DoPostPaint(m_hDcOffscreen, rcPaint);
  1201. }
  1202. ::RestoreDC(m_hDcOffscreen, iSaveDC);
  1203. if (m_bLayered)
  1204. {
  1205. RECT rcWnd = { 0 };
  1206. ::GetWindowRect(m_hWndPaint, &rcWnd);
  1207. if (!m_diLayered.sDrawString.IsEmpty())
  1208. {
  1209. DWORD dwWidth = rcClient.right - rcClient.left;
  1210. DWORD dwHeight = rcClient.bottom - rcClient.top;
  1211. RECT rcLayeredClient = rcClient;
  1212. rcLayeredClient.left += m_rcLayeredInset.left;
  1213. rcLayeredClient.top += m_rcLayeredInset.top;
  1214. rcLayeredClient.right -= m_rcLayeredInset.right;
  1215. rcLayeredClient.bottom -= m_rcLayeredInset.bottom;
  1216. COLORREF* pOffscreenBits = (COLORREF*)m_pOffscreenBits;
  1217. COLORREF* pBackgroundBits = m_pBackgroundBits;
  1218. BYTE A = 0;
  1219. BYTE R = 0;
  1220. BYTE G = 0;
  1221. BYTE B = 0;
  1222. if (!m_diLayered.sDrawString.IsEmpty())
  1223. {
  1224. if (m_hbmpBackground == NULL)
  1225. {
  1226. m_hDcBackground = ::CreateCompatibleDC(m_hDcPaint);
  1227. m_hbmpBackground = CRenderEngine::CreateARGB32Bitmap(m_hDcPaint, dwWidth, dwHeight, (BYTE**)&m_pBackgroundBits);
  1228. ::ZeroMemory(m_pBackgroundBits, dwWidth * dwHeight * 4);
  1229. ::SelectObject(m_hDcBackground, m_hbmpBackground);
  1230. CRenderClip clip;
  1231. CRenderClip::GenerateClip(m_hDcBackground, rcLayeredClient, clip);
  1232. CRenderEngine::DrawImageInfo(m_hDcBackground, this, rcLayeredClient, rcLayeredClient, &m_diLayered);
  1233. }
  1234. else if (m_bLayeredChanged)
  1235. {
  1236. ::ZeroMemory(m_pBackgroundBits, dwWidth * dwHeight * 4);
  1237. CRenderClip clip;
  1238. CRenderClip::GenerateClip(m_hDcBackground, rcLayeredClient, clip);
  1239. CRenderEngine::DrawImageInfo(m_hDcBackground, this, rcLayeredClient, rcLayeredClient, &m_diLayered);
  1240. }
  1241. for (LONG y = rcClient.bottom - rcPaint.bottom; y < rcClient.bottom - rcPaint.top; ++y)
  1242. {
  1243. for (LONG x = rcPaint.left; x < rcPaint.right; ++x)
  1244. {
  1245. pOffscreenBits = (COLORREF*)(m_pOffscreenBits + y * dwWidth + x);
  1246. pBackgroundBits = m_pBackgroundBits + y * dwWidth + x;
  1247. A = (BYTE)((*pBackgroundBits) >> 24);
  1248. R = (BYTE)((*pOffscreenBits) >> 16) * A / 255;
  1249. G = (BYTE)((*pOffscreenBits) >> 8) * A / 255;
  1250. B = (BYTE)(*pOffscreenBits) * A / 255;
  1251. *pOffscreenBits = RGB(B, G, R) + ((DWORD)A << 24);
  1252. }
  1253. }
  1254. }
  1255. }
  1256. else
  1257. {
  1258. for (LONG y = rcClient.bottom - rcPaint.bottom; y < rcClient.bottom - rcPaint.top; ++y)
  1259. {
  1260. for (LONG x = rcPaint.left; x < rcPaint.right; ++x)
  1261. {
  1262. int i = (y * dwWidth + x) * 4;
  1263. if ((m_pOffscreenBits[i + 3] == 0) && (m_pOffscreenBits[i + 0] != 0 || m_pOffscreenBits[i + 1] != 0 || m_pOffscreenBits[i + 2] != 0))
  1264. m_pOffscreenBits[i + 3] = 255;
  1265. }
  1266. }
  1267. }
  1268. BLENDFUNCTION bf = { AC_SRC_OVER, 0, m_nOpacity, AC_SRC_ALPHA };
  1269. POINT ptPos = { rcWnd.left, rcWnd.top };
  1270. SIZE sizeWnd = { dwWidth, dwHeight };
  1271. POINT ptSrc = { 0, 0 };
  1272. g_fUpdateLayeredWindow(m_hWndPaint, m_hDcPaint, &ptPos, &sizeWnd, m_hDcOffscreen, &ptSrc, 0, &bf, ULW_ALPHA);
  1273. }
  1274. else
  1275. {
  1276. ::BitBlt(m_hDcPaint, rcPaint.left, rcPaint.top, rcPaint.right - rcPaint.left, rcPaint.bottom - rcPaint.top, m_hDcOffscreen, rcPaint.left, rcPaint.top, SRCCOPY);
  1277. }
  1278. ::SelectObject(m_hDcOffscreen, hOldBitmap);
  1279. if (m_bShowUpdateRect && !m_bLayered)
  1280. {
  1281. HPEN hOldPen = (HPEN)::SelectObject(m_hDcPaint, m_hUpdateRectPen);
  1282. ::SelectObject(m_hDcPaint, ::GetStockObject(HOLLOW_BRUSH));
  1283. ::Rectangle(m_hDcPaint, rcPaint.left, rcPaint.top, rcPaint.right, rcPaint.bottom);
  1284. ::SelectObject(m_hDcPaint, hOldPen);
  1285. }
  1286. }
  1287. else
  1288. {
  1289. // A standard paint job
  1290. int iSaveDC = ::SaveDC(m_hDcPaint);
  1291. m_pRoot->Paint(m_hDcPaint, rcPaint, NULL);
  1292. for (int i = 0; i < m_aPostPaintControls.GetSize(); i++)
  1293. {
  1294. CControlUI* pPostPaintControl = static_cast<CControlUI*>(m_aPostPaintControls[i]);
  1295. pPostPaintControl->DoPostPaint(m_hDcPaint, rcPaint);
  1296. }
  1297. ::RestoreDC(m_hDcPaint, iSaveDC);
  1298. }
  1299. // All Done!
  1300. ::EndPaint(m_hWndPaint, &ps);
  1301. // 绘制结束
  1302. SetPainting(false);
  1303. m_bLayeredChanged = false;
  1304. if (m_bUpdateNeeded) Invalidate();
  1305. // 发送窗口大小改变消息
  1306. if (bNeedSizeMsg)
  1307. {
  1308. this->SendNotify(m_pRoot, DUI_MSGTYPE_WINDOWSIZE, 0, 0, true);
  1309. }
  1310. return true;
  1311. }
  1312. case WM_PRINTCLIENT:
  1313. {
  1314. if (m_pRoot == NULL) break;
  1315. RECT rcClient;
  1316. ::GetClientRect(m_hWndPaint, &rcClient);
  1317. HDC hDC = (HDC)wParam;
  1318. int save = ::SaveDC(hDC);
  1319. m_pRoot->Paint(hDC, rcClient, NULL);
  1320. if ((lParam & PRF_CHILDREN) != 0)
  1321. {
  1322. HWND hWndChild = ::GetWindow(m_hWndPaint, GW_CHILD);
  1323. while (hWndChild != NULL)
  1324. {
  1325. RECT rcPos = { 0 };
  1326. ::GetWindowRect(hWndChild, &rcPos);
  1327. ::MapWindowPoints(HWND_DESKTOP, m_hWndPaint, reinterpret_cast<LPPOINT>(&rcPos), 2);
  1328. ::SetWindowOrgEx(hDC, -rcPos.left, -rcPos.top, NULL);
  1329. ::SendMessage(hWndChild, WM_PRINT, wParam, lParam | PRF_NONCLIENT);
  1330. hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT);
  1331. }
  1332. }
  1333. ::RestoreDC(hDC, save);
  1334. }
  1335. break;
  1336. case WM_GETMINMAXINFO:
  1337. {
  1338. MONITORINFO Monitor = {};
  1339. Monitor.cbSize = sizeof(Monitor);
  1340. ::GetMonitorInfo(::MonitorFromWindow(m_hWndPaint, MONITOR_DEFAULTTOPRIMARY), &Monitor);
  1341. RECT rcWork = Monitor.rcWork;
  1342. if (Monitor.dwFlags != MONITORINFOF_PRIMARY)
  1343. {
  1344. ::OffsetRect(&rcWork, -rcWork.left, -rcWork.top);
  1345. }
  1346. LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
  1347. if (m_szMinWindow.cx > 0) lpMMI->ptMinTrackSize.x = m_szMinWindow.cx;
  1348. if (m_szMinWindow.cy > 0) lpMMI->ptMinTrackSize.y = m_szMinWindow.cy;
  1349. if (m_szMaxWindow.cx > 0) lpMMI->ptMaxTrackSize.x = m_szMaxWindow.cx;
  1350. if (m_szMaxWindow.cy > 0) lpMMI->ptMaxTrackSize.y = m_szMaxWindow.cy;
  1351. if (m_szMaxWindow.cx > 0) lpMMI->ptMaxSize.x = m_szMaxWindow.cx;
  1352. if (m_szMaxWindow.cy > 0) lpMMI->ptMaxSize.y = m_szMaxWindow.cy;
  1353. }
  1354. break;
  1355. case WM_SIZE:
  1356. {
  1357. if (m_pFocus != NULL)
  1358. {
  1359. TEventUI event = { 0 };
  1360. event.Type = UIEVENT_WINDOWSIZE;
  1361. event.pSender = m_pFocus;
  1362. event.dwTimestamp = ::GetTickCount();
  1363. m_pFocus->Event(event);
  1364. }
  1365. if (m_pRoot != NULL) m_pRoot->NeedUpdate();
  1366. }
  1367. return true;
  1368. case WM_TIMER:
  1369. {
  1370. for (int i = 0; i < m_aTimers.GetSize(); i++)
  1371. {
  1372. const TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
  1373. if (pTimer->hWnd == m_hWndPaint &&
  1374. pTimer->uWinTimer == LOWORD(wParam) &&
  1375. pTimer->bKilled == false)
  1376. {
  1377. TEventUI event = { 0 };
  1378. event.Type = UIEVENT_TIMER;
  1379. event.pSender = pTimer->pSender;
  1380. event.dwTimestamp = ::GetTickCount();
  1381. event.ptMouse = m_ptLastMousePos;
  1382. event.wKeyState = MapKeyState();
  1383. event.wParam = pTimer->nLocalID;
  1384. event.lParam = lParam;
  1385. pTimer->pSender->Event(event);
  1386. break;
  1387. }
  1388. }
  1389. }
  1390. break;
  1391. case WM_MOUSEHOVER:
  1392. {
  1393. m_bMouseTracking = false;
  1394. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1395. CControlUI* pHover = FindControl(pt);
  1396. if (pHover == NULL) break;
  1397. // Generate mouse hover event
  1398. if (m_pEventHover != NULL)
  1399. {
  1400. TEventUI event = { 0 };
  1401. event.Type = UIEVENT_MOUSEHOVER;
  1402. event.pSender = m_pEventHover;
  1403. event.wParam = wParam;
  1404. event.lParam = lParam;
  1405. event.dwTimestamp = ::GetTickCount();
  1406. event.ptMouse = pt;
  1407. event.wKeyState = MapKeyState();
  1408. m_pEventHover->Event(event);
  1409. }
  1410. // Create tooltip information
  1411. CDuiString sToolTip = pHover->GetToolTip();
  1412. if (sToolTip.IsEmpty()) return true;
  1413. ::ZeroMemory(&m_ToolTip, sizeof(TOOLINFO));
  1414. m_ToolTip.cbSize = sizeof(TOOLINFO);
  1415. m_ToolTip.uFlags = TTF_IDISHWND;
  1416. m_ToolTip.hwnd = m_hWndPaint;
  1417. m_ToolTip.uId = (UINT_PTR)m_hWndPaint;
  1418. m_ToolTip.hinst = m_hInstance;
  1419. m_ToolTip.lpszText = const_cast<LPTSTR>((LPCTSTR)sToolTip);
  1420. m_ToolTip.rect = pHover->GetPos();
  1421. if (m_hwndTooltip == NULL)
  1422. {
  1423. 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);
  1424. ::SendMessage(m_hwndTooltip, TTM_ADDTOOL, 0, (LPARAM)&m_ToolTip);
  1425. ::SendMessage(m_hwndTooltip, TTM_SETMAXTIPWIDTH, 0, pHover->GetToolTipWidth());
  1426. }
  1427. if (!::IsWindowVisible(m_hwndTooltip))
  1428. {
  1429. ::SendMessage(m_hwndTooltip, TTM_SETTOOLINFO, 0, (LPARAM)&m_ToolTip);
  1430. ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&m_ToolTip);
  1431. }
  1432. }
  1433. return true;
  1434. case WM_MOUSELEAVE:
  1435. {
  1436. if (m_hwndTooltip != NULL) ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ToolTip);
  1437. if (m_bMouseTracking)
  1438. {
  1439. POINT pt = { 0 };
  1440. RECT rcWnd = { 0 };
  1441. ::GetCursorPos(&pt);
  1442. ::GetWindowRect(m_hWndPaint, &rcWnd);
  1443. if (!::IsIconic(m_hWndPaint) && ::GetActiveWindow() == m_hWndPaint && ::PtInRect(&rcWnd, pt))
  1444. {
  1445. if (::SendMessage(m_hWndPaint, WM_NCHITTEST, 0, MAKELPARAM(pt.x, pt.y)) == HTCLIENT)
  1446. {
  1447. ::ScreenToClient(m_hWndPaint, &pt);
  1448. ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, MAKELPARAM(pt.x, pt.y));
  1449. }
  1450. else
  1451. ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, (LPARAM)-1);
  1452. }
  1453. else
  1454. ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, (LPARAM)-1);
  1455. }
  1456. m_bMouseTracking = false;
  1457. }
  1458. break;
  1459. case WM_MOUSEMOVE:
  1460. {
  1461. // Start tracking this entire window again...
  1462. if (!m_bMouseTracking)
  1463. {
  1464. TRACKMOUSEEVENT tme = { 0 };
  1465. tme.cbSize = sizeof(TRACKMOUSEEVENT);
  1466. tme.dwFlags = TME_HOVER | TME_LEAVE;
  1467. tme.hwndTrack = m_hWndPaint;
  1468. tme.dwHoverTime = m_hwndTooltip == NULL ? m_iHoverTime : (DWORD) ::SendMessage(m_hwndTooltip, TTM_GETDELAYTIME, TTDT_INITIAL, 0L);
  1469. _TrackMouseEvent(&tme);
  1470. m_bMouseTracking = true;
  1471. }
  1472. // Generate the appropriate mouse messages
  1473. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1474. // 是否移动
  1475. bool bNeedDrag = true;
  1476. if (m_ptLastMousePos.x == pt.x && m_ptLastMousePos.y == pt.y)
  1477. {
  1478. bNeedDrag = false;
  1479. }
  1480. // 记录鼠标位置
  1481. m_ptLastMousePos = pt;
  1482. CControlUI* pNewHover = FindControl(pt);
  1483. if (pNewHover != NULL && pNewHover->GetManager() != this) break;
  1484. // 拖拽事件
  1485. if (bNeedDrag && m_bDragMode && wParam == MK_LBUTTON)
  1486. {
  1487. // 释放Capture
  1488. ::ReleaseCapture();
  1489. // 接口
  1490. if (m_pDragDrop != NULL && m_pDragDrop->OnDragDrop(m_pEventClick))
  1491. {
  1492. m_bDragMode = false;
  1493. break;
  1494. }
  1495. CIDropSource* pdsrc = new CIDropSource;
  1496. if (pdsrc == NULL) return 0;
  1497. pdsrc->AddRef();
  1498. CIDataObject* pdobj = new CIDataObject(pdsrc);
  1499. if (pdobj == NULL) return 0;
  1500. pdobj->AddRef();
  1501. FORMATETC fmtetc = { 0 };
  1502. STGMEDIUM medium = { 0 };
  1503. fmtetc.dwAspect = DVASPECT_CONTENT;
  1504. fmtetc.lindex = -1;
  1505. fmtetc.cfFormat = CF_BITMAP;
  1506. fmtetc.tymed = TYMED_GDI;
  1507. //////////////////////////////////////
  1508. HBITMAP hBitmap = (HBITMAP)OleDuplicateData(m_hDragBitmap, fmtetc.cfFormat, NULL);
  1509. medium.hBitmap = hBitmap;
  1510. pdobj->SetData(&fmtetc, &medium, FALSE);
  1511. //////////////////////////////////////
  1512. BITMAP bmap;
  1513. GetObject(hBitmap, sizeof(BITMAP), &bmap);
  1514. RECT rc = { 0, 0, bmap.bmWidth, bmap.bmHeight };
  1515. fmtetc.cfFormat = CF_ENHMETAFILE;
  1516. fmtetc.tymed = TYMED_ENHMF;
  1517. HDC hMetaDC = CreateEnhMetaFile(m_hDcPaint, NULL, NULL, NULL);
  1518. HDC hdcMem = CreateCompatibleDC(m_hDcPaint);
  1519. HGDIOBJ hOldBmp = ::SelectObject(hdcMem, hBitmap);
  1520. ::BitBlt(hMetaDC, 0, 0, rc.right, rc.bottom, hdcMem, 0, 0, SRCCOPY);
  1521. ::SelectObject(hdcMem, hOldBmp);
  1522. medium.hEnhMetaFile = CloseEnhMetaFile(hMetaDC);
  1523. DeleteDC(hdcMem);
  1524. medium.tymed = TYMED_ENHMF;
  1525. pdobj->SetData(&fmtetc, &medium, TRUE);
  1526. //////////////////////////////////////
  1527. CDragSourceHelper dragSrcHelper;
  1528. POINT ptDrag = { 0 };
  1529. ptDrag.x = bmap.bmWidth / 2;
  1530. ptDrag.y = bmap.bmHeight / 2;
  1531. dragSrcHelper.InitializeFromBitmap(hBitmap, ptDrag, rc, pdobj); //will own the bmp
  1532. DWORD dwEffect;
  1533. HRESULT hr = ::DoDragDrop(pdobj, pdsrc, DROPEFFECT_COPY | DROPEFFECT_MOVE, &dwEffect);
  1534. if (dwEffect) pdsrc->Release();
  1535. else delete pdsrc;
  1536. pdobj->Release();
  1537. m_bDragMode = false;
  1538. break;
  1539. }
  1540. TEventUI event = { 0 };
  1541. event.ptMouse = pt;
  1542. event.wParam = wParam;
  1543. event.lParam = lParam;
  1544. event.dwTimestamp = ::GetTickCount();
  1545. event.wKeyState = MapKeyState();
  1546. if (!IsCaptured())
  1547. {
  1548. pNewHover = FindControl(pt);
  1549. if (pNewHover != NULL && pNewHover->GetManager() != this) break;
  1550. if (pNewHover != m_pEventHover && m_pEventHover != NULL)
  1551. {
  1552. event.Type = UIEVENT_MOUSELEAVE;
  1553. event.pSender = m_pEventHover;
  1554. CStdPtrArray aNeedMouseLeaveNeeded(m_aNeedMouseLeaveNeeded.GetSize());
  1555. aNeedMouseLeaveNeeded.Resize(m_aNeedMouseLeaveNeeded.GetSize());
  1556. ::CopyMemory(aNeedMouseLeaveNeeded.GetData(), m_aNeedMouseLeaveNeeded.GetData(), m_aNeedMouseLeaveNeeded.GetSize() * sizeof(LPVOID));
  1557. for (int i = 0; i < aNeedMouseLeaveNeeded.GetSize(); i++)
  1558. {
  1559. static_cast<CControlUI*>(aNeedMouseLeaveNeeded[i])->Event(event);
  1560. }
  1561. m_pEventHover->Event(event);
  1562. m_pEventHover = NULL;
  1563. if (m_hwndTooltip != NULL) ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ToolTip);
  1564. }
  1565. if (pNewHover != m_pEventHover && pNewHover != NULL)
  1566. {
  1567. event.Type = UIEVENT_MOUSEENTER;
  1568. event.pSender = pNewHover;
  1569. pNewHover->Event(event);
  1570. m_pEventHover = pNewHover;
  1571. }
  1572. }
  1573. if (m_pEventClick != NULL)
  1574. {
  1575. event.Type = UIEVENT_MOUSEMOVE;
  1576. event.pSender = m_pEventClick;
  1577. m_pEventClick->Event(event);
  1578. }
  1579. else if (pNewHover != NULL)
  1580. {
  1581. event.Type = UIEVENT_MOUSEMOVE;
  1582. event.pSender = pNewHover;
  1583. pNewHover->Event(event);
  1584. }
  1585. }
  1586. break;
  1587. case WM_LBUTTONDOWN:
  1588. {
  1589. // We alway set focus back to our app (this helps
  1590. // when Win32 child windows are placed on the dialog
  1591. // and we need to remove them on focus change).
  1592. if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
  1593. if (m_pRoot == NULL) break;
  1594. // 查找控件
  1595. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1596. m_ptLastMousePos = pt;
  1597. CControlUI* pControl = FindControl(pt);
  1598. if (pControl == NULL) break;
  1599. if (pControl->GetManager() != this) break;
  1600. // 准备拖拽
  1601. if (m_bDragDrop && pControl->IsDragEnabled())
  1602. {
  1603. m_bDragMode = true;
  1604. if (m_hDragBitmap != NULL)
  1605. {
  1606. ::DeleteObject(m_hDragBitmap);
  1607. m_hDragBitmap = NULL;
  1608. }
  1609. m_hDragBitmap = CRenderEngine::GenerateBitmap(this, pControl, pControl->GetPos());
  1610. }
  1611. // 开启捕获
  1612. SetCapture();
  1613. // 事件处理
  1614. m_pEventClick = pControl;
  1615. pControl->SetFocus();
  1616. TEventUI event = { 0 };
  1617. event.Type = UIEVENT_BUTTONDOWN;
  1618. event.pSender = pControl;
  1619. event.wParam = wParam;
  1620. event.lParam = lParam;
  1621. event.ptMouse = pt;
  1622. event.wKeyState = (WORD)wParam;
  1623. event.dwTimestamp = ::GetTickCount();
  1624. pControl->Event(event);
  1625. }
  1626. break;
  1627. case WM_LBUTTONDBLCLK:
  1628. {
  1629. if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
  1630. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1631. m_ptLastMousePos = pt;
  1632. CControlUI* pControl = FindControl(pt);
  1633. if (pControl == NULL) break;
  1634. if (pControl->GetManager() != this) break;
  1635. //SetCapture();
  1636. TEventUI event = { 0 };
  1637. event.Type = UIEVENT_DBLCLICK;
  1638. event.pSender = pControl;
  1639. event.ptMouse = pt;
  1640. event.wParam = wParam;
  1641. event.lParam = lParam;
  1642. event.wKeyState = (WORD)wParam;
  1643. event.dwTimestamp = ::GetTickCount();
  1644. pControl->Event(event);
  1645. m_pEventClick = pControl;
  1646. }
  1647. break;
  1648. case WM_LBUTTONUP:
  1649. {
  1650. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1651. m_ptLastMousePos = pt;
  1652. if (m_pEventClick == NULL) break;
  1653. ReleaseCapture();
  1654. TEventUI event = { 0 };
  1655. event.Type = UIEVENT_BUTTONUP;
  1656. event.pSender = m_pEventClick;
  1657. event.wParam = wParam;
  1658. event.lParam = lParam;
  1659. event.ptMouse = pt;
  1660. event.wKeyState = (WORD)wParam;
  1661. event.dwTimestamp = ::GetTickCount();
  1662. CControlUI* pClick = m_pEventClick;
  1663. m_pEventClick = NULL;
  1664. pClick->Event(event);
  1665. }
  1666. break;
  1667. case WM_RBUTTONDOWN:
  1668. {
  1669. if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
  1670. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1671. m_ptLastMousePos = pt;
  1672. CControlUI* pControl = FindControl(pt);
  1673. if (pControl == NULL) break;
  1674. if (pControl->GetManager() != this) break;
  1675. pControl->SetFocus();
  1676. SetCapture();
  1677. TEventUI event = { 0 };
  1678. event.Type = UIEVENT_RBUTTONDOWN;
  1679. event.pSender = pControl;
  1680. event.wParam = wParam;
  1681. event.lParam = lParam;
  1682. event.ptMouse = pt;
  1683. event.wKeyState = (WORD)wParam;
  1684. event.dwTimestamp = ::GetTickCount();
  1685. pControl->Event(event);
  1686. m_pEventRClick = pControl;
  1687. }
  1688. break;
  1689. case WM_RBUTTONUP:
  1690. {
  1691. if (m_bMouseCapture) ReleaseCapture();
  1692. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1693. m_ptLastMousePos = pt;
  1694. m_pEventRClick = FindControl(pt);
  1695. if (m_pEventRClick == NULL) break;
  1696. TEventUI event = { 0 };
  1697. event.Type = UIEVENT_RBUTTONUP;
  1698. event.pSender = m_pEventRClick;
  1699. event.wParam = wParam;
  1700. event.lParam = lParam;
  1701. event.ptMouse = pt;
  1702. event.wKeyState = (WORD)wParam;
  1703. event.dwTimestamp = ::GetTickCount();
  1704. m_pEventRClick->Event(event);
  1705. }
  1706. break;
  1707. case WM_MBUTTONDOWN:
  1708. {
  1709. if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
  1710. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1711. m_ptLastMousePos = pt;
  1712. CControlUI* pControl = FindControl(pt);
  1713. if (pControl == NULL) break;
  1714. if (pControl->GetManager() != this) break;
  1715. pControl->SetFocus();
  1716. SetCapture();
  1717. TEventUI event = { 0 };
  1718. event.Type = UIEVENT_MBUTTONDOWN;
  1719. event.pSender = pControl;
  1720. event.wParam = wParam;
  1721. event.lParam = lParam;
  1722. event.ptMouse = pt;
  1723. event.wKeyState = (WORD)wParam;
  1724. event.dwTimestamp = ::GetTickCount();
  1725. pControl->Event(event);
  1726. m_pEventClick = pControl;
  1727. }
  1728. break;
  1729. case WM_MBUTTONUP:
  1730. {
  1731. if (m_bMouseCapture) ReleaseCapture();
  1732. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1733. m_ptLastMousePos = pt;
  1734. m_pEventClick = FindControl(pt);
  1735. if (m_pEventClick == NULL) break;
  1736. TEventUI event = { 0 };
  1737. event.Type = UIEVENT_MBUTTONUP;
  1738. event.pSender = m_pEventClick;
  1739. event.wParam = wParam;
  1740. event.lParam = lParam;
  1741. event.ptMouse = pt;
  1742. event.wKeyState = (WORD)wParam;
  1743. event.dwTimestamp = ::GetTickCount();
  1744. m_pEventClick->Event(event);
  1745. }
  1746. break;
  1747. case WM_CONTEXTMENU:
  1748. {
  1749. if (m_pRoot == NULL) break;
  1750. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1751. ::ScreenToClient(m_hWndPaint, &pt);
  1752. m_ptLastMousePos = pt;
  1753. if (m_pEventRClick == NULL) break;
  1754. ReleaseCapture();
  1755. TEventUI event = { 0 };
  1756. event.Type = UIEVENT_CONTEXTMENU;
  1757. event.pSender = m_pEventRClick;
  1758. event.wParam = wParam;
  1759. event.lParam = lParam;
  1760. event.ptMouse = pt;
  1761. event.wKeyState = (WORD)wParam;
  1762. event.lParam = (LPARAM)m_pEventRClick;
  1763. event.dwTimestamp = ::GetTickCount();
  1764. m_pEventRClick->Event(event);
  1765. m_pEventRClick = NULL;
  1766. }
  1767. break;
  1768. case WM_MOUSEWHEEL:
  1769. {
  1770. if (m_pRoot == NULL) break;
  1771. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1772. ::ScreenToClient(m_hWndPaint, &pt);
  1773. m_ptLastMousePos = pt;
  1774. CControlUI* pControl = FindControl(pt);
  1775. if (pControl == NULL) break;
  1776. if (pControl->GetManager() != this) break;
  1777. int zDelta = (int)(short)HIWORD(wParam);
  1778. TEventUI event = { 0 };
  1779. event.Type = UIEVENT_SCROLLWHEEL;
  1780. event.pSender = pControl;
  1781. event.wParam = MAKEWPARAM(zDelta < 0 ? SB_LINEDOWN : SB_LINEUP, zDelta);
  1782. event.lParam = lParam;
  1783. event.ptMouse = pt;
  1784. event.wKeyState = MapKeyState();
  1785. event.dwTimestamp = ::GetTickCount();
  1786. pControl->Event(event);
  1787. // Let's make sure that the scroll item below the cursor is the same as before...
  1788. ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, (LPARAM)MAKELPARAM(m_ptLastMousePos.x, m_ptLastMousePos.y));
  1789. }
  1790. break;
  1791. case WM_CHAR:
  1792. {
  1793. if (m_pRoot == NULL) break;
  1794. if (m_pFocus == NULL) break;
  1795. TEventUI event = { 0 };
  1796. event.Type = UIEVENT_CHAR;
  1797. event.pSender = m_pFocus;
  1798. event.wParam = wParam;
  1799. event.lParam = lParam;
  1800. event.chKey = (TCHAR)wParam;
  1801. event.ptMouse = m_ptLastMousePos;
  1802. event.wKeyState = MapKeyState();
  1803. event.dwTimestamp = ::GetTickCount();
  1804. m_pFocus->Event(event);
  1805. }
  1806. break;
  1807. case WM_KEYDOWN:
  1808. {
  1809. if (m_pRoot == NULL) break;
  1810. if (m_pFocus == NULL) break;
  1811. TEventUI event = { 0 };
  1812. event.Type = UIEVENT_KEYDOWN;
  1813. event.pSender = m_pFocus;
  1814. event.wParam = wParam;
  1815. event.lParam = lParam;
  1816. event.chKey = (TCHAR)wParam;
  1817. event.ptMouse = m_ptLastMousePos;
  1818. event.wKeyState = MapKeyState();
  1819. event.dwTimestamp = ::GetTickCount();
  1820. m_pFocus->Event(event);
  1821. m_pEventKey = m_pFocus;
  1822. }
  1823. break;
  1824. case WM_KEYUP:
  1825. {
  1826. if (m_pRoot == NULL) break;
  1827. if (m_pEventKey == NULL) break;
  1828. TEventUI event = { 0 };
  1829. event.Type = UIEVENT_KEYUP;
  1830. event.pSender = m_pEventKey;
  1831. event.wParam = wParam;
  1832. event.lParam = lParam;
  1833. event.chKey = (TCHAR)wParam;
  1834. event.ptMouse = m_ptLastMousePos;
  1835. event.wKeyState = MapKeyState();
  1836. event.dwTimestamp = ::GetTickCount();
  1837. m_pEventKey->Event(event);
  1838. m_pEventKey = NULL;
  1839. }
  1840. break;
  1841. case WM_SETCURSOR:
  1842. {
  1843. if (m_pRoot == NULL) break;
  1844. if (LOWORD(lParam) != HTCLIENT) break;
  1845. if (m_bMouseCapture) return true;
  1846. POINT pt = { 0 };
  1847. ::GetCursorPos(&pt);
  1848. ::ScreenToClient(m_hWndPaint, &pt);
  1849. CControlUI* pControl = FindControl(pt);
  1850. if (pControl == NULL) break;
  1851. if ((pControl->GetControlFlags() & UIFLAG_SETCURSOR) == 0) break;
  1852. TEventUI event = { 0 };
  1853. event.Type = UIEVENT_SETCURSOR;
  1854. event.pSender = pControl;
  1855. event.wParam = wParam;
  1856. event.lParam = lParam;
  1857. event.ptMouse = pt;
  1858. event.wKeyState = MapKeyState();
  1859. event.dwTimestamp = ::GetTickCount();
  1860. pControl->Event(event);
  1861. }
  1862. return true;
  1863. case WM_SETFOCUS:
  1864. {
  1865. if (m_pFocus != NULL)
  1866. {
  1867. TEventUI event = { 0 };
  1868. event.Type = UIEVENT_SETFOCUS;
  1869. event.wParam = wParam;
  1870. event.lParam = lParam;
  1871. event.pSender = m_pFocus;
  1872. event.dwTimestamp = ::GetTickCount();
  1873. m_pFocus->Event(event);
  1874. }
  1875. break;
  1876. }
  1877. case WM_KILLFOCUS:
  1878. {
  1879. if (IsCaptured()) ReleaseCapture();
  1880. break;
  1881. }
  1882. case WM_NOTIFY:
  1883. {
  1884. if (lParam == 0) break;
  1885. LPNMHDR lpNMHDR = (LPNMHDR)lParam;
  1886. if (lpNMHDR != NULL) lRes = ::SendMessage(lpNMHDR->hwndFrom, OCM__BASE + uMsg, wParam, lParam);
  1887. return true;
  1888. }
  1889. break;
  1890. case WM_COMMAND:
  1891. {
  1892. if (lParam == 0) break;
  1893. HWND hWndChild = (HWND)lParam;
  1894. lRes = ::SendMessage(hWndChild, OCM__BASE + uMsg, wParam, lParam);
  1895. if (lRes != 0) return true;
  1896. }
  1897. break;
  1898. case WM_CTLCOLOREDIT:
  1899. case WM_CTLCOLORSTATIC:
  1900. {
  1901. // Refer To: http://msdn.microsoft.com/en-us/library/bb761691(v=vs.85).aspx
  1902. // Read-only or disabled edit controls do not send the WM_CTLCOLOREDIT message; instead, they send the WM_CTLCOLORSTATIC message.
  1903. if (lParam == 0) break;
  1904. HWND hWndChild = (HWND)lParam;
  1905. lRes = ::SendMessage(hWndChild, OCM__BASE + uMsg, wParam, lParam);
  1906. if (lRes != 0) return true;
  1907. }
  1908. break;
  1909. default:
  1910. break;
  1911. }
  1912. return false;
  1913. }
  1914. bool CPaintManagerUI::IsUpdateNeeded() const
  1915. {
  1916. return m_bUpdateNeeded;
  1917. }
  1918. void CPaintManagerUI::NeedUpdate()
  1919. {
  1920. m_bUpdateNeeded = true;
  1921. }
  1922. void CPaintManagerUI::Invalidate()
  1923. {
  1924. RECT rcClient = { 0 };
  1925. ::GetClientRect(m_hWndPaint, &rcClient);
  1926. ::UnionRect(&m_rcLayeredUpdate, &m_rcLayeredUpdate, &rcClient);
  1927. ::InvalidateRect(m_hWndPaint, NULL, FALSE);
  1928. }
  1929. void CPaintManagerUI::Invalidate(RECT& rcItem)
  1930. {
  1931. if (rcItem.left < 0) rcItem.left = 0;
  1932. if (rcItem.top < 0) rcItem.top = 0;
  1933. if (rcItem.right < rcItem.left) rcItem.right = rcItem.left;
  1934. if (rcItem.bottom < rcItem.top) rcItem.bottom = rcItem.top;
  1935. ::UnionRect(&m_rcLayeredUpdate, &m_rcLayeredUpdate, &rcItem);
  1936. ::InvalidateRect(m_hWndPaint, &rcItem, FALSE);
  1937. }
  1938. bool CPaintManagerUI::IsValid()
  1939. {
  1940. return m_hWndPaint != NULL && m_pRoot != NULL;
  1941. }
  1942. bool CPaintManagerUI::AttachDialog(CControlUI* pControl)
  1943. {
  1944. ASSERT(::IsWindow(m_hWndPaint));
  1945. // 创建阴影窗口
  1946. m_shadow.Create(this);
  1947. // Reset any previous attachment
  1948. SetFocus(NULL);
  1949. m_pEventKey = NULL;
  1950. m_pEventHover = NULL;
  1951. m_pEventClick = NULL;
  1952. m_pEventRClick = NULL;
  1953. // Remove the existing control-tree. We might have gotten inside this function as
  1954. // a result of an event fired or similar, so we cannot just delete the objects and
  1955. // pull the internal memory of the calling code. We'll delay the cleanup.
  1956. if (m_pRoot != NULL)
  1957. {
  1958. m_aPostPaintControls.Empty();
  1959. AddDelayedCleanup(m_pRoot);
  1960. }
  1961. // Set the dialog root element
  1962. m_pRoot = pControl;
  1963. // Go ahead...
  1964. m_bUpdateNeeded = true;
  1965. m_bFirstLayout = true;
  1966. m_bFocusNeeded = true;
  1967. // Initiate all control
  1968. return InitControls(pControl);
  1969. }
  1970. bool CPaintManagerUI::InitControls(CControlUI* pControl, CControlUI* pParent /*= NULL*/)
  1971. {
  1972. ASSERT(pControl);
  1973. if (pControl == NULL) return false;
  1974. pControl->SetManager(this, pParent != NULL ? pParent : pControl->GetParent(), true);
  1975. pControl->FindControl(__FindControlFromNameHash, this, UIFIND_ALL);
  1976. return true;
  1977. }
  1978. void CPaintManagerUI::ReapObjects(CControlUI* pControl)
  1979. {
  1980. if (pControl == m_pEventKey) m_pEventKey = NULL;
  1981. if (pControl == m_pEventHover) m_pEventHover = NULL;
  1982. if (pControl == m_pEventClick) m_pEventClick = NULL;
  1983. if (pControl == m_pEventRClick) m_pEventRClick = NULL;
  1984. if (pControl == m_pFocus) m_pFocus = NULL;
  1985. KillTimer(pControl);
  1986. const CDuiString& sName = pControl->GetName();
  1987. if (!sName.IsEmpty())
  1988. {
  1989. if (pControl == FindControl(sName)) m_mNameHash.Remove(sName);
  1990. }
  1991. for (int i = 0; i < m_aAsyncNotify.GetSize(); i++)
  1992. {
  1993. TNotifyUI* pMsg = static_cast<TNotifyUI*>(m_aAsyncNotify[i]);
  1994. if (pMsg->pSender == pControl) pMsg->pSender = NULL;
  1995. }
  1996. }
  1997. bool CPaintManagerUI::AddOptionGroup(LPCTSTR pStrGroupName, CControlUI* pControl)
  1998. {
  1999. LPVOID lp = m_mOptionGroup.Find(pStrGroupName);
  2000. if (lp)
  2001. {
  2002. CStdPtrArray* aOptionGroup = static_cast<CStdPtrArray*>(lp);
  2003. for (int i = 0; i < aOptionGroup->GetSize(); i++)
  2004. {
  2005. if (static_cast<CControlUI*>(aOptionGroup->GetAt(i)) == pControl)
  2006. {
  2007. return false;
  2008. }
  2009. }
  2010. aOptionGroup->Add(pControl);
  2011. }
  2012. else
  2013. {
  2014. CStdPtrArray* aOptionGroup = new CStdPtrArray(6);
  2015. aOptionGroup->Add(pControl);
  2016. m_mOptionGroup.Insert(pStrGroupName, aOptionGroup);
  2017. }
  2018. return true;
  2019. }
  2020. CStdPtrArray* CPaintManagerUI::GetOptionGroup(LPCTSTR pStrGroupName)
  2021. {
  2022. LPVOID lp = m_mOptionGroup.Find(pStrGroupName);
  2023. if (lp) return static_cast<CStdPtrArray*>(lp);
  2024. return NULL;
  2025. }
  2026. void CPaintManagerUI::RemoveOptionGroup(LPCTSTR pStrGroupName, CControlUI* pControl)
  2027. {
  2028. LPVOID lp = m_mOptionGroup.Find(pStrGroupName);
  2029. if (lp)
  2030. {
  2031. CStdPtrArray* aOptionGroup = static_cast<CStdPtrArray*>(lp);
  2032. if (aOptionGroup == NULL) return;
  2033. for (int i = 0; i < aOptionGroup->GetSize(); i++)
  2034. {
  2035. if (static_cast<CControlUI*>(aOptionGroup->GetAt(i)) == pControl)
  2036. {
  2037. aOptionGroup->Remove(i);
  2038. break;
  2039. }
  2040. }
  2041. if (aOptionGroup->IsEmpty())
  2042. {
  2043. delete aOptionGroup;
  2044. m_mOptionGroup.Remove(pStrGroupName);
  2045. }
  2046. }
  2047. }
  2048. void CPaintManagerUI::RemoveAllOptionGroups()
  2049. {
  2050. CStdPtrArray* aOptionGroup;
  2051. for (int i = 0; i < m_mOptionGroup.GetSize(); i++)
  2052. {
  2053. if (LPCTSTR key = m_mOptionGroup.GetAt(i))
  2054. {
  2055. aOptionGroup = static_cast<CStdPtrArray*>(m_mOptionGroup.Find(key));
  2056. delete aOptionGroup;
  2057. }
  2058. }
  2059. m_mOptionGroup.RemoveAll();
  2060. }
  2061. void CPaintManagerUI::MessageLoop()
  2062. {
  2063. MSG msg = { 0 };
  2064. while (::GetMessage(&msg, NULL, 0, 0))
  2065. {
  2066. if (!CPaintManagerUI::TranslateMessage(&msg))
  2067. {
  2068. ::TranslateMessage(&msg);
  2069. try
  2070. {
  2071. ::DispatchMessage(&msg);
  2072. }
  2073. catch (...)
  2074. {
  2075. DUITRACE(_T("EXCEPTION: %s(%d)\n"), __FILET__, __LINE__);
  2076. #ifdef _DEBUG
  2077. throw "CPaintManagerUI::MessageLoop";
  2078. #endif
  2079. }
  2080. }
  2081. }
  2082. }
  2083. void CPaintManagerUI::Term()
  2084. {
  2085. // 销毁资源管理器
  2086. CResourceManager::GetInstance()->Release();
  2087. CControlFactory::GetInstance()->Release();
  2088. // 清理共享资源
  2089. // 图片
  2090. TImageInfo* data;
  2091. for (int i = 0; i < m_SharedResInfo.m_ImageHash.GetSize(); i++)
  2092. {
  2093. if (LPCTSTR key = m_SharedResInfo.m_ImageHash.GetAt(i))
  2094. {
  2095. data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(key, false));
  2096. if (data)
  2097. {
  2098. CRenderEngine::FreeImage(data);
  2099. data = NULL;
  2100. }
  2101. }
  2102. }
  2103. m_SharedResInfo.m_ImageHash.RemoveAll();
  2104. // 字体
  2105. TFontInfo* pFontInfo;
  2106. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  2107. {
  2108. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  2109. {
  2110. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key, false));
  2111. if (pFontInfo)
  2112. {
  2113. ::DeleteObject(pFontInfo->hFont);
  2114. delete pFontInfo;
  2115. pFontInfo = NULL;
  2116. }
  2117. }
  2118. }
  2119. m_SharedResInfo.m_CustomFonts.RemoveAll();
  2120. // 默认字体
  2121. if (m_SharedResInfo.m_DefaultFontInfo.hFont != NULL)
  2122. {
  2123. ::DeleteObject(m_SharedResInfo.m_DefaultFontInfo.hFont);
  2124. }
  2125. // 样式
  2126. CDuiString* pStyle;
  2127. for (int i = 0; i < m_SharedResInfo.m_StyleHash.GetSize(); i++)
  2128. {
  2129. if (LPCTSTR key = m_SharedResInfo.m_StyleHash.GetAt(i))
  2130. {
  2131. pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(key, false));
  2132. if (pStyle)
  2133. {
  2134. delete pStyle;
  2135. pStyle = NULL;
  2136. }
  2137. }
  2138. }
  2139. m_SharedResInfo.m_StyleHash.RemoveAll();
  2140. // 样式
  2141. CDuiString* pAttr;
  2142. for (int i = 0; i < m_SharedResInfo.m_AttrHash.GetSize(); i++)
  2143. {
  2144. if (LPCTSTR key = m_SharedResInfo.m_AttrHash.GetAt(i))
  2145. {
  2146. pAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Find(key, false));
  2147. if (pAttr)
  2148. {
  2149. delete pAttr;
  2150. pAttr = NULL;
  2151. }
  2152. }
  2153. }
  2154. m_SharedResInfo.m_AttrHash.RemoveAll();
  2155. // 关闭ZIP
  2156. if (m_bCachedResourceZip && m_hResourceZip != NULL)
  2157. {
  2158. CloseZip((HZIP)m_hResourceZip);
  2159. m_hResourceZip = NULL;
  2160. }
  2161. if (m_cbZipBuf)
  2162. {
  2163. delete[] m_cbZipBuf;
  2164. m_cbZipBuf = nullptr;
  2165. }
  2166. }
  2167. CDPI* DuiLib::CPaintManagerUI::GetDPIObj()
  2168. {
  2169. if (m_pDPI == NULL)
  2170. {
  2171. m_pDPI = new CDPI;
  2172. }
  2173. return m_pDPI;
  2174. }
  2175. void DuiLib::CPaintManagerUI::SetDPI(int iDPI)
  2176. {
  2177. int scale1 = GetDPIObj()->GetScale();
  2178. GetDPIObj()->SetScale(iDPI);
  2179. int scale2 = GetDPIObj()->GetScale();
  2180. ResetDPIAssets();
  2181. RECT rcWnd = { 0 };
  2182. ::GetWindowRect(GetPaintWindow(), &rcWnd);
  2183. RECT* prcNewWindow = &rcWnd;
  2184. if (!::IsZoomed(GetPaintWindow()))
  2185. {
  2186. RECT rc = rcWnd;
  2187. rc.right = rcWnd.left + (rcWnd.right - rcWnd.left) * scale2 / scale1;
  2188. rc.bottom = rcWnd.top + (rcWnd.bottom - rcWnd.top) * scale2 / scale1;
  2189. prcNewWindow = &rc;
  2190. }
  2191. SetWindowPos(GetPaintWindow(), NULL, prcNewWindow->left, prcNewWindow->top, prcNewWindow->right - prcNewWindow->left, prcNewWindow->bottom - prcNewWindow->top, SWP_NOZORDER | SWP_NOACTIVATE);
  2192. if (GetRoot() != NULL) GetRoot()->NeedUpdate();
  2193. ::PostMessage(GetPaintWindow(), UIMSG_SET_DPI, 0, 0);
  2194. }
  2195. void DuiLib::CPaintManagerUI::SetAllDPI(int iDPI)
  2196. {
  2197. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  2198. {
  2199. CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  2200. pManager->SetDPI(iDPI);
  2201. }
  2202. }
  2203. void DuiLib::CPaintManagerUI::ResetDPIAssets()
  2204. {
  2205. RemoveAllDrawInfos();
  2206. RemoveAllImages();;
  2207. for (int it = 0; it < m_ResInfo.m_CustomFonts.GetSize(); it++)
  2208. {
  2209. TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(m_ResInfo.m_CustomFonts[it]));
  2210. RebuildFont(pFontInfo);
  2211. }
  2212. RebuildFont(&m_ResInfo.m_DefaultFontInfo);
  2213. for (int it = 0; it < m_SharedResInfo.m_CustomFonts.GetSize(); it++)
  2214. {
  2215. TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(m_SharedResInfo.m_CustomFonts[it]));
  2216. RebuildFont(pFontInfo);
  2217. }
  2218. RebuildFont(&m_SharedResInfo.m_DefaultFontInfo);
  2219. CStdPtrArray* richEditList = FindSubControlsByClass(GetRoot(), _T("RichEditUI"));
  2220. for (int i = 0; i < richEditList->GetSize(); i++)
  2221. {
  2222. CRichEditUI* pT = static_cast<CRichEditUI*>((*richEditList)[i]);
  2223. pT->SetFont(pT->GetFont());
  2224. }
  2225. }
  2226. void DuiLib::CPaintManagerUI::RebuildFont(TFontInfo* pFontInfo)
  2227. {
  2228. ::DeleteObject(pFontInfo->hFont);
  2229. LOGFONT lf = { 0 };
  2230. ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
  2231. _tcsncpy(lf.lfFaceName, pFontInfo->sFontName, LF_FACESIZE);
  2232. lf.lfCharSet = DEFAULT_CHARSET;
  2233. lf.lfHeight = -GetDPIObj()->Scale(pFontInfo->iSize);
  2234. lf.lfQuality = CLEARTYPE_QUALITY;
  2235. if (pFontInfo->bBold) lf.lfWeight += FW_BOLD;
  2236. if (pFontInfo->bUnderline) lf.lfUnderline = TRUE;
  2237. if (pFontInfo->bItalic) lf.lfItalic = TRUE;
  2238. HFONT hFont = ::CreateFontIndirect(&lf);
  2239. pFontInfo->hFont = hFont;
  2240. ::ZeroMemory(&(pFontInfo->tm), sizeof(pFontInfo->tm));
  2241. if (m_hDcPaint)
  2242. {
  2243. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
  2244. ::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
  2245. ::SelectObject(m_hDcPaint, hOldFont);
  2246. }
  2247. }
  2248. CControlUI* CPaintManagerUI::GetFocus() const
  2249. {
  2250. return m_pFocus;
  2251. }
  2252. void CPaintManagerUI::SetFocus(CControlUI* pControl)
  2253. {
  2254. // Paint manager window has focus?
  2255. HWND hFocusWnd = ::GetFocus();
  2256. if (!m_bNoActivate && hFocusWnd != m_hWndPaint && pControl != m_pFocus) ::SetFocus(m_hWndPaint);
  2257. // Already has focus?
  2258. if (pControl == m_pFocus) return;
  2259. // Remove focus from old control
  2260. if (m_pFocus != NULL)
  2261. {
  2262. TEventUI event = { 0 };
  2263. event.Type = UIEVENT_KILLFOCUS;
  2264. event.pSender = pControl;
  2265. event.dwTimestamp = ::GetTickCount();
  2266. m_pFocus->Event(event);
  2267. SendNotify(m_pFocus, DUI_MSGTYPE_KILLFOCUS);
  2268. m_pFocus = NULL;
  2269. }
  2270. if (pControl == NULL) return;
  2271. // Set focus to new control
  2272. if (pControl != NULL
  2273. && pControl->GetManager() == this
  2274. && pControl->IsVisible()
  2275. && pControl->IsEnabled())
  2276. {
  2277. m_pFocus = pControl;
  2278. TEventUI event = { 0 };
  2279. event.Type = UIEVENT_SETFOCUS;
  2280. event.pSender = pControl;
  2281. event.dwTimestamp = ::GetTickCount();
  2282. m_pFocus->Event(event);
  2283. SendNotify(m_pFocus, DUI_MSGTYPE_SETFOCUS);
  2284. }
  2285. }
  2286. void CPaintManagerUI::SetFocusNeeded(CControlUI* pControl)
  2287. {
  2288. if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
  2289. if (pControl == NULL) return;
  2290. if (m_pFocus != NULL)
  2291. {
  2292. TEventUI event = { 0 };
  2293. event.Type = UIEVENT_KILLFOCUS;
  2294. event.pSender = pControl;
  2295. event.dwTimestamp = ::GetTickCount();
  2296. m_pFocus->Event(event);
  2297. SendNotify(m_pFocus, DUI_MSGTYPE_KILLFOCUS);
  2298. m_pFocus = NULL;
  2299. }
  2300. FINDTABINFO info = { 0 };
  2301. info.pFocus = pControl;
  2302. info.bForward = false;
  2303. m_pFocus = m_pRoot->FindControl(__FindControlFromTab, &info, UIFIND_VISIBLE | UIFIND_ENABLED | UIFIND_ME_FIRST);
  2304. m_bFocusNeeded = true;
  2305. if (m_pRoot != NULL) m_pRoot->NeedUpdate();
  2306. }
  2307. bool CPaintManagerUI::SetTimer(CControlUI* pControl, UINT nTimerID, UINT uElapse)
  2308. {
  2309. ASSERT(pControl != NULL);
  2310. ASSERT(uElapse > 0);
  2311. for (int i = 0; i < m_aTimers.GetSize(); i++)
  2312. {
  2313. TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
  2314. if (pTimer->pSender == pControl
  2315. && pTimer->hWnd == m_hWndPaint
  2316. && pTimer->nLocalID == nTimerID)
  2317. {
  2318. if (pTimer->bKilled == true)
  2319. {
  2320. if (::SetTimer(m_hWndPaint, pTimer->uWinTimer, uElapse, NULL))
  2321. {
  2322. pTimer->bKilled = false;
  2323. return true;
  2324. }
  2325. return false;
  2326. }
  2327. return false;
  2328. }
  2329. }
  2330. m_uTimerID = (++m_uTimerID) % 0xF0; //0xf1-0xfe特殊用途
  2331. if (!::SetTimer(m_hWndPaint, m_uTimerID, uElapse, NULL)) return FALSE;
  2332. TIMERINFO* pTimer = new TIMERINFO;
  2333. if (pTimer == NULL) return FALSE;
  2334. pTimer->hWnd = m_hWndPaint;
  2335. pTimer->pSender = pControl;
  2336. pTimer->nLocalID = nTimerID;
  2337. pTimer->uWinTimer = m_uTimerID;
  2338. pTimer->bKilled = false;
  2339. return m_aTimers.Add(pTimer);
  2340. }
  2341. bool CPaintManagerUI::KillTimer(CControlUI* pControl, UINT nTimerID)
  2342. {
  2343. ASSERT(pControl != NULL);
  2344. for (int i = 0; i < m_aTimers.GetSize(); i++)
  2345. {
  2346. TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
  2347. if (pTimer->pSender == pControl
  2348. && pTimer->hWnd == m_hWndPaint
  2349. && pTimer->nLocalID == nTimerID)
  2350. {
  2351. if (pTimer->bKilled == false)
  2352. {
  2353. if (::IsWindow(m_hWndPaint)) ::KillTimer(pTimer->hWnd, pTimer->uWinTimer);
  2354. pTimer->bKilled = true;
  2355. return true;
  2356. }
  2357. }
  2358. }
  2359. return false;
  2360. }
  2361. void CPaintManagerUI::KillTimer(CControlUI* pControl)
  2362. {
  2363. ASSERT(pControl != NULL);
  2364. int count = m_aTimers.GetSize();
  2365. for (int i = 0, j = 0; i < count; i++)
  2366. {
  2367. TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i - j]);
  2368. if (pTimer->pSender == pControl && pTimer->hWnd == m_hWndPaint)
  2369. {
  2370. if (pTimer->bKilled == false) ::KillTimer(pTimer->hWnd, pTimer->uWinTimer);
  2371. delete pTimer;
  2372. m_aTimers.Remove(i - j);
  2373. j++;
  2374. }
  2375. }
  2376. }
  2377. void CPaintManagerUI::RemoveAllTimers()
  2378. {
  2379. for (int i = 0; i < m_aTimers.GetSize(); i++)
  2380. {
  2381. TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
  2382. if (pTimer->hWnd == m_hWndPaint)
  2383. {
  2384. if (pTimer->bKilled == false)
  2385. {
  2386. if (::IsWindow(m_hWndPaint)) ::KillTimer(m_hWndPaint, pTimer->uWinTimer);
  2387. }
  2388. delete pTimer;
  2389. }
  2390. }
  2391. m_aTimers.Empty();
  2392. }
  2393. void CPaintManagerUI::SetCapture()
  2394. {
  2395. ::SetCapture(m_hWndPaint);
  2396. m_bMouseCapture = true;
  2397. }
  2398. void CPaintManagerUI::ReleaseCapture()
  2399. {
  2400. ::ReleaseCapture();
  2401. m_bMouseCapture = false;
  2402. m_bDragMode = false;
  2403. }
  2404. bool CPaintManagerUI::IsCaptured()
  2405. {
  2406. return m_bMouseCapture;
  2407. }
  2408. bool CPaintManagerUI::IsPainting()
  2409. {
  2410. return m_bIsPainting;
  2411. }
  2412. void CPaintManagerUI::SetPainting(bool bIsPainting)
  2413. {
  2414. m_bIsPainting = bIsPainting;
  2415. }
  2416. bool CPaintManagerUI::SetNextTabControl(bool bForward)
  2417. {
  2418. // If we're in the process of restructuring the layout we can delay the
  2419. // focus calulation until the next repaint.
  2420. if (m_bUpdateNeeded && bForward)
  2421. {
  2422. m_bFocusNeeded = true;
  2423. ::InvalidateRect(m_hWndPaint, NULL, FALSE);
  2424. return true;
  2425. }
  2426. // Find next/previous tabbable control
  2427. FINDTABINFO info1 = { 0 };
  2428. info1.pFocus = m_pFocus;
  2429. info1.bForward = bForward;
  2430. CControlUI* pControl = m_pRoot->FindControl(__FindControlFromTab, &info1, UIFIND_VISIBLE | UIFIND_ENABLED | UIFIND_ME_FIRST);
  2431. if (pControl == NULL)
  2432. {
  2433. if (bForward)
  2434. {
  2435. // Wrap around
  2436. FINDTABINFO info2 = { 0 };
  2437. info2.pFocus = bForward ? NULL : info1.pLast;
  2438. info2.bForward = bForward;
  2439. pControl = m_pRoot->FindControl(__FindControlFromTab, &info2, UIFIND_VISIBLE | UIFIND_ENABLED | UIFIND_ME_FIRST);
  2440. }
  2441. else
  2442. {
  2443. pControl = info1.pLast;
  2444. }
  2445. }
  2446. if (pControl != NULL) SetFocus(pControl);
  2447. m_bFocusNeeded = false;
  2448. return true;
  2449. }
  2450. bool CPaintManagerUI::AddNotifier(INotifyUI* pNotifier)
  2451. {
  2452. ASSERT(m_aNotifiers.Find(pNotifier) < 0);
  2453. return m_aNotifiers.Add(pNotifier);
  2454. }
  2455. bool CPaintManagerUI::RemoveNotifier(INotifyUI* pNotifier)
  2456. {
  2457. for (int i = 0; i < m_aNotifiers.GetSize(); i++)
  2458. {
  2459. if (static_cast<INotifyUI*>(m_aNotifiers[i]) == pNotifier)
  2460. {
  2461. return m_aNotifiers.Remove(i);
  2462. }
  2463. }
  2464. return false;
  2465. }
  2466. bool CPaintManagerUI::AddPreMessageFilter(IMessageFilterUI* pFilter)
  2467. {
  2468. ASSERT(m_aPreMessageFilters.Find(pFilter) < 0);
  2469. return m_aPreMessageFilters.Add(pFilter);
  2470. }
  2471. bool CPaintManagerUI::RemovePreMessageFilter(IMessageFilterUI* pFilter)
  2472. {
  2473. for (int i = 0; i < m_aPreMessageFilters.GetSize(); i++)
  2474. {
  2475. if (static_cast<IMessageFilterUI*>(m_aPreMessageFilters[i]) == pFilter)
  2476. {
  2477. return m_aPreMessageFilters.Remove(i);
  2478. }
  2479. }
  2480. return false;
  2481. }
  2482. bool CPaintManagerUI::AddMessageFilter(IMessageFilterUI* pFilter)
  2483. {
  2484. ASSERT(m_aMessageFilters.Find(pFilter) < 0);
  2485. return m_aMessageFilters.Add(pFilter);
  2486. }
  2487. bool CPaintManagerUI::RemoveMessageFilter(IMessageFilterUI* pFilter)
  2488. {
  2489. for (int i = 0; i < m_aMessageFilters.GetSize(); i++)
  2490. {
  2491. if (static_cast<IMessageFilterUI*>(m_aMessageFilters[i]) == pFilter)
  2492. {
  2493. return m_aMessageFilters.Remove(i);
  2494. }
  2495. }
  2496. return false;
  2497. }
  2498. int CPaintManagerUI::GetPostPaintCount() const
  2499. {
  2500. return m_aPostPaintControls.GetSize();
  2501. }
  2502. bool CPaintManagerUI::IsPostPaint(CControlUI* pControl)
  2503. {
  2504. return m_aPostPaintControls.Find(pControl) >= 0;
  2505. }
  2506. bool CPaintManagerUI::AddPostPaint(CControlUI* pControl)
  2507. {
  2508. ASSERT(m_aPostPaintControls.Find(pControl) < 0);
  2509. return m_aPostPaintControls.Add(pControl);
  2510. }
  2511. bool CPaintManagerUI::RemovePostPaint(CControlUI* pControl)
  2512. {
  2513. for (int i = 0; i < m_aPostPaintControls.GetSize(); i++)
  2514. {
  2515. if (static_cast<CControlUI*>(m_aPostPaintControls[i]) == pControl)
  2516. {
  2517. return m_aPostPaintControls.Remove(i);
  2518. }
  2519. }
  2520. return false;
  2521. }
  2522. bool CPaintManagerUI::SetPostPaintIndex(CControlUI* pControl, int iIndex)
  2523. {
  2524. RemovePostPaint(pControl);
  2525. return m_aPostPaintControls.InsertAt(iIndex, pControl);
  2526. }
  2527. int CPaintManagerUI::GetNativeWindowCount() const
  2528. {
  2529. return m_aNativeWindow.GetSize();
  2530. }
  2531. bool CPaintManagerUI::AddNativeWindow(CControlUI* pControl, HWND hChildWnd)
  2532. {
  2533. if (pControl == NULL || hChildWnd == NULL) return false;
  2534. RECT rcChildWnd = GetNativeWindowRect(hChildWnd);
  2535. Invalidate(rcChildWnd);
  2536. if (m_aNativeWindow.Find(hChildWnd) >= 0) return false;
  2537. if (m_aNativeWindow.Add(hChildWnd))
  2538. {
  2539. m_aNativeWindowControl.Add(pControl);
  2540. return true;
  2541. }
  2542. return false;
  2543. }
  2544. bool CPaintManagerUI::RemoveNativeWindow(HWND hChildWnd)
  2545. {
  2546. for (int i = 0; i < m_aNativeWindow.GetSize(); i++)
  2547. {
  2548. if (static_cast<HWND>(m_aNativeWindow[i]) == hChildWnd)
  2549. {
  2550. if (m_aNativeWindow.Remove(i))
  2551. {
  2552. m_aNativeWindowControl.Remove(i);
  2553. return true;
  2554. }
  2555. return false;
  2556. }
  2557. }
  2558. return false;
  2559. }
  2560. RECT CPaintManagerUI::GetNativeWindowRect(HWND hChildWnd)
  2561. {
  2562. RECT rcChildWnd;
  2563. ::GetWindowRect(hChildWnd, &rcChildWnd);
  2564. ::ScreenToClient(m_hWndPaint, (LPPOINT)(&rcChildWnd));
  2565. ::ScreenToClient(m_hWndPaint, (LPPOINT)(&rcChildWnd) + 1);
  2566. return rcChildWnd;
  2567. }
  2568. void CPaintManagerUI::AddDelayedCleanup(CControlUI* pControl)
  2569. {
  2570. if (pControl == NULL) return;
  2571. pControl->SetManager(this, NULL, false);
  2572. m_aDelayedCleanup.Add(pControl);
  2573. PostAsyncNotify();
  2574. }
  2575. void CPaintManagerUI::AddMouseLeaveNeeded(CControlUI* pControl)
  2576. {
  2577. if (pControl == NULL) return;
  2578. for (int i = 0; i < m_aNeedMouseLeaveNeeded.GetSize(); i++)
  2579. {
  2580. if (static_cast<CControlUI*>(m_aNeedMouseLeaveNeeded[i]) == pControl)
  2581. {
  2582. return;
  2583. }
  2584. }
  2585. m_aNeedMouseLeaveNeeded.Add(pControl);
  2586. }
  2587. bool CPaintManagerUI::RemoveMouseLeaveNeeded(CControlUI* pControl)
  2588. {
  2589. if (pControl == NULL) return false;
  2590. for (int i = 0; i < m_aNeedMouseLeaveNeeded.GetSize(); i++)
  2591. {
  2592. if (static_cast<CControlUI*>(m_aNeedMouseLeaveNeeded[i]) == pControl)
  2593. {
  2594. return m_aNeedMouseLeaveNeeded.Remove(i);
  2595. }
  2596. }
  2597. return false;
  2598. }
  2599. void CPaintManagerUI::SendNotify(CControlUI* pControl, LPCTSTR pstrMessage, WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/, bool bAsync /*= false*/)
  2600. {
  2601. TNotifyUI Msg;
  2602. Msg.pSender = pControl;
  2603. Msg.sType = pstrMessage;
  2604. Msg.wParam = wParam;
  2605. Msg.lParam = lParam;
  2606. SendNotify(Msg, bAsync);
  2607. }
  2608. void CPaintManagerUI::SendNotify(TNotifyUI& Msg, bool bAsync /*= false*/)
  2609. {
  2610. Msg.ptMouse = m_ptLastMousePos;
  2611. Msg.dwTimestamp = ::GetTickCount();
  2612. if (m_bUsedVirtualWnd)
  2613. {
  2614. Msg.sVirtualWnd = Msg.pSender->GetVirtualWnd();
  2615. }
  2616. if (!bAsync)
  2617. {
  2618. // Send to all listeners
  2619. if (Msg.pSender != NULL)
  2620. {
  2621. if (Msg.pSender->OnNotify) Msg.pSender->OnNotify(&Msg);
  2622. }
  2623. for (int i = 0; i < m_aNotifiers.GetSize(); i++)
  2624. {
  2625. static_cast<INotifyUI*>(m_aNotifiers[i])->Notify(Msg);
  2626. }
  2627. }
  2628. else
  2629. {
  2630. TNotifyUI* pMsg = new TNotifyUI;
  2631. pMsg->sVirtualWnd = Msg.sVirtualWnd;
  2632. pMsg->pSender = Msg.pSender;
  2633. pMsg->sType = Msg.sType;
  2634. pMsg->wParam = Msg.wParam;
  2635. pMsg->lParam = Msg.lParam;
  2636. pMsg->ptMouse = Msg.ptMouse;
  2637. pMsg->dwTimestamp = Msg.dwTimestamp;
  2638. m_aAsyncNotify.Add(pMsg);
  2639. PostAsyncNotify();
  2640. }
  2641. }
  2642. bool CPaintManagerUI::IsForceUseSharedRes() const
  2643. {
  2644. return m_bForceUseSharedRes;
  2645. }
  2646. void CPaintManagerUI::SetForceUseSharedRes(bool bForce)
  2647. {
  2648. m_bForceUseSharedRes = bForce;
  2649. }
  2650. DWORD CPaintManagerUI::GetDefaultDisabledColor() const
  2651. {
  2652. return m_ResInfo.m_dwDefaultDisabledColor;
  2653. }
  2654. void CPaintManagerUI::SetDefaultDisabledColor(DWORD dwColor, bool bShared)
  2655. {
  2656. if (bShared)
  2657. {
  2658. if (m_ResInfo.m_dwDefaultDisabledColor == m_SharedResInfo.m_dwDefaultDisabledColor)
  2659. m_ResInfo.m_dwDefaultDisabledColor = dwColor;
  2660. m_SharedResInfo.m_dwDefaultDisabledColor = dwColor;
  2661. }
  2662. else
  2663. {
  2664. m_ResInfo.m_dwDefaultDisabledColor = dwColor;
  2665. }
  2666. }
  2667. DWORD CPaintManagerUI::GetDefaultFontColor() const
  2668. {
  2669. return m_ResInfo.m_dwDefaultFontColor;
  2670. }
  2671. void CPaintManagerUI::SetDefaultFontColor(DWORD dwColor, bool bShared)
  2672. {
  2673. if (bShared)
  2674. {
  2675. if (m_ResInfo.m_dwDefaultFontColor == m_SharedResInfo.m_dwDefaultFontColor)
  2676. m_ResInfo.m_dwDefaultFontColor = dwColor;
  2677. m_SharedResInfo.m_dwDefaultFontColor = dwColor;
  2678. }
  2679. else
  2680. {
  2681. m_ResInfo.m_dwDefaultFontColor = dwColor;
  2682. }
  2683. }
  2684. DWORD CPaintManagerUI::GetDefaultLinkFontColor() const
  2685. {
  2686. return m_ResInfo.m_dwDefaultLinkFontColor;
  2687. }
  2688. void CPaintManagerUI::SetDefaultLinkFontColor(DWORD dwColor, bool bShared)
  2689. {
  2690. if (bShared)
  2691. {
  2692. if (m_ResInfo.m_dwDefaultLinkFontColor == m_SharedResInfo.m_dwDefaultLinkFontColor)
  2693. m_ResInfo.m_dwDefaultLinkFontColor = dwColor;
  2694. m_SharedResInfo.m_dwDefaultLinkFontColor = dwColor;
  2695. }
  2696. else
  2697. {
  2698. m_ResInfo.m_dwDefaultLinkFontColor = dwColor;
  2699. }
  2700. }
  2701. DWORD CPaintManagerUI::GetDefaultLinkHoverFontColor() const
  2702. {
  2703. return m_ResInfo.m_dwDefaultLinkHoverFontColor;
  2704. }
  2705. void CPaintManagerUI::SetDefaultLinkHoverFontColor(DWORD dwColor, bool bShared)
  2706. {
  2707. if (bShared)
  2708. {
  2709. if (m_ResInfo.m_dwDefaultLinkHoverFontColor == m_SharedResInfo.m_dwDefaultLinkHoverFontColor)
  2710. m_ResInfo.m_dwDefaultLinkHoverFontColor = dwColor;
  2711. m_SharedResInfo.m_dwDefaultLinkHoverFontColor = dwColor;
  2712. }
  2713. else
  2714. {
  2715. m_ResInfo.m_dwDefaultLinkHoverFontColor = dwColor;
  2716. }
  2717. }
  2718. DWORD CPaintManagerUI::GetDefaultSelectedBkColor() const
  2719. {
  2720. return m_ResInfo.m_dwDefaultSelectedBkColor;
  2721. }
  2722. void CPaintManagerUI::SetDefaultSelectedBkColor(DWORD dwColor, bool bShared)
  2723. {
  2724. if (bShared)
  2725. {
  2726. if (m_ResInfo.m_dwDefaultSelectedBkColor == m_SharedResInfo.m_dwDefaultSelectedBkColor)
  2727. m_ResInfo.m_dwDefaultSelectedBkColor = dwColor;
  2728. m_SharedResInfo.m_dwDefaultSelectedBkColor = dwColor;
  2729. }
  2730. else
  2731. {
  2732. m_ResInfo.m_dwDefaultSelectedBkColor = dwColor;
  2733. }
  2734. }
  2735. TFontInfo* CPaintManagerUI::GetDefaultFontInfo()
  2736. {
  2737. if (m_ResInfo.m_DefaultFontInfo.sFontName.IsEmpty())
  2738. {
  2739. if (m_SharedResInfo.m_DefaultFontInfo.tm.tmHeight == 0)
  2740. {
  2741. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, m_SharedResInfo.m_DefaultFontInfo.hFont);
  2742. ::GetTextMetrics(m_hDcPaint, &m_SharedResInfo.m_DefaultFontInfo.tm);
  2743. ::SelectObject(m_hDcPaint, hOldFont);
  2744. }
  2745. return &m_SharedResInfo.m_DefaultFontInfo;
  2746. }
  2747. else
  2748. {
  2749. if (m_ResInfo.m_DefaultFontInfo.tm.tmHeight == 0)
  2750. {
  2751. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, m_ResInfo.m_DefaultFontInfo.hFont);
  2752. ::GetTextMetrics(m_hDcPaint, &m_ResInfo.m_DefaultFontInfo.tm);
  2753. ::SelectObject(m_hDcPaint, hOldFont);
  2754. }
  2755. return &m_ResInfo.m_DefaultFontInfo;
  2756. }
  2757. }
  2758. void CPaintManagerUI::SetDefaultFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared)
  2759. {
  2760. LOGFONT lf = { 0 };
  2761. ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
  2762. if (lstrlen(pStrFontName) > 0)
  2763. {
  2764. TCHAR szFaceName[32] = { 0 };//_T("@");
  2765. _tcsncat(szFaceName, pStrFontName, LF_FACESIZE);
  2766. _tcsncpy(lf.lfFaceName, szFaceName, LF_FACESIZE);
  2767. }
  2768. lf.lfCharSet = DEFAULT_CHARSET;
  2769. lf.lfHeight = -GetDPIObj()->Scale(nSize);;
  2770. if (bBold) lf.lfWeight += FW_BOLD;
  2771. if (bUnderline) lf.lfUnderline = TRUE;
  2772. if (bItalic) lf.lfItalic = TRUE;
  2773. if (bStrikeout) lf.lfStrikeOut = TRUE;
  2774. HFONT hFont = ::CreateFontIndirect(&lf);
  2775. if (hFont == NULL) return;
  2776. if (bShared)
  2777. {
  2778. ::DeleteObject(m_SharedResInfo.m_DefaultFontInfo.hFont);
  2779. m_SharedResInfo.m_DefaultFontInfo.hFont = hFont;
  2780. m_SharedResInfo.m_DefaultFontInfo.sFontName = lf.lfFaceName;
  2781. m_SharedResInfo.m_DefaultFontInfo.iSize = nSize;
  2782. m_SharedResInfo.m_DefaultFontInfo.bBold = bBold;
  2783. m_SharedResInfo.m_DefaultFontInfo.bUnderline = bUnderline;
  2784. m_SharedResInfo.m_DefaultFontInfo.bItalic = bItalic;
  2785. m_SharedResInfo.m_DefaultFontInfo.bStrikeout = bStrikeout;
  2786. ::ZeroMemory(&m_SharedResInfo.m_DefaultFontInfo.tm, sizeof(m_SharedResInfo.m_DefaultFontInfo.tm));
  2787. if (m_hDcPaint)
  2788. {
  2789. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
  2790. ::GetTextMetrics(m_hDcPaint, &m_SharedResInfo.m_DefaultFontInfo.tm);
  2791. ::SelectObject(m_hDcPaint, hOldFont);
  2792. }
  2793. }
  2794. else
  2795. {
  2796. ::DeleteObject(m_ResInfo.m_DefaultFontInfo.hFont);
  2797. m_ResInfo.m_DefaultFontInfo.hFont = hFont;
  2798. m_ResInfo.m_DefaultFontInfo.sFontName = lf.lfFaceName;
  2799. m_ResInfo.m_DefaultFontInfo.iSize = nSize;
  2800. m_ResInfo.m_DefaultFontInfo.bBold = bBold;
  2801. m_ResInfo.m_DefaultFontInfo.bUnderline = bUnderline;
  2802. m_ResInfo.m_DefaultFontInfo.bItalic = bItalic;
  2803. m_ResInfo.m_DefaultFontInfo.bStrikeout = bStrikeout;
  2804. ::ZeroMemory(&m_ResInfo.m_DefaultFontInfo.tm, sizeof(m_ResInfo.m_DefaultFontInfo.tm));
  2805. if (m_hDcPaint)
  2806. {
  2807. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
  2808. ::GetTextMetrics(m_hDcPaint, &m_ResInfo.m_DefaultFontInfo.tm);
  2809. ::SelectObject(m_hDcPaint, hOldFont);
  2810. }
  2811. }
  2812. }
  2813. DWORD CPaintManagerUI::GetCustomFontCount(bool bShared) const
  2814. {
  2815. if (bShared)
  2816. return m_SharedResInfo.m_CustomFonts.GetSize();
  2817. else
  2818. return m_ResInfo.m_CustomFonts.GetSize();
  2819. }
  2820. HFONT CPaintManagerUI::AddFont(int id, LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared)
  2821. {
  2822. LOGFONT lf = { 0 };
  2823. ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
  2824. if (lstrlen(pStrFontName) > 0)
  2825. {
  2826. TCHAR szFaceName[32] = { 0 };//_T("@");
  2827. _tcsncat(szFaceName, pStrFontName, LF_FACESIZE);
  2828. _tcsncpy(lf.lfFaceName, szFaceName, LF_FACESIZE);
  2829. }
  2830. lf.lfCharSet = DEFAULT_CHARSET;
  2831. lf.lfHeight = -GetDPIObj()->Scale(nSize);
  2832. if (bBold) lf.lfWeight = FW_BOLD;
  2833. if (bUnderline) lf.lfUnderline = TRUE;
  2834. if (bItalic) lf.lfItalic = TRUE;
  2835. if (bStrikeout) lf.lfStrikeOut = TRUE;
  2836. HFONT hFont = ::CreateFontIndirect(&lf);
  2837. if (hFont == NULL) return NULL;
  2838. TFontInfo* pFontInfo = new TFontInfo;
  2839. if (!pFontInfo) return false;
  2840. ::ZeroMemory(pFontInfo, sizeof(TFontInfo));
  2841. pFontInfo->hFont = hFont;
  2842. pFontInfo->sFontName = lf.lfFaceName;
  2843. pFontInfo->iSize = nSize;
  2844. pFontInfo->bBold = bBold;
  2845. pFontInfo->bUnderline = bUnderline;
  2846. pFontInfo->bItalic = bItalic;
  2847. pFontInfo->bStrikeout = bStrikeout;
  2848. if (m_hDcPaint)
  2849. {
  2850. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
  2851. ::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
  2852. ::SelectObject(m_hDcPaint, hOldFont);
  2853. }
  2854. TCHAR idBuffer[16];
  2855. ::ZeroMemory(idBuffer, sizeof(idBuffer));
  2856. _itot(id, idBuffer, 10);
  2857. if (bShared || m_bForceUseSharedRes)
  2858. {
  2859. TFontInfo* pOldFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(idBuffer));
  2860. if (pOldFontInfo)
  2861. {
  2862. ::DeleteObject(pOldFontInfo->hFont);
  2863. delete pOldFontInfo;
  2864. m_SharedResInfo.m_CustomFonts.Remove(idBuffer);
  2865. }
  2866. if (!m_SharedResInfo.m_CustomFonts.Insert(idBuffer, pFontInfo))
  2867. {
  2868. ::DeleteObject(hFont);
  2869. delete pFontInfo;
  2870. return NULL;
  2871. }
  2872. }
  2873. else
  2874. {
  2875. TFontInfo* pOldFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(idBuffer));
  2876. if (pOldFontInfo)
  2877. {
  2878. ::DeleteObject(pOldFontInfo->hFont);
  2879. delete pOldFontInfo;
  2880. m_ResInfo.m_CustomFonts.Remove(idBuffer);
  2881. }
  2882. if (!m_ResInfo.m_CustomFonts.Insert(idBuffer, pFontInfo))
  2883. {
  2884. ::DeleteObject(hFont);
  2885. delete pFontInfo;
  2886. return NULL;
  2887. }
  2888. }
  2889. return hFont;
  2890. }
  2891. void CPaintManagerUI::AddFontArray(LPCTSTR pstrPath)
  2892. {
  2893. LPBYTE pData = NULL;
  2894. DWORD dwSize = 0;
  2895. do
  2896. {
  2897. CDuiString sFile = CPaintManagerUI::GetResourcePath();
  2898. if (CPaintManagerUI::GetResourceZip().IsEmpty())
  2899. {
  2900. sFile += pstrPath;
  2901. HANDLE hFile = ::CreateFile(sFile.GetData(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
  2902. FILE_ATTRIBUTE_NORMAL, NULL);
  2903. if (hFile == INVALID_HANDLE_VALUE) break;
  2904. dwSize = ::GetFileSize(hFile, NULL);
  2905. if (dwSize == 0) break;
  2906. DWORD dwRead = 0;
  2907. pData = new BYTE[dwSize];
  2908. ::ReadFile(hFile, pData, dwSize, &dwRead, NULL);
  2909. ::CloseHandle(hFile);
  2910. if (dwRead != dwSize)
  2911. {
  2912. delete[] pData;
  2913. pData = NULL;
  2914. break;
  2915. }
  2916. }
  2917. else
  2918. {
  2919. sFile += CPaintManagerUI::GetResourceZip();
  2920. HZIP hz = NULL;
  2921. if (CPaintManagerUI::IsCachedResourceZip()) hz = (HZIP)CPaintManagerUI::GetResourceZipHandle();
  2922. else
  2923. {
  2924. CDuiString sFilePwd = CPaintManagerUI::GetResourceZipPwd();
  2925. #ifdef UNICODE
  2926. char* pwd = w2a((wchar_t*)sFilePwd.GetData());
  2927. hz = OpenZip(sFile.GetData(), pwd);
  2928. if (pwd) delete[] pwd;
  2929. #else
  2930. hz = OpenZip(sFile.GetData(), sFilePwd.GetData());
  2931. #endif
  2932. }
  2933. if (hz == NULL) break;
  2934. ZIPENTRY ze;
  2935. int i = 0;
  2936. CDuiString key = pstrPath;
  2937. key.Replace(_T("\\"), _T("/"));
  2938. if (FindZipItem(hz, key, true, &i, &ze) != 0) break;
  2939. dwSize = ze.unc_size;
  2940. if (dwSize == 0) break;
  2941. pData = new BYTE[dwSize];
  2942. int res = UnzipItem(hz, i, pData, dwSize);
  2943. if (res != 0x00000000 && res != 0x00000600)
  2944. {
  2945. delete[] pData;
  2946. pData = NULL;
  2947. if (!CPaintManagerUI::IsCachedResourceZip()) CloseZip(hz);
  2948. break;
  2949. }
  2950. if (!CPaintManagerUI::IsCachedResourceZip()) CloseZip(hz);
  2951. }
  2952. } while (0);
  2953. while (!pData)
  2954. {
  2955. //读不到图片, 则直接去读取bitmap.m_lpstr指向的路径
  2956. HANDLE hFile = ::CreateFile(pstrPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  2957. if (hFile == INVALID_HANDLE_VALUE) break;
  2958. dwSize = ::GetFileSize(hFile, NULL);
  2959. if (dwSize == 0) break;
  2960. DWORD dwRead = 0;
  2961. pData = new BYTE[dwSize];
  2962. ::ReadFile(hFile, pData, dwSize, &dwRead, NULL);
  2963. ::CloseHandle(hFile);
  2964. if (dwRead != dwSize)
  2965. {
  2966. delete[] pData;
  2967. pData = NULL;
  2968. }
  2969. break;
  2970. }
  2971. DWORD nFonts;
  2972. HANDLE hFont = ::AddFontMemResourceEx(pData, dwSize, NULL, &nFonts);
  2973. delete[] pData;
  2974. pData = NULL;
  2975. m_aFonts.Add(hFont);
  2976. }
  2977. HFONT CPaintManagerUI::GetFont(int id)
  2978. {
  2979. if (id < 0) return GetDefaultFontInfo()->hFont;
  2980. TCHAR idBuffer[16];
  2981. ::ZeroMemory(idBuffer, sizeof(idBuffer));
  2982. _itot(id, idBuffer, 10);
  2983. TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(idBuffer));
  2984. if (!pFontInfo) pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(idBuffer));
  2985. if (!pFontInfo) return GetDefaultFontInfo()->hFont;
  2986. return pFontInfo->hFont;
  2987. }
  2988. HFONT CPaintManagerUI::GetFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout)
  2989. {
  2990. TFontInfo* pFontInfo = NULL;
  2991. for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
  2992. {
  2993. if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
  2994. {
  2995. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
  2996. if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
  2997. pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
  2998. && pFontInfo->bStrikeout == bStrikeout)
  2999. return pFontInfo->hFont;
  3000. }
  3001. }
  3002. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  3003. {
  3004. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  3005. {
  3006. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
  3007. if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
  3008. pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
  3009. && pFontInfo->bStrikeout == bStrikeout)
  3010. return pFontInfo->hFont;
  3011. }
  3012. }
  3013. return NULL;
  3014. }
  3015. int CPaintManagerUI::GetFontIndex(HFONT hFont, bool bShared)
  3016. {
  3017. TFontInfo* pFontInfo = NULL;
  3018. if (bShared)
  3019. {
  3020. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  3021. {
  3022. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  3023. {
  3024. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
  3025. if (pFontInfo && pFontInfo->hFont == hFont) return _ttoi(key);
  3026. }
  3027. }
  3028. }
  3029. else
  3030. {
  3031. for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
  3032. {
  3033. if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
  3034. {
  3035. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
  3036. if (pFontInfo && pFontInfo->hFont == hFont) return _ttoi(key);
  3037. }
  3038. }
  3039. }
  3040. return -1;
  3041. }
  3042. int CPaintManagerUI::GetFontIndex(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared)
  3043. {
  3044. TFontInfo* pFontInfo = NULL;
  3045. if (bShared)
  3046. {
  3047. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  3048. {
  3049. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  3050. {
  3051. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
  3052. if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
  3053. pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
  3054. && pFontInfo->bStrikeout == bStrikeout)
  3055. return _ttoi(key);
  3056. }
  3057. }
  3058. }
  3059. else
  3060. {
  3061. for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
  3062. {
  3063. if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
  3064. {
  3065. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
  3066. if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
  3067. pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
  3068. && pFontInfo->bStrikeout == bStrikeout)
  3069. return _ttoi(key);
  3070. }
  3071. }
  3072. }
  3073. return -1;
  3074. }
  3075. void CPaintManagerUI::RemoveFont(HFONT hFont, bool bShared)
  3076. {
  3077. TFontInfo* pFontInfo = NULL;
  3078. if (bShared)
  3079. {
  3080. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  3081. {
  3082. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  3083. {
  3084. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
  3085. if (pFontInfo && pFontInfo->hFont == hFont)
  3086. {
  3087. ::DeleteObject(pFontInfo->hFont);
  3088. delete pFontInfo;
  3089. m_SharedResInfo.m_CustomFonts.Remove(key);
  3090. return;
  3091. }
  3092. }
  3093. }
  3094. }
  3095. else
  3096. {
  3097. for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
  3098. {
  3099. if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
  3100. {
  3101. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
  3102. if (pFontInfo && pFontInfo->hFont == hFont)
  3103. {
  3104. ::DeleteObject(pFontInfo->hFont);
  3105. delete pFontInfo;
  3106. m_ResInfo.m_CustomFonts.Remove(key);
  3107. return;
  3108. }
  3109. }
  3110. }
  3111. }
  3112. }
  3113. void CPaintManagerUI::RemoveFont(int id, bool bShared)
  3114. {
  3115. TCHAR idBuffer[16];
  3116. ::ZeroMemory(idBuffer, sizeof(idBuffer));
  3117. _itot(id, idBuffer, 10);
  3118. TFontInfo* pFontInfo = NULL;
  3119. if (bShared)
  3120. {
  3121. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(idBuffer));
  3122. if (pFontInfo)
  3123. {
  3124. ::DeleteObject(pFontInfo->hFont);
  3125. delete pFontInfo;
  3126. m_SharedResInfo.m_CustomFonts.Remove(idBuffer);
  3127. }
  3128. }
  3129. else
  3130. {
  3131. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(idBuffer));
  3132. if (pFontInfo)
  3133. {
  3134. ::DeleteObject(pFontInfo->hFont);
  3135. delete pFontInfo;
  3136. m_ResInfo.m_CustomFonts.Remove(idBuffer);
  3137. }
  3138. }
  3139. }
  3140. void CPaintManagerUI::RemoveAllFonts(bool bShared)
  3141. {
  3142. TFontInfo* pFontInfo;
  3143. if (bShared)
  3144. {
  3145. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  3146. {
  3147. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  3148. {
  3149. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key, false));
  3150. if (pFontInfo)
  3151. {
  3152. ::DeleteObject(pFontInfo->hFont);
  3153. delete pFontInfo;
  3154. }
  3155. }
  3156. }
  3157. m_SharedResInfo.m_CustomFonts.RemoveAll();
  3158. }
  3159. else
  3160. {
  3161. for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
  3162. {
  3163. if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
  3164. {
  3165. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key, false));
  3166. if (pFontInfo)
  3167. {
  3168. ::DeleteObject(pFontInfo->hFont);
  3169. delete pFontInfo;
  3170. }
  3171. }
  3172. }
  3173. m_ResInfo.m_CustomFonts.RemoveAll();
  3174. }
  3175. }
  3176. TFontInfo* CPaintManagerUI::GetFontInfo(int id)
  3177. {
  3178. if (id < 0) return GetDefaultFontInfo();
  3179. TCHAR idBuffer[16];
  3180. ::ZeroMemory(idBuffer, sizeof(idBuffer));
  3181. _itot(id, idBuffer, 10);
  3182. TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(idBuffer));
  3183. if (!pFontInfo) pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(idBuffer));
  3184. if (!pFontInfo) pFontInfo = GetDefaultFontInfo();
  3185. if (pFontInfo->tm.tmHeight == 0)
  3186. {
  3187. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, pFontInfo->hFont);
  3188. ::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
  3189. ::SelectObject(m_hDcPaint, hOldFont);
  3190. }
  3191. return pFontInfo;
  3192. }
  3193. TFontInfo* CPaintManagerUI::GetFontInfo(HFONT hFont)
  3194. {
  3195. TFontInfo* pFontInfo = NULL;
  3196. for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
  3197. {
  3198. if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
  3199. {
  3200. pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
  3201. if (pFontInfo && pFontInfo->hFont == hFont) break;
  3202. }
  3203. }
  3204. if (!pFontInfo)
  3205. {
  3206. for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
  3207. {
  3208. if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
  3209. {
  3210. pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
  3211. if (pFontInfo && pFontInfo->hFont == hFont) break;
  3212. }
  3213. }
  3214. }
  3215. if (!pFontInfo) pFontInfo = GetDefaultFontInfo();
  3216. if (pFontInfo->tm.tmHeight == 0)
  3217. {
  3218. HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, pFontInfo->hFont);
  3219. ::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
  3220. ::SelectObject(m_hDcPaint, hOldFont);
  3221. }
  3222. return pFontInfo;
  3223. }
  3224. const TImageInfo* CPaintManagerUI::GetImage(LPCTSTR bitmap)
  3225. {
  3226. TImageInfo* data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
  3227. if (!data) data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
  3228. return data;
  3229. }
  3230. const TImageInfo* CPaintManagerUI::GetImageEx(LPCTSTR bitmap, LPCTSTR type, DWORD mask, bool bUseHSL, bool bGdiplus, HINSTANCE instance)
  3231. {
  3232. const TImageInfo* data = GetImage(bitmap);
  3233. if (!data)
  3234. {
  3235. if (AddImage(bitmap, type, mask, bUseHSL, bGdiplus, false, instance))
  3236. {
  3237. if (m_bForceUseSharedRes) data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
  3238. else data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
  3239. }
  3240. }
  3241. return data;
  3242. }
  3243. const TImageInfo* CPaintManagerUI::AddImage(LPCTSTR bitmap, LPCTSTR type, DWORD mask, bool bUseHSL, bool bGdiplus, bool bShared, HINSTANCE instance)
  3244. {
  3245. if (bitmap == NULL || bitmap[0] == _T('\0')) return NULL;
  3246. TImageInfo* data = NULL;
  3247. if (type != NULL && lstrlen(type) > 0)
  3248. {
  3249. if (isdigit(*bitmap))
  3250. {
  3251. LPTSTR pstr = NULL;
  3252. int iIndex = _tcstol(bitmap, &pstr, 10);
  3253. data = bGdiplus ? CRenderEngine::GdiplusLoadImage(iIndex, type, mask, instance) : CRenderEngine::LoadImage(iIndex, type, mask, instance);
  3254. }
  3255. }
  3256. else
  3257. {
  3258. data = bGdiplus ? CRenderEngine::GdiplusLoadImage(bitmap, NULL, mask, instance) : CRenderEngine::LoadImage(bitmap, NULL, mask, instance);
  3259. if (!data)
  3260. {
  3261. CDuiString sImageName = bitmap;
  3262. int iAtIdx = sImageName.ReverseFind(_T('@'));
  3263. int iDotIdx = sImageName.ReverseFind(_T('.'));
  3264. if (iAtIdx != -1 && iDotIdx != -1)
  3265. {
  3266. CDuiString sExe = sImageName.Mid(iDotIdx);
  3267. sImageName = sImageName.Left(iAtIdx) + sExe;
  3268. data = bGdiplus ? CRenderEngine::GdiplusLoadImage(sImageName.GetData(), NULL, mask, instance) : CRenderEngine::LoadImage(sImageName.GetData(), NULL, mask, instance);
  3269. }
  3270. }
  3271. }
  3272. if (data == NULL)
  3273. {
  3274. return NULL;
  3275. }
  3276. data->bUseHSL = bUseHSL;
  3277. if (type != NULL) data->sResType = type;
  3278. data->dwMask = mask;
  3279. if (data->bUseHSL)
  3280. {
  3281. data->pSrcBits = new BYTE[data->nX * data->nY * 4];
  3282. ::CopyMemory(data->pSrcBits, data->pBits, data->nX * data->nY * 4);
  3283. }
  3284. else data->pSrcBits = NULL;
  3285. if (m_bUseHSL) CRenderEngine::AdjustImage(true, data, m_H, m_S, m_L);
  3286. if (data)
  3287. {
  3288. if (bShared || m_bForceUseSharedRes)
  3289. {
  3290. TImageInfo* pOldImageInfo = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
  3291. if (pOldImageInfo)
  3292. {
  3293. CRenderEngine::FreeImage(pOldImageInfo);
  3294. m_SharedResInfo.m_ImageHash.Remove(bitmap);
  3295. }
  3296. if (!m_SharedResInfo.m_ImageHash.Insert(bitmap, data))
  3297. {
  3298. CRenderEngine::FreeImage(data);
  3299. data = NULL;
  3300. }
  3301. }
  3302. else
  3303. {
  3304. TImageInfo* pOldImageInfo = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
  3305. if (pOldImageInfo)
  3306. {
  3307. CRenderEngine::FreeImage(pOldImageInfo);
  3308. m_ResInfo.m_ImageHash.Remove(bitmap);
  3309. }
  3310. if (!m_ResInfo.m_ImageHash.Insert(bitmap, data))
  3311. {
  3312. CRenderEngine::FreeImage(data);
  3313. data = NULL;
  3314. }
  3315. }
  3316. }
  3317. return data;
  3318. }
  3319. const TImageInfo* CPaintManagerUI::AddImage(LPCTSTR bitmap, HBITMAP hBitmap, int iWidth, int iHeight, bool bAlpha, bool bShared)
  3320. {
  3321. // 因无法确定外部HBITMAP格式,不能使用hsl调整
  3322. if (bitmap == NULL || bitmap[0] == _T('\0')) return NULL;
  3323. if (hBitmap == NULL || iWidth <= 0 || iHeight <= 0) return NULL;
  3324. TImageInfo* data = new TImageInfo;
  3325. data->pBits = NULL;
  3326. data->pSrcBits = NULL;
  3327. data->hBitmap = hBitmap;
  3328. data->pBits = NULL;
  3329. data->nX = iWidth;
  3330. data->nY = iHeight;
  3331. data->bAlpha = bAlpha;
  3332. data->bUseHSL = false;
  3333. data->pSrcBits = NULL;
  3334. data->dwMask = 0;
  3335. if (bShared || m_bForceUseSharedRes)
  3336. {
  3337. if (!m_SharedResInfo.m_ImageHash.Insert(bitmap, data))
  3338. {
  3339. CRenderEngine::FreeImage(data);
  3340. data = NULL;
  3341. }
  3342. }
  3343. else
  3344. {
  3345. if (!m_ResInfo.m_ImageHash.Insert(bitmap, data))
  3346. {
  3347. CRenderEngine::FreeImage(data);
  3348. data = NULL;
  3349. }
  3350. }
  3351. return data;
  3352. }
  3353. void CPaintManagerUI::RemoveImage(LPCTSTR bitmap, bool bShared)
  3354. {
  3355. TImageInfo* data = NULL;
  3356. if (bShared)
  3357. {
  3358. data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
  3359. if (data)
  3360. {
  3361. CRenderEngine::FreeImage(data);
  3362. m_SharedResInfo.m_ImageHash.Remove(bitmap);
  3363. }
  3364. }
  3365. else
  3366. {
  3367. data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
  3368. if (data)
  3369. {
  3370. CRenderEngine::FreeImage(data);
  3371. m_ResInfo.m_ImageHash.Remove(bitmap);
  3372. }
  3373. }
  3374. }
  3375. void CPaintManagerUI::RemoveAllImages(bool bShared)
  3376. {
  3377. if (bShared)
  3378. {
  3379. TImageInfo* data;
  3380. for (int i = 0; i < m_SharedResInfo.m_ImageHash.GetSize(); i++)
  3381. {
  3382. if (LPCTSTR key = m_SharedResInfo.m_ImageHash.GetAt(i))
  3383. {
  3384. data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(key, false));
  3385. if (data)
  3386. {
  3387. CRenderEngine::FreeImage(data);
  3388. }
  3389. }
  3390. }
  3391. m_SharedResInfo.m_ImageHash.RemoveAll();
  3392. }
  3393. else
  3394. {
  3395. TImageInfo* data;
  3396. for (int i = 0; i < m_ResInfo.m_ImageHash.GetSize(); i++)
  3397. {
  3398. if (LPCTSTR key = m_ResInfo.m_ImageHash.GetAt(i))
  3399. {
  3400. data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(key, false));
  3401. if (data)
  3402. {
  3403. CRenderEngine::FreeImage(data);
  3404. }
  3405. }
  3406. }
  3407. m_ResInfo.m_ImageHash.RemoveAll();
  3408. }
  3409. }
  3410. void CPaintManagerUI::AdjustSharedImagesHSL()
  3411. {
  3412. TImageInfo* data;
  3413. for (int i = 0; i < m_SharedResInfo.m_ImageHash.GetSize(); i++)
  3414. {
  3415. if (LPCTSTR key = m_SharedResInfo.m_ImageHash.GetAt(i))
  3416. {
  3417. data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(key));
  3418. if (data && data->bUseHSL)
  3419. {
  3420. CRenderEngine::AdjustImage(m_bUseHSL, data, m_H, m_S, m_L);
  3421. }
  3422. }
  3423. }
  3424. }
  3425. void CPaintManagerUI::AdjustImagesHSL()
  3426. {
  3427. TImageInfo* data;
  3428. for (int i = 0; i < m_ResInfo.m_ImageHash.GetSize(); i++)
  3429. {
  3430. if (LPCTSTR key = m_ResInfo.m_ImageHash.GetAt(i))
  3431. {
  3432. data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(key));
  3433. if (data && data->bUseHSL)
  3434. {
  3435. CRenderEngine::AdjustImage(m_bUseHSL, data, m_H, m_S, m_L);
  3436. }
  3437. }
  3438. }
  3439. Invalidate();
  3440. }
  3441. void CPaintManagerUI::PostAsyncNotify()
  3442. {
  3443. if (!m_bAsyncNotifyPosted)
  3444. {
  3445. ::PostMessage(m_hWndPaint, WM_APP + 1, 0, 0L);
  3446. m_bAsyncNotifyPosted = true;
  3447. }
  3448. }
  3449. void CPaintManagerUI::ReloadSharedImages()
  3450. {
  3451. TImageInfo* data = NULL;
  3452. TImageInfo* pNewData = NULL;
  3453. for (int i = 0; i < m_SharedResInfo.m_ImageHash.GetSize(); i++)
  3454. {
  3455. if (LPCTSTR bitmap = m_SharedResInfo.m_ImageHash.GetAt(i))
  3456. {
  3457. data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
  3458. if (data != NULL)
  3459. {
  3460. if (!data->sResType.IsEmpty())
  3461. {
  3462. if (isdigit(*bitmap))
  3463. {
  3464. LPTSTR pstr = NULL;
  3465. int iIndex = _tcstol(bitmap, &pstr, 10);
  3466. pNewData = CRenderEngine::LoadImage(iIndex, data->sResType.GetData(), data->dwMask);
  3467. }
  3468. }
  3469. else
  3470. {
  3471. pNewData = CRenderEngine::LoadImage(bitmap, NULL, data->dwMask);
  3472. }
  3473. if (pNewData == NULL) continue;
  3474. CRenderEngine::FreeImage(data, false);
  3475. data->hBitmap = pNewData->hBitmap;
  3476. data->pImage = pNewData->pImage;
  3477. data->pBits = pNewData->pBits;
  3478. data->nX = pNewData->nX;
  3479. data->nY = pNewData->nY;
  3480. data->bAlpha = pNewData->bAlpha;
  3481. data->pSrcBits = NULL;
  3482. if (data->bUseHSL)
  3483. {
  3484. data->pSrcBits = new BYTE[data->nX * data->nY * 4];
  3485. ::CopyMemory(data->pSrcBits, data->pBits, data->nX * data->nY * 4);
  3486. }
  3487. else data->pSrcBits = NULL;
  3488. if (m_bUseHSL) CRenderEngine::AdjustImage(true, data, m_H, m_S, m_L);
  3489. delete pNewData;
  3490. }
  3491. }
  3492. }
  3493. }
  3494. void CPaintManagerUI::ReloadImages()
  3495. {
  3496. RemoveAllDrawInfos();
  3497. TImageInfo* data = NULL;
  3498. TImageInfo* pNewData = NULL;
  3499. for (int i = 0; i < m_ResInfo.m_ImageHash.GetSize(); i++)
  3500. {
  3501. if (LPCTSTR bitmap = m_ResInfo.m_ImageHash.GetAt(i))
  3502. {
  3503. data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
  3504. if (data != NULL)
  3505. {
  3506. if (!data->sResType.IsEmpty())
  3507. {
  3508. if (isdigit(*bitmap))
  3509. {
  3510. LPTSTR pstr = NULL;
  3511. int iIndex = _tcstol(bitmap, &pstr, 10);
  3512. pNewData = CRenderEngine::LoadImage(iIndex, data->sResType.GetData(), data->dwMask);
  3513. }
  3514. }
  3515. else
  3516. {
  3517. pNewData = CRenderEngine::LoadImage(bitmap, NULL, data->dwMask);
  3518. }
  3519. CRenderEngine::FreeImage(data, false);
  3520. if (pNewData == NULL)
  3521. {
  3522. m_ResInfo.m_ImageHash.Remove(bitmap);
  3523. continue;
  3524. }
  3525. data->hBitmap = pNewData->hBitmap;
  3526. data->pBits = pNewData->pBits;
  3527. data->nX = pNewData->nX;
  3528. data->nY = pNewData->nY;
  3529. data->bAlpha = pNewData->bAlpha;
  3530. data->pSrcBits = NULL;
  3531. if (data->bUseHSL)
  3532. {
  3533. data->pSrcBits = new BYTE[data->nX * data->nY * 4];
  3534. ::CopyMemory(data->pSrcBits, data->pBits, data->nX * data->nY * 4);
  3535. }
  3536. else data->pSrcBits = NULL;
  3537. if (m_bUseHSL) CRenderEngine::AdjustImage(true, data, m_H, m_S, m_L);
  3538. delete pNewData;
  3539. }
  3540. }
  3541. }
  3542. if (m_pRoot) m_pRoot->Invalidate();
  3543. }
  3544. const TDrawInfo* CPaintManagerUI::GetDrawInfo(LPCTSTR pStrImage, LPCTSTR pStrModify)
  3545. {
  3546. CDuiString sKey;
  3547. sKey.Format(_T("%s%s"), pStrImage == NULL ? _T("") : pStrImage, pStrModify == NULL ? _T("") : pStrModify);
  3548. TDrawInfo* pDrawInfo = static_cast<TDrawInfo*>(m_ResInfo.m_DrawInfoHash.Find(sKey));
  3549. if (pDrawInfo == NULL && !sKey.IsEmpty())
  3550. {
  3551. pDrawInfo = new TDrawInfo();
  3552. pDrawInfo->Parse(pStrImage, pStrModify, this);
  3553. m_ResInfo.m_DrawInfoHash.Insert(sKey, pDrawInfo);
  3554. }
  3555. return pDrawInfo;
  3556. }
  3557. void CPaintManagerUI::RemoveDrawInfo(LPCTSTR pStrImage, LPCTSTR pStrModify)
  3558. {
  3559. CDuiString sKey;
  3560. sKey.Format(_T("%s%s"), pStrImage == NULL ? _T("") : pStrImage, pStrModify == NULL ? _T("") : pStrModify);
  3561. TDrawInfo* pDrawInfo = static_cast<TDrawInfo*>(m_ResInfo.m_DrawInfoHash.Find(sKey));
  3562. if (pDrawInfo != NULL)
  3563. {
  3564. m_ResInfo.m_DrawInfoHash.Remove(sKey);
  3565. delete pDrawInfo;
  3566. pDrawInfo = NULL;
  3567. }
  3568. }
  3569. void CPaintManagerUI::RemoveAllDrawInfos()
  3570. {
  3571. TDrawInfo* pDrawInfo = NULL;
  3572. for (int i = 0; i < m_ResInfo.m_DrawInfoHash.GetSize(); i++)
  3573. {
  3574. LPCTSTR key = m_ResInfo.m_DrawInfoHash.GetAt(i);
  3575. if (key != NULL)
  3576. {
  3577. pDrawInfo = static_cast<TDrawInfo*>(m_ResInfo.m_DrawInfoHash.Find(key, false));
  3578. if (pDrawInfo)
  3579. {
  3580. delete pDrawInfo;
  3581. pDrawInfo = NULL;
  3582. }
  3583. }
  3584. }
  3585. m_ResInfo.m_DrawInfoHash.RemoveAll();
  3586. }
  3587. void CPaintManagerUI::AddDefaultAttributeList(LPCTSTR pStrControlName, LPCTSTR pStrControlAttrList, bool bShared)
  3588. {
  3589. if (bShared || m_bForceUseSharedRes)
  3590. {
  3591. CDuiString* pDefaultAttr = new CDuiString(pStrControlAttrList);
  3592. if (pDefaultAttr != NULL)
  3593. {
  3594. CDuiString* pOldDefaultAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Set(pStrControlName, (LPVOID)pDefaultAttr));
  3595. if (pOldDefaultAttr) delete pOldDefaultAttr;
  3596. }
  3597. }
  3598. else
  3599. {
  3600. CDuiString* pDefaultAttr = new CDuiString(pStrControlAttrList);
  3601. if (pDefaultAttr != NULL)
  3602. {
  3603. CDuiString* pOldDefaultAttr = static_cast<CDuiString*>(m_ResInfo.m_AttrHash.Set(pStrControlName, (LPVOID)pDefaultAttr));
  3604. if (pOldDefaultAttr) delete pOldDefaultAttr;
  3605. }
  3606. }
  3607. }
  3608. LPCTSTR CPaintManagerUI::GetDefaultAttributeList(LPCTSTR pStrControlName) const
  3609. {
  3610. CDuiString* pDefaultAttr = static_cast<CDuiString*>(m_ResInfo.m_AttrHash.Find(pStrControlName));
  3611. if (!pDefaultAttr) pDefaultAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Find(pStrControlName));
  3612. if (pDefaultAttr) return pDefaultAttr->GetData();
  3613. return NULL;
  3614. }
  3615. bool CPaintManagerUI::RemoveDefaultAttributeList(LPCTSTR pStrControlName, bool bShared)
  3616. {
  3617. if (bShared)
  3618. {
  3619. CDuiString* pDefaultAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Find(pStrControlName));
  3620. if (!pDefaultAttr) return false;
  3621. delete pDefaultAttr;
  3622. return m_SharedResInfo.m_AttrHash.Remove(pStrControlName);
  3623. }
  3624. else
  3625. {
  3626. CDuiString* pDefaultAttr = static_cast<CDuiString*>(m_ResInfo.m_AttrHash.Find(pStrControlName));
  3627. if (!pDefaultAttr) return false;
  3628. delete pDefaultAttr;
  3629. return m_ResInfo.m_AttrHash.Remove(pStrControlName);
  3630. }
  3631. }
  3632. void CPaintManagerUI::RemoveAllDefaultAttributeList(bool bShared)
  3633. {
  3634. if (bShared)
  3635. {
  3636. CDuiString* pDefaultAttr;
  3637. for (int i = 0; i < m_SharedResInfo.m_AttrHash.GetSize(); i++)
  3638. {
  3639. if (LPCTSTR key = m_SharedResInfo.m_AttrHash.GetAt(i))
  3640. {
  3641. pDefaultAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Find(key));
  3642. if (pDefaultAttr) delete pDefaultAttr;
  3643. }
  3644. }
  3645. m_SharedResInfo.m_AttrHash.RemoveAll();
  3646. }
  3647. else
  3648. {
  3649. CDuiString* pDefaultAttr;
  3650. for (int i = 0; i < m_ResInfo.m_AttrHash.GetSize(); i++)
  3651. {
  3652. if (LPCTSTR key = m_ResInfo.m_AttrHash.GetAt(i))
  3653. {
  3654. pDefaultAttr = static_cast<CDuiString*>(m_ResInfo.m_AttrHash.Find(key));
  3655. if (pDefaultAttr) delete pDefaultAttr;
  3656. }
  3657. }
  3658. m_ResInfo.m_AttrHash.RemoveAll();
  3659. }
  3660. }
  3661. void CPaintManagerUI::AddWindowCustomAttribute(LPCTSTR pstrName, LPCTSTR pstrAttr)
  3662. {
  3663. if (pstrName == NULL || pstrName[0] == _T('\0') || pstrAttr == NULL || pstrAttr[0] == _T('\0')) return;
  3664. CDuiString* pCostomAttr = new CDuiString(pstrAttr);
  3665. if (pCostomAttr != NULL)
  3666. {
  3667. if (m_mWindowCustomAttrHash.Find(pstrName) == NULL)
  3668. m_mWindowCustomAttrHash.Set(pstrName, (LPVOID)pCostomAttr);
  3669. else
  3670. delete pCostomAttr;
  3671. }
  3672. }
  3673. LPCTSTR CPaintManagerUI::GetWindowCustomAttribute(LPCTSTR pstrName) const
  3674. {
  3675. if (pstrName == NULL || pstrName[0] == _T('\0')) return NULL;
  3676. CDuiString* pCostomAttr = static_cast<CDuiString*>(m_mWindowCustomAttrHash.Find(pstrName));
  3677. if (pCostomAttr) return pCostomAttr->GetData();
  3678. return NULL;
  3679. }
  3680. bool CPaintManagerUI::RemoveWindowCustomAttribute(LPCTSTR pstrName)
  3681. {
  3682. if (pstrName == NULL || pstrName[0] == _T('\0')) return NULL;
  3683. CDuiString* pCostomAttr = static_cast<CDuiString*>(m_mWindowCustomAttrHash.Find(pstrName));
  3684. if (!pCostomAttr) return false;
  3685. delete pCostomAttr;
  3686. return m_mWindowCustomAttrHash.Remove(pstrName);
  3687. }
  3688. void CPaintManagerUI::RemoveAllWindowCustomAttribute()
  3689. {
  3690. CDuiString* pCostomAttr;
  3691. for (int i = 0; i < m_mWindowCustomAttrHash.GetSize(); i++)
  3692. {
  3693. if (LPCTSTR key = m_mWindowCustomAttrHash.GetAt(i))
  3694. {
  3695. pCostomAttr = static_cast<CDuiString*>(m_mWindowCustomAttrHash.Find(key));
  3696. delete pCostomAttr;
  3697. }
  3698. }
  3699. m_mWindowCustomAttrHash.Resize();
  3700. }
  3701. CControlUI* CPaintManagerUI::GetRoot() const
  3702. {
  3703. ASSERT(m_pRoot);
  3704. return m_pRoot;
  3705. }
  3706. CControlUI* CPaintManagerUI::FindControl(POINT pt) const
  3707. {
  3708. ASSERT(m_pRoot);
  3709. return m_pRoot->FindControl(__FindControlFromPoint, &pt, UIFIND_VISIBLE | UIFIND_HITTEST | UIFIND_TOP_FIRST);
  3710. }
  3711. CControlUI* CPaintManagerUI::FindControl(LPCTSTR pstrName) const
  3712. {
  3713. ASSERT(m_pRoot);
  3714. return static_cast<CControlUI*>(m_mNameHash.Find(pstrName));
  3715. }
  3716. CControlUI* CPaintManagerUI::FindSubControlByPoint(CControlUI* pParent, POINT pt) const
  3717. {
  3718. if (pParent == NULL) pParent = GetRoot();
  3719. ASSERT(pParent);
  3720. return pParent->FindControl(__FindControlFromPoint, &pt, UIFIND_VISIBLE | UIFIND_HITTEST | UIFIND_TOP_FIRST);
  3721. }
  3722. CControlUI* CPaintManagerUI::FindSubControlByName(CControlUI* pParent, LPCTSTR pstrName) const
  3723. {
  3724. if (pParent == NULL) pParent = GetRoot();
  3725. ASSERT(pParent);
  3726. return pParent->FindControl(__FindControlFromName, (LPVOID)pstrName, UIFIND_ALL);
  3727. }
  3728. CControlUI* CPaintManagerUI::FindSubControlByClass(CControlUI* pParent, LPCTSTR pstrClass, int iIndex)
  3729. {
  3730. if (pParent == NULL) pParent = GetRoot();
  3731. ASSERT(pParent);
  3732. m_aFoundControls.Resize(iIndex + 1);
  3733. return pParent->FindControl(__FindControlFromClass, (LPVOID)pstrClass, UIFIND_ALL);
  3734. }
  3735. CStdPtrArray* CPaintManagerUI::FindSubControlsByClass(CControlUI* pParent, LPCTSTR pstrClass)
  3736. {
  3737. if (pParent == NULL) pParent = GetRoot();
  3738. ASSERT(pParent);
  3739. m_aFoundControls.Empty();
  3740. pParent->FindControl(__FindControlsFromClass, (LPVOID)pstrClass, UIFIND_ALL);
  3741. return &m_aFoundControls;
  3742. }
  3743. CStdPtrArray* CPaintManagerUI::GetFoundControls()
  3744. {
  3745. return &m_aFoundControls;
  3746. }
  3747. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromNameHash(CControlUI* pThis, LPVOID pData)
  3748. {
  3749. CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(pData);
  3750. const CDuiString& sName = pThis->GetName();
  3751. if (sName.IsEmpty()) return NULL;
  3752. // Add this control to the hash list
  3753. pManager->m_mNameHash.Set(sName, pThis);
  3754. return NULL; // Attempt to add all controls
  3755. }
  3756. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromCount(CControlUI* /*pThis*/, LPVOID pData)
  3757. {
  3758. int* pnCount = static_cast<int*>(pData);
  3759. (*pnCount)++;
  3760. return NULL; // Count all controls
  3761. }
  3762. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromPoint(CControlUI* pThis, LPVOID pData)
  3763. {
  3764. LPPOINT pPoint = static_cast<LPPOINT>(pData);
  3765. return ::PtInRect(&pThis->GetPos(), *pPoint) ? pThis : NULL;
  3766. }
  3767. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromTab(CControlUI* pThis, LPVOID pData)
  3768. {
  3769. FINDTABINFO* pInfo = static_cast<FINDTABINFO*>(pData);
  3770. if (pInfo->pFocus == pThis)
  3771. {
  3772. if (pInfo->bForward) pInfo->bNextIsIt = true;
  3773. return pInfo->bForward ? NULL : pInfo->pLast;
  3774. }
  3775. if ((pThis->GetControlFlags() & UIFLAG_TABSTOP) == 0) return NULL;
  3776. pInfo->pLast = pThis;
  3777. if (pInfo->bNextIsIt) return pThis;
  3778. if (pInfo->pFocus == NULL) return pThis;
  3779. return NULL; // Examine all controls
  3780. }
  3781. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromShortcut(CControlUI* pThis, LPVOID pData)
  3782. {
  3783. if (!pThis->IsVisible()) return NULL;
  3784. FINDSHORTCUT* pFS = static_cast<FINDSHORTCUT*>(pData);
  3785. if (pFS->ch == toupper(pThis->GetShortcut())) pFS->bPickNext = true;
  3786. if (_tcsstr(pThis->GetClass(), _T("LabelUI")) != NULL) return NULL; // Labels never get focus!
  3787. return pFS->bPickNext ? pThis : NULL;
  3788. }
  3789. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromName(CControlUI* pThis, LPVOID pData)
  3790. {
  3791. LPCTSTR pstrName = static_cast<LPCTSTR>(pData);
  3792. const CDuiString& sName = pThis->GetName();
  3793. if (sName.IsEmpty()) return NULL;
  3794. return (_tcsicmp(sName, pstrName) == 0) ? pThis : NULL;
  3795. }
  3796. CControlUI* CALLBACK CPaintManagerUI::__FindControlFromClass(CControlUI* pThis, LPVOID pData)
  3797. {
  3798. LPCTSTR pstrType = static_cast<LPCTSTR>(pData);
  3799. LPCTSTR pType = pThis->GetClass();
  3800. CStdPtrArray* pFoundControls = pThis->GetManager()->GetFoundControls();
  3801. if (_tcscmp(pstrType, _T("*")) == 0 || _tcscmp(pstrType, pType) == 0)
  3802. {
  3803. int iIndex = -1;
  3804. while (pFoundControls->GetAt(++iIndex) != NULL);
  3805. if (iIndex < pFoundControls->GetSize()) pFoundControls->SetAt(iIndex, pThis);
  3806. }
  3807. if (pFoundControls->GetAt(pFoundControls->GetSize() - 1) != NULL) return pThis;
  3808. return NULL;
  3809. }
  3810. CControlUI* CALLBACK CPaintManagerUI::__FindControlsFromClass(CControlUI* pThis, LPVOID pData)
  3811. {
  3812. LPCTSTR pstrType = static_cast<LPCTSTR>(pData);
  3813. LPCTSTR pType = pThis->GetClass();
  3814. if (_tcscmp(pstrType, _T("*")) == 0 || _tcscmp(pstrType, pType) == 0)
  3815. pThis->GetManager()->GetFoundControls()->Add((LPVOID)pThis);
  3816. return NULL;
  3817. }
  3818. CControlUI* CALLBACK CPaintManagerUI::__FindControlsFromUpdate(CControlUI* pThis, LPVOID pData)
  3819. {
  3820. if (pThis->IsUpdateNeeded())
  3821. {
  3822. pThis->GetManager()->GetFoundControls()->Add((LPVOID)pThis);
  3823. return pThis;
  3824. }
  3825. return NULL;
  3826. }
  3827. bool CPaintManagerUI::TranslateAccelerator(LPMSG pMsg)
  3828. {
  3829. for (int i = 0; i < m_aTranslateAccelerator.GetSize(); i++)
  3830. {
  3831. LRESULT lResult = static_cast<ITranslateAccelerator*>(m_aTranslateAccelerator[i])->TranslateAccelerator(pMsg);
  3832. if (lResult == S_OK) return true;
  3833. }
  3834. return false;
  3835. }
  3836. bool CPaintManagerUI::TranslateMessage(const LPMSG pMsg)
  3837. {
  3838. // Pretranslate Message takes care of system-wide messages, such as
  3839. // tabbing and shortcut key-combos. We'll look for all messages for
  3840. // each window and any child control attached.
  3841. UINT uStyle = GetWindowStyle(pMsg->hwnd);
  3842. UINT uChildRes = uStyle & WS_CHILD;
  3843. LRESULT lRes = 0;
  3844. if (uChildRes != 0)
  3845. {
  3846. HWND hWndParent = ::GetParent(pMsg->hwnd);
  3847. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  3848. {
  3849. CPaintManagerUI* pT = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  3850. HWND hTempParent = hWndParent;
  3851. while (hTempParent)
  3852. {
  3853. if (pMsg->hwnd == pT->GetPaintWindow() || hTempParent == pT->GetPaintWindow())
  3854. {
  3855. if (pT->TranslateAccelerator(pMsg))
  3856. return true;
  3857. pT->PreMessageHandler(pMsg->message, pMsg->wParam, pMsg->lParam, lRes);
  3858. }
  3859. hTempParent = GetParent(hTempParent);
  3860. }
  3861. }
  3862. }
  3863. else
  3864. {
  3865. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  3866. {
  3867. CPaintManagerUI* pT = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  3868. if (pMsg->hwnd == pT->GetPaintWindow())
  3869. {
  3870. if (pT->TranslateAccelerator(pMsg))
  3871. return true;
  3872. if (pT->PreMessageHandler(pMsg->message, pMsg->wParam, pMsg->lParam, lRes))
  3873. return true;
  3874. return false;
  3875. }
  3876. }
  3877. }
  3878. return false;
  3879. }
  3880. bool CPaintManagerUI::AddTranslateAccelerator(ITranslateAccelerator* pTranslateAccelerator)
  3881. {
  3882. ASSERT(m_aTranslateAccelerator.Find(pTranslateAccelerator) < 0);
  3883. return m_aTranslateAccelerator.Add(pTranslateAccelerator);
  3884. }
  3885. bool CPaintManagerUI::RemoveTranslateAccelerator(ITranslateAccelerator* pTranslateAccelerator)
  3886. {
  3887. for (int i = 0; i < m_aTranslateAccelerator.GetSize(); i++)
  3888. {
  3889. if (static_cast<ITranslateAccelerator*>(m_aTranslateAccelerator[i]) == pTranslateAccelerator)
  3890. {
  3891. return m_aTranslateAccelerator.Remove(i);
  3892. }
  3893. }
  3894. return false;
  3895. }
  3896. void CPaintManagerUI::UsedVirtualWnd(bool bUsed)
  3897. {
  3898. m_bUsedVirtualWnd = bUsed;
  3899. }
  3900. // 样式管理
  3901. void CPaintManagerUI::AddStyle(LPCTSTR pName, LPCTSTR pDeclarationList, bool bShared)
  3902. {
  3903. CDuiString* pStyle = new CDuiString(pDeclarationList);
  3904. if (bShared || m_bForceUseSharedRes)
  3905. {
  3906. if (!m_SharedResInfo.m_StyleHash.Insert(pName, pStyle))
  3907. {
  3908. delete pStyle;
  3909. }
  3910. }
  3911. else
  3912. {
  3913. if (!m_ResInfo.m_StyleHash.Insert(pName, pStyle))
  3914. {
  3915. delete pStyle;
  3916. }
  3917. }
  3918. }
  3919. LPCTSTR CPaintManagerUI::GetStyle(LPCTSTR pName) const
  3920. {
  3921. CDuiString* pStyle = static_cast<CDuiString*>(m_ResInfo.m_StyleHash.Find(pName));
  3922. if (!pStyle) pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(pName));
  3923. if (pStyle) return pStyle->GetData();
  3924. else return NULL;
  3925. }
  3926. BOOL CPaintManagerUI::RemoveStyle(LPCTSTR pName, bool bShared)
  3927. {
  3928. CDuiString* pStyle = NULL;
  3929. if (bShared)
  3930. {
  3931. pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(pName));
  3932. if (pStyle)
  3933. {
  3934. delete pStyle;
  3935. m_SharedResInfo.m_StyleHash.Remove(pName);
  3936. }
  3937. }
  3938. else
  3939. {
  3940. pStyle = static_cast<CDuiString*>(m_ResInfo.m_StyleHash.Find(pName));
  3941. if (pStyle)
  3942. {
  3943. delete pStyle;
  3944. m_ResInfo.m_StyleHash.Remove(pName);
  3945. }
  3946. }
  3947. return true;
  3948. }
  3949. const CStdStringPtrMap& CPaintManagerUI::GetStyles(bool bShared) const
  3950. {
  3951. if (bShared) return m_SharedResInfo.m_StyleHash;
  3952. else return m_ResInfo.m_StyleHash;
  3953. }
  3954. void CPaintManagerUI::RemoveAllStyle(bool bShared)
  3955. {
  3956. if (bShared)
  3957. {
  3958. CDuiString* pStyle;
  3959. for (int i = 0; i < m_SharedResInfo.m_StyleHash.GetSize(); i++)
  3960. {
  3961. if (LPCTSTR key = m_SharedResInfo.m_StyleHash.GetAt(i))
  3962. {
  3963. pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(key));
  3964. delete pStyle;
  3965. }
  3966. }
  3967. m_SharedResInfo.m_StyleHash.RemoveAll();
  3968. }
  3969. else
  3970. {
  3971. CDuiString* pStyle;
  3972. for (int i = 0; i < m_ResInfo.m_StyleHash.GetSize(); i++)
  3973. {
  3974. if (LPCTSTR key = m_ResInfo.m_StyleHash.GetAt(i))
  3975. {
  3976. pStyle = static_cast<CDuiString*>(m_ResInfo.m_StyleHash.Find(key));
  3977. delete pStyle;
  3978. }
  3979. }
  3980. m_ResInfo.m_StyleHash.RemoveAll();
  3981. }
  3982. }
  3983. const TImageInfo* CPaintManagerUI::GetImageString(LPCTSTR pStrImage, LPCTSTR pStrModify)
  3984. {
  3985. CDuiString sImageName = pStrImage;
  3986. CDuiString sImageResType = _T("");
  3987. DWORD dwMask = 0;
  3988. CDuiString sItem;
  3989. CDuiString sValue;
  3990. LPTSTR pstr = NULL;
  3991. for (int i = 0; i < 2; ++i)
  3992. {
  3993. if (i == 1)
  3994. pStrImage = pStrModify;
  3995. if (!pStrImage) continue;
  3996. while (*pStrImage != _T('\0'))
  3997. {
  3998. sItem.Empty();
  3999. sValue.Empty();
  4000. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  4001. while (*pStrImage != _T('\0') && *pStrImage != _T('=') && *pStrImage > _T(' '))
  4002. {
  4003. LPTSTR pstrTemp = ::CharNext(pStrImage);
  4004. while (pStrImage < pstrTemp)
  4005. {
  4006. sItem += *pStrImage++;
  4007. }
  4008. }
  4009. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  4010. if (*pStrImage++ != _T('=')) break;
  4011. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  4012. if (*pStrImage++ != _T('\'')) break;
  4013. while (*pStrImage != _T('\0') && *pStrImage != _T('\''))
  4014. {
  4015. LPTSTR pstrTemp = ::CharNext(pStrImage);
  4016. while (pStrImage < pstrTemp)
  4017. {
  4018. sValue += *pStrImage++;
  4019. }
  4020. }
  4021. if (*pStrImage++ != _T('\'')) break;
  4022. if (!sValue.IsEmpty())
  4023. {
  4024. if (sItem == _T("file") || sItem == _T("res"))
  4025. {
  4026. sImageName = sValue;
  4027. }
  4028. else if (sItem == _T("restype"))
  4029. {
  4030. sImageResType = sValue;
  4031. }
  4032. else if (sItem == _T("mask"))
  4033. {
  4034. if (sValue[0] == _T('#')) dwMask = _tcstoul(sValue.GetData() + 1, &pstr, 16);
  4035. else dwMask = _tcstoul(sValue.GetData(), &pstr, 16);
  4036. }
  4037. }
  4038. if (*pStrImage++ != _T(' ')) break;
  4039. }
  4040. }
  4041. return GetImageEx(sImageName, sImageResType, dwMask);
  4042. }
  4043. bool CPaintManagerUI::EnableDragDrop(bool bEnable)
  4044. {
  4045. if (m_bDragDrop == bEnable) return false;
  4046. m_bDragDrop = bEnable;
  4047. if (bEnable)
  4048. {
  4049. AddRef();
  4050. if (FAILED(RegisterDragDrop(m_hWndPaint, this)))
  4051. {
  4052. return false;
  4053. }
  4054. SetTargetWnd(m_hWndPaint);
  4055. FORMATETC ftetc = { 0 };
  4056. ftetc.cfFormat = CF_BITMAP;
  4057. ftetc.dwAspect = DVASPECT_CONTENT;
  4058. ftetc.lindex = -1;
  4059. ftetc.tymed = TYMED_GDI;
  4060. AddSuportedFormat(ftetc);
  4061. ftetc.cfFormat = CF_DIB;
  4062. ftetc.tymed = TYMED_HGLOBAL;
  4063. AddSuportedFormat(ftetc);
  4064. ftetc.cfFormat = CF_HDROP;
  4065. ftetc.tymed = TYMED_HGLOBAL;
  4066. AddSuportedFormat(ftetc);
  4067. ftetc.cfFormat = CF_ENHMETAFILE;
  4068. ftetc.tymed = TYMED_ENHMF;
  4069. AddSuportedFormat(ftetc);
  4070. }
  4071. else
  4072. {
  4073. Release();
  4074. if (FAILED(RevokeDragDrop(m_hWndPaint)))
  4075. {
  4076. return false;
  4077. }
  4078. }
  4079. return true;
  4080. }
  4081. void CPaintManagerUI::SetDragDrop(IDragDropUI* pDragDrop)
  4082. {
  4083. m_pDragDrop = pDragDrop;
  4084. }
  4085. static WORD DIBNumColors(void* pv)
  4086. {
  4087. int bits;
  4088. LPBITMAPINFOHEADER lpbi;
  4089. LPBITMAPCOREHEADER lpbc;
  4090. lpbi = ((LPBITMAPINFOHEADER)pv);
  4091. lpbc = ((LPBITMAPCOREHEADER)pv);
  4092. /* With the BITMAPINFO format headers, the size of the palette
  4093. * is in biClrUsed, whereas in the BITMAPCORE - style headers, it
  4094. * is dependent on the bits per pixel ( = 2 raised to the power of
  4095. * bits/pixel).
  4096. */
  4097. if (lpbi->biSize != sizeof(BITMAPCOREHEADER))
  4098. {
  4099. if (lpbi->biClrUsed != 0)
  4100. return (WORD)lpbi->biClrUsed;
  4101. bits = lpbi->biBitCount;
  4102. }
  4103. else
  4104. bits = lpbc->bcBitCount;
  4105. switch (bits)
  4106. {
  4107. case 1:
  4108. return 2;
  4109. case 4:
  4110. return 16;
  4111. case 8:
  4112. return 256;
  4113. default:
  4114. /* A 24 bitcount DIB has no color table */
  4115. return 0;
  4116. }
  4117. }
  4118. //code taken from SEEDIB MSDN sample
  4119. static WORD ColorTableSize(LPVOID lpv)
  4120. {
  4121. LPBITMAPINFOHEADER lpbih = (LPBITMAPINFOHEADER)lpv;
  4122. if (lpbih->biSize != sizeof(BITMAPCOREHEADER))
  4123. {
  4124. if (((LPBITMAPINFOHEADER)(lpbih))->biCompression == BI_BITFIELDS)
  4125. /* Remember that 16/32bpp dibs can still have a color table */
  4126. return (sizeof(DWORD) * 3) + (DIBNumColors(lpbih) * sizeof(RGBQUAD));
  4127. else
  4128. return (WORD)(DIBNumColors(lpbih) * sizeof(RGBQUAD));
  4129. }
  4130. else
  4131. return (WORD)(DIBNumColors(lpbih) * sizeof(RGBTRIPLE));
  4132. }
  4133. bool CPaintManagerUI::OnDrop(FORMATETC* pFmtEtc, STGMEDIUM& medium, DWORD* pdwEffect)
  4134. {
  4135. POINT ptMouse = { 0 };
  4136. GetCursorPos(&ptMouse);
  4137. ::SendMessage(m_hTargetWnd, WM_LBUTTONUP, NULL, MAKELPARAM(ptMouse.x, ptMouse.y));
  4138. if (pFmtEtc->cfFormat == CF_DIB && medium.tymed == TYMED_HGLOBAL)
  4139. {
  4140. if (medium.hGlobal != NULL)
  4141. {
  4142. LPBITMAPINFOHEADER lpbi = (BITMAPINFOHEADER*)GlobalLock(medium.hGlobal);
  4143. if (lpbi != NULL)
  4144. {
  4145. HBITMAP hbm = NULL;
  4146. HDC hdc = GetDC(NULL);
  4147. if (hdc != NULL)
  4148. {
  4149. int i = ((BITMAPFILEHEADER*)lpbi)->bfOffBits;
  4150. hbm = CreateDIBitmap(hdc, (LPBITMAPINFOHEADER)lpbi,
  4151. (LONG)CBM_INIT,
  4152. (LPSTR)lpbi + lpbi->biSize + ColorTableSize(lpbi),
  4153. (LPBITMAPINFO)lpbi, DIB_RGB_COLORS);
  4154. ::ReleaseDC(NULL, hdc);
  4155. }
  4156. GlobalUnlock(medium.hGlobal);
  4157. if (hbm != NULL)
  4158. hbm = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbm);
  4159. if (hbm != NULL)
  4160. DeleteObject(hbm);
  4161. return true; //release the medium
  4162. }
  4163. }
  4164. }
  4165. if (pFmtEtc->cfFormat == CF_BITMAP && medium.tymed == TYMED_GDI)
  4166. {
  4167. if (medium.hBitmap != NULL)
  4168. {
  4169. HBITMAP hBmp = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)medium.hBitmap);
  4170. if (hBmp != NULL)
  4171. DeleteObject(hBmp);
  4172. return false; //don't free the bitmap
  4173. }
  4174. }
  4175. if (pFmtEtc->cfFormat == CF_ENHMETAFILE && medium.tymed == TYMED_ENHMF)
  4176. {
  4177. ENHMETAHEADER emh;
  4178. GetEnhMetaFileHeader(medium.hEnhMetaFile, sizeof(ENHMETAHEADER), &emh);
  4179. RECT rc;//={0,0,EnhMetaHdr.rclBounds.right-EnhMetaHdr.rclBounds.left, EnhMetaHdr.rclBounds.bottom-EnhMetaHdr.rclBounds.top};
  4180. HDC hDC = GetDC(m_hTargetWnd);
  4181. //start code: taken from ENHMETA.EXE MSDN Sample
  4182. //*ALSO NEED to GET the pallete (select and RealizePalette it, but i was too lazy*
  4183. // Get the characteristics of the output device
  4184. float PixelsX = (float)GetDeviceCaps(hDC, HORZRES);
  4185. float PixelsY = (float)GetDeviceCaps(hDC, VERTRES);
  4186. float MMX = (float)GetDeviceCaps(hDC, HORZSIZE);
  4187. float MMY = (float)GetDeviceCaps(hDC, VERTSIZE);
  4188. // Calculate the rect in which to draw the metafile based on the
  4189. // intended size and the current output device resolution
  4190. // Remember that the intended size is given in 0.01mm units, so
  4191. // convert those to device units on the target device
  4192. rc.top = (int)((float)(emh.rclFrame.top) * PixelsY / (MMY * 100.0f));
  4193. rc.left = (int)((float)(emh.rclFrame.left) * PixelsX / (MMX * 100.0f));
  4194. rc.right = (int)((float)(emh.rclFrame.right) * PixelsX / (MMX * 100.0f));
  4195. rc.bottom = (int)((float)(emh.rclFrame.bottom) * PixelsY / (MMY * 100.0f));
  4196. //end code: taken from ENHMETA.EXE MSDN Sample
  4197. HDC hdcMem = CreateCompatibleDC(hDC);
  4198. HGDIOBJ hBmpMem = CreateCompatibleBitmap(hDC, emh.rclBounds.right, emh.rclBounds.bottom);
  4199. HGDIOBJ hOldBmp = ::SelectObject(hdcMem, hBmpMem);
  4200. PlayEnhMetaFile(hdcMem, medium.hEnhMetaFile, &rc);
  4201. HBITMAP hBmp = (HBITMAP)::SelectObject(hdcMem, hOldBmp);
  4202. DeleteDC(hdcMem);
  4203. ReleaseDC(m_hTargetWnd, hDC);
  4204. hBmp = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBmp);
  4205. if (hBmp != NULL)
  4206. DeleteObject(hBmp);
  4207. return true;
  4208. }
  4209. if (pFmtEtc->cfFormat == CF_HDROP && medium.tymed == TYMED_HGLOBAL)
  4210. {
  4211. HDROP hDrop = (HDROP)GlobalLock(medium.hGlobal);
  4212. if (hDrop != NULL)
  4213. {
  4214. TCHAR szFileName[MAX_PATH];
  4215. UINT cFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
  4216. if (cFiles > 0)
  4217. {
  4218. DragQueryFile(hDrop, 0, szFileName, sizeof(szFileName));
  4219. HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
  4220. if (hBitmap)
  4221. {
  4222. HBITMAP hBmp = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
  4223. if (hBmp != NULL)
  4224. DeleteObject(hBmp);
  4225. }
  4226. }
  4227. }
  4228. GlobalUnlock(medium.hGlobal);
  4229. }
  4230. return true; //let base free the medium
  4231. }
  4232. } // namespace DuiLib
  4233. #pragma warning(pop)