UIManager.cpp 138 KB

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