UIManager.cpp 225 KB

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