UIManager.cpp 168 KB

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