UIManager.cpp 166 KB

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