| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457 |
- #include "StdAfx.h"
- #include <zmouse.h>
- // [触屏诊断] 显式引入,避免依赖 tchar.h 的传递包含
- #include <stdio.h>
- #include <stdarg.h>
- #include <tchar.h>
- #pragma warning(push)
- #pragma warning(disable:4838 4244)
- namespace DuiLib
- {
- /////////////////////////////////////////////////////////////////////////////////////
- //
- //
- // 触屏参数(NIM Duilib 模型)
- // TOUCHINPUT 的坐标以 1/100 像素为单位,必须先换算成像素
- #ifndef TOUCH_COORD_TO_PIXEL
- #define TOUCH_COORD_TO_PIXEL(l) ((l) / 100)
- #endif
- // [触屏模型] 触摸只做两件事,不做任何“触摸→鼠标”的归一化:
- // ①点击:由 TOUCHUP 在确认未拖动后派发 BUTTONDOWN/UP,
- // 由 CButtonUI 等控件原生 Activate();
- // ②滚动:WM_TOUCH 直接派发 UIEVENT_TOUCHDOWN/MOVE/UP(见 OnTouchInput),
- // 容器据此 1:1 跟手平移、滚动条据此拖滑块。
- // “轻点 vs 拖拽”使用 m_bTouchMoved 区分,拖动不会激活控件。
- // WM_TOUCH 消息 ID 同样受 `#if _WIN32_WINNT >= 0x0601` 保护,DuiLib 工程未定义
- // _WIN32_WINNT(StdAfx.h 兜底为 XP)→ 必须自行声明,否则 switch 里引用该消息 ID
- // 会报 C2065(未声明的标识符)与 C2051(case 表达式不是常量)。实测固定值 0x0240。
- // 注:Win7+ 段受保护的不仅有结构体与函数,**消息 ID 与辅助宏同样在内**,
- // TOUCH_COORD_TO_PIXEL 也在上面用 #ifndef 兜底,原因相同。
- #ifndef WM_TOUCH
- #define WM_TOUCH 0x0240
- #endif
- // ---------------------------------------------------------------------------
- // WM_POINTER* 消息 ID(Win8+)。参考 nim_duilib NativeWindow_Windows::OnPointerMsgs。
- //
- // 为什么必须自行定义:这些宏在 winuser.h 里受 `#if _WIN32_WINNT >= 0x0602` 保护,
- // 而本工程编译目标是 0x0601(Win7),编译期不可见。用字面量绕过(数值固定)。
- //
- // 为什么必须处理:Win10 在 Pointer 栈可用时会优先投递 WM_POINTER*,而**不是** legacy
- // 的 WM_TOUCH。我们此前完全不处理这条通道 —— 一旦系统走了 Pointer 路径,窗口就
- // 既没有 WM_TOUCH 状态机、又没有触发鼠标提升,表现就是"什么都点不动"。
- // nim_duilib 的注释还明确指出:WM_POINTERCAPTURECHANGED 若不设置 bHandled,
- // 系统会把它转换成 WM_BUTTON 类消息 —— 说明系统确实在这两条通道间做隐式转换。
- // ---------------------------------------------------------------------------
- namespace PointerMsg
- {
- const UINT DUI_WM_POINTERUPDATE = 0x0245;
- const UINT DUI_WM_POINTERDOWN = 0x0246;
- const UINT DUI_WM_POINTERUP = 0x0247;
- const UINT DUI_WM_POINTERENTER = 0x0249;
- const UINT DUI_WM_POINTERLEAVE = 0x024A;
- const UINT DUI_WM_POINTERACTIVATE = 0x024B;
- const UINT DUI_WM_POINTERCAPTURECHANGED= 0x024C;
- const UINT DUI_WM_POINTERWHEEL = 0x024E;
- const UINT DUI_WM_POINTERHWHEEL = 0x024F;
- // POINTER_INFO 里我们只关心主指针判定:
- // IS_POINTER_PRIMARY_WPARAM 的等价判断 —— 低位 bit0 = isPrimary。
- //
- // ⚠️⚠️【这台收银机上这个位域完全不可信,三个消息各不相同】⚠️⚠️
- //
- // 真机日志实测(同一次触摸的三个阶段):
- // msg=0x0246 WM_POINTERDOWN w=0x60170CC7 HIWORD=0x6017 bit0=1 ✓
- // msg=0x0245 WM_POINTERUPDATE w=0x60160CC7 HIWORD=0x6016 bit0=0 ✗
- // msg=0x0247 WM_POINTERUP w=0x60000CC7 HIWORD=0x6000 bit0=0 ✗
- //
- // 也就是说 **HIWORD 根本不是 MSDN 描述的 POINTER_INFO 位域**(0x6016/0x6017/0x6000
- // 这三个值里只有 bit0 在 0x6016→0x6017 之间来回抖,看不出 primary 语义)。
- // 后果极其隐蔽:
- // · DOWN 恰好读到 bit0=1 ⇒ 蒙对了,手势能建立;
- // · UPDATE 读到 bit0=0 ⇒ 被当成"非主指针"**全部丢掉**,实测 486/486 条全丢
- // ⇒ 手势建立成功、抬手也能派发点击(UP 早已豁免),
- // 但**整个过程没有任何位移信息** ⇒ 表现为"能点击,但怎么划都滑不动"。
- // · UP 读到 bit0=0 ⇒ 当初就踩过,已单独豁免。
- //
- // 结论(照搬 nim_duilib 的做法):**不要再依赖这个位域做过滤**。
- // nim_duilib 的 OnPointerMsgs 虽然也写了 IS_POINTER_PRIMARY_WPARAM,
- // 但它只处理单指场景、且按 pointerId 取坐标,对错位不敏感。
- // 本项目改为:DOWN/UPDATE/UP 一律不做 primary 过滤,
- // 统一用 m_dwActivePointerId 精确配对(是不是"发起本次手势的那根手指")——
- // 这个判据来自 pointerId(LOWORD,即日志里的 0x0CC7=3271),是可靠的。
- //
- // 本函数保留但恒返回 true,仅作为"位域不可信的显式记录点",勿再改回位域判断。
- inline bool IsPrimaryPointer(WPARAM wParam)
- {
- (void)wParam;
- return true;
- }
- }
- // ---------------------------------------------------------------------------
- // [触屏诊断] 保留日志入口以便后续定位,但当前默认关闭。
- // ---------------------------------------------------------------------------
- namespace TouchTrace
- {
- inline bool Enabled()
- {
- return true;
- }
- // 极简 TCHAR* → UTF-8 char* 转换(仅用于日志路径;非线程安全,够用即可)
- inline const char* Narrow(LPCTSTR s)
- {
- static char s_buf[MAX_PATH * 3] = { 0 };
- #ifdef _UNICODE
- s_buf[0] = '\0';
- if (s != NULL)
- ::WideCharToMultiByte(CP_UTF8, 0, s, -1, s_buf, (int)sizeof(s_buf) - 1, NULL, NULL);
- return s_buf;
- #else
- return s;
- #endif
- }
- void Log(LPCTSTR fmt, ...)
- {
- if (!Enabled()) return;
- TCHAR path[MAX_PATH] = { 0 };
- ::GetModuleFileName(NULL, path, MAX_PATH);
- TCHAR* p = _tcsrchr(path, _T('\\'));
- if (p != NULL) *(p + 1) = _T('\0');
- _tcscat_s(path, MAX_PATH, _T("touch_trace.log"));
- TCHAR msg[1024] = { 0 };
- va_list ap;
- va_start(ap, fmt);
- _vsntprintf_s(msg, _countof(msg), _TRUNCATE, fmt, ap);
- va_end(ap);
- // 统一转 UTF-8 写盘(Unicode/ANSI 工程都能用文本编辑器正常查看)
- char utf8[2048] = { 0 };
- #ifdef _UNICODE
- if (::WideCharToMultiByte(CP_UTF8, 0, msg, -1, utf8, (int)sizeof(utf8) - 1, NULL, NULL) <= 0)
- return;
- #else
- _tcscpy_s(utf8, _countof(utf8), msg);
- #endif
- FILE* f = NULL;
- // 保护:单个日志超过 32MB 就不再追加,避免在收银机上长期运行把日志写到失控。
- // (全量 INPUT-TRACE 很啰嗦,阈值从 4MB 放宽到 32MB,否则一轮诊断还没跑完就静默停写了。)
- // 注意:GetFileAttributesEx 是 TCHAR 宏(Unicode 工程下展开为 ...ExW),
- // 必须传 TCHAR 的 path,不能传 Narrow(path) 得到的 UTF-8 char*(否则 C2664)。
- WIN32_FILE_ATTRIBUTE_DATA fad = { 0 };
- if (::GetFileAttributesEx(path, GetFileExInfoStandard, &fad)
- && fad.nFileSizeHigh == 0 && fad.nFileSizeLow >= (32u * 1024u * 1024u))
- return;
- // fopen_s 只吃 char*(窄字符路径),这里才需要 UTF-8 的 Narrow(path)
- if (fopen_s(&f, Narrow(path), "a") == 0 && f != NULL)
- {
- fprintf(f, "[%8u][T%5u] %s\n", (unsigned)::GetTickCount(), (unsigned)::GetCurrentThreadId(), utf8);
- // 立刻冲盘:程序被强杀 / 挂死 / 断电时,日志尾部(往往正是关键现场)不能丢。
- fflush(f);
- fclose(f);
- }
- }
- }
- static void GetChildWndRect(HWND hWnd, HWND hChildWnd, RECT& rcChildWnd)
- {
- ::GetWindowRect(hChildWnd, &rcChildWnd);
- POINT pt;
- pt.x = rcChildWnd.left;
- pt.y = rcChildWnd.top;
- ::ScreenToClient(hWnd, &pt);
- rcChildWnd.left = pt.x;
- rcChildWnd.top = pt.y;
- pt.x = rcChildWnd.right;
- pt.y = rcChildWnd.bottom;
- ::ScreenToClient(hWnd, &pt);
- rcChildWnd.right = pt.x;
- rcChildWnd.bottom = pt.y;
- }
- static UINT MapKeyState()
- {
- UINT uState = 0;
- if (::GetKeyState(VK_CONTROL) < 0) uState |= MK_CONTROL;
- if (::GetKeyState(VK_LBUTTON) < 0) uState |= MK_LBUTTON;
- if (::GetKeyState(VK_RBUTTON) < 0) uState |= MK_RBUTTON;
- if (::GetKeyState(VK_SHIFT) < 0) uState |= MK_SHIFT;
- if (::GetKeyState(VK_MENU) < 0) uState |= MK_ALT;
- return uState;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///
- typedef struct tagFINDTABINFO
- {
- CControlUI* pFocus;
- CControlUI* pLast;
- bool bForward;
- bool bNextIsIt;
- } FINDTABINFO;
- typedef struct tagFINDSHORTCUT
- {
- TCHAR ch;
- bool bPickNext;
- } FINDSHORTCUT;
- typedef struct tagTIMERINFO
- {
- CControlUI* pSender;
- UINT nLocalID;
- HWND hWnd;
- UINT uWinTimer;
- bool bKilled;
- } TIMERINFO;
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///
- tagTImageInfo::tagTImageInfo()
- {
- pImage = NULL;
- hBitmap = NULL;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///
- tagTDrawInfo::tagTDrawInfo()
- {
- Clear();
- }
- void tagTDrawInfo::Parse(LPCTSTR pStrImage, LPCTSTR pStrModify, CPaintManagerUI* pManager)
- {
- // 1、aaa.jpg
- // 2、file='aaa.jpg' res='' restype='0' dest='0,0,0,0' source='0,0,0,0' corner='0,0,0,0'
- // mask='#FF0000' fade='255' hole='false' xtiled='false' ytiled='false'
- sDrawString = pStrImage;
- sDrawModify = pStrModify;
- sImageName = pStrImage;
- CDuiString sItem;
- CDuiString sValue;
- LPTSTR pstr = NULL;
- for (int i = 0; i < 2; ++i)
- {
- if (i == 1) pStrImage = pStrModify;
- if (!pStrImage) continue;
- while (*pStrImage != _T('\0'))
- {
- sItem.Empty();
- sValue.Empty();
- while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
- while (*pStrImage != _T('\0') && *pStrImage != _T('=') && *pStrImage > _T(' '))
- {
- LPTSTR pstrTemp = ::CharNext(pStrImage);
- while (pStrImage < pstrTemp)
- {
- sItem += *pStrImage++;
- }
- }
- while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
- if (*pStrImage++ != _T('=')) break;
- while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
- if (*pStrImage++ != _T('\'')) break;
- while (*pStrImage != _T('\0') && *pStrImage != _T('\''))
- {
- LPTSTR pstrTemp = ::CharNext(pStrImage);
- while (pStrImage < pstrTemp)
- {
- sValue += *pStrImage++;
- }
- }
- if (*pStrImage++ != _T('\'')) break;
- if (!sValue.IsEmpty())
- {
- if (sItem == _T("file") || sItem == _T("res"))
- {
- sImageName = sValue;
- }
- else if (sItem == _T("restype"))
- {
- sResType = sValue;
- }
- else if (sItem == _T("dest"))
- {
- rcDest.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
- rcDest.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- rcDest.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- rcDest.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- //if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcDest);
- }
- else if (sItem == _T("source"))
- {
- rcSource.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
- rcSource.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- rcSource.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- rcSource.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- //if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcSource);
- }
- else if (sItem == _T("corner"))
- {
- rcCorner.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
- rcCorner.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- rcCorner.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- rcCorner.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- //if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcCorner);
- }
- else if (sItem == _T("mask"))
- {
- if (sValue[0] == _T('#')) dwMask = _tcstoul(sValue.GetData() + 1, &pstr, 16);
- else dwMask = _tcstoul(sValue.GetData(), &pstr, 16);
- }
- else if (sItem == _T("fade"))
- {
- uFade = (UINT)_tcstoul(sValue.GetData(), &pstr, 10);
- }
- else if (sItem == _T("rotate"))
- {
- uRotate = (UINT)_tcstoul(sValue.GetData(), &pstr, 10);
- bGdiplus = true;
- }
- else if (sItem == _T("gdiplus"))
- {
- bGdiplus = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
- }
- else if (sItem == _T("hole"))
- {
- bHole = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
- }
- else if (sItem == _T("xtiled"))
- {
- bTiledX = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
- }
- else if (sItem == _T("ytiled"))
- {
- bTiledY = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
- }
- else if (sItem == _T("hsl"))
- {
- bHSL = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
- }
- else if (sItem == _T("size"))
- {
- szImage.cx = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
- szImage.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- }
- else if (sItem == _T("align"))
- {
- sAlign = sValue;
- }
- else if (sItem == _T("padding"))
- {
- rcPadding.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
- rcPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- rcPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- rcPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- //if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcPadding);
- }
- }
- if (*pStrImage++ != _T(' ')) break;
- }
- }
- // 调整DPI资源
- if (pManager != NULL && pManager->GetDPIObj()->GetScale() != 100)
- {
- CDuiString sScale;
- sScale.Format(_T("@%d."), pManager->GetDPIObj()->GetScale());
- sImageName.Replace(_T("."), sScale);
- }
- }
- void tagTDrawInfo::Clear()
- {
- sDrawString.Empty();
- sDrawModify.Empty();
- sImageName.Empty();
- memset(&rcDest, 0, sizeof(RECT));
- memset(&rcSource, 0, sizeof(RECT));
- memset(&rcCorner, 0, sizeof(RECT));
- dwMask = 0;
- uFade = 255;
- uRotate = 0;
- bHole = false;
- bTiledX = false;
- bTiledY = false;
- bHSL = false;
- bGdiplus = false;
- szImage.cx = szImage.cy = 0;
- sAlign.Empty();
- memset(&rcPadding, 0, sizeof(RECT));
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///
- typedef BOOL(__stdcall* PFUNCUPDATELAYEREDWINDOW)(HWND, HDC, POINT*, SIZE*, HDC, POINT*, COLORREF, BLENDFUNCTION*, DWORD);
- PFUNCUPDATELAYEREDWINDOW g_fUpdateLayeredWindow = NULL;
- HPEN m_hUpdateRectPen = NULL;
- HINSTANCE CPaintManagerUI::m_hResourceInstance = NULL;
- CDuiString CPaintManagerUI::m_pStrResourcePath;
- CDuiString CPaintManagerUI::m_pStrResourceZip;
- CDuiString CPaintManagerUI::m_pStrResourceZipPwd; //Garfield 20160325 带密码zip包解密
- HANDLE CPaintManagerUI::m_hResourceZip = NULL;
- bool CPaintManagerUI::m_bCachedResourceZip = true;
- BYTE* CPaintManagerUI::m_cbZipBuf = nullptr;
- int CPaintManagerUI::m_nResType = UILIB_FILE;
- TResInfo CPaintManagerUI::m_SharedResInfo;
- HINSTANCE CPaintManagerUI::m_hInstance = NULL;
- bool CPaintManagerUI::m_bUseHSL = false;
- short CPaintManagerUI::m_H = 180;
- short CPaintManagerUI::m_S = 100;
- short CPaintManagerUI::m_L = 100;
- CStdPtrArray CPaintManagerUI::m_aPreMessages;
- CStdPtrArray CPaintManagerUI::m_aPlugins;
- CPaintManagerUI::CPaintManagerUI() :
- m_hWndPaint(NULL),
- m_hDcPaint(NULL),
- m_hDcOffscreen(NULL),
- m_hDcBackground(NULL),
- m_bOffscreenPaint(true),
- m_hbmpOffscreen(NULL),
- m_pOffscreenBits(NULL),
- m_hbmpBackground(NULL),
- m_pBackgroundBits(NULL),
- m_hwndTooltip(NULL),
- m_uTimerID(0x1000),
- m_pRoot(NULL),
- m_pFocus(NULL),
- m_pEventHover(NULL),
- m_pEventClick(NULL),
- m_pEventRClick(NULL),
- m_pEventKey(NULL),
- m_bFirstLayout(true),
- m_bFocusNeeded(false),
- m_bUpdateNeeded(false),
- m_bMouseTracking(false),
- m_bMouseCapture(false),
- m_bAsyncNotifyPosted(false),
- m_bUsedVirtualWnd(false),
- m_bForceUseSharedRes(false),
- m_nOpacity(0xFF),
- m_bLayered(false),
- m_bLayeredChanged(false),
- m_bShowUpdateRect(false),
- m_bUseGdiplusText(false),
- m_trh(0),
- m_bDragDrop(false),
- m_bDragMode(false),
- m_hDragBitmap(NULL),
- m_pDPI(NULL),
- m_iHoverTime(400UL),
- // [FIX] m_bNoActivate 原本未初始化:Debug 堆新块填充 0xCD -> 读到 true;
- // Release 下则是随机垃圾值,属偶发性隐患。
- // 后果:本文件中 3 处 "if (!m_bNoActivate) ::SetFocus(m_hWndPaint);"
- // (WM_LBUTTONDOWN 1738 / CPaintManagerUI::SetFocus 2442 / SetFocusNeeded 2475) 全部被跳过,
- // 系统焦点无法回到主窗口 -> 原生 EDIT 子窗 CEditWnd 收不到 WM_KILLFOCUS ->
- // 其唯一的销毁途径 PostMessage(WM_CLOSE) 永不触发 -> 光标常闪、点空白不丢焦点。
- // XML 侧仅在显式书写 noactivate 属性时才赋值(UIDlgBuilder.cpp),故此处必须先给确定初值。
- m_bNoActivate(false),
- // 以下两个成员同样是漏网之鱼(全库无赋值点),一并清零以防后患。
- m_bIsPainting(false),
- m_pLastToolTip(NULL),
- // [指针通道] 默认【开启】。原因见成员声明处的长注释:
- // 之前"默认关闭、收到 WM_TOUCH 即永久关闭"的策略害死了真模态弹框 ——
- // 这台受测试的收银机上,WM_TOUCH 的"贪婪归属权"卡在主窗口,
- // 弹框永远收不到 WM_TOUCH,只收得到按命中投递的 WM_POINTER*;
- // 指针通道又被关掉 ⇒ 弹框彻底没有输入源,怎么点都没反应。
- // 现在改为【始终开启 + 手势级让路】:只要某一侧先开始驱动本次手势,
- // 另一侧就整段手势安静,绝不出现两套 WM_LBUTTONDOWN/UP。
- m_bPointerNormalizeEnabled(true),
- m_bPointerGestureActive(false),
- m_dwActivePointerId(0),
- m_dwPointerDownTick(0),
- // 顺序必须与 UIManager.h 里的声明顺序一致,否则 MSVC 报 -Wreorder(C5038)。
- m_pfnDefProcRelay(NULL),
- m_hWndDefProcRelay(NULL),
- m_bRelayDefProcForTouchMove(false),
- m_uDefProcRelayCount(0)
- {
- // [WM_TOUCH 归属权] 补调 DefWindowProc 的开关读取(无需重新编译即可切换)。
- // 设 DUI_RELAY_DEFPROC=1 开启;只在进程启动时读一次。
- {
- TCHAR szBuf[16] = { 0 };
- const DWORD dwLen = ::GetEnvironmentVariable(_T("DUI_RELAY_DEFPROC"), szBuf, _countof(szBuf));
- if (dwLen > 0 && dwLen < _countof(szBuf) && szBuf[0] == _T('1'))
- {
- m_bRelayDefProcForTouchMove = true;
- TouchTrace::Log(_T("DEFPROC-RELAY enabled by env (DUI_RELAY_DEFPROC=1)"));
- }
- }
- if (m_SharedResInfo.m_DefaultFontInfo.sFontName.IsEmpty())
- {
- m_SharedResInfo.m_dwDefaultDisabledColor = 0xFFA7A6AA;
- m_SharedResInfo.m_dwDefaultFontColor = 0xFF000000;
- m_SharedResInfo.m_dwDefaultLinkFontColor = 0xFF0000FF;
- m_SharedResInfo.m_dwDefaultLinkHoverFontColor = 0xFFD3215F;
- m_SharedResInfo.m_dwDefaultSelectedBkColor = 0xFFBAE4FF;
- LOGFONT lf = { 0 };
- ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
- lf.lfCharSet = DEFAULT_CHARSET;
- HFONT hDefaultFont = ::CreateFontIndirect(&lf);
- m_SharedResInfo.m_DefaultFontInfo.hFont = hDefaultFont;
- m_SharedResInfo.m_DefaultFontInfo.sFontName = lf.lfFaceName;
- m_SharedResInfo.m_DefaultFontInfo.iSize = -lf.lfHeight;
- m_SharedResInfo.m_DefaultFontInfo.bBold = (lf.lfWeight >= FW_BOLD);
- m_SharedResInfo.m_DefaultFontInfo.bUnderline = (lf.lfUnderline == TRUE);
- m_SharedResInfo.m_DefaultFontInfo.bItalic = (lf.lfItalic == TRUE);
- ::ZeroMemory(&m_SharedResInfo.m_DefaultFontInfo.tm, sizeof(m_SharedResInfo.m_DefaultFontInfo.tm));
- }
- m_ResInfo.m_dwDefaultDisabledColor = m_SharedResInfo.m_dwDefaultDisabledColor;
- m_ResInfo.m_dwDefaultFontColor = m_SharedResInfo.m_dwDefaultFontColor;
- m_ResInfo.m_dwDefaultLinkFontColor = m_SharedResInfo.m_dwDefaultLinkFontColor;
- m_ResInfo.m_dwDefaultLinkHoverFontColor = m_SharedResInfo.m_dwDefaultLinkHoverFontColor;
- m_ResInfo.m_dwDefaultSelectedBkColor = m_SharedResInfo.m_dwDefaultSelectedBkColor;
- if (m_hUpdateRectPen == NULL)
- {
- m_hUpdateRectPen = ::CreatePen(PS_SOLID, 1, RGB(220, 0, 0));
- // Boot Windows Common Controls (for the ToolTip control)
- ::InitCommonControls();
- ::LoadLibrary(_T("msimg32.dll"));
- }
- m_szMinWindow.cx = 0;
- m_szMinWindow.cy = 0;
- m_szMaxWindow.cx = 0;
- m_szMaxWindow.cy = 0;
- m_szInitWindowSize.cx = 0;
- m_szInitWindowSize.cy = 0;
- m_szRoundCorner.cx = m_szRoundCorner.cy = 0;
- ::ZeroMemory(&m_rcSizeBox, sizeof(m_rcSizeBox));
- ::ZeroMemory(&m_rcCaption, sizeof(m_rcCaption));
- ::ZeroMemory(&m_rcLayeredInset, sizeof(m_rcLayeredInset));
- ::ZeroMemory(&m_rcLayeredUpdate, sizeof(m_rcLayeredUpdate));
- m_ptLastMousePos.x = m_ptLastMousePos.y = -1;
- m_pGdiplusStartupInput = new Gdiplus::GdiplusStartupInput;
- Gdiplus::GdiplusStartup(&m_gdiplusToken, m_pGdiplusStartupInput, NULL); // 加载GDI接口
- CShadowUI::Initialize(m_hInstance);
- m_pDragDrop = NULL;
- }
- CPaintManagerUI::~CPaintManagerUI()
- {
- // Delete the control-tree structures
- for (int i = 0; i < m_aDelayedCleanup.GetSize(); i++) delete static_cast<CControlUI*>(m_aDelayedCleanup[i]);
- m_aDelayedCleanup.Resize(0);
- for (int i = 0; i < m_aAsyncNotify.GetSize(); i++) delete static_cast<TNotifyUI*>(m_aAsyncNotify[i]);
- m_aAsyncNotify.Resize(0);
- m_mNameHash.Resize(0);
- if (m_pRoot != NULL) delete m_pRoot;
- ::DeleteObject(m_ResInfo.m_DefaultFontInfo.hFont);
- RemoveAllFonts();
- RemoveAllImages();
- RemoveAllStyle();
- RemoveAllDefaultAttributeList();
- RemoveAllWindowCustomAttribute();
- RemoveAllOptionGroups();
- RemoveAllTimers();
- RemoveAllDrawInfos();
- if (m_hwndTooltip != NULL)
- {
- ::DestroyWindow(m_hwndTooltip);
- m_hwndTooltip = NULL;
- }
- if (!m_aFonts.IsEmpty())
- {
- for (int i = 0; i < m_aFonts.GetSize(); ++i)
- {
- HANDLE handle = static_cast<HANDLE>(m_aFonts.GetAt(i));
- ::RemoveFontMemResourceEx(handle);
- }
- }
- if (m_hDcOffscreen != NULL) ::DeleteDC(m_hDcOffscreen);
- if (m_hDcBackground != NULL) ::DeleteDC(m_hDcBackground);
- if (m_hbmpOffscreen != NULL) ::DeleteObject(m_hbmpOffscreen);
- if (m_hbmpBackground != NULL) ::DeleteObject(m_hbmpBackground);
- if (m_hDcPaint != NULL) ::ReleaseDC(m_hWndPaint, m_hDcPaint);
- m_aPreMessages.Remove(m_aPreMessages.Find(this));
- // 销毁拖拽图片
- if (m_hDragBitmap != NULL) ::DeleteObject(m_hDragBitmap);
- //卸载GDIPlus
- Gdiplus::GdiplusShutdown(m_gdiplusToken);
- delete m_pGdiplusStartupInput;
- // DPI管理对象
- if (m_pDPI != NULL)
- {
- delete m_pDPI;
- m_pDPI = NULL;
- }
- }
- void CPaintManagerUI::Init(HWND hWnd, LPCTSTR pstrName)
- {
- ASSERT(::IsWindow(hWnd));
- m_mNameHash.Resize();
- RemoveAllFonts();
- RemoveAllImages();
- RemoveAllStyle();
- RemoveAllDefaultAttributeList();
- RemoveAllWindowCustomAttribute();
- RemoveAllOptionGroups();
- RemoveAllTimers();
- m_sName.Empty();
- if (pstrName != NULL) m_sName = pstrName;
- if (m_hWndPaint != hWnd)
- {
- m_hWndPaint = hWnd;
- m_hDcPaint = ::GetDC(hWnd);
- m_aPreMessages.Add(this);
- // 触屏兼容:每个窗口(主窗口 + 所有弹框)统一注册 WM_TOUCH,由本类库级接管,
- // 无需 CTouchWindowBase 外挂,也无需逐窗口样板。注册是幂等的,重复调用无害。
- InitTouchSupport();
- }
- }
- // ---------------------------------------------------------------------------
- // 真模态(DialogBox)支持:manager 的 paint window 迁移。
- // 详见 UIManager.h 里的说明。这两个方法**不**触碰任何资源状态,只换窗口绑定。
- // ---------------------------------------------------------------------------
- void CPaintManagerUI::DetachPaintWindow()
- {
- if (m_hDcPaint != NULL)
- {
- ::ReleaseDC(m_hWndPaint, m_hDcPaint);
- m_hDcPaint = NULL;
- }
- const int nIndex = m_aPreMessages.Find(this);
- if (nIndex >= 0) m_aPreMessages.Remove(nIndex);
- TouchTrace::Log(_T("MANAGER-DETACH manager=%p from hwnd=%p"), (void*)this, (void*)m_hWndPaint);
- // 注意:m_hWndPaint 暂不置空 —— AttachPaintWindow 会覆盖它。
- // 也不要清空控件树 / 字体 / 图片:那些是"内容",迁移后要继续用。
- }
- void CPaintManagerUI::AttachPaintWindow(HWND hWnd)
- {
- ASSERT(::IsWindow(hWnd));
- if (hWnd == NULL || !::IsWindow(hWnd)) return;
- m_hWndPaint = hWnd;
- m_hDcPaint = ::GetDC(hWnd);
- // 重新加入全局表(Detach 时摘除过)。若之前没摘(异常路径),避免重复添加。
- if (m_aPreMessages.Find(this) < 0) m_aPreMessages.Add(this);
- // 新窗口必须注册触摸,否则收不到 WM_TOUCH。
- InitTouchSupport();
- TouchTrace::Log(_T("MANAGER-ATTACH manager=%p to hwnd=%p dc=%p"),
- (void*)this, (void*)m_hWndPaint, (void*)m_hDcPaint);
- }
- // ---------------------------------------------------------------------------
- // 触摸 API 懒加载包装(对标 NIM Duilib 的 RegisterTouchWindowWrapper 系列)。
- //
- // 为什么不能直接调用这些 API:DuiLib 工程自身没有定义 _WIN32_WINNT,
- // StdAfx.h 会把它兜底设成 XP(0x0501);而 TOUCHINPUT / GetTouchInputInfo /
- // RegisterTouchWindow 在 winuser.h 里受 `#if _WIN32_WINNT >= 0x0601` 保护,
- // 直接引用会编译报错(C2065/C3861)。(主工程定义了 0x0601,所以
- // CTouchWindowBase 里能直接用,那里不受影响。)
- //
- // 因此这里完全自给自足:自行声明与 SDK 完全一致的 TOUCHINPUT 布局和事件标志,
- // 再用 GetProcAddress 取函数地址。这样:
- // * 无论 DuiLib 的 target 版本如何都能编译通过;
- // * 在没有触摸 API 的系统上自动降级为空操作,不会崩溃。
- // ---------------------------------------------------------------------------
- namespace TouchApi
- {
- // 与 winuser.h 的 tagTOUCHINPUT 布局逐字段一致
- typedef struct tagTouchInput
- {
- LONG x;
- LONG y;
- HANDLE hSource;
- DWORD dwID;
- DWORD dwFlags;
- DWORD dwMask;
- DWORD dwTime;
- ULONG_PTR dwExtraInfo;
- DWORD cxContact;
- DWORD cyContact;
- } TOUCHINPUT, *PTOUCHINPUT;
- // 事件标志,数值与 winuser.h 的触摸事件标志一致。
- // 名字必须避开 SDK 的同名宏:部分 SDK 把它们定义成【宏】,且不受 _WIN32_WINNT 版本段保护,
- // 同名会被预处理器展开——声明变成 `const DWORD 0x0002 = 0x0002;`(C2059),
- // 限定使用变成 `TouchApi::0x0004`(C2589)。故改用 TOUCHEVTFLAG 前缀。
- const DWORD TOUCHEVTFLAG_MOVE = 0x0001;
- const DWORD TOUCHEVTFLAG_DOWN = 0x0002;
- const DWORD TOUCHEVTFLAG_UP = 0x0004;
- typedef BOOL (WINAPI *PFN_RegisterTouchWindow)(HWND, ULONG);
- typedef BOOL (WINAPI *PFN_GetTouchInputInfo)(HANDLE, UINT, PTOUCHINPUT, int);
- typedef BOOL (WINAPI *PFN_CloseTouchInputHandle)(HANDLE);
- inline HMODULE GetUser32()
- {
- static HMODULE s_hMod = NULL;
- static bool s_bInited = false;
- if (!s_bInited)
- {
- s_bInited = true;
- s_hMod = ::GetModuleHandle(_T("user32.dll"));
- }
- return s_hMod;
- }
- inline PFN_RegisterTouchWindow GetRegisterTouchWindow()
- {
- static PFN_RegisterTouchWindow s_pfn = NULL;
- static bool s_bInited = false;
- if (!s_bInited)
- {
- s_bInited = true;
- HMODULE hMod = GetUser32();
- if (hMod != NULL) s_pfn = (PFN_RegisterTouchWindow)::GetProcAddress(hMod, "RegisterTouchWindow");
- }
- return s_pfn;
- }
- inline PFN_GetTouchInputInfo GetGetTouchInputInfo()
- {
- static PFN_GetTouchInputInfo s_pfn = NULL;
- static bool s_bInited = false;
- if (!s_bInited)
- {
- s_bInited = true;
- HMODULE hMod = GetUser32();
- if (hMod != NULL) s_pfn = (PFN_GetTouchInputInfo)::GetProcAddress(hMod, "GetTouchInputInfo");
- }
- return s_pfn;
- }
- inline PFN_CloseTouchInputHandle GetCloseTouchInputHandle()
- {
- static PFN_CloseTouchInputHandle s_pfn = NULL;
- static bool s_bInited = false;
- if (!s_bInited)
- {
- s_bInited = true;
- HMODULE hMod = GetUser32();
- if (hMod != NULL) s_pfn = (PFN_CloseTouchInputHandle)::GetProcAddress(hMod, "CloseTouchInputHandle");
- }
- return s_pfn;
- }
- }
- // ---------------------------------------------------------------------------
- // [触屏] 手指容差(滚动条专治)
- // 滚动条默认仅 16px 宽(CScrollBarUI::DEFAULT_SCROLLBAR_SIZE),指尖按偏 5~10px 就落到
- // 右侧内容上:内容会跟手滚动,但“滑块拖不动”——用户感知就是“滚动条没法在触屏上滑动”。
- // 两个措施:①把触点横向收进滚动条 rect,再交给滚动条自己判滑块;②若直接命中的不是
- // 滚动条,但触点容差范围内正好压在某个滚动条的滑块上,则改判为命中该滚动条。
- // ---------------------------------------------------------------------------
- namespace
- {
- const int kScrollBarTouchSlop = 12; // 手指离滚动条 rect 的最大容差(像素)
- // Windows 会把“触摸提升(promoted)出来的合成鼠标消息”打上 MOUSEEVENTF_FROMTOUCH 标记
- // (位于 GetMessageExtraInfo() 的高位,MSDN 推荐判定值 0xFF515700)。
- // 这里**只**用它来区分“真鼠标移动”与“触摸补发的鼠标移动”,从而决定要不要更新悬浮态;
- // 消息本身绝不丢弃——历史教训:整条吞掉会让点击状态机(m_pEventClick)失效、点击全灭。
- inline bool IsPromotedTouchMouseMessage()
- {
- return (::GetMessageExtraInfo() & 0xFFFFFF80) == 0xFF515700;
- }
- // 把触点收进滚动条自身 rect:垂直滚动条主要收敛 x(y 原样保留,纵向拖拽量完好)。
- inline POINT Touch_SnapIntoScrollBar(const CControlUI* pBar, POINT pt)
- {
- if (pBar == NULL) return pt;
- RECT rc = pBar->GetPos();
- if (pt.x < rc.left) pt.x = rc.left;
- if (pt.x > rc.right) pt.x = rc.right;
- if (pt.y < rc.top) pt.y = rc.top;
- if (pt.y > rc.bottom) pt.y = rc.bottom;
- return pt;
- }
- // 沿“命中控件 → 祖先链”找最近的、满足容差条件的滚动条滑块;找不到返回 NULL。
- inline CControlUI* Touch_ResolveScrollBarThumb(CControlUI* pStart, POINT pt)
- {
- for (CControlUI* p = pStart; p != NULL; p = p->GetParent())
- {
- if (p->GetInterface(DUI_CTR_SCROLLBAR) != NULL) return NULL; // 本身已是滚动条,无需改判
- CContainerUI* pCont = static_cast<CContainerUI*>(p->GetInterface(DUI_CTR_CONTAINER));
- if (pCont == NULL) continue;
- CScrollBarUI* pBars[2] = { pCont->GetVerticalScrollBar(), pCont->GetHorizontalScrollBar() };
- for (int i = 0; i < 2; ++i)
- {
- if (pBars[i] != NULL && pBars[i]->IsThumbHit(pt, kScrollBarTouchSlop))
- return pBars[i];
- }
- }
- return NULL;
- }
- inline CScrollBarUI* Touch_FindScrollBar(CControlUI* pStart, POINT pt)
- {
- for (CControlUI* p = pStart; p != NULL; p = p->GetParent())
- {
- CContainerUI* pContainer = static_cast<CContainerUI*>(p->GetInterface(DUI_CTR_CONTAINER));
- if (pContainer == NULL) continue;
- CScrollBarUI* bars[2] = {
- pContainer->GetVerticalScrollBar(),
- pContainer->GetHorizontalScrollBar()
- };
- for (int i = 0; i < 2; ++i)
- {
- if (bars[i] == NULL || !bars[i]->IsVisible() || !bars[i]->IsEnabled())
- continue;
- RECT rc = bars[i]->GetPos();
- ::InflateRect(&rc, kScrollBarTouchSlop, kScrollBarTouchSlop);
- if (::PtInRect(&rc, pt)) return bars[i];
- }
- }
- return NULL;
- }
- inline CContainerUI* Touch_FindScrollContainer(CControlUI* pStart)
- {
- for (CControlUI* p = pStart; p != NULL; p = p->GetParent())
- {
- CContainerUI* pContainer = static_cast<CContainerUI*>(p->GetInterface(DUI_CTR_CONTAINER));
- if (pContainer == NULL) continue;
- CScrollBarUI* pVertical = pContainer->GetVerticalScrollBar();
- CScrollBarUI* pHorizontal = pContainer->GetHorizontalScrollBar();
- if ((pVertical != NULL && pVertical->IsVisible() && pVertical->IsEnabled())
- || (pHorizontal != NULL && pHorizontal->IsVisible() && pHorizontal->IsEnabled()))
- return pContainer;
- }
- return NULL;
- }
- }
- // ---------------------------------------------------------------------------
- // 当前模态弹框句柄(由 CWindowWnd::ShowModal 进入/退出时设置)。
- // Win8/10 指针栈下,触摸消息(WM_TOUCH / WM_POINTER)并不总是投递给"视觉上在最前面的弹框":
- // 当弹框的 owner 被 EnableWindow(FALSE) 禁用时,部分系统会把手势转投给 owner。
- // 这里做一道保险:只要当前有模态弹框,任何【非该弹框】的窗口收到 WM_TOUCH 一律忽略,
- // 避免"弹框还开着,手指却把主窗口点动了"的穿透。
- // ---------------------------------------------------------------------------
- static HWND s_hModalWindow = NULL;
- void CPaintManagerUI::SetModalWindow(HWND hWnd)
- {
- s_hModalWindow = hWnd;
- TouchTrace::Log(_T("MODAL-WINDOW set hwnd=%p"), (void*)hWnd);
- }
- HWND CPaintManagerUI::GetModalWindow()
- {
- return s_hModalWindow;
- }
- void CPaintManagerUI::InitTouchSupport()
- {
- if (m_hWndPaint == NULL || !::IsWindow(m_hWndPaint)) return;
- TouchApi::PFN_RegisterTouchWindow pfnRegister = TouchApi::GetRegisterTouchWindow();
- BOOL bRegistered = FALSE;
- if (pfnRegister != NULL)
- bRegistered = pfnRegister(m_hWndPaint, 0);
- TouchTrace::Log(_T("REGISTER hwnd=%p manager=%p result=%d error=%u style=0x%08X ex=0x%08X"),
- (void*)m_hWndPaint, (void*)this, (int)bRegistered, bRegistered ? 0u : ::GetLastError(),
- (unsigned)::GetWindowLong(m_hWndPaint, GWL_STYLE),
- (unsigned)::GetWindowLong(m_hWndPaint, GWL_EXSTYLE));
- }
- // ===========================================================================
- // [通道无关的滑动状态机] Begin / Update / End
- //
- // 背景(这是本文件里最容易踩坑的一块,先把结论写在前面):
- // "滑动"这件事在本库里的实现只有一份 —— 派发 UIEVENT_TOUCHMOVE,由
- // · CScrollBarUI 消费 → 拖滑块;
- // · 否则冒泡到 CContainerUI → 1:1 跟手平移;
- // · 谁都不认时 → 合成 UIEVENT_SCROLLWHEEL(Δy 累加满 120 一次)。
- // 过去这三个函数体是**内联写在 OnTouchInput(WM_TOUCH 通道)里的**。
- //
- // 问题:Win10 上同一次触摸会在两条互相独立的通道上都产生消息
- // (WM_TOUCH 与 WM_POINTER*),而"谁驱动本次手势"是运行时决定的:
- // · 主窗口常常是 WM_TOUCH 先到 → 老代码能滑;
- // · 弹框 / 被指针抢走的手势 ⇒ 只有 WM_POINTER* →
- // **整段滑动逻辑一条都没执行** ⇒ 「滑不动」;
- // 同时 UP 走指针 → 归一化成 WM_LBUTTONUP → 原生点击路径,
- // 而 tap/drag 判定所需的 m_bTouchMoved 从未被置真 ⇒「滑动反触发点击」。
- //
- // 修法:把滑动逻辑抽成不关心通道的这三个函数,两条通道都调用同一套。
- // 于是"谁驱动手势"不再影响"能不能滑",只影响消息从哪儿来。
- //
- // 状态机的三个相位:
- // Begin : 命中控件 + 找可滚容器 + 记起点/上一包点 + 复位位移标志
- // Update : Δ=(pt - 上一包点) 去重 → 置 m_bTouchMoved → 派发 TOUCHMOVE → 可能合成滚轮
- // End : 派发 TOUCHUP → 返回"是否算轻点"(未越过拖拽阈值)
- //
- // ⚠️ 三条不变量:
- // 1) Begin 必须把 m_bTouchSlideActive 置真;End 必须置假 + 清释放态。
- // 2) Update/End 在 !m_bTouchSlideActive 时必须安全空转(另一条通道可能没走 Begin)。
- // 3) 坐标一律是**客户区坐标**(调用方负责把屏幕坐标转好)。
- // ===========================================================================
- void CPaintManagerUI::BeginTouchSlideGesture(POINT pt)
- {
- TouchTrace::Log(_T("SLIDE-BEGIN pt=%d,%d"), pt.x, pt.y);
- m_ptLastTouchPos = pt;
- // 触摸没有"悬浮"语义。新手势开始前先清掉上一次鼠标路径遗留的 HOT 状态,
- // 防止首个触摸 DOWN 恰好发生在合成 WM_MOUSEMOVE 之前。
- ClearTouchHoverState(pt);
- // 先把手势状态复位:即使这一按没命中任何控件,也不能让上一轮的 m_pEventTouch 残留
- // 下去(否则后续 TOUCHMOVE 会发给一个早已松开的手指所对应的控件)。
- m_pEventTouch = NULL;
- m_pTouchScrollContainer = NULL;
- m_bTouchMoved = false;
- m_ptTouchStart = pt;
- m_dwTouchDownTick = ::GetTickCount(); // 供 MessageHandler 做"TOUCHUP 丢失"超时自愈
- // 滑动累加器:nim_duilib 把 TOUCHEVENTF_MOVE 的 Δy 攒够一个 WHEEL_DELTA(120) 就合成
- // WM_MOUSEWHEEL。这里同样复位,避免上一轮手势的余量污染新手势。
- m_nTouchWheelAccum = 0;
- // 本标志是"滑动状态机活着"的唯一判据(End 之前一直为真)。
- m_bTouchSlideActive = true;
- CControlUI* pControl = FindControl(pt);
- if (pControl == NULL)
- {
- TouchTrace::Log(_T("SLIDE-BEGIN no control pt=%d,%d"), pt.x, pt.y);
- // 没命中控件也要保持 active:随后的 Update 仍需被安全吸收掉,
- // 由 End 统一收尾(否则 Update 会跑到空指针上)。
- return;
- }
- if (pControl->GetManager() != this)
- {
- TouchTrace::Log(_T("SLIDE-BEGIN foreign control=%s manager=%p self=%p"),
- pControl->GetName().GetData(), (void*)pControl->GetManager(), (void*)this);
- return;
- }
- // [触屏] 手指容差改判:直接命中的不是滚动条时,看看是不是"手指离滚动条滑块很近"。
- // 典型场景:列表右侧竖直滚动条仅 16px 宽,用户想拖滑块却按到了旁边的列表项上。
- if (pControl->GetInterface(DUI_CTR_SCROLLBAR) == NULL)
- {
- CControlUI* pBar = Touch_FindScrollBar(pControl, pt);
- if (pBar == NULL)
- pBar = Touch_ResolveScrollBarThumb(pControl, pt);
- if (pBar != NULL)
- {
- TouchTrace::Log(_T("SLIDE-BEGIN-FATFINGER hit=%s -> scrollbar=%s pt=%d,%d"),
- pControl->GetName().GetData(), pBar->GetName().GetData(), pt.x, pt.y);
- pControl = pBar;
- }
- }
- const bool bTouchIsScrollBar = (pControl->GetInterface(DUI_CTR_SCROLLBAR) != NULL);
- // 命中滚动条时把触点收进滚动条 rect(横向按偏也能拖滑块);事件点用收敛后的,
- // 但 m_ptLastTouchPos / m_ptTouchStart 仍记真实坐标(去重与位移判定按真实手指轨迹走)。
- const POINT ptEvent = bTouchIsScrollBar ? Touch_SnapIntoScrollBar(pControl, pt) : pt;
- TouchTrace::Log(_T("SLIDE-BEGIN hit=%s cls=%s scrollbar=%d scrollContainer=%p ptEvent=%d,%d"),
- pControl->GetName().GetData(), pControl->GetClass(), (int)bTouchIsScrollBar,
- (void*)Touch_FindScrollContainer(pControl), ptEvent.x, ptEvent.y);
- m_pEventTouch = pControl;
- m_pTouchScrollContainer = bTouchIsScrollBar ? NULL : Touch_FindScrollContainer(pControl);
- if (!bTouchIsScrollBar)
- m_pEventClick = pControl;
- // 触摸没有悬浮语义;整个手势期间以及抬起后的合成鼠标移动都不应重建 HOT。
- m_bSuppressHoverAfterTouch = true;
- pControl->SetFocus();
- SetCapture(); // mgr 级:m_bMouseCapture=true + ::SetCapture
- // 滚动条:主动发一次 TOUCHDOWN 让它抓滑块。
- // (TouchDOWN 与鼠标 BUTTONDOWN 在 CScrollBarUI::DoEvent 里等价处理。)
- //
- // ⚠️⚠️ 【这一发 TOUCHDOWN 是"触摸能点击"的必要条件,绝不能删】⚠️⚠️
- //
- // 早先的注释写的是"触摸按下只建立候选点击,不立即发送任何按下事件"——那个策略
- // 在**旧架构**下能活,因为旧架构依赖 Windows 为触摸提升出来的
- // WM_LBUTTONDOWN/UP 去驱动点击(原生路径会自己发 UIEVENT_BUTTONDOWN)。
- //
- // 但现在(通道无关改造之后)触摸链已经把原生鼠标路径**有意让开**:
- // · MessageHandler 的 WM_LBUTTONDOWN 在 m_pEventTouch != NULL 时直接 break;
- // · OnPointerInput 的 DOWN 也不再调 MessageHandler(WM_LBUTTONDOWN)。
- // ⇒ 唯一能告诉控件"用户按下了"的通道就是这一发 TOUCHDOWN。
- //
- // 而控件侧的点击状态机是**按下/抬起配对**的,例如 CButtonUI::DoEvent:
- // UIEVENT_TOUCHDOWN → m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED
- // UIEVENT_TOUCHUP → 清 PUSHED|CAPTURED
- // UIEVENT_BUTTONUP → if ((m_uButtonState & UISTATE_CAPTURED) != 0) { ...; Activate(); }
- // 如果 DOWN 阶段完全不派发,按钮的 CAPTURED 就永远不会置位 ⇒ 抬手时进不了
- // BUTTONUP 分支(或进了也因守卫失败而跳过)⇒ **Activate 根本不被调用**
- // ⇒ 表现为"主窗口触摸点击全灭"。
- //
- // 另外:本函数**只发 TOUCHDOWN,不发 BUTTONDOWN** —— 这是有意为之。
- // 因为整个手势可能演变成拖动(滚动列表),此时不能让控件提前进入"已按下"的业务态;
- // 真正的点击由 UP 阶段依据 tap 判定(EndTouchSlideGesture 的返回值)成对合成
- // BUTTONDOWN/BUTTONUP 来派发。
- // 换句话说:TOUCHDOWN/TOUCHUP 只负责**控件内部视觉与状态记账**,
- // BUTTONDOWN/BUTTONUP 才负责**业务点击**。两者分工明确,不要合并。
- TEventUI event = { 0 };
- event.Type = UIEVENT_TOUCHDOWN;
- event.pSender = pControl;
- event.ptMouse = ptEvent;
- event.dwTimestamp = ::GetTickCount();
- event.wKeyState = MapKeyState();
- pControl->Event(event);
- }
- void CPaintManagerUI::UpdateTouchSlideGesture(POINT pt)
- {
- // 另一条通道可能没走 Begin(例如 WM_TOUCH 被让路、手势实际由指针驱动);
- // 此时 Update 必须安全空转,绝不能访问 m_pEventTouch。
- if (!m_bTouchSlideActive) return;
- // 与上一包完全相同的点 -> 去重,避免无谓派发(NIM 同款守卫)
- if (m_ptLastTouchPos.x == pt.x && m_ptLastTouchPos.y == pt.y) return;
- // 本次位移增量(相对上一包),随事件下发,滚动容器据此做 1:1 跟手平移
- const LONG dx = pt.x - m_ptLastTouchPos.x;
- const LONG dy = pt.y - m_ptLastTouchPos.y;
- m_ptLastTouchPos = pt;
- // 相对手势起点的累计位移超过阈值 -> 判定为拖拽(用于松开时抑制点击)。
- const LONG ox = pt.x - m_ptTouchStart.x;
- const LONG oy = pt.y - m_ptTouchStart.y;
- // 阈值取 8px:真实滚动位移远大于此,而指尖"按下去时的轻微滚动"通常在 8px 内。
- // 阈值过小会把抖动误判成拖拽 → 该次抬起的 BUTTONUP 被抑制 → 按钮停在按下态。
- if (ox > 8 || ox < -8 || oy > 8 || oy < -8)
- m_bTouchMoved = true;
- if (m_pEventTouch == NULL) return;
- TouchTrace::Log(_T("SLIDE-MOVE touch=%s cls=%s d=%d,%d moved=%d click=%s"),
- m_pEventTouch->GetName().GetData(), m_pEventTouch->GetClass(),
- (int)dx, (int)dy, (int)m_bTouchMoved,
- m_pEventClick != NULL ? m_pEventClick->GetName().GetData() : _T("null"));
- TEventUI event = { 0 };
- event.Type = UIEVENT_TOUCHMOVE;
- CControlUI* pTouchTarget = m_pTouchScrollContainer != NULL
- ? static_cast<CControlUI*>(m_pTouchScrollContainer) : m_pEventTouch;
- event.pSender = pTouchTarget;
- // 滚动条:触点同样收进滚动条 rect(与 Begin 保持一致,避免首帧跳变)。
- event.ptMouse = (m_pEventTouch->GetInterface(DUI_CTR_SCROLLBAR) != NULL)
- ? Touch_SnapIntoScrollBar(m_pEventTouch, pt) : pt;
- event.dwTimestamp = ::GetTickCount();
- event.wKeyState = MapKeyState();
- event.wParam = (WPARAM)(int)dx;
- event.lParam = (LPARAM)(int)dy;
- pTouchTarget->Event(event);
- // ------------------------------------------------------------------
- // [对标 nim_duilib] 竖向滑动 → 合成 WM_MOUSEWHEEL。
- //
- // 为什么需要它:真实鼠标"滚动"靠 WM_MOUSEWHEEL 驱动,而触摸只有位移增量。
- // 只处理 TOUCHMOVE 的容器虽然能 1:1 跟手平移,但那些只认滚轮的控件
- // (如挂在窗口上的通用滚动、以及部分不认 TOUCHMOVE 的自定义控件)
- // 在触摸下永远滚不动 —— Win7 之所以"感觉正常",是因为它走的是"把触摸提升成
- // 鼠标"的老栈,系统会自己补 WM_MOUSEWHEEL;Win10 走指针/触摸栈就不会补了。
- //
- // 因此这里补齐:把 Δy 累加,每满一个 WHEEL_DELTA(120) 就投一次 SCROLLWHEEL。
- // 符号按 Windows 惯例取反:手指下滑(Δy>0,内容下移)→ 负增量。
- //
- // 只在没有 1:1 跟手平移(即没有可滚容器接管)时才合成,避免"内容被平移一次、
- // 又被滚轮滚一次"的双倍滚动。命中滚动条时不合成(滑块本身有自己的拖拽语义)。
- // ------------------------------------------------------------------
- if (m_pTouchScrollContainer == NULL
- && m_pEventTouch->GetInterface(DUI_CTR_SCROLLBAR) == NULL)
- {
- if (dy != 0) m_nTouchWheelAccum -= dy;
- while (m_nTouchWheelAccum >= 120 || m_nTouchWheelAccum <= -120)
- {
- const int nNotches = m_nTouchWheelAccum / 120;
- m_nTouchWheelAccum -= nNotches * 120;
- const int zDelta = nNotches * 120;
- CControlUI* pWheel = (m_pEventTouch != NULL) ? m_pEventTouch : FindControl(pt);
- if (pWheel == NULL) break;
- TEventUI evWheel = { 0 };
- evWheel.Type = UIEVENT_SCROLLWHEEL;
- evWheel.pSender = pWheel;
- evWheel.wParam = MAKEWPARAM(zDelta < 0 ? SB_LINEDOWN : SB_LINEUP, zDelta);
- evWheel.lParam = 0;
- evWheel.ptMouse = pt;
- evWheel.wKeyState = MapKeyState();
- evWheel.dwTimestamp = ::GetTickCount();
- TouchTrace::Log(_T("SLIDE-MOVE->WHEEL control=%s zDelta=%d pt=%d,%d"),
- pWheel->GetName().GetData(), zDelta, pt.x, pt.y);
- pWheel->Event(evWheel);
- }
- }
- }
- bool CPaintManagerUI::EndTouchSlideGesture(POINT pt)
- {
- if (!m_bTouchSlideActive) return false;
- m_bTouchSlideActive = false;
- // ⚠️⚠️ 【m_pEventTouch 为空时必须直接判"不算轻点",且必须在这里就返回】⚠️⚠️
- //
- // 这里曾经是"先打完 SLIDE-END 日志、再在下面才 return false",看起来等价,
- // 实际上踩过一个真实死锁:只要 m_pEventTouch 被别处(曾经的"状态交叉"纠偏)
- // 提前清成 NULL,本函数就会带着 m_bTouchSlideActive 已置假、但对外仍报
- // "这是一次滑动"的结论返回 —— 调用方于是走 `!bTapLike` 的"consumed as slide"
- // 分支,把点击**静默吞掉**,同时也不会把手势交还给原生鼠标路径。
- // 结果:点击完全失灵(真机日志:`SLIDE-END touch=null moved=0` 紧跟
- // `POINTER-MSG UP consumed as slide (no click)`,每一个按钮都点不动)。
- //
- // 因此把这条早退路提到最前面:没有命中控件 ⇒ 语义上"没点到东西",
- // 既不是滑动也不是点击,直接给出明确结论,绝不拖到后面再决策。
- if (m_pEventTouch == NULL)
- {
- m_ptLastTouchPos = pt;
- m_pTouchScrollContainer = NULL;
- m_bTouchMoved = false;
- m_nTouchWheelAccum = 0;
- m_bSuppressHoverAfterTouch = true;
- m_bMouseTracking = false;
- // [NIM 风格] 这里不需要"解除让路"——让路已改由 m_pEventTouch 这个**标记**表达。
- TouchTrace::Log(_T("SLIDE-END no-control pt=%d,%d (not a tap)", pt.x, pt.y));
- return false;
- }
- TouchTrace::Log(_T("SLIDE-END pt=%d,%d touch=%s moved=%d"),
- pt.x, pt.y,
- m_pEventTouch->GetName().GetData(),
- (int)m_bTouchMoved);
- m_ptLastTouchPos = pt;
- // 注:`m_pEventTouch == NULL` 的早退路已提到本函数最开头(见上),
- // 到这里 m_pEventTouch 必然非空,可以安全解引用。
- ClearTouchHoverState(pt);
- // ⚠️⚠️ 【顺序是死线:先派发 TOUCHUP,再 ReleaseCapture】⚠️⚠️
- //
- // CPaintManagerUI::ReleaseCapture() 不只是调 Win32 那句 ::ReleaseCapture(),
- // 它同时把 m_bMouseCapture 置假、并把 m_bDragMode 置假(见本函数定义处)。
- // 而 CControlUI::Activate()(CButtonUI::Activate 的第一步就是调它)里有捕获保护:
- // if (!m_pManager->IsCaptured() && !(m_uButtonState & UISTATE_CAPTURED)) return false;
- // (IsCaptured() 返回的就是 m_bMouseCapture)
- //
- // 于是原来的「先 ReleaseCapture 再 TOUCHUP」会出现这样的连锁:
- // · Begin 时只发了 UIEVENT_TOUCHDOWN,没给按钮发 BUTTONDOWN
- // ⇒ 按钮的 m_uButtonState 里从来没有 UISTATE_CAPTURED;
- // · End 时先 ReleaseCapture ⇒ m_bMouseCapture 已假 ⇒ 随后调用方合成
- // UIEVENT_BUTTONUP、按钮的 `if ((m_uButtonState & UISTATE_CAPTURED) != 0)`
- // 分支因没有 CAPTURED 而不进 ⇒ Activate() 根本不会被调用;
- // · 即便被调用,CControlUI::Activate 的捕获保护也会因为两个条件都为假而 return false。
- // ⇒ **按钮的 click 通知永远发不出来**(按下有高亮、抬手没反应)。
- //
- // 把 ReleaseCapture 挪到 TOUCHUP 之后:按钮在收到 TOUCHUP 时仍处于"管理器认为有捕获"
- // 的状态;等控件侧处理完再由这里收尾释放,语义上也更正确(手势真正结束了)。
- //
- // 注意:这里移的是 **TOUCHUP 与 ReleaseCapture 的相对顺序**,
- // 不能把 ReleaseCapture 整个删掉(否则 m_bMouseCapture 与 Win32 捕获都会泄漏)。
- TEventUI event = { 0 };
- event.Type = UIEVENT_TOUCHUP;
- event.pSender = m_pEventTouch;
- // 与 Begin/Update 一致:滚动条用收敛点,保证 ptMouse 轨迹连续、位移计算不失真。
- event.ptMouse = (m_pEventTouch->GetInterface(DUI_CTR_SCROLLBAR) != NULL)
- ? Touch_SnapIntoScrollBar(m_pEventTouch, pt) : pt;
- event.dwTimestamp = ::GetTickCount();
- event.wKeyState = MapKeyState();
- m_pEventTouch->Event(event);
- // TOUCHUP 已派发完毕 ⇒ 现在才释放捕获(手势真正结束)。
- ReleaseCapture();
- // [NIM 风格] 让路无需任何"时间戳清理" —— 它现在由 m_pEventTouch 这个标记表达,
- // 下面的 `m_pEventTouch = NULL` 就是"本次 WM_TOUCH 手势结束"的全部收尾动作。
- //
- // 历史教训(保留备查,勿再引入同类写法):
- // 曾经用一个"让路时间戳"同时表示"让路中"和"指针手势计时",
- // 导致 (GetTickCount() - 该值) < 3000 恒成立 ⇒ 指针通道自己驱动的手势被自己判定成
- // "让路中" ⇒ UP 走进"只清状态、不合成点击"的早退路 ⇒ 两条点击路径同时断掉。
- // 改用标记后,判据是"m_pEventTouch 是否非空",与"WM_TOUCH 是否真的接管了手势"
- // 严格一致,不存在跨手势残留,也不需要 3 秒超时兜底。
- // 关键:手指抬起后禁止"悬浮态复活"。
- // Windows 会为触摸补发合成的 WM_MOUSEMOVE;且真实 WM_MOUSELEAVE 在 m_bMouseTracking
- // 为真时会自己给自己重发 WM_MOUSEMOVE(见 WM_MOUSELEAVE 分支)→ 刚清掉的悬浮态立刻
- // 被重新点亮。这里置抑制位 + 主动撤下鼠标跟踪,直到用户真的移动鼠标才恢复。
- m_bSuppressHoverAfterTouch = true;
- m_bMouseTracking = false;
- // ⚠️⚠️ 【必须在返回之前清掉 m_pEventTouch】—— 这是"主窗口触摸点击全灭"的元凶之一。⚠️⚠️
- //
- // 死锁链条(每一步都真实存在):
- // 1) 本函数被调用时 m_pEventTouch 已经不再需要 —— TOUCHUP 已派发、捕获已释放、
- // 悬浮态已清。它的语义是"本次触摸进行中命中的控件",手势结束就该失效。
- // 2) 但 `WM_LBUTTONUP` 的原生分支开头是:
- // if (m_pEventTouch != NULL || m_bSuppressPromotedTouchMouse) { ...; break; }
- // 这个 break **把 ReleaseCapture() 一起跳过了**。
- // 3) 于是只要 m_pEventTouch 残留非 NULL,系统随后提升出来的那条 WM_LBUTTONUP
- // 就会"只清抑制位、不释放捕获"地溜过去 ⇒ 窗口持有 capture 不放。
- // 4) 持有 capture 期间,鼠标/指针消息被强制路由到本窗口,**别处一律点不动**;
- // 且 m_bSuppressPromotedTouchMouse 被反复清成 0,WM_LBUTTONDOWN 的守卫
- // `if (m_pEventTouch != NULL ...) break;` 又继续拦 → 本窗口也点不动。
- // ⇒ 整个应用触摸点击全灭,且不会自愈(capture 没有超时机制)。
- //
- // 注意这与"轻点分支里也清了 m_pEventTouch"不冲突:那条路走的是"已合成点击",
- // 这里是手势收尾的公共清理。**两条路径都必须清**,缺一条就留下上面那个死锁。
- //
- // 清理顺序要求:必须在下面读 m_pEventClick 的 tap 判定**之前**完成 ——
- // Begin 里给滚动条命中的分支会特意不设 m_pEventClick(滚动条不做点击),
- // 所以 bTapLike 的计算只依赖 m_pEventTouch 的 rect 语义已落地(TOUCHUP 已派发),
- // 与本指针无关;但为便于阅读,统一挪到 tap 判定之前一次清干净。
- m_pEventTouch = NULL;
- // 是否按"点击"处理。默认要求本次手势没有位移,但触摸屏上手指按下时的轻微抖动
- // 超过 8px 阈值非常常见(弹框里的小按钮尤其如此)——一旦判成拖拽,这里不合成点击,
- // 而提升的 WM_LBUTTONDOWN 早已在 DOWN 阶段被拦掉,于是两条点击路径同时断掉,
- // 表现为"弹框里怎么点都没反应"。
- // 因此补一条与鼠标语义一致的判定:位移虽大,但抬起时手指仍落在【按下时命中的同一个
- // 控件】上,且该控件不在可滚动容器内(即这次位移不可能是"滚动"),同样按点击处理。
- //
- // ⚠️ 这里的 pt **必须**与 Begin 用同一个收敛规则,否则命中判定会失真:
- // 命中滚动条时 Begin 把事件点收进滚动条 rect(Touch_SnapIntoScrollBar)。
- // 如果本函数拿原始 pt 去 FindControl,一根在滚动条上但横向偏出几像素的手指
- // 会 FindControl 到滚动条旁边的列表项 ⇒ 与 m_pEventClick 不相等 ⇒ 轻点被判掉。
- // (滚动条本来就已经被下面的返回值排除在点击之外,这里只是保持两条路径一致。)
- const POINT ptTap = (m_pEventTouch != NULL
- && m_pEventTouch->GetInterface(DUI_CTR_SCROLLBAR) != NULL)
- ? Touch_SnapIntoScrollBar(m_pEventTouch, pt) : pt;
- bool bTapLike = !m_bTouchMoved;
- if (!bTapLike && m_pEventClick != NULL && m_pTouchScrollContainer == NULL
- && m_pEventClick->GetInterface(DUI_CTR_SCROLLBAR) == NULL)
- {
- if (FindControl(ptTap) == m_pEventClick) bTapLike = true;
- TouchTrace::Log(_T("SLIDE-END tap-by-same-control hit=%d control=%s"),
- (int)bTapLike, m_pEventClick->GetName().GetData());
- }
- // 清空滚轮累加余量,避免串到下一次手势造成"一按就滚"。
- m_nTouchWheelAccum = 0;
- // 返回值 = "本次手势算轻点"。调用方据此决定是否派发点击。
- return (m_pEventClick != NULL && bTapLike
- && m_pEventClick->GetInterface(DUI_CTR_SCROLLBAR) == NULL);
- }
- void CPaintManagerUI::OnTouchInput(WPARAM wParam, LPARAM lParam)
- {
- // 触屏模型(参考 NIM Duilib 的 Window::HandleMessage case WM_TOUCH,但**不可**照搬它的鼠标守卫,
- // 旧版 DuiLib 的点击状态机强依赖原生 WM_LBUTTONDOWN/UP,照搬会让点击全灭——已实测踩过):
- // 1) 独立的 m_pEventTouch 追踪"本次触摸命中控件",DOWN 设定、UP 清空;绝不复用 m_pEventClick。
- // 2) 只向控件派发“原生触摸事件” UIEVENT_TOUCHDOWN / UIEVENT_TOUCHMOVE / UIEVENT_TOUCHUP:
- // · CContainerUI 认 TOUCHMOVE → 内容 1:1 跟手平移(对标 NIM ScrollableBox);
- // · CScrollBarUI 认 TOUCHDOWN/MOVE/UP → 拖滑块 / 点轨道翻页(不依赖合成鼠标移动);
- // · 其余控件忽略触摸事件(未认领的会沿 DoEvent 冒泡给父容器)。
- // 3) 按钮"点击"由本通道的 TOUCHUP 分支依据 EndTouchSlideGesture 的 tap 判定合成
- // (位移未越阈值才算轻点)—— 见下方 for 循环里的 UP 分支。
- // ⚠️ 历史注记:更早的版本依赖"Windows 为触摸自动提升的 WM_LBUTTONDOWN/UP"
- // 去驱动点击,但那条路会把**拖动**也当成点击,且指针通道抢走手势时完全失效。
- // 现已统一由 tap 判定派发,两条通道行为一致。
- // 4) 不做任何"触摸→鼠标"归一化、不做 NIM 那种 m_pEventTouch!=NULL 通用守卫
- // (旧版 DuiLib 的点击状态机强依赖原生 WM_LBUTTONDOWN/UP,照搬守卫曾让点击全灭);
- // "轻点 vs 拖拽"只用 m_bTouchMoved 在 UP 处抑制误点。
- // 5) 触摸**不参与悬浮态**:WM_MOUSEMOVE 里用 MOUSEEVENTF_FROMTOUCH 标记把"提升出来的鼠标消息"
- // 挡在悬浮 enter/leave 之外(触摸没有"光标停留"语义)——这是"点过的控件一直高亮 /
- // 多个控件同时高亮"的根治手段;此处再叠加 m_bSuppressHoverAfterTouch 抑制抬起后的复活。
- TouchApi::PFN_GetTouchInputInfo pfnGetInfo = TouchApi::GetGetTouchInputInfo();
- // 【已移除】"把 WM_TOUCH 同步转发给模态弹框"的兜底块。
- //
- // 历史背景:假模态时期,系统会把 WM_TOUCH 投给 owner(被禁用的主窗口)而非弹框,
- // 于是这里加了一段"发现弹框存在就 SendMessage 转发过去"的补救。
- //
- // 为什么现在必须删掉:
- // 1. ShowModal 已改为真模态(DialogBoxIndirectParam,参考 nim_duilib)。系统对话框管理器
- // 自己就是触摸目标窗口,弹框直接收到 WM_TOUCH,不需要任何转发 —— 这段代码永远不会
- // 再被触发(owner 已被系统禁用,收不到消息)。
- // 2. 就算被触发也是错的:MSDN 明确 WM_TOUCH 的 lParam(HTOUCHINPUT) 经 SendMessage
- // 转发后立即失效,两边只能有一方解析并 CloseTouchInputHandle,极易造成句柄泄漏或
- // 双重关闭;且真模态期间 m_hWndPaint 属于 owner 的情况下,转发会把 owner 的触摸
- // 塞给弹框,坐标语义错位。
- // 3. 它依赖 s_hModalWindow 这个假模态时代的全局状态,与新架构的 CPaintManagerUI
- // 迁移方案(DetachPaintWindow/AttachPaintWindow)职责重叠、互相干扰。
- //
- // 因此整段删除:触摸一律由"收到消息的那个窗口自己的 manager"处理,链路单一清晰。
- if (pfnGetInfo == NULL) return;
- // 本次手势确认系统走的是 WM_TOUCH 通道 ⇒ 【本次手势内】让指针通道让路。
- // ⚠️ 注意这里是"手势级让路",不是"永久关闭指针通道"。
- // 永久关闭会害死真模态弹框:本台收银机 WM_TOUCH 的贪婪归属权卡在主窗口,
- // 弹框永远收不到 WM_TOUCH;一旦指针通道被永久关掉,弹框就彻底没有输入源。
- // 所以让路只标记"直到本次手势结束"(由 UP / 3 秒超时自动解禁)。
- //
- // 另外,若本窗口的本次手势**已经**由指针通道驱动(m_bPointerGestureActive),
- // 说明这是同一次触摸的另一条通道送来的一份重复消息 —— 整条忽略,绝不能双份驱动。
- if (m_bPointerGestureActive)
- {
- TouchTrace::Log(_T("WM_TOUCH ignored (pointer channel owns this gesture) hwnd=%p inputs=%u"),
- (void*)m_hWndPaint, (unsigned)LOWORD(wParam));
- return;
- }
- // 注意:让路时间戳**不在这里**打 —— 必须等确认这是一条有效的手势消息之后再打。
- // 曾经在函数入口就打,结果 `cInputs == 0` / GetTouchInputInfo 失败这类"空消息"
- // 也会把指针通道压住 3 秒(没有任何手势在进行,却拒绝了指针)—— 白白制造"顿一下"。
- // 标记触摸输入句柄是否已在“点击候选”分支里提前关闭(避免函数末尾重复关闭)。
- bool bTouchInputClosed = false;
- UINT cInputs = LOWORD(wParam);
- TouchTrace::Log(_T("WM_TOUCH begin inputs=%u handle=%p"), cInputs, (HANDLE)lParam);
- if (cInputs == 0)
- {
- TouchTrace::Log(_T("WM_TOUCH ignored: inputs=0"));
- return;
- }
- std::vector<TouchApi::TOUCHINPUT> inputs(cInputs);
- if (!pfnGetInfo((HANDLE)lParam, cInputs, inputs.data(), (int)sizeof(TouchApi::TOUCHINPUT)))
- {
- TouchTrace::Log(_T("WM_TOUCH GetTouchInputInfo failed error=%u"), ::GetLastError());
- return;
- }
- // 到这里确认:这是一条**有效**的 WM_TOUCH(能取到触摸点)⇒ 正式开始本次手势。
- //
- // [NIM 风格] 这里**不再**写任何"让路时间戳"。
- // 让路由下面的 DOWN 分支里 m_pEventTouch != NULL 这个**标记**表达:
- // BeginTouchSlideGesture 会置 m_pEventTouch,指针通道看到它非空就退让。
- // 标记的生命周期与"WM_TOUCH 是否真的接管了本次手势"严格一致 ——
- // 这比时间戳可靠(时间戳会因为"指针通道自己也写它"而恒真,已踩过)。
- TouchTrace::Log(_T("WM_TOUCH owns this gesture (pointer channel will see m_pEventTouch) hwnd=%p"), (void*)m_hWndPaint);
- for (UINT i = 0; i < cInputs; ++i)
- {
- TouchApi::TOUCHINPUT& ti = inputs[i];
- TouchTrace::Log(_T("WM_TOUCH input[%u] id=%u flags=0x%08X screen=%d,%d"),
- i, ti.dwID, ti.dwFlags, TOUCH_COORD_TO_PIXEL(ti.x), TOUCH_COORD_TO_PIXEL(ti.y));
- POINT pt;
- pt.x = TOUCH_COORD_TO_PIXEL(ti.x);
- pt.y = TOUCH_COORD_TO_PIXEL(ti.y);
- ::ScreenToClient(m_hWndPaint, &pt);
- if (ti.dwFlags & TouchApi::TOUCHEVTFLAG_DOWN)
- {
- TouchTrace::Log(_T("TOUCHDOWN begin id=%u client=%d,%d"), ti.dwID, pt.x, pt.y);
- // 这里【不再】用 `if (m_pEventClick != NULL) break;` 做通用守卫:Windows 会在 WM_TOUCH
- // 前后补发合成的 WM_LBUTTONDOWN(同样设置 m_pEventClick),通用守卫会让 m_pEventTouch
- // 永不设置 → 容器收不到 TOUCHMOVE,列表滚不动。
- //
- // 滚动条也不再“交回原生鼠标”:实测部分触摸收银机在手指拖拽期间【不补发合成的
- // WM_MOUSEMOVE】,导致滑块拖不动。现在改为由 WM_TOUCH 直接驱动——CScrollBarUI 已把
- // TOUCHDOWN/MOVE/UP 与鼠标事件等价处理,这里让滚动条走常规接管流程,
- // 后续 TOUCHMOVE 由它消费(不会冒泡到容器把内容滚走)。
- ::SetFocus(m_hWndPaint);
- // [通道无关] 滑动状态机的建立逻辑已抽到 BeginTouchSlideGesture,
- // 指针通道(OnPointerInput)也调用同一个函数,保证两条通道滑动行为一致。
- BeginTouchSlideGesture(pt);
- }
- else if (ti.dwFlags & TouchApi::TOUCHEVTFLAG_MOVE)
- {
- TouchTrace::Log(_T("TOUCHMOVE begin id=%u client=%d,%d active=%s scrollContainer=%p"),
- ti.dwID, pt.x, pt.y,
- m_pEventTouch != NULL ? m_pEventTouch->GetName().GetData() : _T("null"),
- (void*)m_pTouchScrollContainer);
- // 【不要】在这里对滚动条让路。滚动条现在完全由触摸事件驱动:Begin 已把 m_pEventTouch
- // 设为"滚动条自身"(不再交回原生鼠标),CScrollBarUI 也已把 TOUCHDOWN/MOVE/UP 与鼠标
- // 事件等价处理。若此处仍 `m_pEventClick->GetInterface("ScrollBar") != NULL` 就 break,
- // TOUCHMOVE 永远到不了滚动条 → 滑块拖不动(历史症状)。滚动条收到 TOUCHMOVE 会自行消费
- // (CScrollBarUI::DoEvent 命中已捕获分支后 return true),不会冒泡到容器把内容滚走。
- //
- // [通道无关] 去重 / Δ 计算 / 派发 TOUCHMOVE / 合成滚轮 已全部抽到
- // UpdateTouchSlideGesture,指针通道也调用同一个函数。
- UpdateTouchSlideGesture(pt);
- }
- else if (ti.dwFlags & TouchApi::TOUCHEVTFLAG_UP)
- {
- TouchTrace::Log(_T("TOUCHUP begin id=%u client=%d,%d active=%s moved=%d"),
- ti.dwID, pt.x, pt.y,
- m_pEventTouch != NULL ? m_pEventTouch->GetName().GetData() : _T("null"),
- (int)m_bTouchMoved);
- // [通道无关] 收尾逻辑(派发 TOUCHUP、清悬浮态、让路解禁、tap 判定)已抽到
- // EndTouchSlideGesture;指针通道也调用同一个函数。
- //
- // 返回值 = "本次手势算一次轻点"(未越过拖拽阈值,或抬起时仍落在同一控件上)。
- // 为真时这里合成 BUTTONDOWN/BUTTONUP ——
- // 这是与 WM_TOUCH 时代完全一致的行为:点击由触摸链统一判定与派发,
- // 而不是让系统提升出来的鼠标消息去触发(那条路会把拖拽也算成点击)。
- CControlUI* pClick = m_pEventClick;
- const bool bTapLike = EndTouchSlideGesture(pt);
- // [与指针通道同构的语义修正] 本次手势若根本没有"可滑动的对象"
- // (没命中可滚容器、也没命中滚动条),那 8px 阈值判出来的"拖动"
- // 在物理上不可能代表滚动 —— 用户只是按得抖了一点。
- // 此时必须仍按点击处理,否则表现为"按钮点不动"。
- // 判定必须放在 EndTouchSlideGesture 之后(它在内部才定下终值)。
- bool bTapLikeFinal = bTapLike;
- if (!bTapLikeFinal && m_pEventClick != NULL
- && m_pTouchScrollContainer == NULL
- && m_pEventClick->GetInterface(DUI_CTR_SCROLLBAR) == NULL)
- {
- TouchTrace::Log(_T("TOUCHUP no-scrollable-target -> treat as tap %s cls=%s moved=%d"),
- m_pEventClick->GetName().GetData(), m_pEventClick->GetClass(), (int)m_bTouchMoved);
- bTapLikeFinal = true;
- }
- if (bTapLikeFinal && pClick != NULL)
- {
- m_pEventClick = NULL;
- m_pTouchScrollContainer = NULL;
- m_pEventTouch = NULL;
- // 本次已合成点击 ⇒ 屏蔽随后提升的鼠标消息(避免同一次触摸触发两次)。
- m_bSuppressPromotedTouchMouse = true;
- m_dwSuppressTick = ::GetTickCount();
- // 句柄经本窗口消费完毕即关闭(MSDN:句柄转交/关闭后即失效)。
- TouchApi::PFN_CloseTouchInputHandle pfnCloseClick = TouchApi::GetCloseTouchInputHandle();
- if (pfnCloseClick != NULL)
- {
- pfnCloseClick((HANDLE)lParam);
- bTouchInputClosed = true;
- }
- TouchTrace::Log(_T("TOUCHUP click-candidate %s cls=%s pt=%d,%d"),
- pClick->GetName().GetData(), pClick->GetClass(), pt.x, pt.y);
- // 【顺序很关键】本次触摸的状态已在上面收干净,然后才派发点击事件。
- // 因为下面的 pClick->Event(BUTTONUP) 是同步回调,业务层会在里面 ShowModal()
- // 开出嵌套消息循环 —— 若把复位放在事件之后,整个弹框存活期间本窗口的触摸状态机
- // 都被冻在"TOUCHUP 处理中",提升的鼠标点击又会被 MessageHandler 的守卫吞掉
- // →"弹框开着时主窗口完全点不动";触摸输入句柄也会一直占着不释放。
- TEventUI clickEvent = { 0 };
- clickEvent.Type = UIEVENT_BUTTONDOWN;
- clickEvent.pSender = pClick;
- clickEvent.ptMouse = pt;
- clickEvent.dwTimestamp = ::GetTickCount();
- clickEvent.wKeyState = MapKeyState();
- pClick->Event(clickEvent);
- clickEvent.Type = UIEVENT_BUTTONUP;
- pClick->Event(clickEvent);
- }
- else
- {
- // 拖动 / 命中滚动条 / 未命中控件 ⇒ 丢弃候选点击。
- //
- // 这里【不】置 m_bSuppressPromotedTouchMouse,与"轻点"分支刚好相反 ——
- // 两个分支的语义别搞混:
- // · 轻点分支:**我们**已经合成了 BUTTONDOWN/BUTTONUP ⇒ 必须屏蔽随后
- // 被系统提升出来的鼠标点击,否则同一次触摸会触发两次。
- // · 本分支:我们没有合成任何点击 ⇒ 要放行提升出来的鼠标消息,
- // 否则会踩到两个历史问题 ——
- // (a) 触摸拖拽结束后按钮卡在按下态(PUSHED 状态没人来清);
- // (b) "弹框里点不动"(弹框的点击依赖那条提升消息)。
- // WM_LBUTTONUP 原生分支本身已有 bTouchDragged 保护
- // (ptMouse 置 (-1,-1) 让它命中测试失败),所以放行是安全的:
- // 它会清掉按下态,但不会 Activate 控件 —— 这正是"滑动不误触发点击"。
- m_pEventClick = NULL;
- m_pTouchScrollContainer = NULL;
- m_bSuppressPromotedTouchMouse = false;
- m_dwSuppressTick = 0;
- TouchTrace::Log(_T("TOUCHUP drop-candidate moved=%d"), (int)m_bTouchMoved);
- }
- TouchTrace::Log(_T("TOUCHUP end synthesized=%d suppress=%d"),
- (int)bTapLikeFinal, (int)m_bSuppressPromotedTouchMouse);
- }
- }
- // 收尾:若上面“点击候选”分支已经把句柄消费掉了(bTouchInputClosed),这里不能再关一次。
- TouchApi::PFN_CloseTouchInputHandle pfnClose = TouchApi::GetCloseTouchInputHandle();
- if (!bTouchInputClosed && pfnClose != NULL)
- pfnClose((HANDLE)lParam);
- }
- // ---------------------------------------------------------------------------
- // WM_POINTER* 处理 —— Win8+ 的另一条触摸/笔输入通道。
- //
- // 参考 nim_duilib NativeWindow_Windows::OnPointerMsgs()。
- //
- // 策略与 nim_duilib 一致:**归一化成鼠标消息**,直接喂给现有的鼠标处理链,
- // 从而不必维护"原生鼠标 / 触摸提升鼠标 / 指针"三套并行的点击状态机。
- // 具体:
- // WM_POINTERDOWN → WM_LBUTTONDOWN
- // WM_POINTERUPDATE → WM_MOUSEMOVE
- // WM_POINTERUP → WM_LBUTTONUP
- // WM_POINTERWHEEL → 滚轮
- //
- // 只处理主指针(IS_POINTER_PRIMARY_WPARAM):多指时其余手指一律忽略,
- // 避免多点同时驱动同一个点击状态机导致互相打断。
- //
- // ⚠️ 但 **UP 例外**:实测本台收银机上 WM_POINTERUP 的 wParam 高位不是真正的
- // POINTER_INFO 位域(HIWORD=0x6000,primary 位读到 0),照字面判定会把抬起丢掉,
- // 结果"按下有反应、永远不触发点击",同时让路状态也解不开。
- // 所以 UP 不做 primary 过滤,改用 m_dwActivePointerId 精确配对。
- //
- // 返回值语义:true = 已处理(调用方应 return true 阻止默认处理)。
- //
- // 【不变量】任何路径都**不得**让 m_bPointerGestureActive 悬空为 true:
- // · 它是"本次手势归指针管"的独占标记,为真期间 OnTouchInput 会忽略 WM_TOUCH;
- // · 只有 UP / CAPTURECHANGED / 3 秒超时能把它清掉;
- // · 所以 UP 在这条链路上必须走完全程(可以"只清状态不派发点击",但不能被 return true 截住)。
- // ---------------------------------------------------------------------------
- bool CPaintManagerUI::OnPointerInput(UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- using namespace PointerMsg;
- const DWORD dwPointerId = (DWORD)LOWORD(wParam);
- // ---- [NIM 风格] 通道互斥:本次手势已被 WM_TOUCH 接管,指针整段安静 ----
- //
- // 照搬 nim_duilib Window.cpp 的判据(**不看时间戳,只看归属标记**):
- //
- // WM_TOUCH DOWN: if (m_pEventClick != NULL || m_pEventPointer != NULL) break;
- // WM_POINTER DOWN: if (m_pEventClick != NULL || m_pEventTouch != NULL) { handled=true; break; }
- //
- // 即"谁先建立了占用标记,另一条通道就退让",由手势状态天然驱动、无需计时器。
- // 本项目的对应标记:
- // · m_pEventTouch ← 触摸链的占用标记(BeginTouchSlideGesture 里置)
- // · m_bPointerGestureActive ← 指针链的占用标记(下面 DOWN 分支里置)
- //
- // ⚠️⚠️ 【UP 必须放行,绝不能在这里 return true】⚠️⚠️
- // 这是实测踩过的坑,后果极严重(主窗口触摸点击全灭):
- // WM_POINTERUP 是**唯一**能把 m_bPointerGestureActive 归零的消息。
- // 早期版本把 DOWN/UPDATE/UP 不加区分地一起吞掉,于是:
- // 1) 用户第一次点击:WM_TOUCH 先到 ⇒ 这里把 UP 吃掉
- // ⇒ m_bPointerGestureActive 永久停在 true(上一次手势的残留值);
- // 2) 之后每次触摸的 WM_POINTERDOWN 都被当成"重复按下"挡掉
- // ⇒ 再也不产生 POINTER-MSG DOWN ⇒ 该窗口彻底聋掉。
- // 正确做法:让路只压住"驱动点击"的两个消息(DOWN/UPDATE),UP 一律放行 ——
- // 放行后**只做状态清理、不派发 WM_LBUTTONUP**(本次点击本来就不归指针管)。
- //
- // ⚠️ 与上一版的关键差异:这里用 `m_pEventTouch != NULL` 而非时间戳。
- // m_pEventTouch 的语义是"WM_TOUCH 手势进行中命中的控件",
- // 它在 BeginTouchSlideGesture 里置、在 EndTouchSlideGesture 里清,
- // 生命周期与"WM_TOUCH 是否真的接管了本次手势"严格一致 —— 这正是我们想要的。
- // 用时间戳的旧写法会因"指针通道自己也写它"而恒真,导致点击全灭(已踩过)。
- //
- // ⚠️⚠️ 【两条标记同时为真是**正常**路径,绝不能"清洗"它】⚠️⚠️
- //
- // 曾经在这里写过一段"状态交叉"纠偏:发现 m_pEventTouch != NULL 且
- // m_bPointerGestureActive 为真,就认定异常,把 m_pEventTouch 置回 NULL。
- // 那是个**严重的误判**,直接造成"整个应用什么都点不动"(真机日志可复现):
- //
- // 指针通道的 DOWN 分支会调 BeginTouchSlideGesture,而该函数**本来就会**
- // 把 m_pEventTouch 置成命中控件 —— 也就是说"指针手势进行中 + m_pEventTouch 非空"
- // 是每一次指针手势的**标准状态**,不是异常。
- // 于是那段"纠偏"在第一条 UPDATE 上就把 m_pEventTouch 抹成 NULL:
- // · UpdateTouchSlideGesture 里 `if (m_pEventTouch == NULL) return;`
- // ⇒ 位移信息全丢,永远不产生 TOUCHMOVE ⇒ 滑不动;
- // · EndTouchSlideGesture 里 `if (m_pEventTouch == NULL)` 走早退路
- // ⇒ 返回 false = "不算轻点" ⇒ 点击被静默吞掉 ⇒ 什么都点不动。
- //
- // [NIM] 对照:NIM 的两条通道标记(m_pEventTouch / m_pEventPointer)是**并列**的,
- // 互斥只表达在"谁先占、另一方就退让"那一步,NIM 从不事后清洗标记。
- // 本项目的互斥同理:靠下面 bYieldToTouchChannel 的**入口快照**表达即可,
- // 不需要、也不能有任何"把另一个标记清掉"的动作。
- const bool bYieldToTouchChannel = (m_pEventTouch != NULL) && (!m_bPointerGestureActive);
- if (bYieldToTouchChannel
- && (uMsg == DUI_WM_POINTERDOWN || uMsg == DUI_WM_POINTERUPDATE))
- {
- TouchTrace::Log(_T("POINTER-MSG silent (yield to WM_TOUCH) msg=0x%04X w=0x%08X"), uMsg, (unsigned)wParam);
- return true; // 吃掉,不驱动点击;也不 return false,免得被系统再转成鼠标消息
- }
- if (bYieldToTouchChannel && uMsg == DUI_WM_POINTERUP)
- {
- // 让路期间的 UP:放行到下面的 switch,由 DUI_WM_POINTERUP 分支只清状态、不派发点击。
- TouchTrace::Log(_T("POINTER-MSG UP passes through while yielding (state cleanup only) w=0x%08X"), (unsigned)wParam);
- }
- // 下面各分支统一用这个名字(保持旧代码可读性)
- const bool bPointerYieldedThisGesture = bYieldToTouchChannel;
- // ---- [NIM 风格] pointerId 配对:只让"发起本次手势的那根手指"驱动状态机 ----
- //
- // nim_duilib 的 OnPointerMsgs 里对应的写法是:
- // if (!IS_POINTER_PRIMARY_WPARAM(wParam)) { handled = true; break; }
- // 本项目**不能照抄**这一句 —— 真机实测该位域在这台机器上错位(见 PointerMsg::
- // IsPrimaryPointer 处的大段注释),UPDATE 会被全部误杀,症状是"能点不能滑"。
- //
- // 因此改用**唯一可靠的判据:pointerId**(wParam 低 16 位,日志里形如 0x0CC7=3271)。
- // 语义:"只有当初(或在让路解禁后补建时)记下的那根手指,才有资格驱动 UPDATE/UP。"
- //
- // ⚠️ 豁免条件(否则会踩真实死锁):
- // (a) UP 必须无条件放行 —— 它是**唯一**能清 m_bPointerGestureActive 的消息,
- // 丢了它手势状态会永久悬空(3 秒超时只是兜底,体验上是"点一下卡三秒")。
- // (b) UPDATE 在"没有活跃指针手势"时必须放行 —— 此时没有 id 可比对,
- // 而且它正是"DOWN 被系统丢了、只有 UPDATE 能救回来"的那条自愈路径;
- // 下面的 UPDATE 分支本身有 `if (m_bTouchSlideActive)` 守卫,空转是安全的。
- if (m_bPointerGestureActive && dwPointerId != m_dwActivePointerId
- && uMsg != DUI_WM_POINTERUP)
- {
- TouchTrace::Log(_T("POINTER-MSG foreign-pointer ignored msg=0x%04X id=%u active=%u"),
- uMsg, (unsigned)dwPointerId, (unsigned)m_dwActivePointerId);
- return true;
- }
- // 非坐标类指针消息(没有 pointerId 语义),直接交回系统,绝不吞。
- if (uMsg == DUI_WM_POINTERCAPTURECHANGED || uMsg == DUI_WM_POINTERLEAVE)
- return true;
- // 坐标:WM_POINTER* 的 lParam 是**屏幕坐标**(与 WM_TOUCH 一致),需转客户区。
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- ::ScreenToClient(m_hWndPaint, &pt);
- switch (uMsg)
- {
- case DUI_WM_POINTERDOWN:
- {
- TouchTrace::Log(_T("POINTER-MSG DOWN hwnd=%p pt=%d,%d id=%u"), (void*)m_hWndPaint, pt.x, pt.y, (unsigned)dwPointerId);
- // 记下"本次手势由指针通道驱动":随后同一次触摸的 WM_TOUCH 会被 OnTouchInput 忽略。
- m_bPointerGestureActive = true;
- m_dwActivePointerId = dwPointerId;
- // 计时基准(**仅**供 3 秒超时自愈用,不参与任何归属判定)。
- m_dwPointerDownTick = ::GetTickCount();
- // ⚠️⚠️ 【这里绝对不能写"让路时间戳"这类全局归属字段】⚠️⚠️
- //
- // 早先这里写的是"同时刷新让路时间戳,作为指针手势进行中的计时基准" ——
- // 那是个语义污染,直接造成"主窗口触摸点击全灭"(真机日志可复现):
- // 指针通道自己 DOWN 时把时间戳写成当前时刻 ⇒ 之后同一手势内
- // `(GetTickCount() - 该值) < 3000` 恒成立 ⇒ 让路判定恒真
- // ⇒ UP 走"只清状态、绝不合成点击"的早退路 ⇒ 点击两条路全断。
- // (日志特征:yield=1 active=1 slide=1 moved=0 + 没有 click-candidate)
- //
- // [NIM 风格] 现在归属完全由 m_bPointerGestureActive 这个布尔标记表达,
- // 超时自愈由专门的 m_dwPointerDownTick 计时,两者职责分离、互不干扰。
- // 与 WM_LBUTTONDOWN 分支一致:先把焦点给本窗口,否则输入法/键盘焦点
- // 可能仍停在别的窗口上(真模态下更要注意别把焦点让给 owner)。
- if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
- // ---- [通道无关滑动] 指针通道也要建立滑动状态机 ----
- // 这是"滑不动"的根治点。原先滑动逻辑只写在 WM_TOUCH 通道里,指针通道一旦
- // 抢到手势就完全没有滑动能力,只会在 UP 时把整个手势当一次点击。
- // 现在两条通道共用 BeginTouchSlideGesture:命中控件、找可滚容器、抓滚动条滑块
- // 全部照做,后续 UPDATE 才有 m_pTouchScrollContainer 可用。
- //
- // 该函数内部已完成原 DOWN 分支需要的一切:FindControl、SetFocus、SetCapture、
- // 建立候选点击 m_pEventClick、以及给滚动条派发 UIEVENT_TOUCHDOWN。
- // 所以**不再**调用 MessageHandler(WM_LBUTTONDOWN) ——
- // 那条原生路径会在抬手时直接 Activate 控件("滑动也触发点击"的元凶),
- // 而点击已改由 UP 分支经 tap 判定统一派发。
- BeginTouchSlideGesture(pt);
- return true;
- }
- case DUI_WM_POINTERUPDATE:
- {
- // [通道无关滑动] 指针的位移同样走共享滑动逻辑:
- // 去重 / 置 m_bTouchMoved / 派发 UIEVENT_TOUCHMOVE 给容器或滚动条 / 合成滚轮。
- // 这样指针通道下的滑动行为与 WM_TOUCH 通道**完全一致**。
- //
- // ⚠️ 这里**不再**额外喂 WM_MOUSEMOVE 给原生鼠标路径。
- // 原因:本次手势已由触摸语义接管(m_bTouchSlideActive 为真),原生鼠标路径
- // 只会带来副作用而没有任何收益 ——
- // · 它会走 m_bDragMode / 拖放、并重建 HOT 悬浮态(触摸没有悬浮语义);
- // · 它还会让"提升出来的鼠标移动"参与 m_bTouchMoved 的二次判定,与
- // UpdateTouchSlideGesture 的判定重复。
- // 而控件真正需要的移动语义,已被 UIEVENT_TOUCHMOVE(容器/滚动条)覆盖;
- // 其余控件对"拖动中移动"本来就不关心(只认按下与抬起)。
- // 需要鼠标侧更新的场景(真实鼠标)不经这条分支,所以不受影响。
- if (m_bTouchSlideActive)
- {
- UpdateTouchSlideGesture(pt);
- // 手势进行中:这条指针消息由我们完全接管,不再回落。
- return true;
- }
- // 没有活跃的触摸滑动状态(例如 Begin 因 FindControl 落空而空转):
- // 退化为纯粹的鼠标移动语义,交给原生路径。
- LRESULT lRes = 0;
- if (MessageHandler(WM_MOUSEMOVE, MK_LBUTTON, MAKELPARAM(pt.x, pt.y), lRes))
- return true;
- return false;
- }
- case DUI_WM_POINTERUP:
- {
- TouchTrace::Log(_T("POINTER-MSG UP hwnd=%p pt=%d,%d id=%u yield=%d active=%d slide=%d"),
- (void*)m_hWndPaint, pt.x, pt.y, (unsigned)dwPointerId,
- (int)bPointerYieldedThisGesture, (int)m_bPointerGestureActive, (int)m_bTouchSlideActive);
- // 手势结束:立刻解除"指针驱动中",让下一个手势可以重新选择通道。
- m_bPointerGestureActive = false;
- m_dwActivePointerId = 0;
- m_dwPointerDownTick = 0;
- // 让路期间的 UP 只用来"解死锁":本次点击归 WM_TOUCH 管,指针侧绝不能
- // 再补一个 WM_LBUTTONUP —— 否则会把 WM_TOUCH 路径建立的 m_pEventClick 提前
- // 清掉(产生"按下有反应、点击不触发"),甚至给同一次触摸补出第二个 BUTTONUP。
- if (bPointerYieldedThisGesture)
- {
- // 让路中不应有活跃的滑动状态;万一有(两通道交叉),也要收掉,
- // 否则 m_bTouchSlideActive 会残留到下一次手势,导致 Update 算错 Δ。
- TouchTrace::Log(_T("POINTER-MSG UP yielded-path (no click synthesized) pt=%d,%d slide=%d"),
- pt.x, pt.y, (int)m_bTouchSlideActive);
- if (m_bTouchSlideActive) EndTouchSlideGesture(pt);
- return false;
- }
- // [通道无关滑动] 先走共享收尾:派发 TOUCHUP 给滚动条/容器、清悬浮态、让路解禁、
- // 以及最重要的 tap 判定。返回值 = "本次手势算一次轻点"。
- //
- // ⚠️ 为什么这里必须**自己**派发点击、而不能交给 MessageHandler(WM_LBUTTONUP):
- // DOWN 分支调 BeginTouchSlideGesture 后 m_pEventTouch 已被置成命中控件,
- // 这会让原生的 WM_LBUTTONDOWN 提前 break、**不建立 m_pEventClick**。
- // 而原生 WM_LBUTTONUP 恰恰依赖 m_pEventClick(见该分支的 `if (pClick == NULL) break;`)
- // ⇒ 走原生路径永远点不出来。所以必须与 OnTouchInput 的 TOUCHUP 分支一样,
- // 在这里自己合成 BUTTONDOWN/BUTTONUP。
- CControlUI* pClick = m_pEventClick;
- const bool bTapLike = EndTouchSlideGesture(pt);
- // ⚠️⚠️ 【关键:本次手势根本没有"可滑动的对象"时,绝不能吞点击】⚠️⚠️
- //
- // 真机日志暴露过一种极其常见的形态(几乎是主窗口上每一次触摸):
- // SLIDE-BEGIN hit=main_shangpin cls=OptionUI scrollbar=0 scrollContainer=00000000
- // 即:命中了控件,但该控件**不在任何可滚容器里**、也没命中滚动条。
- // 这种情况下本次手势【在物理上不可能产生滚动】—— 没有对象可滚。
- //
- // 而 EndTouchSlideGesture 的 tap 判定里有一条:
- // bTapLike = !m_bTouchMoved;
- // 只要位移超过 8px,bTapLike 就为假,于是走进下面的 "consumed as slide" 分支
- // 把点击吃掉 —— 可是根本没有东西在滚,用户看到的现象就是
- // **"我明明点了按钮,却一点反应都没有"**(真实日志里 btn_*_pay 全部如此)。
- //
- // 因此这里补一条语义修正:没有可滚对象 ⇒ 本次手势的位移不构成"滑动",
- // 无条件按点击处理。注意必须放在 EndTouchSlideGesture **之后**判断,
- // 因为它内部才会把 m_pTouchScrollContainer 的终值确定下来。
- //
- // 反过来说,只要有可滚容器或命中滚动条,就仍然严格按 tap 阈值判定,
- // 不会把"滚动列表时手指必然产生的位移"误当成点击。
- bool bTapLikeFinal = bTapLike;
- if (!bTapLikeFinal && m_pEventClick != NULL
- && m_pTouchScrollContainer == NULL
- && m_pEventClick->GetInterface(DUI_CTR_SCROLLBAR) == NULL)
- {
- TouchTrace::Log(_T("POINTER-MSG UP no-scrollable-target -> treat as tap %s cls=%s moved=%d"),
- m_pEventClick->GetName().GetData(), m_pEventClick->GetClass(), (int)m_bTouchMoved);
- bTapLikeFinal = true;
- }
- if (!bTapLikeFinal || pClick == NULL)
- {
- // 滑动过 / 没命中控件 / 命中的是滚动条 ⇒ 绝不能触发点击。
- // 这正是"滑动反倒触发点击"的根治点:吃掉这条 UP,不让它作为
- // 鼠标抬起去 Activate 控件。
- TouchTrace::Log(_T("POINTER-MSG UP consumed as slide (no click) pt=%d,%d"), pt.x, pt.y);
- m_pEventClick = NULL;
- m_pEventTouch = NULL;
- m_pTouchScrollContainer = NULL;
- m_bSuppressPromotedTouchMouse = false;
- m_dwSuppressTick = 0;
- return true;
- }
- // 轻点:合成点击,与 WM_TOUCH 通道完全一致。
- m_pEventClick = NULL;
- m_pTouchScrollContainer = NULL;
- m_pEventTouch = NULL;
- // 已合成点击 ⇒ 屏蔽随后被系统提升出来的鼠标点击,避免同一次触摸触发两次。
- m_bSuppressPromotedTouchMouse = true;
- m_dwSuppressTick = ::GetTickCount();
- TouchTrace::Log(_T("POINTER-MSG UP click-candidate %s cls=%s pt=%d,%d"),
- pClick->GetName().GetData(), pClick->GetClass(), pt.x, pt.y);
- // 【顺序很关键】状态已收干净,然后才派发点击事件(回调里可能 ShowModal 开嵌套循环)。
- TEventUI clickEvent = { 0 };
- clickEvent.Type = UIEVENT_BUTTONDOWN;
- clickEvent.pSender = pClick;
- clickEvent.ptMouse = pt;
- clickEvent.dwTimestamp = ::GetTickCount();
- clickEvent.wKeyState = MapKeyState();
- pClick->Event(clickEvent);
- clickEvent.Type = UIEVENT_BUTTONUP;
- pClick->Event(clickEvent);
- // 已消费:不再回落 MessageHandler,避免又走一遍原生鼠标抬起路径。
- return true;
- }
- case DUI_WM_POINTERWHEEL:
- {
- // wParam 高位是滚轮增量(与 WM_MOUSEWHEEL 一致)
- const short zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
- TouchTrace::Log(_T("POINTER-MSG WHEEL hwnd=%p delta=%d"), (void*)m_hWndPaint, (int)zDelta);
- LRESULT lRes = 0;
- if (MessageHandler(WM_MOUSEWHEEL, MAKEWPARAM(0, zDelta), MAKELPARAM(pt.x, pt.y), lRes))
- return true;
- return false;
- }
- case DUI_WM_POINTERLEAVE:
- // 指针离开窗口:清悬浮态(触摸本无悬浮语义,这里只做兜底清理)
- ClearTouchHoverState(pt);
- return true;
- case DUI_WM_POINTERCAPTURECHANGED:
- // 捕获被系统夺走(典型:手势被中断、窗口失活)。
- // nim_duilib 注释:若不设置 bHandled,系统会把它转成 WM_BUTTON 类消息 ——
- // 我们这里明确吃掉并复位状态,避免状态残留导致后续点击全被吞。
- TouchTrace::Log(_T("POINTER-MSG CAPTURECHANGED hwnd=%p"), (void*)m_hWndPaint);
- m_bPointerGestureActive = false;
- m_dwActivePointerId = 0;
- m_dwPointerDownTick = 0;
- m_pEventTouch = NULL;
- m_pTouchScrollContainer = NULL;
- m_bTouchMoved = false;
- m_bSuppressPromotedTouchMouse = false;
- m_dwTouchDownTick = 0;
- m_dwSuppressTick = 0;
- m_nTouchWheelAccum = 0;
- // [通道无关滑动] 滑动状态机也必须一起收掉 —— 否则 m_bTouchSlideActive 残留为真,
- // 下一次手势若走 WM_TOUCH,Update 会误用上一次的 m_ptLastTouchPos 算出巨大 Δ,
- // 表现为"一按下内容就猛地跳一段"。
- m_bTouchSlideActive = false;
- return true;
- default:
- break;
- }
- // POINTERENTER / POINTERACTIVATE / POINTERHWHEEL 等:交回默认处理。
- return false;
- }
- // ---------------------------------------------------------------------------
- // [WM_TOUCH 归属权] "补调 DefWindowProc" 钩子。
- //
- // 背景(详见 UIManager.h 里的长注释):WM_TOUCH 不是被系统直接投递的,
- // 而是由 DefWindowProc 在收到一条"特殊标记的 WM_MOUSEMOVE / WM_INPUT"时
- // 才"翻译"并派发出来。所以只要有一层吞掉了那条标记消息且不回落到
- // DefWindowProc,该窗口后续就再也收不到 WM_TOUCH。
- //
- // 本库的 MessageHandler 为了不让"触摸提升出来的鼠标移动"污染拖拽/悬浮状态机,
- // 会在两个地方吞掉它(均 return true)—— 这正是要补调的原因。
- // 补调只做"副作用",我们仍然吃掉消息,DuiLib 的状态机不受影响。
- // ---------------------------------------------------------------------------
- void CPaintManagerUI::SetDefProcRelay(PFN_RELAY_DEFPROC pfnRelay, HWND hWndTarget)
- {
- m_pfnDefProcRelay = pfnRelay;
- m_hWndDefProcRelay = hWndTarget;
- TouchTrace::Log(_T("DEFPROC-RELAY set manager=%p hwnd=%p pfn=%p enabled=%d"),
- (void*)this, (void*)hWndTarget, (void*)pfnRelay, (int)m_bRelayDefProcForTouchMove);
- }
- void CPaintManagerUI::ClearDefProcRelay(HWND hWndTarget)
- {
- // 只在"绑定的窗口确实是这个"时清,避免嵌套弹框里把新弹框的钩子误清掉。
- if (hWndTarget == NULL || m_hWndDefProcRelay == hWndTarget)
- {
- if (m_pfnDefProcRelay != NULL)
- {
- TouchTrace::Log(_T("DEFPROC-RELAY clear manager=%p hwnd=%p pfn=%p relayed=%u"),
- (void*)this, (void*)m_hWndDefProcRelay, (void*)m_pfnDefProcRelay, m_uDefProcRelayCount);
- }
- m_pfnDefProcRelay = NULL;
- m_hWndDefProcRelay = NULL;
- }
- }
- void CPaintManagerUI::RelayDefWindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam, LPCTSTR pszWhy)
- {
- if (!m_bRelayDefProcForTouchMove) return;
- if (m_pfnDefProcRelay == NULL || m_hWndPaint == NULL) return;
- // 绑定窗口已变(如真模态换 HWND):钩子失效,别再调用悬垂的函数指针。
- if (m_hWndDefProcRelay != NULL && m_hWndDefProcRelay != m_hWndPaint)
- {
- TouchTrace::Log(_T("DEFPROC-RELAY skipped: bound=%p cur=%p why=%s"),
- (void*)m_hWndDefProcRelay, (void*)m_hWndPaint, pszWhy != NULL ? pszWhy : _T("?"));
- return;
- }
- ++m_uDefProcRelayCount;
- // 每 20 次打一条,避免日志被刷爆(触摸移动消息很密集)。
- const bool bLog = (m_uDefProcRelayCount <= 5) || (m_uDefProcRelayCount % 20 == 0);
- if (bLog)
- {
- TouchTrace::Log(_T("DEFPROC-RELAY call #%u msg=0x%04X hwnd=%p lParam=%d,%d why=%s"),
- m_uDefProcRelayCount, uMsg, (void*)m_hWndPaint,
- (int)GET_X_LPARAM(lParam), (int)GET_Y_LPARAM(lParam),
- pszWhy != NULL ? pszWhy : _T("?"));
- }
- // 只关心副作用(让系统的 WM_TOUCH 翻译链继续跑),返回值丢弃。
- (void)m_pfnDefProcRelay(m_hWndPaint, uMsg, wParam, lParam);
- }
- void CPaintManagerUI::ClearTouchHoverState(POINT pt)
- {
- TouchTrace::Log(_T("CLEAR-HOVER pt=%d,%d hover=%s leaveCount=%d"),
- pt.x, pt.y,
- m_pEventHover != NULL ? m_pEventHover->GetName().GetData() : _T("null"),
- m_aNeedMouseLeaveNeeded.GetSize());
- TEventUI event = { 0 };
- event.Type = UIEVENT_MOUSELEAVE;
- event.ptMouse = pt;
- event.wKeyState = MapKeyState();
- event.dwTimestamp = ::GetTickCount();
- CStdPtrArray controls(m_aNeedMouseLeaveNeeded.GetSize());
- controls.Resize(m_aNeedMouseLeaveNeeded.GetSize());
- ::CopyMemory(controls.GetData(), m_aNeedMouseLeaveNeeded.GetData(),
- m_aNeedMouseLeaveNeeded.GetSize() * sizeof(LPVOID));
- for (int i = 0; i < controls.GetSize(); ++i)
- {
- CControlUI* pControl = static_cast<CControlUI*>(controls[i]);
- if (pControl != NULL)
- {
- event.pSender = pControl;
- pControl->Event(event);
- }
- }
- if (m_pEventHover != NULL)
- {
- event.pSender = m_pEventHover;
- m_pEventHover->Event(event);
- m_pEventHover = NULL;
- }
- m_aNeedMouseLeaveNeeded.Empty();
- if (m_hwndTooltip != NULL)
- ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ToolTip);
- }
- void CPaintManagerUI::DeletePtr(void* ptr)
- {
- if (ptr)
- {
- delete ptr; ptr = NULL;
- }
- }
- HINSTANCE CPaintManagerUI::GetInstance()
- {
- return m_hInstance;
- }
- CDuiString CPaintManagerUI::GetInstancePath()
- {
- if (m_hInstance == NULL) return _T('\0');
- TCHAR tszModule[MAX_PATH + 1] = { 0 };
- ::GetModuleFileName(m_hInstance, tszModule, MAX_PATH);
- CDuiString sInstancePath = tszModule;
- int pos = sInstancePath.ReverseFind(_T('\\'));
- if (pos >= 0) sInstancePath = sInstancePath.Left(pos + 1);
- return sInstancePath;
- }
- CDuiString CPaintManagerUI::GetCurrentPath()
- {
- TCHAR tszModule[MAX_PATH + 1] = { 0 };
- ::GetCurrentDirectory(MAX_PATH, tszModule);
- return tszModule;
- }
- HINSTANCE CPaintManagerUI::GetResourceDll()
- {
- if (m_hResourceInstance == NULL) return m_hInstance;
- return m_hResourceInstance;
- }
- const CDuiString& CPaintManagerUI::GetResourcePath()
- {
- return m_pStrResourcePath;
- }
- const CDuiString& CPaintManagerUI::GetResourceZip()
- {
- return m_pStrResourceZip;
- }
- const CDuiString& CPaintManagerUI::GetResourceZipPwd()
- {
- return m_pStrResourceZipPwd;
- }
- bool CPaintManagerUI::IsCachedResourceZip()
- {
- return m_bCachedResourceZip;
- }
- HANDLE CPaintManagerUI::GetResourceZipHandle()
- {
- return m_hResourceZip;
- }
- void CPaintManagerUI::SetInstance(HINSTANCE hInst)
- {
- m_hInstance = hInst;
- }
- void CPaintManagerUI::SetCurrentPath(LPCTSTR pStrPath)
- {
- ::SetCurrentDirectory(pStrPath);
- }
- void CPaintManagerUI::SetResourceDll(HINSTANCE hInst)
- {
- m_hResourceInstance = hInst;
- }
- void CPaintManagerUI::SetResourcePath(LPCTSTR pStrPath)
- {
- m_pStrResourcePath = pStrPath;
- if (m_pStrResourcePath.IsEmpty()) return;
- TCHAR cEnd = m_pStrResourcePath.GetAt(m_pStrResourcePath.GetLength() - 1);
- if (cEnd != _T('\\') && cEnd != _T('/')) m_pStrResourcePath += _T('\\');
- }
- void CPaintManagerUI::SetResourceZip(LPVOID pVoid, unsigned int len, LPCTSTR password)
- {
- if (m_pStrResourceZip == _T("membuffer")) return;
- if (m_bCachedResourceZip && m_hResourceZip != NULL)
- {
- CloseZip((HZIP)m_hResourceZip);
- m_hResourceZip = NULL;
- }
- m_pStrResourceZip = _T("membuffer");
- if (m_cbZipBuf)
- {
- delete[] m_cbZipBuf;
- m_cbZipBuf = nullptr;
- }
- if (!m_cbZipBuf)
- {
- m_cbZipBuf = new BYTE[len];
- memcpy(m_cbZipBuf, pVoid, len);
- }
- m_bCachedResourceZip = true;
- m_pStrResourceZipPwd = password; //Garfield 20160325 带密码zip包解密
- if (m_bCachedResourceZip)
- {
- #ifdef UNICODE
- char* pwd = w2a((wchar_t*)password);
- m_hResourceZip = (HANDLE)OpenZip(m_cbZipBuf, len, pwd);
- if (pwd)
- {
- delete[] pwd;
- pwd = NULL;
- }
- #else
- m_hResourceZip = (HANDLE)OpenZip(m_cbZipBuf, len, password);
- #endif
- }
- }
- void CPaintManagerUI::SetResourceZip(LPCTSTR pStrPath, bool bCachedResourceZip, LPCTSTR password)
- {
- if (m_pStrResourceZip == pStrPath && m_bCachedResourceZip == bCachedResourceZip) return;
- if (m_bCachedResourceZip && m_hResourceZip != NULL)
- {
- CloseZip((HZIP)m_hResourceZip);
- m_hResourceZip = NULL;
- }
- m_pStrResourceZip = pStrPath;
- m_bCachedResourceZip = bCachedResourceZip;
- m_pStrResourceZipPwd = password;
- if (m_bCachedResourceZip)
- {
- CDuiString sFile = CPaintManagerUI::GetResourcePath();
- sFile += CPaintManagerUI::GetResourceZip();
- #ifdef UNICODE
- char* pwd = w2a((wchar_t*)password);
- m_hResourceZip = (HANDLE)OpenZip(sFile.GetData(), pwd);
- if (pwd)
- {
- delete[] pwd;
- pwd = NULL;
- }
- #else
- m_hResourceZip = (HANDLE)OpenZip(sFile.GetData(), password);
- #endif
- }
- }
- void CPaintManagerUI::SetResourceType(int nType)
- {
- m_nResType = nType;
- }
- int CPaintManagerUI::GetResourceType()
- {
- return m_nResType;
- }
- bool CPaintManagerUI::GetHSL(short* H, short* S, short* L)
- {
- *H = m_H;
- *S = m_S;
- *L = m_L;
- return m_bUseHSL;
- }
- void CPaintManagerUI::SetHSL(bool bUseHSL, short H, short S, short L)
- {
- if (m_bUseHSL || m_bUseHSL != bUseHSL)
- {
- m_bUseHSL = bUseHSL;
- if (H == m_H && S == m_S && L == m_L) return;
- m_H = CLAMP(H, 0, 360);
- m_S = CLAMP(S, 0, 200);
- m_L = CLAMP(L, 0, 200);
- AdjustSharedImagesHSL();
- for (int i = 0; i < m_aPreMessages.GetSize(); i++)
- {
- CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
- if (pManager != NULL) pManager->AdjustImagesHSL();
- }
- }
- }
- void CPaintManagerUI::ReloadSkin()
- {
- ReloadSharedImages();
- for (int i = 0; i < m_aPreMessages.GetSize(); i++)
- {
- CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
- pManager->ReloadImages();
- }
- }
- CPaintManagerUI* CPaintManagerUI::GetPaintManager(LPCTSTR pstrName)
- {
- if (pstrName == NULL) return NULL;
- CDuiString sName = pstrName;
- if (sName.IsEmpty()) return NULL;
- for (int i = 0; i < m_aPreMessages.GetSize(); i++)
- {
- CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
- if (pManager != NULL && sName == pManager->GetName()) return pManager;
- }
- return NULL;
- }
- CStdPtrArray* CPaintManagerUI::GetPaintManagers()
- {
- return &m_aPreMessages;
- }
- bool CPaintManagerUI::LoadPlugin(LPCTSTR pstrModuleName)
- {
- ASSERT(!::IsBadStringPtr(pstrModuleName, -1) || pstrModuleName == NULL);
- if (pstrModuleName == NULL) return false;
- HMODULE hModule = ::LoadLibrary(pstrModuleName);
- if (hModule != NULL)
- {
- LPCREATECONTROL lpCreateControl = (LPCREATECONTROL)::GetProcAddress(hModule, "CreateControl");
- if (lpCreateControl != NULL)
- {
- if (m_aPlugins.Find(lpCreateControl) >= 0) return true;
- m_aPlugins.Add(lpCreateControl);
- return true;
- }
- }
- return false;
- }
- CStdPtrArray* CPaintManagerUI::GetPlugins()
- {
- return &m_aPlugins;
- }
- HWND CPaintManagerUI::GetPaintWindow() const
- {
- return m_hWndPaint;
- }
- HWND CPaintManagerUI::GetTooltipWindow() const
- {
- return m_hwndTooltip;
- }
- int CPaintManagerUI::GetHoverTime() const
- {
- return m_iHoverTime;
- }
- void CPaintManagerUI::SetHoverTime(int iTime)
- {
- m_iHoverTime = iTime;
- }
- LPCTSTR CPaintManagerUI::GetName() const
- {
- return m_sName;
- }
- HDC CPaintManagerUI::GetPaintDC() const
- {
- return m_hDcPaint;
- }
- POINT CPaintManagerUI::GetMousePos() const
- {
- return m_ptLastMousePos;
- }
- SIZE CPaintManagerUI::GetClientSize() const
- {
- RECT rcClient = { 0 };
- ::GetClientRect(m_hWndPaint, &rcClient);
- return CDuiSize(rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
- }
- SIZE CPaintManagerUI::GetInitSize()
- {
- return m_szInitWindowSize;
- }
- void CPaintManagerUI::SetInitSize(int cx, int cy)
- {
- m_szInitWindowSize.cx = cx;
- m_szInitWindowSize.cy = cy;
- if (m_pRoot == NULL && m_hWndPaint != NULL)
- {
- ::SetWindowPos(m_hWndPaint, NULL, 0, 0, cx, cy, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
- }
- }
- RECT CPaintManagerUI::GetSizeBox()
- {
- return GetDPIObj()->Scale(m_rcSizeBox);
- }
- void CPaintManagerUI::SetSizeBox(RECT& rcSizeBox)
- {
- m_rcSizeBox = rcSizeBox;
- }
- RECT CPaintManagerUI::GetCaptionRect()
- {
- return GetDPIObj()->Scale(m_rcCaption);
- }
- void CPaintManagerUI::SetCaptionRect(RECT& rcCaption)
- {
- m_rcCaption = rcCaption;
- }
- SIZE CPaintManagerUI::GetRoundCorner()
- {
- return GetDPIObj()->Scale(m_szRoundCorner);
- }
- void CPaintManagerUI::SetRoundCorner(int cx, int cy)
- {
- m_szRoundCorner.cx = cx;
- m_szRoundCorner.cy = cy;
- }
- SIZE CPaintManagerUI::GetMinInfo()
- {
- return GetDPIObj()->Scale(m_szMinWindow);
- }
- void CPaintManagerUI::SetMinInfo(int cx, int cy)
- {
- ASSERT(cx >= 0 && cy >= 0);
- m_szMinWindow.cx = cx;
- m_szMinWindow.cy = cy;
- }
- SIZE CPaintManagerUI::GetMaxInfo()
- {
- return GetDPIObj()->Scale(m_szMaxWindow);
- }
- void CPaintManagerUI::SetMaxInfo(int cx, int cy)
- {
- ASSERT(cx >= 0 && cy >= 0);
- m_szMaxWindow.cx = cx;
- m_szMaxWindow.cy = cy;
- }
- bool CPaintManagerUI::IsShowUpdateRect()
- {
- return m_bShowUpdateRect;
- }
- void CPaintManagerUI::SetShowUpdateRect(bool show)
- {
- m_bShowUpdateRect = show;
- }
- bool CPaintManagerUI::IsNoActivate()
- {
- return m_bNoActivate;
- }
- void CPaintManagerUI::SetNoActivate(bool bNoActivate)
- {
- m_bNoActivate = bNoActivate;
- }
- BYTE CPaintManagerUI::GetOpacity() const
- {
- return m_nOpacity;
- }
- void CPaintManagerUI::SetOpacity(BYTE nOpacity)
- {
- m_nOpacity = nOpacity;
- if (m_hWndPaint != NULL)
- {
- typedef BOOL(__stdcall* PFUNCSETLAYEREDWINDOWATTR)(HWND, COLORREF, BYTE, DWORD);
- PFUNCSETLAYEREDWINDOWATTR fSetLayeredWindowAttributes = NULL;
- HMODULE hUser32 = ::GetModuleHandle(_T("User32.dll"));
- if (hUser32)
- {
- fSetLayeredWindowAttributes =
- (PFUNCSETLAYEREDWINDOWATTR)::GetProcAddress(hUser32, "SetLayeredWindowAttributes");
- if (fSetLayeredWindowAttributes == NULL) return;
- }
- DWORD dwStyle = ::GetWindowLong(m_hWndPaint, GWL_EXSTYLE);
- DWORD dwNewStyle = dwStyle;
- if (nOpacity >= 0 && nOpacity < 256) dwNewStyle |= WS_EX_LAYERED;
- else dwNewStyle &= ~WS_EX_LAYERED;
- if (dwStyle != dwNewStyle) ::SetWindowLong(m_hWndPaint, GWL_EXSTYLE, dwNewStyle);
- fSetLayeredWindowAttributes(m_hWndPaint, 0, nOpacity, LWA_ALPHA);
- }
- }
- bool CPaintManagerUI::IsLayered()
- {
- return m_bLayered;
- }
- void CPaintManagerUI::SetLayered(bool bLayered)
- {
- if (m_hWndPaint != NULL && bLayered != m_bLayered)
- {
- UINT uStyle = GetWindowStyle(m_hWndPaint);
- if ((uStyle & WS_CHILD) != 0) return;
- if (g_fUpdateLayeredWindow == NULL)
- {
- HMODULE hUser32 = ::GetModuleHandle(_T("User32.dll"));
- if (hUser32)
- {
- g_fUpdateLayeredWindow =
- (PFUNCUPDATELAYEREDWINDOW)::GetProcAddress(hUser32, "UpdateLayeredWindow");
- if (g_fUpdateLayeredWindow == NULL) return;
- }
- }
- m_bLayered = bLayered;
- if (m_pRoot != NULL) m_pRoot->NeedUpdate();
- Invalidate();
- }
- }
- RECT& CPaintManagerUI::GetLayeredInset()
- {
- return m_rcLayeredInset;
- }
- void CPaintManagerUI::SetLayeredInset(RECT& rcLayeredInset)
- {
- m_rcLayeredInset = rcLayeredInset;
- m_bLayeredChanged = true;
- Invalidate();
- }
- BYTE CPaintManagerUI::GetLayeredOpacity()
- {
- return m_nOpacity;
- }
- void CPaintManagerUI::SetLayeredOpacity(BYTE nOpacity)
- {
- m_nOpacity = nOpacity;
- m_bLayeredChanged = true;
- Invalidate();
- }
- LPCTSTR CPaintManagerUI::GetLayeredImage()
- {
- return m_diLayered.sDrawString;
- }
- void CPaintManagerUI::SetLayeredImage(LPCTSTR pstrImage)
- {
- m_diLayered.sDrawString = pstrImage;
- RECT rcNull = { 0 };
- CRenderEngine::DrawImageInfo(NULL, this, rcNull, rcNull, &m_diLayered);
- m_bLayeredChanged = true;
- Invalidate();
- }
- CShadowUI* CPaintManagerUI::GetShadow()
- {
- return &m_shadow;
- }
- void CPaintManagerUI::SetUseGdiplusText(bool bUse)
- {
- m_bUseGdiplusText = bUse;
- }
- bool CPaintManagerUI::IsUseGdiplusText() const
- {
- return m_bUseGdiplusText;
- }
- void CPaintManagerUI::SetGdiplusTextRenderingHint(int trh)
- {
- m_trh = trh;
- }
- int CPaintManagerUI::GetGdiplusTextRenderingHint() const
- {
- return m_trh;
- }
- bool CPaintManagerUI::PreMessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lRes)
- {
- for (int i = 0; i < m_aPreMessageFilters.GetSize(); i++)
- {
- bool bHandled = false;
- LRESULT lResult = static_cast<IMessageFilterUI*>(m_aPreMessageFilters[i])->MessageHandler(uMsg, wParam, lParam, bHandled);
- if (bHandled)
- {
- lRes = lResult;
- return true;
- }
- }
- switch (uMsg)
- {
- case WM_KEYDOWN:
- {
- // Tabbing between controls
- if (wParam == VK_TAB)
- {
- if (m_pFocus && m_pFocus->IsVisible() && m_pFocus->IsEnabled() && _tcsstr(m_pFocus->GetClass(), _T("RichEditUI")) != NULL)
- {
- if (static_cast<CRichEditUI*>(m_pFocus)->IsWantTab()) return false;
- }
- if (m_pFocus && m_pFocus->IsVisible() && m_pFocus->IsEnabled() && _tcsstr(m_pFocus->GetClass(), _T("WkeWebkitUI")) != NULL)
- {
- return false;
- }
- SetNextTabControl(::GetKeyState(VK_SHIFT) >= 0);
- return false;
- }
- }
- break;
- case WM_SYSCHAR:
- {
- // Handle ALT-shortcut key-combinations
- FINDSHORTCUT fs = { 0 };
- fs.ch = toupper((int)wParam);
- CControlUI* pControl = m_pRoot->FindControl(__FindControlFromShortcut, &fs, UIFIND_ENABLED | UIFIND_ME_FIRST | UIFIND_TOP_FIRST);
- if (pControl != NULL)
- {
- pControl->SetFocus();
- pControl->Activate();
- return true;
- }
- }
- break;
- case WM_SYSKEYDOWN:
- {
- if (m_pFocus != NULL)
- {
- TEventUI event = { 0 };
- event.Type = UIEVENT_SYSKEY;
- event.chKey = (TCHAR)wParam;
- event.ptMouse = m_ptLastMousePos;
- event.wKeyState = MapKeyState();
- event.dwTimestamp = ::GetTickCount();
- m_pFocus->Event(event);
- }
- }
- break;
- }
- return false;
- }
- bool CPaintManagerUI::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lRes)
- {
- if (m_hWndPaint == NULL) return false;
- // ------------------------------------------------------------------
- // [临时诊断] 心跳快照。每 2 秒最多一条,记录三个窗口的触摸注册 / 前台 / 焦点状态。
- //
- // 为什么必须要有它:如果系统压根不给弹框投任何消息(这正是当前症状 ——
- // 弹框只收到 WM_MOUSEMOVE,零 WM_TOUCH),那么"收到消息才打日志"的诊断就永远
- // 触发不了,日志里只会看到一片沉默,无法区分下面两种完全不同的情况:
- // (a) 弹框的触摸注册丢了 → 系统根本不会给它投 WM_TOUCH;
- // (b) 注册没丢,但所有权(greedy ownership)还在 owner 手里 → 投给了 owner。
- // 心跳能直接分辨 (a)/(b),因为它主动去问系统"谁现在注册着触摸"。
- // ------------------------------------------------------------------
- {
- static DWORD s_dwLastHeartbeat = 0;
- const DWORD dwNowHb = ::GetTickCount();
- // 2000ms:常规节奏。调试期曾临时调到 1000ms 以便核对滑动轨迹
- // (每 2 秒一条在快速滑动时样本太少,看不出 Δ 轨迹);
- // 触摸功能已验证正常(主窗口 / 模态弹框的点击与滑动均可用),故改回 2000ms。
- if ((LONG)(dwNowHb - s_dwLastHeartbeat) >= 2000)
- {
- s_dwLastHeartbeat = dwNowHb;
- // [手势让路] 打点前先取一份本 manager 的输入通道状态:
- // ptrActive=1 ⇒ 本次手势正由指针通道驱动(WM_TOUCH 会被忽略)
- // ptrYield=1 ⇒ 本次手势已让给 WM_TOUCH(指针消息会被忽略)
- // 两者都为 0 ⇒ 两条通道都空闲,下一次触摸谁先到谁驱动。
- const int bNewGestureActive = (int)m_bPointerGestureActive;
- const DWORD dwNewActivePointerId = m_dwActivePointerId;
- const int bNewPointerYielded = (m_pEventTouch != NULL) ? 1 : 0;
- HMODULE hU32 = ::GetModuleHandle(_T("user32.dll"));
- typedef BOOL (WINAPI* PFN_IS_TOUCH_WINDOW)(HWND, PULONG);
- PFN_IS_TOUCH_WINDOW pfnIsTouch = (hU32 != NULL)
- ? reinterpret_cast<PFN_IS_TOUCH_WINDOW>(::GetProcAddress(hU32, "IsTouchWindow")) : NULL;
- ULONG tfSelf = 0, tfModal = 0;
- const BOOL bSelf = (pfnIsTouch != NULL) ? pfnIsTouch(m_hWndPaint, &tfSelf) : FALSE;
- HWND hModal = s_hModalWindow;
- const BOOL bModal = (pfnIsTouch != NULL && hModal != NULL && ::IsWindow(hModal))
- ? pfnIsTouch(hModal, &tfModal) : FALSE;
- 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"),
- (void*)m_hWndPaint, (int)bSelf, (unsigned)tfSelf,
- (void*)hModal, (int)bModal, (unsigned)tfModal,
- (hModal != NULL) ? (void*)::GetWindow(hModal, GW_OWNER) : NULL,
- (void*)::GetForegroundWindow(), (void*)::GetFocus(), (void*)::GetActiveWindow(),
- (unsigned)uMsg,
- (int)bNewGestureActive, (unsigned)dwNewActivePointerId, (int)bNewPointerYielded);
- }
- }
- // ------------------------------------------------------------------
- // 触摸状态超时自愈(防“窗口突然完全点不动”):
- // TOUCHDOWN 会置起 m_pEventTouch,合成点击后会置起 m_bSuppressPromotedTouchMouse;
- // 若对应的 TOUCHUP / 提升的 WM_LBUTTONUP 因故丢失(手势被系统中断、激活或前台被抢、
- // 窗口被遮挡、消息被别处吞掉等),这两个状态会永久残留 → 之后所有提升的鼠标消息都被
- // 当成“触摸重复”吞掉,表现为“点着点着就完全没反应了”。这里粗粒度复位保证能自愈。
- //
- // ⚠️ 判据用的是 m_dwSuppressTick 而【不是】m_dwTouchDownTick:
- // 曾经写成 m_dwTouchDownTick != 0 才自愈 —— 但 m_bSuppressPromotedTouchMouse 有可能是
- // 在没有 TOUCHDOWN 的情况下被置真的(两条输入流交叉时),此时 m_dwTouchDownTick 仍为 0,
- // 自愈条件永远不成立 ⇒ 状态永久残留 ⇒ 【所有窗口点击彻底失效】。这是实测踩过的坑。
- // 现在改为:只要存在"抑制位",就按它被置位的时刻计时,到点无条件复位。
- // ------------------------------------------------------------------
- if (m_bSuppressPromotedTouchMouse != false && m_dwSuppressTick == 0)
- m_dwSuppressTick = ::GetTickCount();
- if (m_pEventTouch != NULL || m_bSuppressPromotedTouchMouse)
- {
- const DWORD dwRefTick = (m_dwTouchDownTick != 0) ? m_dwTouchDownTick : m_dwSuppressTick;
- if (dwRefTick != 0 && (DWORD)(::GetTickCount() - dwRefTick) > 3000)
- {
- TouchTrace::Log(_T("TOUCH-STATE timeout-reset hwnd=%p touch=%s suppress=%d elapsed=%u"),
- (void*)m_hWndPaint,
- m_pEventTouch != NULL ? m_pEventTouch->GetName().GetData() : _T("null"),
- (int)m_bSuppressPromotedTouchMouse,
- (unsigned)(::GetTickCount() - dwRefTick));
- m_pEventTouch = NULL;
- m_pTouchScrollContainer = NULL;
- m_bTouchMoved = false;
- m_bSuppressPromotedTouchMouse = false;
- m_dwTouchDownTick = 0;
- m_dwSuppressTick = 0;
- m_nTouchWheelAccum = 0;
- // [通道无关滑动] 滑动状态机同步收掉(否则下一个手势的 Δ 会以过期起点计算,
- // 表现为"一按下内容就跳一段")。
- m_bTouchSlideActive = false;
- }
- }
- else
- {
- m_dwSuppressTick = 0;
- }
- // 指针通道的手势状态也要能自愈:万一 WM_POINTERUP 丢失(手势被系统中断、
- // 窗口被遮挡、捕获被夺),m_bPointerGestureActive 会永久为真 ⇒ 之后所有
- // WM_TOUCH 都被"指针驱动中"挡掉 ⇒ 该窗口从此只能靠指针通道,行为不稳定。
- //
- // [NIM 风格] 计时基准改用 m_dwPointerDownTick(专门的字段),
- // 不再借用让路时间戳(那是语义污染,已删)。
- if (m_bPointerGestureActive)
- {
- if (m_dwPointerDownTick == 0)
- m_dwPointerDownTick = ::GetTickCount();
- if ((DWORD)(::GetTickCount() - m_dwPointerDownTick) > 3000)
- {
- TouchTrace::Log(_T("POINTER-GESTURE timeout-reset hwnd=%p active=%d elapsed=%u"),
- (void*)m_hWndPaint, (int)m_bPointerGestureActive,
- (unsigned)(::GetTickCount() - m_dwPointerDownTick));
- m_bPointerGestureActive = false;
- m_dwActivePointerId = 0;
- m_dwPointerDownTick = 0;
- // [通道无关滑动] 指针手势已被判定超时 ⇒ 滑动状态机同样不能悬空,
- // 否则下一次手势的 UpdateTouchSlideGesture 会带着过期起点算 Δ。
- m_bTouchSlideActive = false;
- m_pEventTouch = NULL;
- m_pTouchScrollContainer = NULL;
- m_bTouchMoved = false;
- }
- }
- if (uMsg == WM_TOUCH || uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONUP)
- {
- // [临时诊断] 顺带把"本窗口 / 模态弹框 / 前台窗口"三者的触摸注册状态打出来。
- // 关键疑点:弹框在 ShowModal 里 RegisterTouchWindow 过,但初始化时可能又被
- // InitTouchSupport 之外的路径改掉样式(WS_EX_* 变更会隐式注销触摸注册),
- // 一旦注册丢失,系统就绝不会再给弹框投 WM_TOUCH —— 这与"零 WNDPROC-TOUCH"完全吻合。
- // IsTouchWindow(hwnd, &flags):TRUE 表示该窗口当前确实在触摸注册表里。
- HMODULE hU32 = ::GetModuleHandle(_T("user32.dll"));
- typedef BOOL (WINAPI* PFN_IS_TOUCH_WINDOW)(HWND, PULONG);
- PFN_IS_TOUCH_WINDOW pfnIsTouch = (hU32 != NULL)
- ? reinterpret_cast<PFN_IS_TOUCH_WINDOW>(::GetProcAddress(hU32, "IsTouchWindow")) : NULL;
- ULONG tfSelf = 0, tfModal = 0, tfFg = 0;
- const BOOL bSelf = (pfnIsTouch != NULL) ? pfnIsTouch(m_hWndPaint, &tfSelf) : FALSE;
- HWND hModal = s_hModalWindow;
- const BOOL bModal = (pfnIsTouch != NULL && hModal != NULL && ::IsWindow(hModal))
- ? pfnIsTouch(hModal, &tfModal) : FALSE;
- HWND hFg = ::GetForegroundWindow();
- const BOOL bFg = (pfnIsTouch != NULL && hFg != NULL) ? pfnIsTouch(hFg, &tfFg) : FALSE;
- TouchTrace::Log(_T("MANAGER-MSG hwnd=%p manager=%p msg=0x%04X w=0x%p l=0x%p touch=%p suppress=%d"),
- (void*)m_hWndPaint, (void*)this, uMsg, (void*)wParam, (void*)lParam,
- (void*)m_pEventTouch, (int)m_bSuppressPromotedTouchMouse);
- 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"),
- (void*)m_hWndPaint, (int)bSelf, (unsigned)tfSelf,
- (void*)hModal, (int)bModal, (unsigned)tfModal,
- (void*)hFg, (int)bFg, (unsigned)tfFg,
- (void*)::GetFocus(), (void*)::GetActiveWindow());
- }
- // 触摸点击优先由 WM_TOUCH 状态机驱动 BUTTONDOWN/BUTTONUP。
- // 只有当前 manager 已经收到本次 WM_TOUCH,或刚刚完成了触摸点击,
- // 才拦截随后提升的鼠标消息。不能仅凭 FROMTOUCH 标记无条件拦截,
- // 因为部分模态窗口只收到 Windows 提升的鼠标消息而没有收到 WM_TOUCH,
- // 无条件拦截会导致模态窗口完全无法触摸点击。
- //
- // ⚠️ 注意这里的 return true 会【吞掉】消息、不让它进控件树。
- // 只有当"确实有一次触摸手势在进行中/刚结束"时才该吞:
- // · m_pEventTouch != NULL —— 本次手势已由 WM_TOUCH 接管
- // · m_bSuppressPromotedTouchMouse —— 刚合成过点击,丢弃重复的提升鼠标
- // 两个条件任一为假,都必须放行 —— 否则真实鼠标点击也会被吞("主窗口点不动")。
- if (uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONUP || uMsg == WM_LBUTTONDBLCLK)
- {
- if (m_pEventTouch != NULL || m_bSuppressPromotedTouchMouse)
- {
- if (uMsg == WM_LBUTTONUP || uMsg == WM_LBUTTONDBLCLK)
- {
- m_bSuppressPromotedTouchMouse = false;
- m_dwSuppressTick = 0;
- }
- TouchTrace::Log(_T("PROMOTED-MOUSE swallowed msg=0x%04X touch=%p suppress=%d"),
- uMsg, (void*)m_pEventTouch, (int)m_bSuppressPromotedTouchMouse);
- return true;
- }
- }
- // [触屏兼容] 不再过滤 Windows 为触摸模拟的鼠标消息(MOUSEEVENTF_FROMTOUCH)。
- // 触摸的“点击”正来自这些模拟的 WM_LBUTTONDOWN/UP,由原生鼠标路径交给 CButtonUI 等控件
- // Activate() 触发——而 DuiLib 的点击状态机依赖 WM_LBUTTONDOWN 设置 m_pEventClick,故绝不能
- // 在鼠标分支吞掉这些模拟消息(之前那样做会导致触摸与鼠标点击全失效)。
- // 为避免“拖拽=滚动”又“误点按钮”,改用 m_bTouchMoved:若本次手势发生位移,在 WM_LBUTTONUP 抑制点击。
- // ------------------------------------------------------------------
- // [对标 nim_duilib] WM_POINTER*(Win8+ 指针栈)归一化成鼠标消息。
- //
- // ⚠️ 这里的核心是【手势级让路】,不是"通道二选一"。改回二选一会立刻坏掉真模态弹框:
- // 本台收银机上 WM_TOUCH 的贪婪归属权卡在主窗口(MSDN:收到第一条触摸消息后
- // 所有触摸消息都发给该窗口,直到另一个窗口获得焦点),弹框被程序 SetFocus
- // 而非触摸点出焦点 ⇒ 弹框永远收不到 WM_TOUCH,只能收到按命中投递的 WM_POINTER*。
- // 所以指针通道必须始终开着,弹框才有输入源。
- //
- // 那怎么避免"两条通道产生两套 WM_LBUTTONDOWN/UP 把点击状态机打乱"(曾经导致
- // 所有窗口点击全失效)?答案是:**同一时刻只让一条通道驱动**。
- // · 指针先 DOWN ⇒ m_bPointerGestureActive=true,本次手势内 OnTouchInput 直接忽略 WM_TOUCH;
- // · WM_TOUCH 先 DOWN ⇒ m_pEventTouch 置位,本次手势内这里拒绝指针;
- // · 手势一结束(UP / 超时 / 捕获被夺)立刻解禁,下一个手势可以换通道。
- // 让路只加在 UP 的"驱动点击"上:同一手势里两条通道的**按下**才是一对重复;
- // UP/MOVE 一侧不加让路,避免某一侧的 UP 被误丢导致状态永久悬空。
- // ⚠️ 【血的教训】曾经让路连 WM_POINTERUP 一起吞 —— UP 是唯一能解除
- // m_bPointerGestureActive 的消息,吞掉它会让指针手势状态永久为真,
- // 随后的 WM_POINTERDOWN 全被当成"重复按下"挡掉 ⇒ 整个窗口触摸点击全灭。
- // 现在 UP/UPDATE 一律送到 OnPointerInput,由那里自行决定"只清状态"还是"派发点击"。
- //
- // 另外,只处理 DOWN/UP/UPDATE 三个真正驱动点击的消息;
- // ENTER/LEAVE/ACTIVATE/HWHEEL/CAPTURECHANGED 交回系统(返回 false),绝不吞掉。
- //
- // 注意:本项目 _WIN32_WINNT=0x0601,WM_POINTER* 宏在编译期不可见,
- // 只能用字面量常量区间判断(见文件头 PointerMsg 命名空间)。
- // ------------------------------------------------------------------
- if (m_bPointerNormalizeEnabled
- && (uMsg == PointerMsg::DUI_WM_POINTERDOWN
- || uMsg == PointerMsg::DUI_WM_POINTERUP
- || uMsg == PointerMsg::DUI_WM_POINTERUPDATE))
- {
- // [NIM 风格] 这里**不再**做让路判定 —— 让路完全下沉到 OnPointerInput,
- // 由 m_pEventTouch(WM_TOUCH 的占用标记)与 m_bPointerGestureActive 互斥决定。
- // 原先在这里用时间戳预判让路,会导致"UP 被漏放/被多放"两类边界问题,
- // 而 OnPointerInput 内部本就有完整的 UP 放行规则(它才是权威)。
- //
- // 三个消息一律委托,由 OnPointerInput 决定"吃掉"还是"放行":
- // · DOWN/UPDATE 且 WM_TOUCH 已占用 ⇒ 返回 true(吃掉)
- // · UP 永远返回 false(放行给系统收尾),但内部已清干净状态
- if (OnPointerInput(uMsg, wParam, lParam))
- {
- lRes = 0;
- return true;
- }
- }
- else if (uMsg >= PointerMsg::DUI_WM_POINTERUPDATE && uMsg <= PointerMsg::DUI_WM_POINTERHWHEEL)
- {
- // [诊断] 未进入归一化的指针消息(通道关闭、或 ENTER/ACTIVATE/HWHEEL 等),
- // 只记录"系统确实在投指针消息"这一事实,不做任何处理
- // (绝不能 return true —— 交回系统,保住它自己的指针状态机)。
- TouchTrace::Log(_T("POINTER-MSG observed-but-passed msg=0x%04X w=0x%08X enabled=%d hwnd=%p"),
- uMsg, (unsigned)wParam, (int)m_bPointerNormalizeEnabled, (void*)m_hWndPaint);
- }
- // Cycle through listeners
- for (int i = 0; i < m_aMessageFilters.GetSize(); i++)
- {
- bool bHandled = false;
- LRESULT lResult = static_cast<IMessageFilterUI*>(m_aMessageFilters[i])->MessageHandler(uMsg, wParam, lParam, bHandled);
- if (bHandled)
- {
- lRes = lResult;
- switch (uMsg)
- {
- case WM_MOUSEMOVE:
- case WM_LBUTTONDOWN:
- case WM_LBUTTONDBLCLK:
- case WM_LBUTTONUP:
- {
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- m_ptLastMousePos = pt;
- }
- break;
- case WM_CONTEXTMENU:
- case WM_MOUSEWHEEL:
- {
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- ::ScreenToClient(m_hWndPaint, &pt);
- m_ptLastMousePos = pt;
- }
- break;
- }
- return true;
- }
- }
- if (m_bLayered)
- {
- switch (uMsg)
- {
- case WM_NCACTIVATE:
- if (!::IsIconic(m_hWndPaint))
- {
- lRes = (wParam == 0) ? TRUE : FALSE;
- return true;
- }
- break;
- case WM_NCCALCSIZE:
- case WM_NCPAINT:
- lRes = 0;
- return true;
- }
- }
- // Custom handling of events
- switch (uMsg)
- {
- case WM_APP + 1:
- {
- for (int i = 0; i < m_aDelayedCleanup.GetSize(); i++)
- delete static_cast<CControlUI*>(m_aDelayedCleanup[i]);
- m_aDelayedCleanup.Empty();
- m_bAsyncNotifyPosted = false;
- TNotifyUI* pMsg = NULL;
- while (pMsg = static_cast<TNotifyUI*>(m_aAsyncNotify.GetAt(0)))
- {
- m_aAsyncNotify.Remove(0);
- if (pMsg->pSender != NULL)
- {
- if (pMsg->pSender->OnNotify) pMsg->pSender->OnNotify(pMsg);
- }
- for (int j = 0; j < m_aNotifiers.GetSize(); j++)
- {
- static_cast<INotifyUI*>(m_aNotifiers[j])->Notify(*pMsg);
- }
- delete pMsg;
- }
- }
- break;
- case WM_CLOSE:
- {
- // Make sure all matching "closing" events are sent
- TEventUI event = { 0 };
- event.ptMouse = m_ptLastMousePos;
- event.wKeyState = MapKeyState();
- event.dwTimestamp = ::GetTickCount();
- if (m_pEventHover != NULL)
- {
- event.Type = UIEVENT_MOUSELEAVE;
- event.pSender = m_pEventHover;
- m_pEventHover->Event(event);
- }
- if (m_pEventClick != NULL)
- {
- event.Type = UIEVENT_BUTTONUP;
- event.pSender = m_pEventClick;
- m_pEventClick->Event(event);
- }
- if (m_pEventRClick != NULL)
- {
- event.Type = UIEVENT_RBUTTONUP;
- event.pSender = m_pEventClick;
- m_pEventRClick->Event(event);
- }
- SetFocus(NULL);
- if (::GetActiveWindow() == m_hWndPaint)
- {
- HWND hwndParent = GetWindowOwner(m_hWndPaint);
- if (hwndParent != NULL) ::SetFocus(hwndParent);
- }
- if (m_hwndTooltip != NULL)
- {
- ::DestroyWindow(m_hwndTooltip);
- m_hwndTooltip = NULL;
- }
- }
- break;
- case WM_ERASEBKGND:
- {
- // We'll do the painting here...
- lRes = 1;
- }
- return true;
- case WM_PAINT:
- {
- if (m_pRoot == NULL)
- {
- PAINTSTRUCT ps = { 0 };
- ::BeginPaint(m_hWndPaint, &ps);
- CRenderEngine::DrawColor(m_hDcPaint, ps.rcPaint, 0xFF000000);
- ::EndPaint(m_hWndPaint, &ps);
- return true;
- }
- RECT rcClient = { 0 };
- ::GetClientRect(m_hWndPaint, &rcClient);
- RECT rcPaint = { 0 };
- if (!::GetUpdateRect(m_hWndPaint, &rcPaint, FALSE)) return true;
- //if( m_bLayered ) {
- // m_bOffscreenPaint = true;
- // rcPaint = m_rcLayeredUpdate;
- // if( ::IsRectEmpty(&m_rcLayeredUpdate) ) {
- // PAINTSTRUCT ps = { 0 };
- // ::BeginPaint(m_hWndPaint, &ps);
- // ::EndPaint(m_hWndPaint, &ps);
- // return true;
- // }
- // if( rcPaint.right > rcClient.right ) rcPaint.right = rcClient.right;
- // if( rcPaint.bottom > rcClient.bottom ) rcPaint.bottom = rcClient.bottom;
- // ::ZeroMemory(&m_rcLayeredUpdate, sizeof(m_rcLayeredUpdate));
- //}
- //else {
- // if( !::GetUpdateRect(m_hWndPaint, &rcPaint, FALSE) ) return true;
- //}
- // Set focus to first control?
- if (m_bFocusNeeded)
- {
- //修复新窗口自动切换焦点的问题
- //SetNextTabControl();
- m_bFocusNeeded = false;
- }
- SetPainting(true);
- bool bNeedSizeMsg = false;
- DWORD dwWidth = rcClient.right - rcClient.left;
- DWORD dwHeight = rcClient.bottom - rcClient.top;
- SetPainting(true);
- if (m_bUpdateNeeded)
- {
- m_bUpdateNeeded = false;
- if (!::IsRectEmpty(&rcClient) && !::IsIconic(m_hWndPaint))
- {
- if (m_pRoot->IsUpdateNeeded())
- {
- RECT rcRoot = rcClient;
- if (m_hDcOffscreen != NULL) ::DeleteDC(m_hDcOffscreen);
- if (m_hDcBackground != NULL) ::DeleteDC(m_hDcBackground);
- if (m_hbmpOffscreen != NULL) ::DeleteObject(m_hbmpOffscreen);
- if (m_hbmpBackground != NULL) ::DeleteObject(m_hbmpBackground);
- m_hDcOffscreen = NULL;
- m_hDcBackground = NULL;
- m_hbmpOffscreen = NULL;
- m_hbmpBackground = NULL;
- if (m_bLayered)
- {
- rcRoot.left += m_rcLayeredInset.left;
- rcRoot.top += m_rcLayeredInset.top;
- rcRoot.right -= m_rcLayeredInset.right;
- rcRoot.bottom -= m_rcLayeredInset.bottom;
- }
- m_pRoot->SetPos(rcRoot, true);
- bNeedSizeMsg = true;
- }
- else
- {
- CControlUI* pControl = NULL;
- m_aFoundControls.Empty();
- m_pRoot->FindControl(__FindControlsFromUpdate, NULL, UIFIND_VISIBLE | UIFIND_ME_FIRST | UIFIND_UPDATETEST);
- for (int it = 0; it < m_aFoundControls.GetSize(); it++)
- {
- pControl = static_cast<CControlUI*>(m_aFoundControls[it]);
- pControl->SetPos(pControl->GetPos(), true);
- }
- bNeedSizeMsg = true;
- }
- // We'll want to notify the window when it is first initialized
- // with the correct layout. The window form would take the time
- // to submit swipes/animations.
- if (m_bFirstLayout)
- {
- m_bFirstLayout = false;
- SendNotify(m_pRoot, DUI_MSGTYPE_WINDOWINIT, 0, 0, false);
- if (m_bLayered && m_bLayeredChanged)
- {
- Invalidate();
- SetPainting(false);
- return true;
- }
- // 更新阴影窗口显示
- m_shadow.Update(m_hWndPaint);
- }
- }
- }
- else if (m_bLayered && m_bLayeredChanged)
- {
- RECT rcRoot = rcClient;
- if (m_pOffscreenBits) ::ZeroMemory(m_pOffscreenBits, (rcRoot.right - rcRoot.left)
- * (rcRoot.bottom - rcRoot.top) * 4);
- rcRoot.left += m_rcLayeredInset.left;
- rcRoot.top += m_rcLayeredInset.top;
- rcRoot.right -= m_rcLayeredInset.right;
- rcRoot.bottom -= m_rcLayeredInset.bottom;
- m_pRoot->SetPos(rcRoot, true);
- }
- if (m_bLayered)
- {
- DWORD dwExStyle = ::GetWindowLong(m_hWndPaint, GWL_EXSTYLE);
- DWORD dwNewExStyle = dwExStyle | WS_EX_LAYERED;
- if (dwExStyle != dwNewExStyle) ::SetWindowLong(m_hWndPaint, GWL_EXSTYLE, dwNewExStyle);
- m_bOffscreenPaint = true;
- UnionRect(&rcPaint, &rcPaint, &m_rcLayeredUpdate);
- if (rcPaint.right > rcClient.right) rcPaint.right = rcClient.right;
- if (rcPaint.bottom > rcClient.bottom) rcPaint.bottom = rcClient.bottom;
- ::ZeroMemory(&m_rcLayeredUpdate, sizeof(m_rcLayeredUpdate));
- }
- //
- // Render screen
- //
- // Prepare offscreen bitmap
- if (m_bOffscreenPaint && m_hbmpOffscreen == NULL)
- {
- m_hDcOffscreen = ::CreateCompatibleDC(m_hDcPaint);
- m_hbmpOffscreen = CRenderEngine::CreateARGB32Bitmap(m_hDcPaint, dwWidth, dwHeight, (LPBYTE*)&m_pOffscreenBits);
- ASSERT(m_hDcOffscreen);
- ASSERT(m_hbmpOffscreen);
- }
- // Begin Windows paint
- PAINTSTRUCT ps = { 0 };
- ::BeginPaint(m_hWndPaint, &ps);
- if (m_bOffscreenPaint)
- {
- HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(m_hDcOffscreen, m_hbmpOffscreen);
- int iSaveDC = ::SaveDC(m_hDcOffscreen);
- if (m_bLayered)
- {
- for (LONG y = rcClient.bottom - rcPaint.bottom; y < rcClient.bottom - rcPaint.top; ++y)
- {
- for (LONG x = rcPaint.left; x < rcPaint.right; ++x)
- {
- int i = (y * dwWidth + x) * 4;
- *(DWORD*)(&m_pOffscreenBits[i]) = 0;
- }
- }
- }
- m_pRoot->Paint(m_hDcOffscreen, rcPaint, NULL);
- if (m_bLayered)
- {
- for (int i = 0; i < m_aNativeWindow.GetSize(); )
- {
- HWND hChildWnd = static_cast<HWND>(m_aNativeWindow[i]);
- if (!::IsWindow(hChildWnd))
- {
- m_aNativeWindow.Remove(i);
- m_aNativeWindowControl.Remove(i);
- continue;
- }
- ++i;
- if (!::IsWindowVisible(hChildWnd)) continue;
- RECT rcChildWnd = GetNativeWindowRect(hChildWnd);
- RECT rcTemp = { 0 };
- if (!::IntersectRect(&rcTemp, &rcPaint, &rcChildWnd)) continue;
- COLORREF* pChildBitmapBits = NULL;
- HDC hChildMemDC = ::CreateCompatibleDC(m_hDcOffscreen);
- HBITMAP hChildBitmap = CRenderEngine::CreateARGB32Bitmap(hChildMemDC, rcChildWnd.right - rcChildWnd.left, rcChildWnd.bottom - rcChildWnd.top, (BYTE**)&pChildBitmapBits);
- ::ZeroMemory(pChildBitmapBits, (rcChildWnd.right - rcChildWnd.left) * (rcChildWnd.bottom - rcChildWnd.top) * 4);
- HBITMAP hOldChildBitmap = (HBITMAP) ::SelectObject(hChildMemDC, hChildBitmap);
- ::SendMessage(hChildWnd, WM_PRINT, (WPARAM)hChildMemDC, (LPARAM)(PRF_CHECKVISIBLE | PRF_CHILDREN | PRF_CLIENT | PRF_OWNED));
- COLORREF* pChildBitmapBit;
- for (LONG y = 0; y < rcChildWnd.bottom - rcChildWnd.top; y++)
- {
- for (LONG x = 0; x < rcChildWnd.right - rcChildWnd.left; x++)
- {
- pChildBitmapBit = pChildBitmapBits + y * (rcChildWnd.right - rcChildWnd.left) + x;
- if (*pChildBitmapBit != 0x00000000) *pChildBitmapBit |= 0xff000000;
- }
- }
- ::BitBlt(m_hDcOffscreen, rcChildWnd.left, rcChildWnd.top, rcChildWnd.right - rcChildWnd.left,
- rcChildWnd.bottom - rcChildWnd.top, hChildMemDC, 0, 0, SRCCOPY);
- ::SelectObject(hChildMemDC, hOldChildBitmap);
- ::DeleteObject(hChildBitmap);
- ::DeleteDC(hChildMemDC);
- }
- }
- for (int i = 0; i < m_aPostPaintControls.GetSize(); i++)
- {
- CControlUI* pPostPaintControl = static_cast<CControlUI*>(m_aPostPaintControls[i]);
- pPostPaintControl->DoPostPaint(m_hDcOffscreen, rcPaint);
- }
- ::RestoreDC(m_hDcOffscreen, iSaveDC);
- if (m_bLayered)
- {
- RECT rcWnd = { 0 };
- ::GetWindowRect(m_hWndPaint, &rcWnd);
- if (!m_diLayered.sDrawString.IsEmpty())
- {
- DWORD dwWidth = rcClient.right - rcClient.left;
- DWORD dwHeight = rcClient.bottom - rcClient.top;
- RECT rcLayeredClient = rcClient;
- rcLayeredClient.left += m_rcLayeredInset.left;
- rcLayeredClient.top += m_rcLayeredInset.top;
- rcLayeredClient.right -= m_rcLayeredInset.right;
- rcLayeredClient.bottom -= m_rcLayeredInset.bottom;
- COLORREF* pOffscreenBits = (COLORREF*)m_pOffscreenBits;
- COLORREF* pBackgroundBits = m_pBackgroundBits;
- BYTE A = 0;
- BYTE R = 0;
- BYTE G = 0;
- BYTE B = 0;
- if (!m_diLayered.sDrawString.IsEmpty())
- {
- if (m_hbmpBackground == NULL)
- {
- m_hDcBackground = ::CreateCompatibleDC(m_hDcPaint);
- m_hbmpBackground = CRenderEngine::CreateARGB32Bitmap(m_hDcPaint, dwWidth, dwHeight, (BYTE**)&m_pBackgroundBits);
- ::ZeroMemory(m_pBackgroundBits, dwWidth * dwHeight * 4);
- ::SelectObject(m_hDcBackground, m_hbmpBackground);
- CRenderClip clip;
- CRenderClip::GenerateClip(m_hDcBackground, rcLayeredClient, clip);
- CRenderEngine::DrawImageInfo(m_hDcBackground, this, rcLayeredClient, rcLayeredClient, &m_diLayered);
- }
- else if (m_bLayeredChanged)
- {
- ::ZeroMemory(m_pBackgroundBits, dwWidth * dwHeight * 4);
- CRenderClip clip;
- CRenderClip::GenerateClip(m_hDcBackground, rcLayeredClient, clip);
- CRenderEngine::DrawImageInfo(m_hDcBackground, this, rcLayeredClient, rcLayeredClient, &m_diLayered);
- }
- for (LONG y = rcClient.bottom - rcPaint.bottom; y < rcClient.bottom - rcPaint.top; ++y)
- {
- for (LONG x = rcPaint.left; x < rcPaint.right; ++x)
- {
- pOffscreenBits = (COLORREF*)(m_pOffscreenBits + y * dwWidth + x);
- pBackgroundBits = m_pBackgroundBits + y * dwWidth + x;
- A = (BYTE)((*pBackgroundBits) >> 24);
- R = (BYTE)((*pOffscreenBits) >> 16) * A / 255;
- G = (BYTE)((*pOffscreenBits) >> 8) * A / 255;
- B = (BYTE)(*pOffscreenBits) * A / 255;
- *pOffscreenBits = RGB(B, G, R) + ((DWORD)A << 24);
- }
- }
- }
- }
- else
- {
- for (LONG y = rcClient.bottom - rcPaint.bottom; y < rcClient.bottom - rcPaint.top; ++y)
- {
- for (LONG x = rcPaint.left; x < rcPaint.right; ++x)
- {
- int i = (y * dwWidth + x) * 4;
- if ((m_pOffscreenBits[i + 3] == 0) && (m_pOffscreenBits[i + 0] != 0 || m_pOffscreenBits[i + 1] != 0 || m_pOffscreenBits[i + 2] != 0))
- m_pOffscreenBits[i + 3] = 255;
- }
- }
- }
- BLENDFUNCTION bf = { AC_SRC_OVER, 0, m_nOpacity, AC_SRC_ALPHA };
- POINT ptPos = { rcWnd.left, rcWnd.top };
- SIZE sizeWnd = { dwWidth, dwHeight };
- POINT ptSrc = { 0, 0 };
- g_fUpdateLayeredWindow(m_hWndPaint, m_hDcPaint, &ptPos, &sizeWnd, m_hDcOffscreen, &ptSrc, 0, &bf, ULW_ALPHA);
- }
- else
- {
- ::BitBlt(m_hDcPaint, rcPaint.left, rcPaint.top, rcPaint.right - rcPaint.left, rcPaint.bottom - rcPaint.top, m_hDcOffscreen, rcPaint.left, rcPaint.top, SRCCOPY);
- }
- ::SelectObject(m_hDcOffscreen, hOldBitmap);
- if (m_bShowUpdateRect && !m_bLayered)
- {
- HPEN hOldPen = (HPEN)::SelectObject(m_hDcPaint, m_hUpdateRectPen);
- ::SelectObject(m_hDcPaint, ::GetStockObject(HOLLOW_BRUSH));
- ::Rectangle(m_hDcPaint, rcPaint.left, rcPaint.top, rcPaint.right, rcPaint.bottom);
- ::SelectObject(m_hDcPaint, hOldPen);
- }
- }
- else
- {
- // A standard paint job
- int iSaveDC = ::SaveDC(m_hDcPaint);
- m_pRoot->Paint(m_hDcPaint, rcPaint, NULL);
- for (int i = 0; i < m_aPostPaintControls.GetSize(); i++)
- {
- CControlUI* pPostPaintControl = static_cast<CControlUI*>(m_aPostPaintControls[i]);
- pPostPaintControl->DoPostPaint(m_hDcPaint, rcPaint);
- }
- ::RestoreDC(m_hDcPaint, iSaveDC);
- }
- // All Done!
- ::EndPaint(m_hWndPaint, &ps);
- // 绘制结束
- SetPainting(false);
- m_bLayeredChanged = false;
- if (m_bUpdateNeeded) Invalidate();
- // 发送窗口大小改变消息
- if (bNeedSizeMsg)
- {
- this->SendNotify(m_pRoot, DUI_MSGTYPE_WINDOWSIZE, 0, 0, true);
- }
- return true;
- }
- case WM_PRINTCLIENT:
- {
- if (m_pRoot == NULL) break;
- RECT rcClient;
- ::GetClientRect(m_hWndPaint, &rcClient);
- HDC hDC = (HDC)wParam;
- int save = ::SaveDC(hDC);
- m_pRoot->Paint(hDC, rcClient, NULL);
- if ((lParam & PRF_CHILDREN) != 0)
- {
- HWND hWndChild = ::GetWindow(m_hWndPaint, GW_CHILD);
- while (hWndChild != NULL)
- {
- RECT rcPos = { 0 };
- ::GetWindowRect(hWndChild, &rcPos);
- ::MapWindowPoints(HWND_DESKTOP, m_hWndPaint, reinterpret_cast<LPPOINT>(&rcPos), 2);
- ::SetWindowOrgEx(hDC, -rcPos.left, -rcPos.top, NULL);
- ::SendMessage(hWndChild, WM_PRINT, wParam, lParam | PRF_NONCLIENT);
- hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT);
- }
- }
- ::RestoreDC(hDC, save);
- }
- break;
- case WM_GETMINMAXINFO:
- {
- MONITORINFO Monitor = {};
- Monitor.cbSize = sizeof(Monitor);
- ::GetMonitorInfo(::MonitorFromWindow(m_hWndPaint, MONITOR_DEFAULTTOPRIMARY), &Monitor);
- RECT rcWork = Monitor.rcWork;
- if (Monitor.dwFlags != MONITORINFOF_PRIMARY)
- {
- ::OffsetRect(&rcWork, -rcWork.left, -rcWork.top);
- }
- LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
- if (m_szMinWindow.cx > 0) lpMMI->ptMinTrackSize.x = m_szMinWindow.cx;
- if (m_szMinWindow.cy > 0) lpMMI->ptMinTrackSize.y = m_szMinWindow.cy;
- if (m_szMaxWindow.cx > 0) lpMMI->ptMaxTrackSize.x = m_szMaxWindow.cx;
- if (m_szMaxWindow.cy > 0) lpMMI->ptMaxTrackSize.y = m_szMaxWindow.cy;
- if (m_szMaxWindow.cx > 0) lpMMI->ptMaxSize.x = m_szMaxWindow.cx;
- if (m_szMaxWindow.cy > 0) lpMMI->ptMaxSize.y = m_szMaxWindow.cy;
- }
- break;
- case WM_SIZE:
- {
- if (m_pFocus != NULL)
- {
- TEventUI event = { 0 };
- event.Type = UIEVENT_WINDOWSIZE;
- event.pSender = m_pFocus;
- event.dwTimestamp = ::GetTickCount();
- m_pFocus->Event(event);
- }
- if (m_pRoot != NULL) m_pRoot->NeedUpdate();
- }
- return true;
- case WM_TIMER:
- {
- for (int i = 0; i < m_aTimers.GetSize(); i++)
- {
- const TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
- if (pTimer->hWnd == m_hWndPaint &&
- pTimer->uWinTimer == LOWORD(wParam) &&
- pTimer->bKilled == false)
- {
- TEventUI event = { 0 };
- event.Type = UIEVENT_TIMER;
- event.pSender = pTimer->pSender;
- event.dwTimestamp = ::GetTickCount();
- event.ptMouse = m_ptLastMousePos;
- event.wKeyState = MapKeyState();
- event.wParam = pTimer->nLocalID;
- event.lParam = lParam;
- pTimer->pSender->Event(event);
- break;
- }
- }
- }
- break;
- case WM_MOUSEHOVER:
- {
- if (m_bSuppressHoverAfterTouch) return true;
- m_bMouseTracking = false;
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- CControlUI* pHover = FindControl(pt);
- if (pHover == NULL) break;
- // Generate mouse hover event
- if (m_pEventHover != NULL)
- {
- TEventUI event = { 0 };
- event.Type = UIEVENT_MOUSEHOVER;
- event.pSender = m_pEventHover;
- event.wParam = wParam;
- event.lParam = lParam;
- event.dwTimestamp = ::GetTickCount();
- event.ptMouse = pt;
- event.wKeyState = MapKeyState();
- m_pEventHover->Event(event);
- }
- // Create tooltip information
- CDuiString sToolTip = pHover->GetToolTip();
- if (sToolTip.IsEmpty()) return true;
- ::ZeroMemory(&m_ToolTip, sizeof(TOOLINFO));
- m_ToolTip.cbSize = sizeof(TOOLINFO);
- m_ToolTip.uFlags = TTF_IDISHWND;
- m_ToolTip.hwnd = m_hWndPaint;
- m_ToolTip.uId = (UINT_PTR)m_hWndPaint;
- m_ToolTip.hinst = m_hInstance;
- m_ToolTip.lpszText = const_cast<LPTSTR>((LPCTSTR)sToolTip);
- m_ToolTip.rect = pHover->GetPos();
- if (m_hwndTooltip == NULL)
- {
- 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);
- ::SendMessage(m_hwndTooltip, TTM_ADDTOOL, 0, (LPARAM)&m_ToolTip);
- ::SendMessage(m_hwndTooltip, TTM_SETMAXTIPWIDTH, 0, pHover->GetToolTipWidth());
- }
- if (!::IsWindowVisible(m_hwndTooltip))
- {
- ::SendMessage(m_hwndTooltip, TTM_SETTOOLINFO, 0, (LPARAM)&m_ToolTip);
- ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&m_ToolTip);
- }
- }
- return true;
- case WM_MOUSELEAVE:
- {
- TouchTrace::Log(_T("WM_MOUSELEAVE tracking=%d hover=%s leaveArr=%d click=%s"),
- (int)m_bMouseTracking,
- m_pEventHover != NULL ? m_pEventHover->GetName().GetData() : _T("null"),
- m_aNeedMouseLeaveNeeded.GetSize(),
- m_pEventClick != NULL ? m_pEventClick->GetName().GetData() : _T("null"));
- if (m_hwndTooltip != NULL) ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ToolTip);
- // 触屏抬起刚清过悬浮态时,不要执行下面这段“自回发 WM_MOUSEMOVE”:
- // 光标通常仍在窗口内 → 会立刻重建悬浮态,导致“悬浮态不消失/多控件同时悬浮”。
- if (!m_bSuppressHoverAfterTouch && m_bMouseTracking)
- {
- POINT pt = { 0 };
- RECT rcWnd = { 0 };
- ::GetCursorPos(&pt);
- ::GetWindowRect(m_hWndPaint, &rcWnd);
- if (!::IsIconic(m_hWndPaint) && ::GetActiveWindow() == m_hWndPaint && ::PtInRect(&rcWnd, pt))
- {
- if (::SendMessage(m_hWndPaint, WM_NCHITTEST, 0, MAKELPARAM(pt.x, pt.y)) == HTCLIENT)
- {
- ::ScreenToClient(m_hWndPaint, &pt);
- ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, MAKELPARAM(pt.x, pt.y));
- }
- else
- ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, (LPARAM)-1);
- }
- else
- ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, (LPARAM)-1);
- }
- m_bMouseTracking = false;
- }
- break;
- case WM_MOUSEMOVE:
- {
- const bool bPromotedTouchMove = IsPromotedTouchMouseMessage();
- if (bPromotedTouchMove && m_pEventTouch != NULL)
- {
- POINT ptTouch = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- const int kTouchSlop = 8;
- const int dxTouch = ptTouch.x - m_ptTouchStart.x;
- const int dyTouch = ptTouch.y - m_ptTouchStart.y;
- if (dxTouch > kTouchSlop || dxTouch < -kTouchSlop
- || dyTouch > kTouchSlop || dyTouch < -kTouchSlop)
- m_bTouchMoved = true;
- m_ptLastMousePos = ptTouch;
- // [WM_TOUCH 归属权] 这条被吞掉的"触摸提升鼠标移动"其实是系统
- // 用来触发 WM_TOUCH 翻译的信号(见 UIManager.h 的长注释)。
- // 吞掉它没问题,但必须补一次原窗口过程的副作用,
- // 否则本窗口(尤其 #32770 真模态弹框)后续再也收不到 WM_TOUCH。
- RelayDefWindowProc(uMsg, wParam, lParam, _T("promoted-touch-move/tracking"));
- return true;
- }
- if (m_bSuppressHoverAfterTouch)
- {
- m_ptLastMousePos = POINT{ GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- // [WM_TOUCH 归属权] 同上:抬手后抑制悬浮复活时丢弃的合成移动,
- // 也必须补调一次,否则 WM_TOUCH 翻译链会在这段时间里断掉。
- RelayDefWindowProc(uMsg, wParam, lParam, _T("suppress-hover-after-touch"));
- return true;
- }
- if ((INT_PTR)lParam == -1)
- break;
- POINT ptCheck = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- if (m_bSuppressHoverAfterTouch)
- {
- // 合成消息通常仍停在触摸结束点;只有明显离开该点的鼠标移动
- // 才能重新建立鼠标悬浮语义。
- const int dx = ptCheck.x - m_ptLastTouchPos.x;
- const int dy = ptCheck.y - m_ptLastTouchPos.y;
- if (dx <= 8 && dx >= -8 && dy <= 8 && dy >= -8)
- break;
- m_bSuppressHoverAfterTouch = false;
- }
- // Start tracking this entire window again...
- if (!m_bMouseTracking)
- {
- TRACKMOUSEEVENT tme = { 0 };
- tme.cbSize = sizeof(TRACKMOUSEEVENT);
- tme.dwFlags = TME_HOVER | TME_LEAVE;
- tme.hwndTrack = m_hWndPaint;
- tme.dwHoverTime = m_hwndTooltip == NULL ? m_iHoverTime : (DWORD) ::SendMessage(m_hwndTooltip, TTM_GETDELAYTIME, TTDT_INITIAL, 0L);
- _TrackMouseEvent(&tme);
- m_bMouseTracking = true;
- }
- // Generate the appropriate mouse messages
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- // 是否移动
- bool bNeedDrag = true;
- if (m_ptLastMousePos.x == pt.x && m_ptLastMousePos.y == pt.y)
- {
- bNeedDrag = false;
- }
- // 记录鼠标位置
- m_ptLastMousePos = pt;
- CControlUI* pNewHover = FindControl(pt);
- if (pNewHover != NULL && pNewHover->GetManager() != this) break;
- // 拖拽事件
- if (bNeedDrag && m_bDragMode && wParam == MK_LBUTTON)
- {
- // 释放Capture
- ::ReleaseCapture();
- // 接口
- if (m_pDragDrop != NULL && m_pDragDrop->OnDragDrop(m_pEventClick))
- {
- m_bDragMode = false;
- break;
- }
- CIDropSource* pdsrc = new CIDropSource;
- if (pdsrc == NULL) return 0;
- pdsrc->AddRef();
- CIDataObject* pdobj = new CIDataObject(pdsrc);
- if (pdobj == NULL) return 0;
- pdobj->AddRef();
- FORMATETC fmtetc = { 0 };
- STGMEDIUM medium = { 0 };
- fmtetc.dwAspect = DVASPECT_CONTENT;
- fmtetc.lindex = -1;
- fmtetc.cfFormat = CF_BITMAP;
- fmtetc.tymed = TYMED_GDI;
- //////////////////////////////////////
- HBITMAP hBitmap = (HBITMAP)OleDuplicateData(m_hDragBitmap, fmtetc.cfFormat, NULL);
- medium.hBitmap = hBitmap;
- pdobj->SetData(&fmtetc, &medium, FALSE);
- //////////////////////////////////////
- BITMAP bmap;
- GetObject(hBitmap, sizeof(BITMAP), &bmap);
- RECT rc = { 0, 0, bmap.bmWidth, bmap.bmHeight };
- fmtetc.cfFormat = CF_ENHMETAFILE;
- fmtetc.tymed = TYMED_ENHMF;
- HDC hMetaDC = CreateEnhMetaFile(m_hDcPaint, NULL, NULL, NULL);
- HDC hdcMem = CreateCompatibleDC(m_hDcPaint);
- HGDIOBJ hOldBmp = ::SelectObject(hdcMem, hBitmap);
- ::BitBlt(hMetaDC, 0, 0, rc.right, rc.bottom, hdcMem, 0, 0, SRCCOPY);
- ::SelectObject(hdcMem, hOldBmp);
- medium.hEnhMetaFile = CloseEnhMetaFile(hMetaDC);
- DeleteDC(hdcMem);
- medium.tymed = TYMED_ENHMF;
- pdobj->SetData(&fmtetc, &medium, TRUE);
- //////////////////////////////////////
- CDragSourceHelper dragSrcHelper;
- POINT ptDrag = { 0 };
- ptDrag.x = bmap.bmWidth / 2;
- ptDrag.y = bmap.bmHeight / 2;
- dragSrcHelper.InitializeFromBitmap(hBitmap, ptDrag, rc, pdobj); //will own the bmp
- DWORD dwEffect;
- HRESULT hr = ::DoDragDrop(pdobj, pdsrc, DROPEFFECT_COPY | DROPEFFECT_MOVE, &dwEffect);
- if (dwEffect) pdsrc->Release();
- else delete pdsrc;
- pdobj->Release();
- m_bDragMode = false;
- break;
- }
- TEventUI event = { 0 };
- event.ptMouse = pt;
- event.wParam = wParam;
- event.lParam = lParam;
- event.dwTimestamp = ::GetTickCount();
- event.wKeyState = MapKeyState();
- // 悬浮态总闸:触摸提升出来的合成鼠标消息(MOUSEEVENTF_FROMTOUCH)不参与
- // 悬浮 enter/leave。触摸没有“光标停留”语义,一旦让它参与,就会出现
- // “点过的控件一直高亮 / 连点几个控件后多个同时高亮、怎么点都清不掉”的顽疾
- // (按钮的 UISTATE_HOT、列表项的 UISTATE_HOT 全靠 MOUSEENTER/MOUSELEAVE 维护)。
- // 该判断只影响悬浮段,消息依旧往下走 → 点击/拖拽状态机不受影响。
- if (!IsCaptured() && !bPromotedTouchMove)
- {
- pNewHover = FindControl(pt);
- if (pNewHover != NULL && pNewHover->GetManager() != this) break;
- if (pNewHover != m_pEventHover && m_pEventHover != NULL)
- {
- event.Type = UIEVENT_MOUSELEAVE;
- event.pSender = m_pEventHover;
- CStdPtrArray aNeedMouseLeaveNeeded(m_aNeedMouseLeaveNeeded.GetSize());
- aNeedMouseLeaveNeeded.Resize(m_aNeedMouseLeaveNeeded.GetSize());
- ::CopyMemory(aNeedMouseLeaveNeeded.GetData(), m_aNeedMouseLeaveNeeded.GetData(), m_aNeedMouseLeaveNeeded.GetSize() * sizeof(LPVOID));
- for (int i = 0; i < aNeedMouseLeaveNeeded.GetSize(); i++)
- {
- static_cast<CControlUI*>(aNeedMouseLeaveNeeded[i])->Event(event);
- }
- TouchTrace::Log(_T("MOUSEMOVE-LEAVE old=%s leaveArr=%d new=%s pt=%d,%d"),
- m_pEventHover->GetName().GetData(), m_aNeedMouseLeaveNeeded.GetSize(),
- pNewHover != NULL ? pNewHover->GetName().GetData() : _T("null"), pt.x, pt.y);
- m_pEventHover->Event(event);
- m_pEventHover = NULL;
- if (m_hwndTooltip != NULL) ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ToolTip);
- }
- if (pNewHover != m_pEventHover && pNewHover != NULL)
- {
- event.Type = UIEVENT_MOUSEENTER;
- event.pSender = pNewHover;
- pNewHover->Event(event);
- m_pEventHover = pNewHover;
- TouchTrace::Log(_T("HOVER-ENTER %s cls=%s pt=%d,%d"),
- pNewHover->GetName().GetData(), pNewHover->GetClass(), pt.x, pt.y);
- }
- }
- if (m_pEventClick != NULL)
- {
- event.Type = UIEVENT_MOUSEMOVE;
- event.pSender = m_pEventClick;
- m_pEventClick->Event(event);
- }
- else if (pNewHover != NULL)
- {
- event.Type = UIEVENT_MOUSEMOVE;
- event.pSender = pNewHover;
- pNewHover->Event(event);
- }
- }
- break;
- case WM_LBUTTONDOWN:
- {
- TouchTrace::Log(_T("WM_LBUTTONDOWN promoted=%d pt=%d,%d eventTouch=%s eventClick=%s"),
- (int)IsPromotedTouchMouseMessage(), GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam),
- m_pEventTouch != NULL ? m_pEventTouch->GetName().GetData() : _T("null"),
- m_pEventClick != NULL ? m_pEventClick->GetName().GetData() : _T("null"));
- // WM_TOUCH 后 Windows 可能补发 WM_LBUTTONDOWN。触摸点击由 TOUCHUP
- // 的 tap 判定统一处理,不能让这条合成消息提前建立鼠标点击状态。
- if (m_pEventTouch != NULL || m_bSuppressPromotedTouchMouse)
- break;
- // We alway set focus back to our app (this helps
- // when Win32 child windows are placed on the dialog
- // and we need to remove them on focus change).
- if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
- if (m_pRoot == NULL) break;
- // 查找控件
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- m_ptLastMousePos = pt;
- CControlUI* pControl = FindControl(pt);
- if (pControl == NULL)
- {
- TouchTrace::Log(_T("LBUTTONDOWN no control hwnd=%p pt=%d,%d root=%p"),
- (void*)m_hWndPaint, pt.x, pt.y, (void*)m_pRoot);
- break;
- }
- if (pControl->GetManager() != this)
- {
- TouchTrace::Log(_T("LBUTTONDOWN foreign control hwnd=%p pt=%d,%d name=%s manager=%p self=%p"),
- (void*)m_hWndPaint, pt.x, pt.y, pControl->GetName().GetData(),
- (void*)pControl->GetManager(), (void*)this);
- break;
- }
- TouchTrace::Log(_T("LBUTTONDOWN hit hwnd=%p name=%s class=%s pos=%d,%d,%d,%d enabled=%d visible=%d"),
- (void*)m_hWndPaint, pControl->GetName().GetData(), pControl->GetClass(),
- pControl->GetPos().left, pControl->GetPos().top, pControl->GetPos().right, pControl->GetPos().bottom,
- (int)pControl->IsEnabled(), (int)pControl->IsVisible());
- // 准备拖拽
- if (m_bDragDrop && pControl->IsDragEnabled())
- {
- m_bDragMode = true;
- if (m_hDragBitmap != NULL)
- {
- ::DeleteObject(m_hDragBitmap);
- m_hDragBitmap = NULL;
- }
- m_hDragBitmap = CRenderEngine::GenerateBitmap(this, pControl, pControl->GetPos());
- }
- // 开启捕获
- SetCapture();
- // 事件处理
- m_pEventClick = pControl;
- pControl->SetFocus();
- TEventUI event = { 0 };
- event.Type = UIEVENT_BUTTONDOWN;
- event.pSender = pControl;
- event.wParam = wParam;
- event.lParam = lParam;
- event.ptMouse = pt;
- event.wKeyState = (WORD)wParam;
- event.dwTimestamp = ::GetTickCount();
- pControl->Event(event);
- }
- break;
- case WM_LBUTTONDBLCLK:
- {
- if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- m_ptLastMousePos = pt;
- CControlUI* pControl = FindControl(pt);
- if (pControl == NULL) break;
- if (pControl->GetManager() != this) break;
- //SetCapture();
- TEventUI event = { 0 };
- event.Type = UIEVENT_DBLCLICK;
- event.pSender = pControl;
- event.ptMouse = pt;
- event.wParam = wParam;
- event.lParam = lParam;
- event.wKeyState = (WORD)wParam;
- event.dwTimestamp = ::GetTickCount();
- pControl->Event(event);
- m_pEventClick = pControl;
- }
- break;
- case WM_LBUTTONUP:
- {
- TouchTrace::Log(_T("WM_LBUTTONUP promoted=%d pt=%d,%d eventTouch=%s eventClick=%s moved=%d"),
- (int)IsPromotedTouchMouseMessage(), GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam),
- m_pEventTouch != NULL ? m_pEventTouch->GetName().GetData() : _T("null"),
- m_pEventClick != NULL ? m_pEventClick->GetName().GetData() : _T("null"),
- (int)m_bTouchMoved);
- const bool bPromotedTouchUp = IsPromotedTouchMouseMessage();
- // 触摸手势进行中 / 刚由触摸链合成过点击 ⇒ 这条提升出来的鼠标抬起
- // 一律不参与点击判定,直接丢掉(tap/drag 判定只在触摸链里做一次)。
- //
- // ⚠️ 副作用,别忽视:这条 break 也【丢掉了 ReleaseCapture()】。
- // 所以捕获必须由触摸链自己释放 —— BeginTouchSlideGesture 里 SetCapture(),
- // 对应的 ReleaseCapture() 在 EndTouchSlideGesture(命中控件那条路径)里。
- // 若哪天把 End 里的 ReleaseCapture 挪走,这里就会漏捕获(表现为
- // 后续所有鼠标消息都跑到本窗口、点别处不响应)。
- if (m_pEventTouch != NULL || m_bSuppressPromotedTouchMouse)
- {
- m_bSuppressPromotedTouchMouse = false;
- m_dwSuppressTick = 0;
- break;
- }
- POINT ptButtonUp = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- if (!bPromotedTouchUp)
- {
- // 普通鼠标点击也不会天然产生 MOUSELEAVE。
- // 点击后光标仍停留在控件上时,必须主动清除 HOT 状态,
- // 否则直到移动到下一个控件前都会保持悬浮态。
- ClearTouchHoverState(ptButtonUp);
- }
- if (bPromotedTouchUp)
- {
- ClearTouchHoverState(ptButtonUp);
- m_bSuppressHoverAfterTouch = true;
- m_bMouseTracking = false;
- }
- TouchTrace::Log(_T("LBUTTONUP moved=%d click=%s hover=%s leaveArr=%d capture=%d"),
- (int)m_bTouchMoved,
- m_pEventClick != NULL ? m_pEventClick->GetName().GetData() : _T("null"),
- m_pEventHover != NULL ? m_pEventHover->GetName().GetData() : _T("null"),
- m_aNeedMouseLeaveNeeded.GetSize(), (int)m_bMouseCapture);
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- m_ptLastMousePos = pt;
- CControlUI* pClick = m_pEventClick;
- const bool bTouchDragged = m_bTouchMoved;
- m_pEventClick = NULL;
- m_bTouchMoved = false; // 用完即清,绝不影响后续真实鼠标点击
- if (pClick == NULL)
- {
- TouchTrace::Log(_T("LBUTTONUP no click hwnd=%p pt=%d,%d"),
- (void*)m_hWndPaint, pt.x, pt.y);
- ReleaseCapture();
- break;
- }
- ReleaseCapture();
- TEventUI event = { 0 };
- event.Type = UIEVENT_BUTTONUP;
- event.pSender = pClick;
- event.wParam = wParam;
- event.lParam = lParam;
- event.ptMouse = pt;
- if (bTouchDragged && pClick->GetInterface(DUI_CTR_SCROLLBAR) == NULL)
- {
- // 清除按钮的 PUSHED/CAPTURED 状态,但让 CButtonUI 的命中测试失败,
- // 从而不会在滑动结束时调用 Activate。
- event.ptMouse.x = -1;
- event.ptMouse.y = -1;
- }
- event.wKeyState = (WORD)wParam;
- event.dwTimestamp = ::GetTickCount();
- pClick->Event(event);
- }
- break;
- case WM_RBUTTONDOWN:
- {
- if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- m_ptLastMousePos = pt;
- CControlUI* pControl = FindControl(pt);
- if (pControl == NULL) break;
- if (pControl->GetManager() != this) break;
- pControl->SetFocus();
- SetCapture();
- TEventUI event = { 0 };
- event.Type = UIEVENT_RBUTTONDOWN;
- event.pSender = pControl;
- event.wParam = wParam;
- event.lParam = lParam;
- event.ptMouse = pt;
- event.wKeyState = (WORD)wParam;
- event.dwTimestamp = ::GetTickCount();
- pControl->Event(event);
- m_pEventRClick = pControl;
- }
- break;
- case WM_RBUTTONUP:
- {
- if (m_bMouseCapture) ReleaseCapture();
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- m_ptLastMousePos = pt;
- m_pEventRClick = FindControl(pt);
- if (m_pEventRClick == NULL) break;
- TEventUI event = { 0 };
- event.Type = UIEVENT_RBUTTONUP;
- event.pSender = m_pEventRClick;
- event.wParam = wParam;
- event.lParam = lParam;
- event.ptMouse = pt;
- event.wKeyState = (WORD)wParam;
- event.dwTimestamp = ::GetTickCount();
- m_pEventRClick->Event(event);
- }
- break;
- case WM_MBUTTONDOWN:
- {
- if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- m_ptLastMousePos = pt;
- CControlUI* pControl = FindControl(pt);
- if (pControl == NULL) break;
- if (pControl->GetManager() != this) break;
- pControl->SetFocus();
- SetCapture();
- TEventUI event = { 0 };
- event.Type = UIEVENT_MBUTTONDOWN;
- event.pSender = pControl;
- event.wParam = wParam;
- event.lParam = lParam;
- event.ptMouse = pt;
- event.wKeyState = (WORD)wParam;
- event.dwTimestamp = ::GetTickCount();
- pControl->Event(event);
- m_pEventClick = pControl;
- }
- break;
- case WM_MBUTTONUP:
- {
- if (m_bMouseCapture) ReleaseCapture();
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- m_ptLastMousePos = pt;
- m_pEventClick = FindControl(pt);
- if (m_pEventClick == NULL) break;
- TEventUI event = { 0 };
- event.Type = UIEVENT_MBUTTONUP;
- event.pSender = m_pEventClick;
- event.wParam = wParam;
- event.lParam = lParam;
- event.ptMouse = pt;
- event.wKeyState = (WORD)wParam;
- event.dwTimestamp = ::GetTickCount();
- m_pEventClick->Event(event);
- }
- break;
- case WM_CONTEXTMENU:
- {
- if (m_pRoot == NULL) break;
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- ::ScreenToClient(m_hWndPaint, &pt);
- m_ptLastMousePos = pt;
- if (m_pEventRClick == NULL) break;
- ReleaseCapture();
- TEventUI event = { 0 };
- event.Type = UIEVENT_CONTEXTMENU;
- event.pSender = m_pEventRClick;
- event.wParam = wParam;
- event.lParam = lParam;
- event.ptMouse = pt;
- event.wKeyState = (WORD)wParam;
- event.lParam = (LPARAM)m_pEventRClick;
- event.dwTimestamp = ::GetTickCount();
- m_pEventRClick->Event(event);
- m_pEventRClick = NULL;
- }
- break;
- case WM_TOUCH:
- {
- OnTouchInput(wParam, lParam);
- lRes = 0;
- return true;
- }
- case WM_MOUSEWHEEL:
- {
- if (m_pRoot == NULL) break;
- POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
- ::ScreenToClient(m_hWndPaint, &pt);
- m_ptLastMousePos = pt;
- CControlUI* pControl = FindControl(pt);
- if (pControl == NULL) break;
- if (pControl->GetManager() != this) break;
- int zDelta = (int)(short)HIWORD(wParam);
- TEventUI event = { 0 };
- event.Type = UIEVENT_SCROLLWHEEL;
- event.pSender = pControl;
- event.wParam = MAKEWPARAM(zDelta < 0 ? SB_LINEDOWN : SB_LINEUP, zDelta);
- event.lParam = lParam;
- event.ptMouse = pt;
- event.wKeyState = MapKeyState();
- event.dwTimestamp = ::GetTickCount();
- pControl->Event(event);
- // Let's make sure that the scroll item below the cursor is the same as before...
- ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, (LPARAM)MAKELPARAM(m_ptLastMousePos.x, m_ptLastMousePos.y));
- }
- break;
- case WM_CHAR:
- {
- if (m_pRoot == NULL) break;
- if (m_pFocus == NULL) break;
- TEventUI event = { 0 };
- event.Type = UIEVENT_CHAR;
- event.pSender = m_pFocus;
- event.wParam = wParam;
- event.lParam = lParam;
- event.chKey = (TCHAR)wParam;
- event.ptMouse = m_ptLastMousePos;
- event.wKeyState = MapKeyState();
- event.dwTimestamp = ::GetTickCount();
- m_pFocus->Event(event);
- }
- break;
- case WM_KEYDOWN:
- {
- if (m_pRoot == NULL) break;
- if (m_pFocus == NULL) break;
- TEventUI event = { 0 };
- event.Type = UIEVENT_KEYDOWN;
- event.pSender = m_pFocus;
- event.wParam = wParam;
- event.lParam = lParam;
- event.chKey = (TCHAR)wParam;
- event.ptMouse = m_ptLastMousePos;
- event.wKeyState = MapKeyState();
- event.dwTimestamp = ::GetTickCount();
- m_pFocus->Event(event);
- m_pEventKey = m_pFocus;
- }
- break;
- case WM_KEYUP:
- {
- if (m_pRoot == NULL) break;
- if (m_pEventKey == NULL) break;
- TEventUI event = { 0 };
- event.Type = UIEVENT_KEYUP;
- event.pSender = m_pEventKey;
- event.wParam = wParam;
- event.lParam = lParam;
- event.chKey = (TCHAR)wParam;
- event.ptMouse = m_ptLastMousePos;
- event.wKeyState = MapKeyState();
- event.dwTimestamp = ::GetTickCount();
- m_pEventKey->Event(event);
- m_pEventKey = NULL;
- }
- break;
- case WM_SETCURSOR:
- {
- if (m_pRoot == NULL) break;
- if (LOWORD(lParam) != HTCLIENT) break;
- if (m_bMouseCapture) return true;
- POINT pt = { 0 };
- ::GetCursorPos(&pt);
- ::ScreenToClient(m_hWndPaint, &pt);
- CControlUI* pControl = FindControl(pt);
- if (pControl == NULL) break;
- if ((pControl->GetControlFlags() & UIFLAG_SETCURSOR) == 0) break;
- TEventUI event = { 0 };
- event.Type = UIEVENT_SETCURSOR;
- event.pSender = pControl;
- event.wParam = wParam;
- event.lParam = lParam;
- event.ptMouse = pt;
- event.wKeyState = MapKeyState();
- event.dwTimestamp = ::GetTickCount();
- pControl->Event(event);
- }
- return true;
- case WM_SETFOCUS:
- {
- if (m_pFocus != NULL)
- {
- TEventUI event = { 0 };
- event.Type = UIEVENT_SETFOCUS;
- event.wParam = wParam;
- event.lParam = lParam;
- event.pSender = m_pFocus;
- event.dwTimestamp = ::GetTickCount();
- m_pFocus->Event(event);
- }
- break;
- }
- case WM_KILLFOCUS:
- {
- if (IsCaptured()) ReleaseCapture();
- break;
- }
- case WM_NOTIFY:
- {
- if (lParam == 0) break;
- LPNMHDR lpNMHDR = (LPNMHDR)lParam;
- if (lpNMHDR != NULL) lRes = ::SendMessage(lpNMHDR->hwndFrom, OCM__BASE + uMsg, wParam, lParam);
- return true;
- }
- break;
- case WM_COMMAND:
- {
- if (lParam == 0) break;
- HWND hWndChild = (HWND)lParam;
- lRes = ::SendMessage(hWndChild, OCM__BASE + uMsg, wParam, lParam);
- if (lRes != 0) return true;
- }
- break;
- case WM_CTLCOLOREDIT:
- case WM_CTLCOLORSTATIC:
- {
- // Refer To: http://msdn.microsoft.com/en-us/library/bb761691(v=vs.85).aspx
- // Read-only or disabled edit controls do not send the WM_CTLCOLOREDIT message; instead, they send the WM_CTLCOLORSTATIC message.
- if (lParam == 0) break;
- HWND hWndChild = (HWND)lParam;
- lRes = ::SendMessage(hWndChild, OCM__BASE + uMsg, wParam, lParam);
- if (lRes != 0) return true;
- }
- break;
- default:
- break;
- }
- return false;
- }
- bool CPaintManagerUI::IsUpdateNeeded() const
- {
- return m_bUpdateNeeded;
- }
- void CPaintManagerUI::NeedUpdate()
- {
- m_bUpdateNeeded = true;
- }
- void CPaintManagerUI::Invalidate()
- {
- RECT rcClient = { 0 };
- ::GetClientRect(m_hWndPaint, &rcClient);
- ::UnionRect(&m_rcLayeredUpdate, &m_rcLayeredUpdate, &rcClient);
- ::InvalidateRect(m_hWndPaint, NULL, FALSE);
- }
- void CPaintManagerUI::Invalidate(RECT& rcItem)
- {
- if (rcItem.left < 0) rcItem.left = 0;
- if (rcItem.top < 0) rcItem.top = 0;
- if (rcItem.right < rcItem.left) rcItem.right = rcItem.left;
- if (rcItem.bottom < rcItem.top) rcItem.bottom = rcItem.top;
- ::UnionRect(&m_rcLayeredUpdate, &m_rcLayeredUpdate, &rcItem);
- ::InvalidateRect(m_hWndPaint, &rcItem, FALSE);
- }
- bool CPaintManagerUI::IsValid()
- {
- return m_hWndPaint != NULL && m_pRoot != NULL;
- }
- bool CPaintManagerUI::AttachDialog(CControlUI* pControl)
- {
- ASSERT(::IsWindow(m_hWndPaint));
- // 创建阴影窗口
- m_shadow.Create(this);
- // Reset any previous attachment
- SetFocus(NULL);
- m_pEventKey = NULL;
- m_pEventHover = NULL;
- m_pEventClick = NULL;
- m_pEventRClick = NULL;
- // Remove the existing control-tree. We might have gotten inside this function as
- // a result of an event fired or similar, so we cannot just delete the objects and
- // pull the internal memory of the calling code. We'll delay the cleanup.
- if (m_pRoot != NULL)
- {
- m_aPostPaintControls.Empty();
- AddDelayedCleanup(m_pRoot);
- }
- // Set the dialog root element
- m_pRoot = pControl;
- // Go ahead...
- m_bUpdateNeeded = true;
- m_bFirstLayout = true;
- m_bFocusNeeded = true;
- // Initiate all control
- return InitControls(pControl);
- }
- bool CPaintManagerUI::InitControls(CControlUI* pControl, CControlUI* pParent /*= NULL*/)
- {
- ASSERT(pControl);
- if (pControl == NULL) return false;
- pControl->SetManager(this, pParent != NULL ? pParent : pControl->GetParent(), true);
- pControl->FindControl(__FindControlFromNameHash, this, UIFIND_ALL);
- return true;
- }
- void CPaintManagerUI::ReapObjects(CControlUI* pControl)
- {
- if (pControl == m_pEventKey) m_pEventKey = NULL;
- if (pControl == m_pEventHover) m_pEventHover = NULL;
- if (pControl == m_pEventClick) m_pEventClick = NULL;
- if (pControl == m_pEventRClick) m_pEventRClick = NULL;
- if (pControl == m_pFocus) m_pFocus = NULL;
- KillTimer(pControl);
- const CDuiString& sName = pControl->GetName();
- if (!sName.IsEmpty())
- {
- if (pControl == FindControl(sName)) m_mNameHash.Remove(sName);
- }
- for (int i = 0; i < m_aAsyncNotify.GetSize(); i++)
- {
- TNotifyUI* pMsg = static_cast<TNotifyUI*>(m_aAsyncNotify[i]);
- if (pMsg->pSender == pControl) pMsg->pSender = NULL;
- }
- }
- bool CPaintManagerUI::AddOptionGroup(LPCTSTR pStrGroupName, CControlUI* pControl)
- {
- LPVOID lp = m_mOptionGroup.Find(pStrGroupName);
- if (lp)
- {
- CStdPtrArray* aOptionGroup = static_cast<CStdPtrArray*>(lp);
- for (int i = 0; i < aOptionGroup->GetSize(); i++)
- {
- if (static_cast<CControlUI*>(aOptionGroup->GetAt(i)) == pControl)
- {
- return false;
- }
- }
- aOptionGroup->Add(pControl);
- }
- else
- {
- CStdPtrArray* aOptionGroup = new CStdPtrArray(6);
- aOptionGroup->Add(pControl);
- m_mOptionGroup.Insert(pStrGroupName, aOptionGroup);
- }
- return true;
- }
- CStdPtrArray* CPaintManagerUI::GetOptionGroup(LPCTSTR pStrGroupName)
- {
- LPVOID lp = m_mOptionGroup.Find(pStrGroupName);
- if (lp) return static_cast<CStdPtrArray*>(lp);
- return NULL;
- }
- void CPaintManagerUI::RemoveOptionGroup(LPCTSTR pStrGroupName, CControlUI* pControl)
- {
- LPVOID lp = m_mOptionGroup.Find(pStrGroupName);
- if (lp)
- {
- CStdPtrArray* aOptionGroup = static_cast<CStdPtrArray*>(lp);
- if (aOptionGroup == NULL) return;
- for (int i = 0; i < aOptionGroup->GetSize(); i++)
- {
- if (static_cast<CControlUI*>(aOptionGroup->GetAt(i)) == pControl)
- {
- aOptionGroup->Remove(i);
- break;
- }
- }
- if (aOptionGroup->IsEmpty())
- {
- delete aOptionGroup;
- m_mOptionGroup.Remove(pStrGroupName);
- }
- }
- }
- void CPaintManagerUI::RemoveAllOptionGroups()
- {
- CStdPtrArray* aOptionGroup;
- for (int i = 0; i < m_mOptionGroup.GetSize(); i++)
- {
- if (LPCTSTR key = m_mOptionGroup.GetAt(i))
- {
- aOptionGroup = static_cast<CStdPtrArray*>(m_mOptionGroup.Find(key));
- delete aOptionGroup;
- }
- }
- m_mOptionGroup.RemoveAll();
- }
- void CPaintManagerUI::MessageLoop()
- {
- MSG msg = { 0 };
- while (::GetMessage(&msg, NULL, 0, 0))
- {
- if (!CPaintManagerUI::TranslateMessage(&msg))
- {
- ::TranslateMessage(&msg);
- try
- {
- ::DispatchMessage(&msg);
- }
- catch (...)
- {
- DUITRACE(_T("EXCEPTION: %s(%d)\n"), __FILET__, __LINE__);
- #ifdef _DEBUG
- throw "CPaintManagerUI::MessageLoop";
- #endif
- }
- }
- }
- }
- void CPaintManagerUI::Term()
- {
- // 销毁资源管理器
- CResourceManager::GetInstance()->Release();
- CControlFactory::GetInstance()->Release();
- // 清理共享资源
- // 图片
- TImageInfo* data;
- for (int i = 0; i < m_SharedResInfo.m_ImageHash.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_ImageHash.GetAt(i))
- {
- data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(key, false));
- if (data)
- {
- CRenderEngine::FreeImage(data);
- data = NULL;
- }
- }
- }
- m_SharedResInfo.m_ImageHash.RemoveAll();
- // 字体
- TFontInfo* pFontInfo;
- for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key, false));
- if (pFontInfo)
- {
- ::DeleteObject(pFontInfo->hFont);
- delete pFontInfo;
- pFontInfo = NULL;
- }
- }
- }
- m_SharedResInfo.m_CustomFonts.RemoveAll();
- // 默认字体
- if (m_SharedResInfo.m_DefaultFontInfo.hFont != NULL)
- {
- ::DeleteObject(m_SharedResInfo.m_DefaultFontInfo.hFont);
- }
- // 样式
- CDuiString* pStyle;
- for (int i = 0; i < m_SharedResInfo.m_StyleHash.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_StyleHash.GetAt(i))
- {
- pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(key, false));
- if (pStyle)
- {
- delete pStyle;
- pStyle = NULL;
- }
- }
- }
- m_SharedResInfo.m_StyleHash.RemoveAll();
- // 样式
- CDuiString* pAttr;
- for (int i = 0; i < m_SharedResInfo.m_AttrHash.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_AttrHash.GetAt(i))
- {
- pAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Find(key, false));
- if (pAttr)
- {
- delete pAttr;
- pAttr = NULL;
- }
- }
- }
- m_SharedResInfo.m_AttrHash.RemoveAll();
- // 关闭ZIP
- if (m_bCachedResourceZip && m_hResourceZip != NULL)
- {
- CloseZip((HZIP)m_hResourceZip);
- m_hResourceZip = NULL;
- }
- if (m_cbZipBuf)
- {
- delete[] m_cbZipBuf;
- m_cbZipBuf = nullptr;
- }
- }
- CDPI* DuiLib::CPaintManagerUI::GetDPIObj()
- {
- if (m_pDPI == NULL)
- {
- m_pDPI = new CDPI;
- }
- return m_pDPI;
- }
- void DuiLib::CPaintManagerUI::SetDPI(int iDPI)
- {
- int scale1 = GetDPIObj()->GetScale();
- GetDPIObj()->SetScale(iDPI);
- int scale2 = GetDPIObj()->GetScale();
- ResetDPIAssets();
- RECT rcWnd = { 0 };
- ::GetWindowRect(GetPaintWindow(), &rcWnd);
- RECT* prcNewWindow = &rcWnd;
- if (!::IsZoomed(GetPaintWindow()))
- {
- RECT rc = rcWnd;
- rc.right = rcWnd.left + (rcWnd.right - rcWnd.left) * scale2 / scale1;
- rc.bottom = rcWnd.top + (rcWnd.bottom - rcWnd.top) * scale2 / scale1;
- prcNewWindow = &rc;
- }
- SetWindowPos(GetPaintWindow(), NULL, prcNewWindow->left, prcNewWindow->top, prcNewWindow->right - prcNewWindow->left, prcNewWindow->bottom - prcNewWindow->top, SWP_NOZORDER | SWP_NOACTIVATE);
- if (GetRoot() != NULL) GetRoot()->NeedUpdate();
- ::PostMessage(GetPaintWindow(), UIMSG_SET_DPI, 0, 0);
- }
- void DuiLib::CPaintManagerUI::SetAllDPI(int iDPI)
- {
- for (int i = 0; i < m_aPreMessages.GetSize(); i++)
- {
- CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
- pManager->SetDPI(iDPI);
- }
- }
- void DuiLib::CPaintManagerUI::ResetDPIAssets()
- {
- RemoveAllDrawInfos();
- RemoveAllImages();;
- for (int it = 0; it < m_ResInfo.m_CustomFonts.GetSize(); it++)
- {
- TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(m_ResInfo.m_CustomFonts[it]));
- RebuildFont(pFontInfo);
- }
- RebuildFont(&m_ResInfo.m_DefaultFontInfo);
- for (int it = 0; it < m_SharedResInfo.m_CustomFonts.GetSize(); it++)
- {
- TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(m_SharedResInfo.m_CustomFonts[it]));
- RebuildFont(pFontInfo);
- }
- RebuildFont(&m_SharedResInfo.m_DefaultFontInfo);
- CStdPtrArray* richEditList = FindSubControlsByClass(GetRoot(), _T("RichEditUI"));
- for (int i = 0; i < richEditList->GetSize(); i++)
- {
- CRichEditUI* pT = static_cast<CRichEditUI*>((*richEditList)[i]);
- pT->SetFont(pT->GetFont());
- }
- }
- void DuiLib::CPaintManagerUI::RebuildFont(TFontInfo* pFontInfo)
- {
- ::DeleteObject(pFontInfo->hFont);
- LOGFONT lf = { 0 };
- ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
- _tcsncpy(lf.lfFaceName, pFontInfo->sFontName, LF_FACESIZE);
- lf.lfCharSet = DEFAULT_CHARSET;
- lf.lfHeight = -GetDPIObj()->Scale(pFontInfo->iSize);
- lf.lfQuality = CLEARTYPE_QUALITY;
- if (pFontInfo->bBold) lf.lfWeight += FW_BOLD;
- if (pFontInfo->bUnderline) lf.lfUnderline = TRUE;
- if (pFontInfo->bItalic) lf.lfItalic = TRUE;
- HFONT hFont = ::CreateFontIndirect(&lf);
- pFontInfo->hFont = hFont;
- ::ZeroMemory(&(pFontInfo->tm), sizeof(pFontInfo->tm));
- if (m_hDcPaint)
- {
- HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
- ::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
- ::SelectObject(m_hDcPaint, hOldFont);
- }
- }
- CControlUI* CPaintManagerUI::GetFocus() const
- {
- return m_pFocus;
- }
- void CPaintManagerUI::SetFocus(CControlUI* pControl)
- {
- // Paint manager window has focus?
- HWND hFocusWnd = ::GetFocus();
- if (!m_bNoActivate && hFocusWnd != m_hWndPaint && pControl != m_pFocus) ::SetFocus(m_hWndPaint);
- // Already has focus?
- if (pControl == m_pFocus) return;
- // Remove focus from old control
- if (m_pFocus != NULL)
- {
- TEventUI event = { 0 };
- event.Type = UIEVENT_KILLFOCUS;
- event.pSender = pControl;
- event.dwTimestamp = ::GetTickCount();
- m_pFocus->Event(event);
- SendNotify(m_pFocus, DUI_MSGTYPE_KILLFOCUS);
- m_pFocus = NULL;
- }
- if (pControl == NULL) return;
- // Set focus to new control
- if (pControl != NULL
- && pControl->GetManager() == this
- && pControl->IsVisible()
- && pControl->IsEnabled())
- {
- m_pFocus = pControl;
- TEventUI event = { 0 };
- event.Type = UIEVENT_SETFOCUS;
- event.pSender = pControl;
- event.dwTimestamp = ::GetTickCount();
- m_pFocus->Event(event);
- SendNotify(m_pFocus, DUI_MSGTYPE_SETFOCUS);
- }
- }
- void CPaintManagerUI::SetFocusNeeded(CControlUI* pControl)
- {
- if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
- if (pControl == NULL) return;
- if (m_pFocus != NULL)
- {
- TEventUI event = { 0 };
- event.Type = UIEVENT_KILLFOCUS;
- event.pSender = pControl;
- event.dwTimestamp = ::GetTickCount();
- m_pFocus->Event(event);
- SendNotify(m_pFocus, DUI_MSGTYPE_KILLFOCUS);
- m_pFocus = NULL;
- }
- FINDTABINFO info = { 0 };
- info.pFocus = pControl;
- info.bForward = false;
- m_pFocus = m_pRoot->FindControl(__FindControlFromTab, &info, UIFIND_VISIBLE | UIFIND_ENABLED | UIFIND_ME_FIRST);
- m_bFocusNeeded = true;
- if (m_pRoot != NULL) m_pRoot->NeedUpdate();
- }
- bool CPaintManagerUI::SetTimer(CControlUI* pControl, UINT nTimerID, UINT uElapse)
- {
- ASSERT(pControl != NULL);
- ASSERT(uElapse > 0);
- for (int i = 0; i < m_aTimers.GetSize(); i++)
- {
- TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
- if (pTimer->pSender == pControl
- && pTimer->hWnd == m_hWndPaint
- && pTimer->nLocalID == nTimerID)
- {
- if (pTimer->bKilled == true)
- {
- if (::SetTimer(m_hWndPaint, pTimer->uWinTimer, uElapse, NULL))
- {
- pTimer->bKilled = false;
- return true;
- }
- return false;
- }
- return false;
- }
- }
- m_uTimerID = (++m_uTimerID) % 0xF0; //0xf1-0xfe特殊用途
- if (!::SetTimer(m_hWndPaint, m_uTimerID, uElapse, NULL)) return FALSE;
- TIMERINFO* pTimer = new TIMERINFO;
- if (pTimer == NULL) return FALSE;
- pTimer->hWnd = m_hWndPaint;
- pTimer->pSender = pControl;
- pTimer->nLocalID = nTimerID;
- pTimer->uWinTimer = m_uTimerID;
- pTimer->bKilled = false;
- return m_aTimers.Add(pTimer);
- }
- bool CPaintManagerUI::KillTimer(CControlUI* pControl, UINT nTimerID)
- {
- ASSERT(pControl != NULL);
- for (int i = 0; i < m_aTimers.GetSize(); i++)
- {
- TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
- if (pTimer->pSender == pControl
- && pTimer->hWnd == m_hWndPaint
- && pTimer->nLocalID == nTimerID)
- {
- if (pTimer->bKilled == false)
- {
- if (::IsWindow(m_hWndPaint)) ::KillTimer(pTimer->hWnd, pTimer->uWinTimer);
- pTimer->bKilled = true;
- return true;
- }
- }
- }
- return false;
- }
- void CPaintManagerUI::KillTimer(CControlUI* pControl)
- {
- ASSERT(pControl != NULL);
- int count = m_aTimers.GetSize();
- for (int i = 0, j = 0; i < count; i++)
- {
- TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i - j]);
- if (pTimer->pSender == pControl && pTimer->hWnd == m_hWndPaint)
- {
- if (pTimer->bKilled == false) ::KillTimer(pTimer->hWnd, pTimer->uWinTimer);
- delete pTimer;
- m_aTimers.Remove(i - j);
- j++;
- }
- }
- }
- void CPaintManagerUI::RemoveAllTimers()
- {
- for (int i = 0; i < m_aTimers.GetSize(); i++)
- {
- TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
- if (pTimer->hWnd == m_hWndPaint)
- {
- if (pTimer->bKilled == false)
- {
- if (::IsWindow(m_hWndPaint)) ::KillTimer(m_hWndPaint, pTimer->uWinTimer);
- }
- delete pTimer;
- }
- }
- m_aTimers.Empty();
- }
- void CPaintManagerUI::SetCapture()
- {
- ::SetCapture(m_hWndPaint);
- m_bMouseCapture = true;
- }
- void CPaintManagerUI::ReleaseCapture()
- {
- ::ReleaseCapture();
- m_bMouseCapture = false;
- m_bDragMode = false;
- }
- bool CPaintManagerUI::IsCaptured()
- {
- return m_bMouseCapture;
- }
- bool CPaintManagerUI::IsPainting()
- {
- return m_bIsPainting;
- }
- void CPaintManagerUI::SetPainting(bool bIsPainting)
- {
- m_bIsPainting = bIsPainting;
- }
- bool CPaintManagerUI::SetNextTabControl(bool bForward)
- {
- // If we're in the process of restructuring the layout we can delay the
- // focus calulation until the next repaint.
- if (m_bUpdateNeeded && bForward)
- {
- m_bFocusNeeded = true;
- ::InvalidateRect(m_hWndPaint, NULL, FALSE);
- return true;
- }
- // Find next/previous tabbable control
- FINDTABINFO info1 = { 0 };
- info1.pFocus = m_pFocus;
- info1.bForward = bForward;
- CControlUI* pControl = m_pRoot->FindControl(__FindControlFromTab, &info1, UIFIND_VISIBLE | UIFIND_ENABLED | UIFIND_ME_FIRST);
- if (pControl == NULL)
- {
- if (bForward)
- {
- // Wrap around
- FINDTABINFO info2 = { 0 };
- info2.pFocus = bForward ? NULL : info1.pLast;
- info2.bForward = bForward;
- pControl = m_pRoot->FindControl(__FindControlFromTab, &info2, UIFIND_VISIBLE | UIFIND_ENABLED | UIFIND_ME_FIRST);
- }
- else
- {
- pControl = info1.pLast;
- }
- }
- if (pControl != NULL) SetFocus(pControl);
- m_bFocusNeeded = false;
- return true;
- }
- bool CPaintManagerUI::AddNotifier(INotifyUI* pNotifier)
- {
- ASSERT(m_aNotifiers.Find(pNotifier) < 0);
- return m_aNotifiers.Add(pNotifier);
- }
- bool CPaintManagerUI::RemoveNotifier(INotifyUI* pNotifier)
- {
- for (int i = 0; i < m_aNotifiers.GetSize(); i++)
- {
- if (static_cast<INotifyUI*>(m_aNotifiers[i]) == pNotifier)
- {
- return m_aNotifiers.Remove(i);
- }
- }
- return false;
- }
- bool CPaintManagerUI::AddPreMessageFilter(IMessageFilterUI* pFilter)
- {
- ASSERT(m_aPreMessageFilters.Find(pFilter) < 0);
- return m_aPreMessageFilters.Add(pFilter);
- }
- bool CPaintManagerUI::RemovePreMessageFilter(IMessageFilterUI* pFilter)
- {
- for (int i = 0; i < m_aPreMessageFilters.GetSize(); i++)
- {
- if (static_cast<IMessageFilterUI*>(m_aPreMessageFilters[i]) == pFilter)
- {
- return m_aPreMessageFilters.Remove(i);
- }
- }
- return false;
- }
- bool CPaintManagerUI::AddMessageFilter(IMessageFilterUI* pFilter)
- {
- ASSERT(m_aMessageFilters.Find(pFilter) < 0);
- return m_aMessageFilters.Add(pFilter);
- }
- bool CPaintManagerUI::RemoveMessageFilter(IMessageFilterUI* pFilter)
- {
- for (int i = 0; i < m_aMessageFilters.GetSize(); i++)
- {
- if (static_cast<IMessageFilterUI*>(m_aMessageFilters[i]) == pFilter)
- {
- return m_aMessageFilters.Remove(i);
- }
- }
- return false;
- }
- int CPaintManagerUI::GetPostPaintCount() const
- {
- return m_aPostPaintControls.GetSize();
- }
- bool CPaintManagerUI::IsPostPaint(CControlUI* pControl)
- {
- return m_aPostPaintControls.Find(pControl) >= 0;
- }
- bool CPaintManagerUI::AddPostPaint(CControlUI* pControl)
- {
- ASSERT(m_aPostPaintControls.Find(pControl) < 0);
- return m_aPostPaintControls.Add(pControl);
- }
- bool CPaintManagerUI::RemovePostPaint(CControlUI* pControl)
- {
- for (int i = 0; i < m_aPostPaintControls.GetSize(); i++)
- {
- if (static_cast<CControlUI*>(m_aPostPaintControls[i]) == pControl)
- {
- return m_aPostPaintControls.Remove(i);
- }
- }
- return false;
- }
- bool CPaintManagerUI::SetPostPaintIndex(CControlUI* pControl, int iIndex)
- {
- RemovePostPaint(pControl);
- return m_aPostPaintControls.InsertAt(iIndex, pControl);
- }
- int CPaintManagerUI::GetNativeWindowCount() const
- {
- return m_aNativeWindow.GetSize();
- }
- bool CPaintManagerUI::AddNativeWindow(CControlUI* pControl, HWND hChildWnd)
- {
- if (pControl == NULL || hChildWnd == NULL) return false;
- RECT rcChildWnd = GetNativeWindowRect(hChildWnd);
- Invalidate(rcChildWnd);
- if (m_aNativeWindow.Find(hChildWnd) >= 0) return false;
- if (m_aNativeWindow.Add(hChildWnd))
- {
- m_aNativeWindowControl.Add(pControl);
- return true;
- }
- return false;
- }
- bool CPaintManagerUI::RemoveNativeWindow(HWND hChildWnd)
- {
- for (int i = 0; i < m_aNativeWindow.GetSize(); i++)
- {
- if (static_cast<HWND>(m_aNativeWindow[i]) == hChildWnd)
- {
- if (m_aNativeWindow.Remove(i))
- {
- m_aNativeWindowControl.Remove(i);
- return true;
- }
- return false;
- }
- }
- return false;
- }
- RECT CPaintManagerUI::GetNativeWindowRect(HWND hChildWnd)
- {
- RECT rcChildWnd;
- ::GetWindowRect(hChildWnd, &rcChildWnd);
- ::ScreenToClient(m_hWndPaint, (LPPOINT)(&rcChildWnd));
- ::ScreenToClient(m_hWndPaint, (LPPOINT)(&rcChildWnd) + 1);
- return rcChildWnd;
- }
- void CPaintManagerUI::AddDelayedCleanup(CControlUI* pControl)
- {
- if (pControl == NULL) return;
- pControl->SetManager(this, NULL, false);
- m_aDelayedCleanup.Add(pControl);
- PostAsyncNotify();
- }
- void CPaintManagerUI::AddMouseLeaveNeeded(CControlUI* pControl)
- {
- if (pControl == NULL) return;
- for (int i = 0; i < m_aNeedMouseLeaveNeeded.GetSize(); i++)
- {
- if (static_cast<CControlUI*>(m_aNeedMouseLeaveNeeded[i]) == pControl)
- {
- return;
- }
- }
- m_aNeedMouseLeaveNeeded.Add(pControl);
- }
- bool CPaintManagerUI::RemoveMouseLeaveNeeded(CControlUI* pControl)
- {
- if (pControl == NULL) return false;
- for (int i = 0; i < m_aNeedMouseLeaveNeeded.GetSize(); i++)
- {
- if (static_cast<CControlUI*>(m_aNeedMouseLeaveNeeded[i]) == pControl)
- {
- return m_aNeedMouseLeaveNeeded.Remove(i);
- }
- }
- return false;
- }
- void CPaintManagerUI::SendNotify(CControlUI* pControl, LPCTSTR pstrMessage, WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/, bool bAsync /*= false*/)
- {
- TNotifyUI Msg;
- Msg.pSender = pControl;
- Msg.sType = pstrMessage;
- Msg.wParam = wParam;
- Msg.lParam = lParam;
- SendNotify(Msg, bAsync);
- }
- void CPaintManagerUI::SendNotify(TNotifyUI& Msg, bool bAsync /*= false*/)
- {
- Msg.ptMouse = m_ptLastMousePos;
- Msg.dwTimestamp = ::GetTickCount();
- if (m_bUsedVirtualWnd)
- {
- Msg.sVirtualWnd = Msg.pSender->GetVirtualWnd();
- }
- if (!bAsync)
- {
- // Send to all listeners
- if (Msg.pSender != NULL)
- {
- if (Msg.pSender->OnNotify) Msg.pSender->OnNotify(&Msg);
- }
- for (int i = 0; i < m_aNotifiers.GetSize(); i++)
- {
- static_cast<INotifyUI*>(m_aNotifiers[i])->Notify(Msg);
- }
- }
- else
- {
- TNotifyUI* pMsg = new TNotifyUI;
- pMsg->sVirtualWnd = Msg.sVirtualWnd;
- pMsg->pSender = Msg.pSender;
- pMsg->sType = Msg.sType;
- pMsg->wParam = Msg.wParam;
- pMsg->lParam = Msg.lParam;
- pMsg->ptMouse = Msg.ptMouse;
- pMsg->dwTimestamp = Msg.dwTimestamp;
- m_aAsyncNotify.Add(pMsg);
- PostAsyncNotify();
- }
- }
- bool CPaintManagerUI::IsForceUseSharedRes() const
- {
- return m_bForceUseSharedRes;
- }
- void CPaintManagerUI::SetForceUseSharedRes(bool bForce)
- {
- m_bForceUseSharedRes = bForce;
- }
- DWORD CPaintManagerUI::GetDefaultDisabledColor() const
- {
- return m_ResInfo.m_dwDefaultDisabledColor;
- }
- void CPaintManagerUI::SetDefaultDisabledColor(DWORD dwColor, bool bShared)
- {
- if (bShared)
- {
- if (m_ResInfo.m_dwDefaultDisabledColor == m_SharedResInfo.m_dwDefaultDisabledColor)
- m_ResInfo.m_dwDefaultDisabledColor = dwColor;
- m_SharedResInfo.m_dwDefaultDisabledColor = dwColor;
- }
- else
- {
- m_ResInfo.m_dwDefaultDisabledColor = dwColor;
- }
- }
- DWORD CPaintManagerUI::GetDefaultFontColor() const
- {
- return m_ResInfo.m_dwDefaultFontColor;
- }
- void CPaintManagerUI::SetDefaultFontColor(DWORD dwColor, bool bShared)
- {
- if (bShared)
- {
- if (m_ResInfo.m_dwDefaultFontColor == m_SharedResInfo.m_dwDefaultFontColor)
- m_ResInfo.m_dwDefaultFontColor = dwColor;
- m_SharedResInfo.m_dwDefaultFontColor = dwColor;
- }
- else
- {
- m_ResInfo.m_dwDefaultFontColor = dwColor;
- }
- }
- DWORD CPaintManagerUI::GetDefaultLinkFontColor() const
- {
- return m_ResInfo.m_dwDefaultLinkFontColor;
- }
- void CPaintManagerUI::SetDefaultLinkFontColor(DWORD dwColor, bool bShared)
- {
- if (bShared)
- {
- if (m_ResInfo.m_dwDefaultLinkFontColor == m_SharedResInfo.m_dwDefaultLinkFontColor)
- m_ResInfo.m_dwDefaultLinkFontColor = dwColor;
- m_SharedResInfo.m_dwDefaultLinkFontColor = dwColor;
- }
- else
- {
- m_ResInfo.m_dwDefaultLinkFontColor = dwColor;
- }
- }
- DWORD CPaintManagerUI::GetDefaultLinkHoverFontColor() const
- {
- return m_ResInfo.m_dwDefaultLinkHoverFontColor;
- }
- void CPaintManagerUI::SetDefaultLinkHoverFontColor(DWORD dwColor, bool bShared)
- {
- if (bShared)
- {
- if (m_ResInfo.m_dwDefaultLinkHoverFontColor == m_SharedResInfo.m_dwDefaultLinkHoverFontColor)
- m_ResInfo.m_dwDefaultLinkHoverFontColor = dwColor;
- m_SharedResInfo.m_dwDefaultLinkHoverFontColor = dwColor;
- }
- else
- {
- m_ResInfo.m_dwDefaultLinkHoverFontColor = dwColor;
- }
- }
- DWORD CPaintManagerUI::GetDefaultSelectedBkColor() const
- {
- return m_ResInfo.m_dwDefaultSelectedBkColor;
- }
- void CPaintManagerUI::SetDefaultSelectedBkColor(DWORD dwColor, bool bShared)
- {
- if (bShared)
- {
- if (m_ResInfo.m_dwDefaultSelectedBkColor == m_SharedResInfo.m_dwDefaultSelectedBkColor)
- m_ResInfo.m_dwDefaultSelectedBkColor = dwColor;
- m_SharedResInfo.m_dwDefaultSelectedBkColor = dwColor;
- }
- else
- {
- m_ResInfo.m_dwDefaultSelectedBkColor = dwColor;
- }
- }
- TFontInfo* CPaintManagerUI::GetDefaultFontInfo()
- {
- if (m_ResInfo.m_DefaultFontInfo.sFontName.IsEmpty())
- {
- if (m_SharedResInfo.m_DefaultFontInfo.tm.tmHeight == 0)
- {
- HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, m_SharedResInfo.m_DefaultFontInfo.hFont);
- ::GetTextMetrics(m_hDcPaint, &m_SharedResInfo.m_DefaultFontInfo.tm);
- ::SelectObject(m_hDcPaint, hOldFont);
- }
- return &m_SharedResInfo.m_DefaultFontInfo;
- }
- else
- {
- if (m_ResInfo.m_DefaultFontInfo.tm.tmHeight == 0)
- {
- HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, m_ResInfo.m_DefaultFontInfo.hFont);
- ::GetTextMetrics(m_hDcPaint, &m_ResInfo.m_DefaultFontInfo.tm);
- ::SelectObject(m_hDcPaint, hOldFont);
- }
- return &m_ResInfo.m_DefaultFontInfo;
- }
- }
- void CPaintManagerUI::SetDefaultFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared)
- {
- LOGFONT lf = { 0 };
- ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
- if (lstrlen(pStrFontName) > 0)
- {
- TCHAR szFaceName[32] = { 0 };//_T("@");
- _tcsncat(szFaceName, pStrFontName, LF_FACESIZE);
- _tcsncpy(lf.lfFaceName, szFaceName, LF_FACESIZE);
- }
- lf.lfCharSet = DEFAULT_CHARSET;
- lf.lfHeight = -GetDPIObj()->Scale(nSize);;
- if (bBold) lf.lfWeight += FW_BOLD;
- if (bUnderline) lf.lfUnderline = TRUE;
- if (bItalic) lf.lfItalic = TRUE;
- if (bStrikeout) lf.lfStrikeOut = TRUE;
- HFONT hFont = ::CreateFontIndirect(&lf);
- if (hFont == NULL) return;
- if (bShared)
- {
- ::DeleteObject(m_SharedResInfo.m_DefaultFontInfo.hFont);
- m_SharedResInfo.m_DefaultFontInfo.hFont = hFont;
- m_SharedResInfo.m_DefaultFontInfo.sFontName = lf.lfFaceName;
- m_SharedResInfo.m_DefaultFontInfo.iSize = nSize;
- m_SharedResInfo.m_DefaultFontInfo.bBold = bBold;
- m_SharedResInfo.m_DefaultFontInfo.bUnderline = bUnderline;
- m_SharedResInfo.m_DefaultFontInfo.bItalic = bItalic;
- m_SharedResInfo.m_DefaultFontInfo.bStrikeout = bStrikeout;
- ::ZeroMemory(&m_SharedResInfo.m_DefaultFontInfo.tm, sizeof(m_SharedResInfo.m_DefaultFontInfo.tm));
- if (m_hDcPaint)
- {
- HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
- ::GetTextMetrics(m_hDcPaint, &m_SharedResInfo.m_DefaultFontInfo.tm);
- ::SelectObject(m_hDcPaint, hOldFont);
- }
- }
- else
- {
- ::DeleteObject(m_ResInfo.m_DefaultFontInfo.hFont);
- m_ResInfo.m_DefaultFontInfo.hFont = hFont;
- m_ResInfo.m_DefaultFontInfo.sFontName = lf.lfFaceName;
- m_ResInfo.m_DefaultFontInfo.iSize = nSize;
- m_ResInfo.m_DefaultFontInfo.bBold = bBold;
- m_ResInfo.m_DefaultFontInfo.bUnderline = bUnderline;
- m_ResInfo.m_DefaultFontInfo.bItalic = bItalic;
- m_ResInfo.m_DefaultFontInfo.bStrikeout = bStrikeout;
- ::ZeroMemory(&m_ResInfo.m_DefaultFontInfo.tm, sizeof(m_ResInfo.m_DefaultFontInfo.tm));
- if (m_hDcPaint)
- {
- HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
- ::GetTextMetrics(m_hDcPaint, &m_ResInfo.m_DefaultFontInfo.tm);
- ::SelectObject(m_hDcPaint, hOldFont);
- }
- }
- }
- DWORD CPaintManagerUI::GetCustomFontCount(bool bShared) const
- {
- if (bShared)
- return m_SharedResInfo.m_CustomFonts.GetSize();
- else
- return m_ResInfo.m_CustomFonts.GetSize();
- }
- HFONT CPaintManagerUI::AddFont(int id, LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared)
- {
- LOGFONT lf = { 0 };
- ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
- if (lstrlen(pStrFontName) > 0)
- {
- TCHAR szFaceName[32] = { 0 };//_T("@");
- _tcsncat(szFaceName, pStrFontName, LF_FACESIZE);
- _tcsncpy(lf.lfFaceName, szFaceName, LF_FACESIZE);
- }
- lf.lfCharSet = DEFAULT_CHARSET;
- lf.lfHeight = -GetDPIObj()->Scale(nSize);
- if (bBold) lf.lfWeight = FW_BOLD;
- if (bUnderline) lf.lfUnderline = TRUE;
- if (bItalic) lf.lfItalic = TRUE;
- if (bStrikeout) lf.lfStrikeOut = TRUE;
- HFONT hFont = ::CreateFontIndirect(&lf);
- if (hFont == NULL) return NULL;
- TFontInfo* pFontInfo = new TFontInfo;
- if (!pFontInfo) return false;
- ::ZeroMemory(pFontInfo, sizeof(TFontInfo));
- pFontInfo->hFont = hFont;
- pFontInfo->sFontName = lf.lfFaceName;
- pFontInfo->iSize = nSize;
- pFontInfo->bBold = bBold;
- pFontInfo->bUnderline = bUnderline;
- pFontInfo->bItalic = bItalic;
- pFontInfo->bStrikeout = bStrikeout;
- if (m_hDcPaint)
- {
- HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, hFont);
- ::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
- ::SelectObject(m_hDcPaint, hOldFont);
- }
- TCHAR idBuffer[16];
- ::ZeroMemory(idBuffer, sizeof(idBuffer));
- _itot(id, idBuffer, 10);
- if (bShared || m_bForceUseSharedRes)
- {
- TFontInfo* pOldFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(idBuffer));
- if (pOldFontInfo)
- {
- ::DeleteObject(pOldFontInfo->hFont);
- delete pOldFontInfo;
- m_SharedResInfo.m_CustomFonts.Remove(idBuffer);
- }
- if (!m_SharedResInfo.m_CustomFonts.Insert(idBuffer, pFontInfo))
- {
- ::DeleteObject(hFont);
- delete pFontInfo;
- return NULL;
- }
- }
- else
- {
- TFontInfo* pOldFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(idBuffer));
- if (pOldFontInfo)
- {
- ::DeleteObject(pOldFontInfo->hFont);
- delete pOldFontInfo;
- m_ResInfo.m_CustomFonts.Remove(idBuffer);
- }
- if (!m_ResInfo.m_CustomFonts.Insert(idBuffer, pFontInfo))
- {
- ::DeleteObject(hFont);
- delete pFontInfo;
- return NULL;
- }
- }
- return hFont;
- }
- void CPaintManagerUI::AddFontArray(LPCTSTR pstrPath)
- {
- LPBYTE pData = NULL;
- DWORD dwSize = 0;
- do
- {
- CDuiString sFile = CPaintManagerUI::GetResourcePath();
- if (CPaintManagerUI::GetResourceZip().IsEmpty())
- {
- sFile += pstrPath;
- HANDLE hFile = ::CreateFile(sFile.GetData(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, \
- FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE) break;
- dwSize = ::GetFileSize(hFile, NULL);
- if (dwSize == 0) break;
- DWORD dwRead = 0;
- pData = new BYTE[dwSize];
- ::ReadFile(hFile, pData, dwSize, &dwRead, NULL);
- ::CloseHandle(hFile);
- if (dwRead != dwSize)
- {
- delete[] pData;
- pData = NULL;
- break;
- }
- }
- else
- {
- sFile += CPaintManagerUI::GetResourceZip();
- HZIP hz = NULL;
- if (CPaintManagerUI::IsCachedResourceZip()) hz = (HZIP)CPaintManagerUI::GetResourceZipHandle();
- else
- {
- CDuiString sFilePwd = CPaintManagerUI::GetResourceZipPwd();
- #ifdef UNICODE
- char* pwd = w2a((wchar_t*)sFilePwd.GetData());
- hz = OpenZip(sFile.GetData(), pwd);
- if (pwd) delete[] pwd;
- #else
- hz = OpenZip(sFile.GetData(), sFilePwd.GetData());
- #endif
- }
- if (hz == NULL) break;
- ZIPENTRY ze;
- int i = 0;
- CDuiString key = pstrPath;
- key.Replace(_T("\\"), _T("/"));
- if (FindZipItem(hz, key, true, &i, &ze) != 0) break;
- dwSize = ze.unc_size;
- if (dwSize == 0) break;
- pData = new BYTE[dwSize];
- int res = UnzipItem(hz, i, pData, dwSize);
- if (res != 0x00000000 && res != 0x00000600)
- {
- delete[] pData;
- pData = NULL;
- if (!CPaintManagerUI::IsCachedResourceZip()) CloseZip(hz);
- break;
- }
- if (!CPaintManagerUI::IsCachedResourceZip()) CloseZip(hz);
- }
- } while (0);
- while (!pData)
- {
- //读不到图片, 则直接去读取bitmap.m_lpstr指向的路径
- HANDLE hFile = ::CreateFile(pstrPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE) break;
- dwSize = ::GetFileSize(hFile, NULL);
- if (dwSize == 0) break;
- DWORD dwRead = 0;
- pData = new BYTE[dwSize];
- ::ReadFile(hFile, pData, dwSize, &dwRead, NULL);
- ::CloseHandle(hFile);
- if (dwRead != dwSize)
- {
- delete[] pData;
- pData = NULL;
- }
- break;
- }
- DWORD nFonts;
- HANDLE hFont = ::AddFontMemResourceEx(pData, dwSize, NULL, &nFonts);
- delete[] pData;
- pData = NULL;
- m_aFonts.Add(hFont);
- }
- HFONT CPaintManagerUI::GetFont(int id)
- {
- if (id < 0) return GetDefaultFontInfo()->hFont;
- TCHAR idBuffer[16];
- ::ZeroMemory(idBuffer, sizeof(idBuffer));
- _itot(id, idBuffer, 10);
- TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(idBuffer));
- if (!pFontInfo) pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(idBuffer));
- if (!pFontInfo) return GetDefaultFontInfo()->hFont;
- return pFontInfo->hFont;
- }
- HFONT CPaintManagerUI::GetFont(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout)
- {
- TFontInfo* pFontInfo = NULL;
- for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
- if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
- pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
- && pFontInfo->bStrikeout == bStrikeout)
- return pFontInfo->hFont;
- }
- }
- for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
- if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
- pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
- && pFontInfo->bStrikeout == bStrikeout)
- return pFontInfo->hFont;
- }
- }
- return NULL;
- }
- int CPaintManagerUI::GetFontIndex(HFONT hFont, bool bShared)
- {
- TFontInfo* pFontInfo = NULL;
- if (bShared)
- {
- for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
- if (pFontInfo && pFontInfo->hFont == hFont) return _ttoi(key);
- }
- }
- }
- else
- {
- for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
- if (pFontInfo && pFontInfo->hFont == hFont) return _ttoi(key);
- }
- }
- }
- return -1;
- }
- int CPaintManagerUI::GetFontIndex(LPCTSTR pStrFontName, int nSize, bool bBold, bool bUnderline, bool bItalic, bool bStrikeout, bool bShared)
- {
- TFontInfo* pFontInfo = NULL;
- if (bShared)
- {
- for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
- if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
- pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
- && pFontInfo->bStrikeout == bStrikeout)
- return _ttoi(key);
- }
- }
- }
- else
- {
- for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
- if (pFontInfo && pFontInfo->sFontName == pStrFontName && pFontInfo->iSize == nSize &&
- pFontInfo->bBold == bBold && pFontInfo->bUnderline == bUnderline && pFontInfo->bItalic == bItalic
- && pFontInfo->bStrikeout == bStrikeout)
- return _ttoi(key);
- }
- }
- }
- return -1;
- }
- void CPaintManagerUI::RemoveFont(HFONT hFont, bool bShared)
- {
- TFontInfo* pFontInfo = NULL;
- if (bShared)
- {
- for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
- if (pFontInfo && pFontInfo->hFont == hFont)
- {
- ::DeleteObject(pFontInfo->hFont);
- delete pFontInfo;
- m_SharedResInfo.m_CustomFonts.Remove(key);
- return;
- }
- }
- }
- }
- else
- {
- for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
- if (pFontInfo && pFontInfo->hFont == hFont)
- {
- ::DeleteObject(pFontInfo->hFont);
- delete pFontInfo;
- m_ResInfo.m_CustomFonts.Remove(key);
- return;
- }
- }
- }
- }
- }
- void CPaintManagerUI::RemoveFont(int id, bool bShared)
- {
- TCHAR idBuffer[16];
- ::ZeroMemory(idBuffer, sizeof(idBuffer));
- _itot(id, idBuffer, 10);
- TFontInfo* pFontInfo = NULL;
- if (bShared)
- {
- pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(idBuffer));
- if (pFontInfo)
- {
- ::DeleteObject(pFontInfo->hFont);
- delete pFontInfo;
- m_SharedResInfo.m_CustomFonts.Remove(idBuffer);
- }
- }
- else
- {
- pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(idBuffer));
- if (pFontInfo)
- {
- ::DeleteObject(pFontInfo->hFont);
- delete pFontInfo;
- m_ResInfo.m_CustomFonts.Remove(idBuffer);
- }
- }
- }
- void CPaintManagerUI::RemoveAllFonts(bool bShared)
- {
- TFontInfo* pFontInfo;
- if (bShared)
- {
- for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key, false));
- if (pFontInfo)
- {
- ::DeleteObject(pFontInfo->hFont);
- delete pFontInfo;
- }
- }
- }
- m_SharedResInfo.m_CustomFonts.RemoveAll();
- }
- else
- {
- for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key, false));
- if (pFontInfo)
- {
- ::DeleteObject(pFontInfo->hFont);
- delete pFontInfo;
- }
- }
- }
- m_ResInfo.m_CustomFonts.RemoveAll();
- }
- }
- TFontInfo* CPaintManagerUI::GetFontInfo(int id)
- {
- if (id < 0) return GetDefaultFontInfo();
- TCHAR idBuffer[16];
- ::ZeroMemory(idBuffer, sizeof(idBuffer));
- _itot(id, idBuffer, 10);
- TFontInfo* pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(idBuffer));
- if (!pFontInfo) pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(idBuffer));
- if (!pFontInfo) pFontInfo = GetDefaultFontInfo();
- if (pFontInfo->tm.tmHeight == 0)
- {
- HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, pFontInfo->hFont);
- ::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
- ::SelectObject(m_hDcPaint, hOldFont);
- }
- return pFontInfo;
- }
- TFontInfo* CPaintManagerUI::GetFontInfo(HFONT hFont)
- {
- TFontInfo* pFontInfo = NULL;
- for (int i = 0; i < m_ResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_ResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_ResInfo.m_CustomFonts.Find(key));
- if (pFontInfo && pFontInfo->hFont == hFont) break;
- }
- }
- if (!pFontInfo)
- {
- for (int i = 0; i < m_SharedResInfo.m_CustomFonts.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_CustomFonts.GetAt(i))
- {
- pFontInfo = static_cast<TFontInfo*>(m_SharedResInfo.m_CustomFonts.Find(key));
- if (pFontInfo && pFontInfo->hFont == hFont) break;
- }
- }
- }
- if (!pFontInfo) pFontInfo = GetDefaultFontInfo();
- if (pFontInfo->tm.tmHeight == 0)
- {
- HFONT hOldFont = (HFONT) ::SelectObject(m_hDcPaint, pFontInfo->hFont);
- ::GetTextMetrics(m_hDcPaint, &pFontInfo->tm);
- ::SelectObject(m_hDcPaint, hOldFont);
- }
- return pFontInfo;
- }
- const TImageInfo* CPaintManagerUI::GetImage(LPCTSTR bitmap)
- {
- TImageInfo* data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
- if (!data) data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
- return data;
- }
- const TImageInfo* CPaintManagerUI::GetImageEx(LPCTSTR bitmap, LPCTSTR type, DWORD mask, bool bUseHSL, bool bGdiplus, HINSTANCE instance)
- {
- const TImageInfo* data = GetImage(bitmap);
- if (!data)
- {
- if (AddImage(bitmap, type, mask, bUseHSL, bGdiplus, false, instance))
- {
- if (m_bForceUseSharedRes) data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
- else data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
- }
- }
- return data;
- }
- const TImageInfo* CPaintManagerUI::AddImage(LPCTSTR bitmap, LPCTSTR type, DWORD mask, bool bUseHSL, bool bGdiplus, bool bShared, HINSTANCE instance)
- {
- if (bitmap == NULL || bitmap[0] == _T('\0')) return NULL;
- TImageInfo* data = NULL;
- if (type != NULL && lstrlen(type) > 0)
- {
- if (isdigit(*bitmap))
- {
- LPTSTR pstr = NULL;
- int iIndex = _tcstol(bitmap, &pstr, 10);
- data = bGdiplus ? CRenderEngine::GdiplusLoadImage(iIndex, type, mask, instance) : CRenderEngine::LoadImage(iIndex, type, mask, instance);
- }
- }
- else
- {
- data = bGdiplus ? CRenderEngine::GdiplusLoadImage(bitmap, NULL, mask, instance) : CRenderEngine::LoadImage(bitmap, NULL, mask, instance);
- if (!data)
- {
- CDuiString sImageName = bitmap;
- int iAtIdx = sImageName.ReverseFind(_T('@'));
- int iDotIdx = sImageName.ReverseFind(_T('.'));
- if (iAtIdx != -1 && iDotIdx != -1)
- {
- CDuiString sExe = sImageName.Mid(iDotIdx);
- sImageName = sImageName.Left(iAtIdx) + sExe;
- data = bGdiplus ? CRenderEngine::GdiplusLoadImage(sImageName.GetData(), NULL, mask, instance) : CRenderEngine::LoadImage(sImageName.GetData(), NULL, mask, instance);
- }
- }
- }
- if (data == NULL)
- {
- return NULL;
- }
- data->bUseHSL = bUseHSL;
- if (type != NULL) data->sResType = type;
- data->dwMask = mask;
- if (data->bUseHSL)
- {
- data->pSrcBits = new BYTE[data->nX * data->nY * 4];
- ::CopyMemory(data->pSrcBits, data->pBits, data->nX * data->nY * 4);
- }
- else data->pSrcBits = NULL;
- if (m_bUseHSL) CRenderEngine::AdjustImage(true, data, m_H, m_S, m_L);
- if (data)
- {
- if (bShared || m_bForceUseSharedRes)
- {
- TImageInfo* pOldImageInfo = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
- if (pOldImageInfo)
- {
- CRenderEngine::FreeImage(pOldImageInfo);
- m_SharedResInfo.m_ImageHash.Remove(bitmap);
- }
- if (!m_SharedResInfo.m_ImageHash.Insert(bitmap, data))
- {
- CRenderEngine::FreeImage(data);
- data = NULL;
- }
- }
- else
- {
- TImageInfo* pOldImageInfo = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
- if (pOldImageInfo)
- {
- CRenderEngine::FreeImage(pOldImageInfo);
- m_ResInfo.m_ImageHash.Remove(bitmap);
- }
- if (!m_ResInfo.m_ImageHash.Insert(bitmap, data))
- {
- CRenderEngine::FreeImage(data);
- data = NULL;
- }
- }
- }
- return data;
- }
- const TImageInfo* CPaintManagerUI::AddImage(LPCTSTR bitmap, HBITMAP hBitmap, int iWidth, int iHeight, bool bAlpha, bool bShared)
- {
- // 因无法确定外部HBITMAP格式,不能使用hsl调整
- if (bitmap == NULL || bitmap[0] == _T('\0')) return NULL;
- if (hBitmap == NULL || iWidth <= 0 || iHeight <= 0) return NULL;
- TImageInfo* data = new TImageInfo;
- data->pBits = NULL;
- data->pSrcBits = NULL;
- data->hBitmap = hBitmap;
- data->pBits = NULL;
- data->nX = iWidth;
- data->nY = iHeight;
- data->bAlpha = bAlpha;
- data->bUseHSL = false;
- data->pSrcBits = NULL;
- data->dwMask = 0;
- if (bShared || m_bForceUseSharedRes)
- {
- if (!m_SharedResInfo.m_ImageHash.Insert(bitmap, data))
- {
- CRenderEngine::FreeImage(data);
- data = NULL;
- }
- }
- else
- {
- if (!m_ResInfo.m_ImageHash.Insert(bitmap, data))
- {
- CRenderEngine::FreeImage(data);
- data = NULL;
- }
- }
- return data;
- }
- void CPaintManagerUI::RemoveImage(LPCTSTR bitmap, bool bShared)
- {
- TImageInfo* data = NULL;
- if (bShared)
- {
- data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
- if (data)
- {
- CRenderEngine::FreeImage(data);
- m_SharedResInfo.m_ImageHash.Remove(bitmap);
- }
- }
- else
- {
- data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
- if (data)
- {
- CRenderEngine::FreeImage(data);
- m_ResInfo.m_ImageHash.Remove(bitmap);
- }
- }
- }
- void CPaintManagerUI::RemoveAllImages(bool bShared)
- {
- if (bShared)
- {
- TImageInfo* data;
- for (int i = 0; i < m_SharedResInfo.m_ImageHash.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_ImageHash.GetAt(i))
- {
- data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(key, false));
- if (data)
- {
- CRenderEngine::FreeImage(data);
- }
- }
- }
- m_SharedResInfo.m_ImageHash.RemoveAll();
- }
- else
- {
- TImageInfo* data;
- for (int i = 0; i < m_ResInfo.m_ImageHash.GetSize(); i++)
- {
- if (LPCTSTR key = m_ResInfo.m_ImageHash.GetAt(i))
- {
- data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(key, false));
- if (data)
- {
- CRenderEngine::FreeImage(data);
- }
- }
- }
- m_ResInfo.m_ImageHash.RemoveAll();
- }
- }
- void CPaintManagerUI::AdjustSharedImagesHSL()
- {
- TImageInfo* data;
- for (int i = 0; i < m_SharedResInfo.m_ImageHash.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_ImageHash.GetAt(i))
- {
- data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(key));
- if (data && data->bUseHSL)
- {
- CRenderEngine::AdjustImage(m_bUseHSL, data, m_H, m_S, m_L);
- }
- }
- }
- }
- void CPaintManagerUI::AdjustImagesHSL()
- {
- TImageInfo* data;
- for (int i = 0; i < m_ResInfo.m_ImageHash.GetSize(); i++)
- {
- if (LPCTSTR key = m_ResInfo.m_ImageHash.GetAt(i))
- {
- data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(key));
- if (data && data->bUseHSL)
- {
- CRenderEngine::AdjustImage(m_bUseHSL, data, m_H, m_S, m_L);
- }
- }
- }
- Invalidate();
- }
- void CPaintManagerUI::PostAsyncNotify()
- {
- if (!m_bAsyncNotifyPosted)
- {
- ::PostMessage(m_hWndPaint, WM_APP + 1, 0, 0L);
- m_bAsyncNotifyPosted = true;
- }
- }
- void CPaintManagerUI::ReloadSharedImages()
- {
- TImageInfo* data = NULL;
- TImageInfo* pNewData = NULL;
- for (int i = 0; i < m_SharedResInfo.m_ImageHash.GetSize(); i++)
- {
- if (LPCTSTR bitmap = m_SharedResInfo.m_ImageHash.GetAt(i))
- {
- data = static_cast<TImageInfo*>(m_SharedResInfo.m_ImageHash.Find(bitmap));
- if (data != NULL)
- {
- if (!data->sResType.IsEmpty())
- {
- if (isdigit(*bitmap))
- {
- LPTSTR pstr = NULL;
- int iIndex = _tcstol(bitmap, &pstr, 10);
- pNewData = CRenderEngine::LoadImage(iIndex, data->sResType.GetData(), data->dwMask);
- }
- }
- else
- {
- pNewData = CRenderEngine::LoadImage(bitmap, NULL, data->dwMask);
- }
- if (pNewData == NULL) continue;
- CRenderEngine::FreeImage(data, false);
- data->hBitmap = pNewData->hBitmap;
- data->pImage = pNewData->pImage;
- data->pBits = pNewData->pBits;
- data->nX = pNewData->nX;
- data->nY = pNewData->nY;
- data->bAlpha = pNewData->bAlpha;
- data->pSrcBits = NULL;
- if (data->bUseHSL)
- {
- data->pSrcBits = new BYTE[data->nX * data->nY * 4];
- ::CopyMemory(data->pSrcBits, data->pBits, data->nX * data->nY * 4);
- }
- else data->pSrcBits = NULL;
- if (m_bUseHSL) CRenderEngine::AdjustImage(true, data, m_H, m_S, m_L);
- delete pNewData;
- }
- }
- }
- }
- void CPaintManagerUI::ReloadImages()
- {
- RemoveAllDrawInfos();
- TImageInfo* data = NULL;
- TImageInfo* pNewData = NULL;
- for (int i = 0; i < m_ResInfo.m_ImageHash.GetSize(); i++)
- {
- if (LPCTSTR bitmap = m_ResInfo.m_ImageHash.GetAt(i))
- {
- data = static_cast<TImageInfo*>(m_ResInfo.m_ImageHash.Find(bitmap));
- if (data != NULL)
- {
- if (!data->sResType.IsEmpty())
- {
- if (isdigit(*bitmap))
- {
- LPTSTR pstr = NULL;
- int iIndex = _tcstol(bitmap, &pstr, 10);
- pNewData = CRenderEngine::LoadImage(iIndex, data->sResType.GetData(), data->dwMask);
- }
- }
- else
- {
- pNewData = CRenderEngine::LoadImage(bitmap, NULL, data->dwMask);
- }
- CRenderEngine::FreeImage(data, false);
- if (pNewData == NULL)
- {
- m_ResInfo.m_ImageHash.Remove(bitmap);
- continue;
- }
- data->hBitmap = pNewData->hBitmap;
- data->pBits = pNewData->pBits;
- data->nX = pNewData->nX;
- data->nY = pNewData->nY;
- data->bAlpha = pNewData->bAlpha;
- data->pSrcBits = NULL;
- if (data->bUseHSL)
- {
- data->pSrcBits = new BYTE[data->nX * data->nY * 4];
- ::CopyMemory(data->pSrcBits, data->pBits, data->nX * data->nY * 4);
- }
- else data->pSrcBits = NULL;
- if (m_bUseHSL) CRenderEngine::AdjustImage(true, data, m_H, m_S, m_L);
- delete pNewData;
- }
- }
- }
- if (m_pRoot) m_pRoot->Invalidate();
- }
- const TDrawInfo* CPaintManagerUI::GetDrawInfo(LPCTSTR pStrImage, LPCTSTR pStrModify)
- {
- CDuiString sKey;
- sKey.Format(_T("%s%s"), pStrImage == NULL ? _T("") : pStrImage, pStrModify == NULL ? _T("") : pStrModify);
- TDrawInfo* pDrawInfo = static_cast<TDrawInfo*>(m_ResInfo.m_DrawInfoHash.Find(sKey));
- if (pDrawInfo == NULL && !sKey.IsEmpty())
- {
- pDrawInfo = new TDrawInfo();
- pDrawInfo->Parse(pStrImage, pStrModify, this);
- m_ResInfo.m_DrawInfoHash.Insert(sKey, pDrawInfo);
- }
- return pDrawInfo;
- }
- void CPaintManagerUI::RemoveDrawInfo(LPCTSTR pStrImage, LPCTSTR pStrModify)
- {
- CDuiString sKey;
- sKey.Format(_T("%s%s"), pStrImage == NULL ? _T("") : pStrImage, pStrModify == NULL ? _T("") : pStrModify);
- TDrawInfo* pDrawInfo = static_cast<TDrawInfo*>(m_ResInfo.m_DrawInfoHash.Find(sKey));
- if (pDrawInfo != NULL)
- {
- m_ResInfo.m_DrawInfoHash.Remove(sKey);
- delete pDrawInfo;
- pDrawInfo = NULL;
- }
- }
- void CPaintManagerUI::RemoveAllDrawInfos()
- {
- TDrawInfo* pDrawInfo = NULL;
- for (int i = 0; i < m_ResInfo.m_DrawInfoHash.GetSize(); i++)
- {
- LPCTSTR key = m_ResInfo.m_DrawInfoHash.GetAt(i);
- if (key != NULL)
- {
- pDrawInfo = static_cast<TDrawInfo*>(m_ResInfo.m_DrawInfoHash.Find(key, false));
- if (pDrawInfo)
- {
- delete pDrawInfo;
- pDrawInfo = NULL;
- }
- }
- }
- m_ResInfo.m_DrawInfoHash.RemoveAll();
- }
- void CPaintManagerUI::AddDefaultAttributeList(LPCTSTR pStrControlName, LPCTSTR pStrControlAttrList, bool bShared)
- {
- if (bShared || m_bForceUseSharedRes)
- {
- CDuiString* pDefaultAttr = new CDuiString(pStrControlAttrList);
- if (pDefaultAttr != NULL)
- {
- CDuiString* pOldDefaultAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Set(pStrControlName, (LPVOID)pDefaultAttr));
- if (pOldDefaultAttr) delete pOldDefaultAttr;
- }
- }
- else
- {
- CDuiString* pDefaultAttr = new CDuiString(pStrControlAttrList);
- if (pDefaultAttr != NULL)
- {
- CDuiString* pOldDefaultAttr = static_cast<CDuiString*>(m_ResInfo.m_AttrHash.Set(pStrControlName, (LPVOID)pDefaultAttr));
- if (pOldDefaultAttr) delete pOldDefaultAttr;
- }
- }
- }
- LPCTSTR CPaintManagerUI::GetDefaultAttributeList(LPCTSTR pStrControlName) const
- {
- CDuiString* pDefaultAttr = static_cast<CDuiString*>(m_ResInfo.m_AttrHash.Find(pStrControlName));
- if (!pDefaultAttr) pDefaultAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Find(pStrControlName));
- if (pDefaultAttr) return pDefaultAttr->GetData();
- return NULL;
- }
- bool CPaintManagerUI::RemoveDefaultAttributeList(LPCTSTR pStrControlName, bool bShared)
- {
- if (bShared)
- {
- CDuiString* pDefaultAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Find(pStrControlName));
- if (!pDefaultAttr) return false;
- delete pDefaultAttr;
- return m_SharedResInfo.m_AttrHash.Remove(pStrControlName);
- }
- else
- {
- CDuiString* pDefaultAttr = static_cast<CDuiString*>(m_ResInfo.m_AttrHash.Find(pStrControlName));
- if (!pDefaultAttr) return false;
- delete pDefaultAttr;
- return m_ResInfo.m_AttrHash.Remove(pStrControlName);
- }
- }
- void CPaintManagerUI::RemoveAllDefaultAttributeList(bool bShared)
- {
- if (bShared)
- {
- CDuiString* pDefaultAttr;
- for (int i = 0; i < m_SharedResInfo.m_AttrHash.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_AttrHash.GetAt(i))
- {
- pDefaultAttr = static_cast<CDuiString*>(m_SharedResInfo.m_AttrHash.Find(key));
- if (pDefaultAttr) delete pDefaultAttr;
- }
- }
- m_SharedResInfo.m_AttrHash.RemoveAll();
- }
- else
- {
- CDuiString* pDefaultAttr;
- for (int i = 0; i < m_ResInfo.m_AttrHash.GetSize(); i++)
- {
- if (LPCTSTR key = m_ResInfo.m_AttrHash.GetAt(i))
- {
- pDefaultAttr = static_cast<CDuiString*>(m_ResInfo.m_AttrHash.Find(key));
- if (pDefaultAttr) delete pDefaultAttr;
- }
- }
- m_ResInfo.m_AttrHash.RemoveAll();
- }
- }
- void CPaintManagerUI::AddWindowCustomAttribute(LPCTSTR pstrName, LPCTSTR pstrAttr)
- {
- if (pstrName == NULL || pstrName[0] == _T('\0') || pstrAttr == NULL || pstrAttr[0] == _T('\0')) return;
- CDuiString* pCostomAttr = new CDuiString(pstrAttr);
- if (pCostomAttr != NULL)
- {
- if (m_mWindowCustomAttrHash.Find(pstrName) == NULL)
- m_mWindowCustomAttrHash.Set(pstrName, (LPVOID)pCostomAttr);
- else
- delete pCostomAttr;
- }
- }
- LPCTSTR CPaintManagerUI::GetWindowCustomAttribute(LPCTSTR pstrName) const
- {
- if (pstrName == NULL || pstrName[0] == _T('\0')) return NULL;
- CDuiString* pCostomAttr = static_cast<CDuiString*>(m_mWindowCustomAttrHash.Find(pstrName));
- if (pCostomAttr) return pCostomAttr->GetData();
- return NULL;
- }
- bool CPaintManagerUI::RemoveWindowCustomAttribute(LPCTSTR pstrName)
- {
- if (pstrName == NULL || pstrName[0] == _T('\0')) return NULL;
- CDuiString* pCostomAttr = static_cast<CDuiString*>(m_mWindowCustomAttrHash.Find(pstrName));
- if (!pCostomAttr) return false;
- delete pCostomAttr;
- return m_mWindowCustomAttrHash.Remove(pstrName);
- }
- void CPaintManagerUI::RemoveAllWindowCustomAttribute()
- {
- CDuiString* pCostomAttr;
- for (int i = 0; i < m_mWindowCustomAttrHash.GetSize(); i++)
- {
- if (LPCTSTR key = m_mWindowCustomAttrHash.GetAt(i))
- {
- pCostomAttr = static_cast<CDuiString*>(m_mWindowCustomAttrHash.Find(key));
- delete pCostomAttr;
- }
- }
- m_mWindowCustomAttrHash.Resize();
- }
- CControlUI* CPaintManagerUI::GetRoot() const
- {
- ASSERT(m_pRoot);
- return m_pRoot;
- }
- CControlUI* CPaintManagerUI::FindControl(POINT pt) const
- {
- ASSERT(m_pRoot);
- return m_pRoot->FindControl(__FindControlFromPoint, &pt, UIFIND_VISIBLE | UIFIND_HITTEST | UIFIND_TOP_FIRST);
- }
- CControlUI* CPaintManagerUI::FindControl(LPCTSTR pstrName) const
- {
- ASSERT(m_pRoot);
- return static_cast<CControlUI*>(m_mNameHash.Find(pstrName));
- }
- CControlUI* CPaintManagerUI::FindSubControlByPoint(CControlUI* pParent, POINT pt) const
- {
- if (pParent == NULL) pParent = GetRoot();
- ASSERT(pParent);
- return pParent->FindControl(__FindControlFromPoint, &pt, UIFIND_VISIBLE | UIFIND_HITTEST | UIFIND_TOP_FIRST);
- }
- CControlUI* CPaintManagerUI::FindSubControlByName(CControlUI* pParent, LPCTSTR pstrName) const
- {
- if (pParent == NULL) pParent = GetRoot();
- ASSERT(pParent);
- return pParent->FindControl(__FindControlFromName, (LPVOID)pstrName, UIFIND_ALL);
- }
- CControlUI* CPaintManagerUI::FindSubControlByClass(CControlUI* pParent, LPCTSTR pstrClass, int iIndex)
- {
- if (pParent == NULL) pParent = GetRoot();
- ASSERT(pParent);
- m_aFoundControls.Resize(iIndex + 1);
- return pParent->FindControl(__FindControlFromClass, (LPVOID)pstrClass, UIFIND_ALL);
- }
- CStdPtrArray* CPaintManagerUI::FindSubControlsByClass(CControlUI* pParent, LPCTSTR pstrClass)
- {
- if (pParent == NULL) pParent = GetRoot();
- ASSERT(pParent);
- m_aFoundControls.Empty();
- pParent->FindControl(__FindControlsFromClass, (LPVOID)pstrClass, UIFIND_ALL);
- return &m_aFoundControls;
- }
- CStdPtrArray* CPaintManagerUI::GetFoundControls()
- {
- return &m_aFoundControls;
- }
- CControlUI* CALLBACK CPaintManagerUI::__FindControlFromNameHash(CControlUI* pThis, LPVOID pData)
- {
- CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(pData);
- const CDuiString& sName = pThis->GetName();
- if (sName.IsEmpty()) return NULL;
- // Add this control to the hash list
- pManager->m_mNameHash.Set(sName, pThis);
- return NULL; // Attempt to add all controls
- }
- CControlUI* CALLBACK CPaintManagerUI::__FindControlFromCount(CControlUI* /*pThis*/, LPVOID pData)
- {
- int* pnCount = static_cast<int*>(pData);
- (*pnCount)++;
- return NULL; // Count all controls
- }
- CControlUI* CALLBACK CPaintManagerUI::__FindControlFromPoint(CControlUI* pThis, LPVOID pData)
- {
- LPPOINT pPoint = static_cast<LPPOINT>(pData);
- return ::PtInRect(&pThis->GetPos(), *pPoint) ? pThis : NULL;
- }
- CControlUI* CALLBACK CPaintManagerUI::__FindControlFromTab(CControlUI* pThis, LPVOID pData)
- {
- FINDTABINFO* pInfo = static_cast<FINDTABINFO*>(pData);
- if (pInfo->pFocus == pThis)
- {
- if (pInfo->bForward) pInfo->bNextIsIt = true;
- return pInfo->bForward ? NULL : pInfo->pLast;
- }
- if ((pThis->GetControlFlags() & UIFLAG_TABSTOP) == 0) return NULL;
- pInfo->pLast = pThis;
- if (pInfo->bNextIsIt) return pThis;
- if (pInfo->pFocus == NULL) return pThis;
- return NULL; // Examine all controls
- }
- CControlUI* CALLBACK CPaintManagerUI::__FindControlFromShortcut(CControlUI* pThis, LPVOID pData)
- {
- if (!pThis->IsVisible()) return NULL;
- FINDSHORTCUT* pFS = static_cast<FINDSHORTCUT*>(pData);
- if (pFS->ch == toupper(pThis->GetShortcut())) pFS->bPickNext = true;
- if (_tcsstr(pThis->GetClass(), _T("LabelUI")) != NULL) return NULL; // Labels never get focus!
- return pFS->bPickNext ? pThis : NULL;
- }
- CControlUI* CALLBACK CPaintManagerUI::__FindControlFromName(CControlUI* pThis, LPVOID pData)
- {
- LPCTSTR pstrName = static_cast<LPCTSTR>(pData);
- const CDuiString& sName = pThis->GetName();
- if (sName.IsEmpty()) return NULL;
- return (_tcsicmp(sName, pstrName) == 0) ? pThis : NULL;
- }
- CControlUI* CALLBACK CPaintManagerUI::__FindControlFromClass(CControlUI* pThis, LPVOID pData)
- {
- LPCTSTR pstrType = static_cast<LPCTSTR>(pData);
- LPCTSTR pType = pThis->GetClass();
- CStdPtrArray* pFoundControls = pThis->GetManager()->GetFoundControls();
- if (_tcscmp(pstrType, _T("*")) == 0 || _tcscmp(pstrType, pType) == 0)
- {
- int iIndex = -1;
- while (pFoundControls->GetAt(++iIndex) != NULL);
- if (iIndex < pFoundControls->GetSize()) pFoundControls->SetAt(iIndex, pThis);
- }
- if (pFoundControls->GetAt(pFoundControls->GetSize() - 1) != NULL) return pThis;
- return NULL;
- }
- CControlUI* CALLBACK CPaintManagerUI::__FindControlsFromClass(CControlUI* pThis, LPVOID pData)
- {
- LPCTSTR pstrType = static_cast<LPCTSTR>(pData);
- LPCTSTR pType = pThis->GetClass();
- if (_tcscmp(pstrType, _T("*")) == 0 || _tcscmp(pstrType, pType) == 0)
- pThis->GetManager()->GetFoundControls()->Add((LPVOID)pThis);
- return NULL;
- }
- CControlUI* CALLBACK CPaintManagerUI::__FindControlsFromUpdate(CControlUI* pThis, LPVOID pData)
- {
- if (pThis->IsUpdateNeeded())
- {
- pThis->GetManager()->GetFoundControls()->Add((LPVOID)pThis);
- return pThis;
- }
- return NULL;
- }
- bool CPaintManagerUI::TranslateAccelerator(LPMSG pMsg)
- {
- for (int i = 0; i < m_aTranslateAccelerator.GetSize(); i++)
- {
- LRESULT lResult = static_cast<ITranslateAccelerator*>(m_aTranslateAccelerator[i])->TranslateAccelerator(pMsg);
- if (lResult == S_OK) return true;
- }
- return false;
- }
- bool CPaintManagerUI::TranslateMessage(const LPMSG pMsg)
- {
- // Pretranslate Message takes care of system-wide messages, such as
- // tabbing and shortcut key-combos. We'll look for all messages for
- // each window and any child control attached.
- UINT uStyle = GetWindowStyle(pMsg->hwnd);
- UINT uChildRes = uStyle & WS_CHILD;
- LRESULT lRes = 0;
- if (uChildRes != 0)
- {
- HWND hWndParent = ::GetParent(pMsg->hwnd);
- for (int i = 0; i < m_aPreMessages.GetSize(); i++)
- {
- CPaintManagerUI* pT = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
- HWND hTempParent = hWndParent;
- while (hTempParent)
- {
- if (pMsg->hwnd == pT->GetPaintWindow() || hTempParent == pT->GetPaintWindow())
- {
- if (pT->TranslateAccelerator(pMsg))
- return true;
- pT->PreMessageHandler(pMsg->message, pMsg->wParam, pMsg->lParam, lRes);
- }
- hTempParent = GetParent(hTempParent);
- }
- }
- }
- else
- {
- for (int i = 0; i < m_aPreMessages.GetSize(); i++)
- {
- CPaintManagerUI* pT = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
- if (pMsg->hwnd == pT->GetPaintWindow())
- {
- if (pT->TranslateAccelerator(pMsg))
- return true;
- if (pT->PreMessageHandler(pMsg->message, pMsg->wParam, pMsg->lParam, lRes))
- return true;
- return false;
- }
- }
- }
- return false;
- }
- bool CPaintManagerUI::AddTranslateAccelerator(ITranslateAccelerator* pTranslateAccelerator)
- {
- ASSERT(m_aTranslateAccelerator.Find(pTranslateAccelerator) < 0);
- return m_aTranslateAccelerator.Add(pTranslateAccelerator);
- }
- bool CPaintManagerUI::RemoveTranslateAccelerator(ITranslateAccelerator* pTranslateAccelerator)
- {
- for (int i = 0; i < m_aTranslateAccelerator.GetSize(); i++)
- {
- if (static_cast<ITranslateAccelerator*>(m_aTranslateAccelerator[i]) == pTranslateAccelerator)
- {
- return m_aTranslateAccelerator.Remove(i);
- }
- }
- return false;
- }
- void CPaintManagerUI::UsedVirtualWnd(bool bUsed)
- {
- m_bUsedVirtualWnd = bUsed;
- }
- // 样式管理
- void CPaintManagerUI::AddStyle(LPCTSTR pName, LPCTSTR pDeclarationList, bool bShared)
- {
- CDuiString* pStyle = new CDuiString(pDeclarationList);
- if (bShared || m_bForceUseSharedRes)
- {
- if (!m_SharedResInfo.m_StyleHash.Insert(pName, pStyle))
- {
- delete pStyle;
- }
- }
- else
- {
- if (!m_ResInfo.m_StyleHash.Insert(pName, pStyle))
- {
- delete pStyle;
- }
- }
- }
- LPCTSTR CPaintManagerUI::GetStyle(LPCTSTR pName) const
- {
- CDuiString* pStyle = static_cast<CDuiString*>(m_ResInfo.m_StyleHash.Find(pName));
- if (!pStyle) pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(pName));
- if (pStyle) return pStyle->GetData();
- else return NULL;
- }
- BOOL CPaintManagerUI::RemoveStyle(LPCTSTR pName, bool bShared)
- {
- CDuiString* pStyle = NULL;
- if (bShared)
- {
- pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(pName));
- if (pStyle)
- {
- delete pStyle;
- m_SharedResInfo.m_StyleHash.Remove(pName);
- }
- }
- else
- {
- pStyle = static_cast<CDuiString*>(m_ResInfo.m_StyleHash.Find(pName));
- if (pStyle)
- {
- delete pStyle;
- m_ResInfo.m_StyleHash.Remove(pName);
- }
- }
- return true;
- }
- const CStdStringPtrMap& CPaintManagerUI::GetStyles(bool bShared) const
- {
- if (bShared) return m_SharedResInfo.m_StyleHash;
- else return m_ResInfo.m_StyleHash;
- }
- void CPaintManagerUI::RemoveAllStyle(bool bShared)
- {
- if (bShared)
- {
- CDuiString* pStyle;
- for (int i = 0; i < m_SharedResInfo.m_StyleHash.GetSize(); i++)
- {
- if (LPCTSTR key = m_SharedResInfo.m_StyleHash.GetAt(i))
- {
- pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(key));
- delete pStyle;
- }
- }
- m_SharedResInfo.m_StyleHash.RemoveAll();
- }
- else
- {
- CDuiString* pStyle;
- for (int i = 0; i < m_ResInfo.m_StyleHash.GetSize(); i++)
- {
- if (LPCTSTR key = m_ResInfo.m_StyleHash.GetAt(i))
- {
- pStyle = static_cast<CDuiString*>(m_ResInfo.m_StyleHash.Find(key));
- delete pStyle;
- }
- }
- m_ResInfo.m_StyleHash.RemoveAll();
- }
- }
- const TImageInfo* CPaintManagerUI::GetImageString(LPCTSTR pStrImage, LPCTSTR pStrModify)
- {
- CDuiString sImageName = pStrImage;
- CDuiString sImageResType = _T("");
- DWORD dwMask = 0;
- CDuiString sItem;
- CDuiString sValue;
- LPTSTR pstr = NULL;
- for (int i = 0; i < 2; ++i)
- {
- if (i == 1)
- pStrImage = pStrModify;
- if (!pStrImage) continue;
- while (*pStrImage != _T('\0'))
- {
- sItem.Empty();
- sValue.Empty();
- while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
- while (*pStrImage != _T('\0') && *pStrImage != _T('=') && *pStrImage > _T(' '))
- {
- LPTSTR pstrTemp = ::CharNext(pStrImage);
- while (pStrImage < pstrTemp)
- {
- sItem += *pStrImage++;
- }
- }
- while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
- if (*pStrImage++ != _T('=')) break;
- while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
- if (*pStrImage++ != _T('\'')) break;
- while (*pStrImage != _T('\0') && *pStrImage != _T('\''))
- {
- LPTSTR pstrTemp = ::CharNext(pStrImage);
- while (pStrImage < pstrTemp)
- {
- sValue += *pStrImage++;
- }
- }
- if (*pStrImage++ != _T('\'')) break;
- if (!sValue.IsEmpty())
- {
- if (sItem == _T("file") || sItem == _T("res"))
- {
- sImageName = sValue;
- }
- else if (sItem == _T("restype"))
- {
- sImageResType = sValue;
- }
- else if (sItem == _T("mask"))
- {
- if (sValue[0] == _T('#')) dwMask = _tcstoul(sValue.GetData() + 1, &pstr, 16);
- else dwMask = _tcstoul(sValue.GetData(), &pstr, 16);
- }
- }
- if (*pStrImage++ != _T(' ')) break;
- }
- }
- return GetImageEx(sImageName, sImageResType, dwMask);
- }
- bool CPaintManagerUI::EnableDragDrop(bool bEnable)
- {
- if (m_bDragDrop == bEnable) return false;
- m_bDragDrop = bEnable;
- if (bEnable)
- {
- AddRef();
- if (FAILED(RegisterDragDrop(m_hWndPaint, this)))
- {
- return false;
- }
- SetTargetWnd(m_hWndPaint);
- FORMATETC ftetc = { 0 };
- ftetc.cfFormat = CF_BITMAP;
- ftetc.dwAspect = DVASPECT_CONTENT;
- ftetc.lindex = -1;
- ftetc.tymed = TYMED_GDI;
- AddSuportedFormat(ftetc);
- ftetc.cfFormat = CF_DIB;
- ftetc.tymed = TYMED_HGLOBAL;
- AddSuportedFormat(ftetc);
- ftetc.cfFormat = CF_HDROP;
- ftetc.tymed = TYMED_HGLOBAL;
- AddSuportedFormat(ftetc);
- ftetc.cfFormat = CF_ENHMETAFILE;
- ftetc.tymed = TYMED_ENHMF;
- AddSuportedFormat(ftetc);
- }
- else
- {
- Release();
- if (FAILED(RevokeDragDrop(m_hWndPaint)))
- {
- return false;
- }
- }
- return true;
- }
- void CPaintManagerUI::SetDragDrop(IDragDropUI* pDragDrop)
- {
- m_pDragDrop = pDragDrop;
- }
- static WORD DIBNumColors(void* pv)
- {
- int bits;
- LPBITMAPINFOHEADER lpbi;
- LPBITMAPCOREHEADER lpbc;
- lpbi = ((LPBITMAPINFOHEADER)pv);
- lpbc = ((LPBITMAPCOREHEADER)pv);
- /* With the BITMAPINFO format headers, the size of the palette
- * is in biClrUsed, whereas in the BITMAPCORE - style headers, it
- * is dependent on the bits per pixel ( = 2 raised to the power of
- * bits/pixel).
- */
- if (lpbi->biSize != sizeof(BITMAPCOREHEADER))
- {
- if (lpbi->biClrUsed != 0)
- return (WORD)lpbi->biClrUsed;
- bits = lpbi->biBitCount;
- }
- else
- bits = lpbc->bcBitCount;
- switch (bits)
- {
- case 1:
- return 2;
- case 4:
- return 16;
- case 8:
- return 256;
- default:
- /* A 24 bitcount DIB has no color table */
- return 0;
- }
- }
- //code taken from SEEDIB MSDN sample
- static WORD ColorTableSize(LPVOID lpv)
- {
- LPBITMAPINFOHEADER lpbih = (LPBITMAPINFOHEADER)lpv;
- if (lpbih->biSize != sizeof(BITMAPCOREHEADER))
- {
- if (((LPBITMAPINFOHEADER)(lpbih))->biCompression == BI_BITFIELDS)
- /* Remember that 16/32bpp dibs can still have a color table */
- return (sizeof(DWORD) * 3) + (DIBNumColors(lpbih) * sizeof(RGBQUAD));
- else
- return (WORD)(DIBNumColors(lpbih) * sizeof(RGBQUAD));
- }
- else
- return (WORD)(DIBNumColors(lpbih) * sizeof(RGBTRIPLE));
- }
- bool CPaintManagerUI::OnDrop(FORMATETC* pFmtEtc, STGMEDIUM& medium, DWORD* pdwEffect)
- {
- POINT ptMouse = { 0 };
- GetCursorPos(&ptMouse);
- ::SendMessage(m_hTargetWnd, WM_LBUTTONUP, NULL, MAKELPARAM(ptMouse.x, ptMouse.y));
- if (pFmtEtc->cfFormat == CF_DIB && medium.tymed == TYMED_HGLOBAL)
- {
- if (medium.hGlobal != NULL)
- {
- LPBITMAPINFOHEADER lpbi = (BITMAPINFOHEADER*)GlobalLock(medium.hGlobal);
- if (lpbi != NULL)
- {
- HBITMAP hbm = NULL;
- HDC hdc = GetDC(NULL);
- if (hdc != NULL)
- {
- int i = ((BITMAPFILEHEADER*)lpbi)->bfOffBits;
- hbm = CreateDIBitmap(hdc, (LPBITMAPINFOHEADER)lpbi,
- (LONG)CBM_INIT,
- (LPSTR)lpbi + lpbi->biSize + ColorTableSize(lpbi),
- (LPBITMAPINFO)lpbi, DIB_RGB_COLORS);
- ::ReleaseDC(NULL, hdc);
- }
- GlobalUnlock(medium.hGlobal);
- if (hbm != NULL)
- hbm = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbm);
- if (hbm != NULL)
- DeleteObject(hbm);
- return true; //release the medium
- }
- }
- }
- if (pFmtEtc->cfFormat == CF_BITMAP && medium.tymed == TYMED_GDI)
- {
- if (medium.hBitmap != NULL)
- {
- HBITMAP hBmp = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)medium.hBitmap);
- if (hBmp != NULL)
- DeleteObject(hBmp);
- return false; //don't free the bitmap
- }
- }
- if (pFmtEtc->cfFormat == CF_ENHMETAFILE && medium.tymed == TYMED_ENHMF)
- {
- ENHMETAHEADER emh;
- GetEnhMetaFileHeader(medium.hEnhMetaFile, sizeof(ENHMETAHEADER), &emh);
- RECT rc;//={0,0,EnhMetaHdr.rclBounds.right-EnhMetaHdr.rclBounds.left, EnhMetaHdr.rclBounds.bottom-EnhMetaHdr.rclBounds.top};
- HDC hDC = GetDC(m_hTargetWnd);
- //start code: taken from ENHMETA.EXE MSDN Sample
- //*ALSO NEED to GET the pallete (select and RealizePalette it, but i was too lazy*
- // Get the characteristics of the output device
- float PixelsX = (float)GetDeviceCaps(hDC, HORZRES);
- float PixelsY = (float)GetDeviceCaps(hDC, VERTRES);
- float MMX = (float)GetDeviceCaps(hDC, HORZSIZE);
- float MMY = (float)GetDeviceCaps(hDC, VERTSIZE);
- // Calculate the rect in which to draw the metafile based on the
- // intended size and the current output device resolution
- // Remember that the intended size is given in 0.01mm units, so
- // convert those to device units on the target device
- rc.top = (int)((float)(emh.rclFrame.top) * PixelsY / (MMY * 100.0f));
- rc.left = (int)((float)(emh.rclFrame.left) * PixelsX / (MMX * 100.0f));
- rc.right = (int)((float)(emh.rclFrame.right) * PixelsX / (MMX * 100.0f));
- rc.bottom = (int)((float)(emh.rclFrame.bottom) * PixelsY / (MMY * 100.0f));
- //end code: taken from ENHMETA.EXE MSDN Sample
- HDC hdcMem = CreateCompatibleDC(hDC);
- HGDIOBJ hBmpMem = CreateCompatibleBitmap(hDC, emh.rclBounds.right, emh.rclBounds.bottom);
- HGDIOBJ hOldBmp = ::SelectObject(hdcMem, hBmpMem);
- PlayEnhMetaFile(hdcMem, medium.hEnhMetaFile, &rc);
- HBITMAP hBmp = (HBITMAP)::SelectObject(hdcMem, hOldBmp);
- DeleteDC(hdcMem);
- ReleaseDC(m_hTargetWnd, hDC);
- hBmp = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBmp);
- if (hBmp != NULL)
- DeleteObject(hBmp);
- return true;
- }
- if (pFmtEtc->cfFormat == CF_HDROP && medium.tymed == TYMED_HGLOBAL)
- {
- HDROP hDrop = (HDROP)GlobalLock(medium.hGlobal);
- if (hDrop != NULL)
- {
- TCHAR szFileName[MAX_PATH];
- UINT cFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
- if (cFiles > 0)
- {
- DragQueryFile(hDrop, 0, szFileName, sizeof(szFileName));
- HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
- if (hBitmap)
- {
- HBITMAP hBmp = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
- if (hBmp != NULL)
- DeleteObject(hBmp);
- }
- }
- }
- GlobalUnlock(medium.hGlobal);
- }
- return true; //let base free the medium
- }
- } // namespace DuiLib
- #pragma warning(pop)
|