UIManager.cpp 141 KB

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