UIManager.cpp 141 KB

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