UIManager.cpp 160 KB

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