UIManager.cpp 138 KB

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