UIManager.cpp 166 KB

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