UIManager.cpp 165 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419
  1. #include "StdAfx.h"
  2. #include <zmouse.h>
  3. // [触屏诊断] 显式引入,避免依赖 tchar.h 的传递包含
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <tchar.h>
  7. #pragma warning(push)
  8. #pragma warning(disable:4838 4244)
  9. namespace DuiLib
  10. {
  11. /////////////////////////////////////////////////////////////////////////////////////
  12. //
  13. //
  14. // 触屏参数(NIM Duilib 模型)
  15. // TOUCHINPUT 的坐标以 1/100 像素为单位,必须先换算成像素
  16. #ifndef TOUCH_COORD_TO_PIXEL
  17. #define TOUCH_COORD_TO_PIXEL(l) ((l) / 100)
  18. #endif
  19. // [触屏模型] 触摸只做两件事,不做任何“触摸→鼠标”的归一化:
  20. // ①点击:由 TOUCHUP 在确认未拖动后派发 BUTTONDOWN/UP,
  21. // 由 CButtonUI 等控件原生 Activate();
  22. // ②滚动:WM_TOUCH 直接派发 UIEVENT_TOUCHDOWN/MOVE/UP(见 OnTouchInput),
  23. // 容器据此 1:1 跟手平移、滚动条据此拖滑块。
  24. // “轻点 vs 拖拽”使用 m_bTouchMoved 区分,拖动不会激活控件。
  25. // WM_TOUCH 消息 ID 同样受 `#if _WIN32_WINNT >= 0x0601` 保护,DuiLib 工程未定义
  26. // _WIN32_WINNT(StdAfx.h 兜底为 XP)→ 必须自行声明,否则 switch 里引用该消息 ID
  27. // 会报 C2065(未声明的标识符)与 C2051(case 表达式不是常量)。实测固定值 0x0240。
  28. // 注:Win7+ 段受保护的不仅有结构体与函数,**消息 ID 与辅助宏同样在内**,
  29. // TOUCH_COORD_TO_PIXEL 也在上面用 #ifndef 兜底,原因相同。
  30. #ifndef WM_TOUCH
  31. #define WM_TOUCH 0x0240
  32. #endif
  33. // ---------------------------------------------------------------------------
  34. // [触屏诊断] 保留日志入口以便后续定位,但当前默认关闭。
  35. // ---------------------------------------------------------------------------
  36. namespace TouchTrace
  37. {
  38. inline bool Enabled()
  39. {
  40. return true;
  41. }
  42. // 极简 TCHAR* → UTF-8 char* 转换(仅用于日志路径;非线程安全,够用即可)
  43. inline const char* Narrow(LPCTSTR s)
  44. {
  45. static char s_buf[MAX_PATH * 3] = { 0 };
  46. #ifdef _UNICODE
  47. s_buf[0] = '\0';
  48. if (s != NULL)
  49. ::WideCharToMultiByte(CP_UTF8, 0, s, -1, s_buf, (int)sizeof(s_buf) - 1, NULL, NULL);
  50. return s_buf;
  51. #else
  52. return s;
  53. #endif
  54. }
  55. inline void Log(LPCTSTR fmt, ...)
  56. {
  57. if (!Enabled()) return;
  58. TCHAR path[MAX_PATH] = { 0 };
  59. ::GetModuleFileName(NULL, path, MAX_PATH);
  60. TCHAR* p = _tcsrchr(path, _T('\\'));
  61. if (p != NULL) *(p + 1) = _T('\0');
  62. _tcscat_s(path, MAX_PATH, _T("touch_trace.log"));
  63. TCHAR msg[1024] = { 0 };
  64. va_list ap;
  65. va_start(ap, fmt);
  66. _vsntprintf_s(msg, _countof(msg), _TRUNCATE, fmt, ap);
  67. va_end(ap);
  68. // 统一转 UTF-8 写盘(Unicode/ANSI 工程都能用文本编辑器正常查看)
  69. char utf8[2048] = { 0 };
  70. #ifdef _UNICODE
  71. if (::WideCharToMultiByte(CP_UTF8, 0, msg, -1, utf8, (int)sizeof(utf8) - 1, NULL, NULL) <= 0)
  72. return;
  73. #else
  74. _tcscpy_s(utf8, _countof(utf8), msg);
  75. #endif
  76. FILE* f = NULL;
  77. // 保护:单个日志超过 4MB 就不再追加,避免在收银机上长期运行把日志写到失控。
  78. // 注意:GetFileAttributesEx 是 TCHAR 宏(Unicode 工程下展开为 ...ExW),
  79. // 必须传 TCHAR 的 path,不能传 Narrow(path) 得到的 UTF-8 char*(否则 C2664)。
  80. WIN32_FILE_ATTRIBUTE_DATA fad = { 0 };
  81. if (::GetFileAttributesEx(path, GetFileExInfoStandard, &fad)
  82. && fad.nFileSizeHigh == 0 && fad.nFileSizeLow >= (4u * 1024u * 1024u))
  83. return;
  84. // fopen_s 只吃 char*(窄字符路径),这里才需要 UTF-8 的 Narrow(path)
  85. if (fopen_s(&f, Narrow(path), "a") == 0 && f != NULL)
  86. {
  87. fprintf(f, "[%8u][T%5u] %s\n", (unsigned)::GetTickCount(), (unsigned)::GetCurrentThreadId(), utf8);
  88. fclose(f);
  89. }
  90. }
  91. }
  92. static void GetChildWndRect(HWND hWnd, HWND hChildWnd, RECT& rcChildWnd)
  93. {
  94. ::GetWindowRect(hChildWnd, &rcChildWnd);
  95. POINT pt;
  96. pt.x = rcChildWnd.left;
  97. pt.y = rcChildWnd.top;
  98. ::ScreenToClient(hWnd, &pt);
  99. rcChildWnd.left = pt.x;
  100. rcChildWnd.top = pt.y;
  101. pt.x = rcChildWnd.right;
  102. pt.y = rcChildWnd.bottom;
  103. ::ScreenToClient(hWnd, &pt);
  104. rcChildWnd.right = pt.x;
  105. rcChildWnd.bottom = pt.y;
  106. }
  107. static UINT MapKeyState()
  108. {
  109. UINT uState = 0;
  110. if (::GetKeyState(VK_CONTROL) < 0) uState |= MK_CONTROL;
  111. if (::GetKeyState(VK_LBUTTON) < 0) uState |= MK_LBUTTON;
  112. if (::GetKeyState(VK_RBUTTON) < 0) uState |= MK_RBUTTON;
  113. if (::GetKeyState(VK_SHIFT) < 0) uState |= MK_SHIFT;
  114. if (::GetKeyState(VK_MENU) < 0) uState |= MK_ALT;
  115. return uState;
  116. }
  117. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  118. ///
  119. typedef struct tagFINDTABINFO
  120. {
  121. CControlUI* pFocus;
  122. CControlUI* pLast;
  123. bool bForward;
  124. bool bNextIsIt;
  125. } FINDTABINFO;
  126. typedef struct tagFINDSHORTCUT
  127. {
  128. TCHAR ch;
  129. bool bPickNext;
  130. } FINDSHORTCUT;
  131. typedef struct tagTIMERINFO
  132. {
  133. CControlUI* pSender;
  134. UINT nLocalID;
  135. HWND hWnd;
  136. UINT uWinTimer;
  137. bool bKilled;
  138. } TIMERINFO;
  139. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  140. ///
  141. tagTImageInfo::tagTImageInfo()
  142. {
  143. pImage = NULL;
  144. hBitmap = NULL;
  145. }
  146. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  147. ///
  148. tagTDrawInfo::tagTDrawInfo()
  149. {
  150. Clear();
  151. }
  152. void tagTDrawInfo::Parse(LPCTSTR pStrImage, LPCTSTR pStrModify, CPaintManagerUI* pManager)
  153. {
  154. // 1、aaa.jpg
  155. // 2、file='aaa.jpg' res='' restype='0' dest='0,0,0,0' source='0,0,0,0' corner='0,0,0,0'
  156. // mask='#FF0000' fade='255' hole='false' xtiled='false' ytiled='false'
  157. sDrawString = pStrImage;
  158. sDrawModify = pStrModify;
  159. sImageName = pStrImage;
  160. CDuiString sItem;
  161. CDuiString sValue;
  162. LPTSTR pstr = NULL;
  163. for (int i = 0; i < 2; ++i)
  164. {
  165. if (i == 1) pStrImage = pStrModify;
  166. if (!pStrImage) continue;
  167. while (*pStrImage != _T('\0'))
  168. {
  169. sItem.Empty();
  170. sValue.Empty();
  171. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  172. while (*pStrImage != _T('\0') && *pStrImage != _T('=') && *pStrImage > _T(' '))
  173. {
  174. LPTSTR pstrTemp = ::CharNext(pStrImage);
  175. while (pStrImage < pstrTemp)
  176. {
  177. sItem += *pStrImage++;
  178. }
  179. }
  180. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  181. if (*pStrImage++ != _T('=')) break;
  182. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  183. if (*pStrImage++ != _T('\'')) break;
  184. while (*pStrImage != _T('\0') && *pStrImage != _T('\''))
  185. {
  186. LPTSTR pstrTemp = ::CharNext(pStrImage);
  187. while (pStrImage < pstrTemp)
  188. {
  189. sValue += *pStrImage++;
  190. }
  191. }
  192. if (*pStrImage++ != _T('\'')) break;
  193. if (!sValue.IsEmpty())
  194. {
  195. if (sItem == _T("file") || sItem == _T("res"))
  196. {
  197. sImageName = sValue;
  198. }
  199. else if (sItem == _T("restype"))
  200. {
  201. sResType = sValue;
  202. }
  203. else if (sItem == _T("dest"))
  204. {
  205. rcDest.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
  206. rcDest.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  207. rcDest.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  208. rcDest.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  209. //if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcDest);
  210. }
  211. else if (sItem == _T("source"))
  212. {
  213. rcSource.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
  214. rcSource.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  215. rcSource.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  216. rcSource.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  217. //if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcSource);
  218. }
  219. else if (sItem == _T("corner"))
  220. {
  221. rcCorner.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
  222. rcCorner.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  223. rcCorner.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  224. rcCorner.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  225. //if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcCorner);
  226. }
  227. else if (sItem == _T("mask"))
  228. {
  229. if (sValue[0] == _T('#')) dwMask = _tcstoul(sValue.GetData() + 1, &pstr, 16);
  230. else dwMask = _tcstoul(sValue.GetData(), &pstr, 16);
  231. }
  232. else if (sItem == _T("fade"))
  233. {
  234. uFade = (UINT)_tcstoul(sValue.GetData(), &pstr, 10);
  235. }
  236. else if (sItem == _T("rotate"))
  237. {
  238. uRotate = (UINT)_tcstoul(sValue.GetData(), &pstr, 10);
  239. bGdiplus = true;
  240. }
  241. else if (sItem == _T("gdiplus"))
  242. {
  243. bGdiplus = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
  244. }
  245. else if (sItem == _T("hole"))
  246. {
  247. bHole = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
  248. }
  249. else if (sItem == _T("xtiled"))
  250. {
  251. bTiledX = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
  252. }
  253. else if (sItem == _T("ytiled"))
  254. {
  255. bTiledY = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
  256. }
  257. else if (sItem == _T("hsl"))
  258. {
  259. bHSL = (_tcsicmp(sValue.GetData(), _T("true")) == 0);
  260. }
  261. else if (sItem == _T("size"))
  262. {
  263. szImage.cx = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
  264. szImage.cy = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  265. }
  266. else if (sItem == _T("align"))
  267. {
  268. sAlign = sValue;
  269. }
  270. else if (sItem == _T("padding"))
  271. {
  272. rcPadding.left = _tcstol(sValue.GetData(), &pstr, 10); ASSERT(pstr);
  273. rcPadding.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  274. rcPadding.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  275. rcPadding.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
  276. //if(pManager != NULL) pManager->GetDPIObj()->Scale(&rcPadding);
  277. }
  278. }
  279. if (*pStrImage++ != _T(' ')) break;
  280. }
  281. }
  282. // 调整DPI资源
  283. if (pManager != NULL && pManager->GetDPIObj()->GetScale() != 100)
  284. {
  285. CDuiString sScale;
  286. sScale.Format(_T("@%d."), pManager->GetDPIObj()->GetScale());
  287. sImageName.Replace(_T("."), sScale);
  288. }
  289. }
  290. void tagTDrawInfo::Clear()
  291. {
  292. sDrawString.Empty();
  293. sDrawModify.Empty();
  294. sImageName.Empty();
  295. memset(&rcDest, 0, sizeof(RECT));
  296. memset(&rcSource, 0, sizeof(RECT));
  297. memset(&rcCorner, 0, sizeof(RECT));
  298. dwMask = 0;
  299. uFade = 255;
  300. uRotate = 0;
  301. bHole = false;
  302. bTiledX = false;
  303. bTiledY = false;
  304. bHSL = false;
  305. bGdiplus = false;
  306. szImage.cx = szImage.cy = 0;
  307. sAlign.Empty();
  308. memset(&rcPadding, 0, sizeof(RECT));
  309. }
  310. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  311. ///
  312. typedef BOOL(__stdcall* PFUNCUPDATELAYEREDWINDOW)(HWND, HDC, POINT*, SIZE*, HDC, POINT*, COLORREF, BLENDFUNCTION*, DWORD);
  313. PFUNCUPDATELAYEREDWINDOW g_fUpdateLayeredWindow = NULL;
  314. HPEN m_hUpdateRectPen = NULL;
  315. HINSTANCE CPaintManagerUI::m_hResourceInstance = NULL;
  316. CDuiString CPaintManagerUI::m_pStrResourcePath;
  317. CDuiString CPaintManagerUI::m_pStrResourceZip;
  318. CDuiString CPaintManagerUI::m_pStrResourceZipPwd; //Garfield 20160325 带密码zip包解密
  319. HANDLE CPaintManagerUI::m_hResourceZip = NULL;
  320. bool CPaintManagerUI::m_bCachedResourceZip = true;
  321. BYTE* CPaintManagerUI::m_cbZipBuf = nullptr;
  322. int CPaintManagerUI::m_nResType = UILIB_FILE;
  323. TResInfo CPaintManagerUI::m_SharedResInfo;
  324. HINSTANCE CPaintManagerUI::m_hInstance = NULL;
  325. bool CPaintManagerUI::m_bUseHSL = false;
  326. short CPaintManagerUI::m_H = 180;
  327. short CPaintManagerUI::m_S = 100;
  328. short CPaintManagerUI::m_L = 100;
  329. CStdPtrArray CPaintManagerUI::m_aPreMessages;
  330. CStdPtrArray CPaintManagerUI::m_aPlugins;
  331. CPaintManagerUI::CPaintManagerUI() :
  332. m_hWndPaint(NULL),
  333. m_hDcPaint(NULL),
  334. m_hDcOffscreen(NULL),
  335. m_hDcBackground(NULL),
  336. m_bOffscreenPaint(true),
  337. m_hbmpOffscreen(NULL),
  338. m_pOffscreenBits(NULL),
  339. m_hbmpBackground(NULL),
  340. m_pBackgroundBits(NULL),
  341. m_hwndTooltip(NULL),
  342. m_uTimerID(0x1000),
  343. m_pRoot(NULL),
  344. m_pFocus(NULL),
  345. m_pEventHover(NULL),
  346. m_pEventClick(NULL),
  347. m_pEventRClick(NULL),
  348. m_pEventKey(NULL),
  349. m_bFirstLayout(true),
  350. m_bFocusNeeded(false),
  351. m_bUpdateNeeded(false),
  352. m_bMouseTracking(false),
  353. m_bMouseCapture(false),
  354. m_bAsyncNotifyPosted(false),
  355. m_bUsedVirtualWnd(false),
  356. m_bForceUseSharedRes(false),
  357. m_nOpacity(0xFF),
  358. m_bLayered(false),
  359. m_bLayeredChanged(false),
  360. m_bShowUpdateRect(false),
  361. m_bUseGdiplusText(false),
  362. m_trh(0),
  363. m_bDragDrop(false),
  364. m_bDragMode(false),
  365. m_hDragBitmap(NULL),
  366. m_pDPI(NULL),
  367. m_iHoverTime(400UL),
  368. // [FIX] m_bNoActivate 原本未初始化:Debug 堆新块填充 0xCD -> 读到 true;
  369. // Release 下则是随机垃圾值,属偶发性隐患。
  370. // 后果:本文件中 3 处 "if (!m_bNoActivate) ::SetFocus(m_hWndPaint);"
  371. // (WM_LBUTTONDOWN 1738 / CPaintManagerUI::SetFocus 2442 / SetFocusNeeded 2475) 全部被跳过,
  372. // 系统焦点无法回到主窗口 -> 原生 EDIT 子窗 CEditWnd 收不到 WM_KILLFOCUS ->
  373. // 其唯一的销毁途径 PostMessage(WM_CLOSE) 永不触发 -> 光标常闪、点空白不丢焦点。
  374. // XML 侧仅在显式书写 noactivate 属性时才赋值(UIDlgBuilder.cpp),故此处必须先给确定初值。
  375. m_bNoActivate(false),
  376. // 以下两个成员同样是漏网之鱼(全库无赋值点),一并清零以防后患。
  377. m_bIsPainting(false),
  378. m_pLastToolTip(NULL)
  379. {
  380. if (m_SharedResInfo.m_DefaultFontInfo.sFontName.IsEmpty())
  381. {
  382. m_SharedResInfo.m_dwDefaultDisabledColor = 0xFFA7A6AA;
  383. m_SharedResInfo.m_dwDefaultFontColor = 0xFF000000;
  384. m_SharedResInfo.m_dwDefaultLinkFontColor = 0xFF0000FF;
  385. m_SharedResInfo.m_dwDefaultLinkHoverFontColor = 0xFFD3215F;
  386. m_SharedResInfo.m_dwDefaultSelectedBkColor = 0xFFBAE4FF;
  387. LOGFONT lf = { 0 };
  388. ::GetObject(::GetStockObject(DEFAULT_GUI_FONT), sizeof(LOGFONT), &lf);
  389. lf.lfCharSet = DEFAULT_CHARSET;
  390. HFONT hDefaultFont = ::CreateFontIndirect(&lf);
  391. m_SharedResInfo.m_DefaultFontInfo.hFont = hDefaultFont;
  392. m_SharedResInfo.m_DefaultFontInfo.sFontName = lf.lfFaceName;
  393. m_SharedResInfo.m_DefaultFontInfo.iSize = -lf.lfHeight;
  394. m_SharedResInfo.m_DefaultFontInfo.bBold = (lf.lfWeight >= FW_BOLD);
  395. m_SharedResInfo.m_DefaultFontInfo.bUnderline = (lf.lfUnderline == TRUE);
  396. m_SharedResInfo.m_DefaultFontInfo.bItalic = (lf.lfItalic == TRUE);
  397. ::ZeroMemory(&m_SharedResInfo.m_DefaultFontInfo.tm, sizeof(m_SharedResInfo.m_DefaultFontInfo.tm));
  398. }
  399. m_ResInfo.m_dwDefaultDisabledColor = m_SharedResInfo.m_dwDefaultDisabledColor;
  400. m_ResInfo.m_dwDefaultFontColor = m_SharedResInfo.m_dwDefaultFontColor;
  401. m_ResInfo.m_dwDefaultLinkFontColor = m_SharedResInfo.m_dwDefaultLinkFontColor;
  402. m_ResInfo.m_dwDefaultLinkHoverFontColor = m_SharedResInfo.m_dwDefaultLinkHoverFontColor;
  403. m_ResInfo.m_dwDefaultSelectedBkColor = m_SharedResInfo.m_dwDefaultSelectedBkColor;
  404. if (m_hUpdateRectPen == NULL)
  405. {
  406. m_hUpdateRectPen = ::CreatePen(PS_SOLID, 1, RGB(220, 0, 0));
  407. // Boot Windows Common Controls (for the ToolTip control)
  408. ::InitCommonControls();
  409. ::LoadLibrary(_T("msimg32.dll"));
  410. }
  411. m_szMinWindow.cx = 0;
  412. m_szMinWindow.cy = 0;
  413. m_szMaxWindow.cx = 0;
  414. m_szMaxWindow.cy = 0;
  415. m_szInitWindowSize.cx = 0;
  416. m_szInitWindowSize.cy = 0;
  417. m_szRoundCorner.cx = m_szRoundCorner.cy = 0;
  418. ::ZeroMemory(&m_rcSizeBox, sizeof(m_rcSizeBox));
  419. ::ZeroMemory(&m_rcCaption, sizeof(m_rcCaption));
  420. ::ZeroMemory(&m_rcLayeredInset, sizeof(m_rcLayeredInset));
  421. ::ZeroMemory(&m_rcLayeredUpdate, sizeof(m_rcLayeredUpdate));
  422. m_ptLastMousePos.x = m_ptLastMousePos.y = -1;
  423. m_pGdiplusStartupInput = new Gdiplus::GdiplusStartupInput;
  424. Gdiplus::GdiplusStartup(&m_gdiplusToken, m_pGdiplusStartupInput, NULL); // 加载GDI接口
  425. CShadowUI::Initialize(m_hInstance);
  426. m_pDragDrop = NULL;
  427. }
  428. CPaintManagerUI::~CPaintManagerUI()
  429. {
  430. // Delete the control-tree structures
  431. for (int i = 0; i < m_aDelayedCleanup.GetSize(); i++) delete static_cast<CControlUI*>(m_aDelayedCleanup[i]);
  432. m_aDelayedCleanup.Resize(0);
  433. for (int i = 0; i < m_aAsyncNotify.GetSize(); i++) delete static_cast<TNotifyUI*>(m_aAsyncNotify[i]);
  434. m_aAsyncNotify.Resize(0);
  435. m_mNameHash.Resize(0);
  436. if (m_pRoot != NULL) delete m_pRoot;
  437. ::DeleteObject(m_ResInfo.m_DefaultFontInfo.hFont);
  438. RemoveAllFonts();
  439. RemoveAllImages();
  440. RemoveAllStyle();
  441. RemoveAllDefaultAttributeList();
  442. RemoveAllWindowCustomAttribute();
  443. RemoveAllOptionGroups();
  444. RemoveAllTimers();
  445. RemoveAllDrawInfos();
  446. if (m_hwndTooltip != NULL)
  447. {
  448. ::DestroyWindow(m_hwndTooltip);
  449. m_hwndTooltip = NULL;
  450. }
  451. if (!m_aFonts.IsEmpty())
  452. {
  453. for (int i = 0; i < m_aFonts.GetSize(); ++i)
  454. {
  455. HANDLE handle = static_cast<HANDLE>(m_aFonts.GetAt(i));
  456. ::RemoveFontMemResourceEx(handle);
  457. }
  458. }
  459. if (m_hDcOffscreen != NULL) ::DeleteDC(m_hDcOffscreen);
  460. if (m_hDcBackground != NULL) ::DeleteDC(m_hDcBackground);
  461. if (m_hbmpOffscreen != NULL) ::DeleteObject(m_hbmpOffscreen);
  462. if (m_hbmpBackground != NULL) ::DeleteObject(m_hbmpBackground);
  463. if (m_hDcPaint != NULL) ::ReleaseDC(m_hWndPaint, m_hDcPaint);
  464. m_aPreMessages.Remove(m_aPreMessages.Find(this));
  465. // 销毁拖拽图片
  466. if (m_hDragBitmap != NULL) ::DeleteObject(m_hDragBitmap);
  467. //卸载GDIPlus
  468. Gdiplus::GdiplusShutdown(m_gdiplusToken);
  469. delete m_pGdiplusStartupInput;
  470. // DPI管理对象
  471. if (m_pDPI != NULL)
  472. {
  473. delete m_pDPI;
  474. m_pDPI = NULL;
  475. }
  476. }
  477. void CPaintManagerUI::Init(HWND hWnd, LPCTSTR pstrName)
  478. {
  479. ASSERT(::IsWindow(hWnd));
  480. m_mNameHash.Resize();
  481. RemoveAllFonts();
  482. RemoveAllImages();
  483. RemoveAllStyle();
  484. RemoveAllDefaultAttributeList();
  485. RemoveAllWindowCustomAttribute();
  486. RemoveAllOptionGroups();
  487. RemoveAllTimers();
  488. m_sName.Empty();
  489. if (pstrName != NULL) m_sName = pstrName;
  490. if (m_hWndPaint != hWnd)
  491. {
  492. m_hWndPaint = hWnd;
  493. m_hDcPaint = ::GetDC(hWnd);
  494. m_aPreMessages.Add(this);
  495. // 触屏兼容:每个窗口(主窗口 + 所有弹框)统一注册 WM_TOUCH,由本类库级接管,
  496. // 无需 CTouchWindowBase 外挂,也无需逐窗口样板。注册是幂等的,重复调用无害。
  497. InitTouchSupport();
  498. }
  499. }
  500. // ---------------------------------------------------------------------------
  501. // 触摸 API 懒加载包装(对标 NIM Duilib 的 RegisterTouchWindowWrapper 系列)。
  502. //
  503. // 为什么不能直接调用这些 API:DuiLib 工程自身没有定义 _WIN32_WINNT,
  504. // StdAfx.h 会把它兜底设成 XP(0x0501);而 TOUCHINPUT / GetTouchInputInfo /
  505. // RegisterTouchWindow 在 winuser.h 里受 `#if _WIN32_WINNT >= 0x0601` 保护,
  506. // 直接引用会编译报错(C2065/C3861)。(主工程定义了 0x0601,所以
  507. // CTouchWindowBase 里能直接用,那里不受影响。)
  508. //
  509. // 因此这里完全自给自足:自行声明与 SDK 完全一致的 TOUCHINPUT 布局和事件标志,
  510. // 再用 GetProcAddress 取函数地址。这样:
  511. // * 无论 DuiLib 的 target 版本如何都能编译通过;
  512. // * 在没有触摸 API 的系统上自动降级为空操作,不会崩溃。
  513. // ---------------------------------------------------------------------------
  514. namespace TouchApi
  515. {
  516. // 与 winuser.h 的 tagTOUCHINPUT 布局逐字段一致
  517. typedef struct tagTouchInput
  518. {
  519. LONG x;
  520. LONG y;
  521. HANDLE hSource;
  522. DWORD dwID;
  523. DWORD dwFlags;
  524. DWORD dwMask;
  525. DWORD dwTime;
  526. ULONG_PTR dwExtraInfo;
  527. DWORD cxContact;
  528. DWORD cyContact;
  529. } TOUCHINPUT, *PTOUCHINPUT;
  530. // 事件标志,数值与 winuser.h 的触摸事件标志一致。
  531. // 名字必须避开 SDK 的同名宏:部分 SDK 把它们定义成【宏】,且不受 _WIN32_WINNT 版本段保护,
  532. // 同名会被预处理器展开——声明变成 `const DWORD 0x0002 = 0x0002;`(C2059),
  533. // 限定使用变成 `TouchApi::0x0004`(C2589)。故改用 TOUCHEVTFLAG 前缀。
  534. const DWORD TOUCHEVTFLAG_MOVE = 0x0001;
  535. const DWORD TOUCHEVTFLAG_DOWN = 0x0002;
  536. const DWORD TOUCHEVTFLAG_UP = 0x0004;
  537. typedef BOOL (WINAPI *PFN_RegisterTouchWindow)(HWND, ULONG);
  538. typedef BOOL (WINAPI *PFN_GetTouchInputInfo)(HANDLE, UINT, PTOUCHINPUT, int);
  539. typedef BOOL (WINAPI *PFN_CloseTouchInputHandle)(HANDLE);
  540. inline HMODULE GetUser32()
  541. {
  542. static HMODULE s_hMod = NULL;
  543. static bool s_bInited = false;
  544. if (!s_bInited)
  545. {
  546. s_bInited = true;
  547. s_hMod = ::GetModuleHandle(_T("user32.dll"));
  548. }
  549. return s_hMod;
  550. }
  551. inline PFN_RegisterTouchWindow GetRegisterTouchWindow()
  552. {
  553. static PFN_RegisterTouchWindow s_pfn = NULL;
  554. static bool s_bInited = false;
  555. if (!s_bInited)
  556. {
  557. s_bInited = true;
  558. HMODULE hMod = GetUser32();
  559. if (hMod != NULL) s_pfn = (PFN_RegisterTouchWindow)::GetProcAddress(hMod, "RegisterTouchWindow");
  560. }
  561. return s_pfn;
  562. }
  563. inline PFN_GetTouchInputInfo GetGetTouchInputInfo()
  564. {
  565. static PFN_GetTouchInputInfo s_pfn = NULL;
  566. static bool s_bInited = false;
  567. if (!s_bInited)
  568. {
  569. s_bInited = true;
  570. HMODULE hMod = GetUser32();
  571. if (hMod != NULL) s_pfn = (PFN_GetTouchInputInfo)::GetProcAddress(hMod, "GetTouchInputInfo");
  572. }
  573. return s_pfn;
  574. }
  575. inline PFN_CloseTouchInputHandle GetCloseTouchInputHandle()
  576. {
  577. static PFN_CloseTouchInputHandle s_pfn = NULL;
  578. static bool s_bInited = false;
  579. if (!s_bInited)
  580. {
  581. s_bInited = true;
  582. HMODULE hMod = GetUser32();
  583. if (hMod != NULL) s_pfn = (PFN_CloseTouchInputHandle)::GetProcAddress(hMod, "CloseTouchInputHandle");
  584. }
  585. return s_pfn;
  586. }
  587. }
  588. // ---------------------------------------------------------------------------
  589. // [触屏] 手指容差(滚动条专治)
  590. // 滚动条默认仅 16px 宽(CScrollBarUI::DEFAULT_SCROLLBAR_SIZE),指尖按偏 5~10px 就落到
  591. // 右侧内容上:内容会跟手滚动,但“滑块拖不动”——用户感知就是“滚动条没法在触屏上滑动”。
  592. // 两个措施:①把触点横向收进滚动条 rect,再交给滚动条自己判滑块;②若直接命中的不是
  593. // 滚动条,但触点容差范围内正好压在某个滚动条的滑块上,则改判为命中该滚动条。
  594. // ---------------------------------------------------------------------------
  595. namespace
  596. {
  597. const int kScrollBarTouchSlop = 12; // 手指离滚动条 rect 的最大容差(像素)
  598. // Windows 会把“触摸提升(promoted)出来的合成鼠标消息”打上 MOUSEEVENTF_FROMTOUCH 标记
  599. // (位于 GetMessageExtraInfo() 的高位,MSDN 推荐判定值 0xFF515700)。
  600. // 这里**只**用它来区分“真鼠标移动”与“触摸补发的鼠标移动”,从而决定要不要更新悬浮态;
  601. // 消息本身绝不丢弃——历史教训:整条吞掉会让点击状态机(m_pEventClick)失效、点击全灭。
  602. inline bool IsPromotedTouchMouseMessage()
  603. {
  604. return (::GetMessageExtraInfo() & 0xFFFFFF80) == 0xFF515700;
  605. }
  606. // 把触点收进滚动条自身 rect:垂直滚动条主要收敛 x(y 原样保留,纵向拖拽量完好)。
  607. inline POINT Touch_SnapIntoScrollBar(const CControlUI* pBar, POINT pt)
  608. {
  609. if (pBar == NULL) return pt;
  610. RECT rc = pBar->GetPos();
  611. if (pt.x < rc.left) pt.x = rc.left;
  612. if (pt.x > rc.right) pt.x = rc.right;
  613. if (pt.y < rc.top) pt.y = rc.top;
  614. if (pt.y > rc.bottom) pt.y = rc.bottom;
  615. return pt;
  616. }
  617. // 沿“命中控件 → 祖先链”找最近的、满足容差条件的滚动条滑块;找不到返回 NULL。
  618. inline CControlUI* Touch_ResolveScrollBarThumb(CControlUI* pStart, POINT pt)
  619. {
  620. for (CControlUI* p = pStart; p != NULL; p = p->GetParent())
  621. {
  622. if (p->GetInterface(DUI_CTR_SCROLLBAR) != NULL) return NULL; // 本身已是滚动条,无需改判
  623. CContainerUI* pCont = static_cast<CContainerUI*>(p->GetInterface(DUI_CTR_CONTAINER));
  624. if (pCont == NULL) continue;
  625. CScrollBarUI* pBars[2] = { pCont->GetVerticalScrollBar(), pCont->GetHorizontalScrollBar() };
  626. for (int i = 0; i < 2; ++i)
  627. {
  628. if (pBars[i] != NULL && pBars[i]->IsThumbHit(pt, kScrollBarTouchSlop))
  629. return pBars[i];
  630. }
  631. }
  632. return NULL;
  633. }
  634. inline CScrollBarUI* Touch_FindScrollBar(CControlUI* pStart, POINT pt)
  635. {
  636. for (CControlUI* p = pStart; p != NULL; p = p->GetParent())
  637. {
  638. CContainerUI* pContainer = static_cast<CContainerUI*>(p->GetInterface(DUI_CTR_CONTAINER));
  639. if (pContainer == NULL) continue;
  640. CScrollBarUI* bars[2] = {
  641. pContainer->GetVerticalScrollBar(),
  642. pContainer->GetHorizontalScrollBar()
  643. };
  644. for (int i = 0; i < 2; ++i)
  645. {
  646. if (bars[i] == NULL || !bars[i]->IsVisible() || !bars[i]->IsEnabled())
  647. continue;
  648. RECT rc = bars[i]->GetPos();
  649. ::InflateRect(&rc, kScrollBarTouchSlop, kScrollBarTouchSlop);
  650. if (::PtInRect(&rc, pt)) return bars[i];
  651. }
  652. }
  653. return NULL;
  654. }
  655. inline CContainerUI* Touch_FindScrollContainer(CControlUI* pStart)
  656. {
  657. for (CControlUI* p = pStart; p != NULL; p = p->GetParent())
  658. {
  659. CContainerUI* pContainer = static_cast<CContainerUI*>(p->GetInterface(DUI_CTR_CONTAINER));
  660. if (pContainer == NULL) continue;
  661. CScrollBarUI* pVertical = pContainer->GetVerticalScrollBar();
  662. CScrollBarUI* pHorizontal = pContainer->GetHorizontalScrollBar();
  663. if ((pVertical != NULL && pVertical->IsVisible() && pVertical->IsEnabled())
  664. || (pHorizontal != NULL && pHorizontal->IsVisible() && pHorizontal->IsEnabled()))
  665. return pContainer;
  666. }
  667. return NULL;
  668. }
  669. }
  670. // ---------------------------------------------------------------------------
  671. // 当前模态弹框句柄(由 CWindowWnd::ShowModal 进入/退出时设置)。
  672. // Win8/10 指针栈下,触摸消息(WM_TOUCH / WM_POINTER)并不总是投递给"视觉上在最前面的弹框":
  673. // 当弹框的 owner 被 EnableWindow(FALSE) 禁用时,部分系统会把手势转投给 owner。
  674. // 这里做一道保险:只要当前有模态弹框,任何【非该弹框】的窗口收到 WM_TOUCH 一律忽略,
  675. // 避免"弹框还开着,手指却把主窗口点动了"的穿透。
  676. // ---------------------------------------------------------------------------
  677. static HWND s_hModalWindow = NULL;
  678. void CPaintManagerUI::SetModalWindow(HWND hWnd)
  679. {
  680. s_hModalWindow = hWnd;
  681. TouchTrace::Log(_T("MODAL-WINDOW set hwnd=%p"), (void*)hWnd);
  682. }
  683. HWND CPaintManagerUI::GetModalWindow()
  684. {
  685. return s_hModalWindow;
  686. }
  687. void CPaintManagerUI::InitTouchSupport()
  688. {
  689. if (m_hWndPaint == NULL || !::IsWindow(m_hWndPaint)) return;
  690. TouchApi::PFN_RegisterTouchWindow pfnRegister = TouchApi::GetRegisterTouchWindow();
  691. BOOL bRegistered = FALSE;
  692. if (pfnRegister != NULL)
  693. bRegistered = pfnRegister(m_hWndPaint, 0);
  694. TouchTrace::Log(_T("REGISTER hwnd=%p manager=%p result=%d error=%u style=0x%08X ex=0x%08X"),
  695. (void*)m_hWndPaint, (void*)this, (int)bRegistered, bRegistered ? 0u : ::GetLastError(),
  696. (unsigned)::GetWindowLong(m_hWndPaint, GWL_STYLE),
  697. (unsigned)::GetWindowLong(m_hWndPaint, GWL_EXSTYLE));
  698. }
  699. void CPaintManagerUI::OnTouchInput(WPARAM wParam, LPARAM lParam)
  700. {
  701. // 触屏模型(参考 NIM Duilib 的 Window::HandleMessage case WM_TOUCH,但**不可**照搬它的鼠标守卫,
  702. // 旧版 DuiLib 的点击状态机强依赖原生 WM_LBUTTONDOWN/UP,照搬会让点击全灭——已实测踩过):
  703. // 1) 独立的 m_pEventTouch 追踪“本次触摸命中控件”,DOWN 设定、UP 清空;绝不复用 m_pEventClick。
  704. // 2) 只向控件派发“原生触摸事件” UIEVENT_TOUCHDOWN / UIEVENT_TOUCHMOVE / UIEVENT_TOUCHUP:
  705. // · CContainerUI 认 TOUCHMOVE → 内容 1:1 跟手平移(对标 NIM ScrollableBox);
  706. // · CScrollBarUI 认 TOUCHDOWN/MOVE/UP → 拖滑块 / 点轨道翻页(不依赖合成鼠标移动);
  707. // · 其余控件忽略触摸事件(未认领的会沿 DoEvent 冒泡给父容器)。
  708. // 3) 按钮“点击”不在这里合成!它来自 Windows 为触摸自动提升(promote)的 WM_LBUTTONDOWN/UP,
  709. // 走 MessageHandler 里原生的鼠标点击路径(CButtonUI 等据此 Activate())。
  710. // 4) 不做任何“触摸→鼠标”归一化、不做 tap-slop 互斥、不做通用 m_pEventClick 守卫;
  711. // “轻点 vs 拖拽”只用 m_bTouchMoved 在 WM_LBUTTONUP 处抑制误点。
  712. // 5) 触摸**不参与悬浮态**:WM_MOUSEMOVE 里用 MOUSEEVENTF_FROMTOUCH 标记把“提升出来的鼠标消息”
  713. // 挡在悬浮 enter/leave 之外(触摸没有“光标停留”语义)——这是“点过的控件一直高亮 /
  714. // 多个控件同时高亮”的根治手段;此处再叠加 m_bSuppressHoverAfterTouch 抑制抬起后的复活。
  715. TouchApi::PFN_GetTouchInputInfo pfnGetInfo = TouchApi::GetGetTouchInputInfo();
  716. // 有模态弹框、但本次 WM_TOUCH 却投到了【别的】窗口(典型:投给了被弹框挡住的 owner,
  717. // 尤其是 owner 曾被 EnableWindow(FALSE) 禁用过的情况):
  718. // 直接同步转发给真正的模态弹框,让弹框照常完成点击 / 滑动。
  719. // 注:MSDN 明确 WM_TOUCH 的 lParam 句柄在经 SendMessage 转发后即失效,
  720. // 所以转发后本窗口【不能】再 CloseTouchInputHandle,也不能再解析,直接返回。
  721. if (s_hModalWindow != NULL && ::IsWindow(s_hModalWindow) && s_hModalWindow != m_hWndPaint)
  722. {
  723. TouchTrace::Log(_T("WM_TOUCH forward to modal from=%p modal=%p inputs=%u"),
  724. (void*)m_hWndPaint, (void*)s_hModalWindow, (UINT)LOWORD(wParam));
  725. ::SendMessage(s_hModalWindow, WM_TOUCH, wParam, lParam);
  726. return;
  727. }
  728. if (pfnGetInfo == NULL) return;
  729. UINT cInputs = LOWORD(wParam);
  730. TouchTrace::Log(_T("WM_TOUCH begin inputs=%u handle=%p"), cInputs, (HANDLE)lParam);
  731. if (cInputs == 0)
  732. {
  733. TouchTrace::Log(_T("WM_TOUCH ignored: inputs=0"));
  734. return;
  735. }
  736. std::vector<TouchApi::TOUCHINPUT> inputs(cInputs);
  737. if (!pfnGetInfo((HANDLE)lParam, cInputs, inputs.data(), (int)sizeof(TouchApi::TOUCHINPUT)))
  738. {
  739. TouchTrace::Log(_T("WM_TOUCH GetTouchInputInfo failed error=%u"), ::GetLastError());
  740. return;
  741. }
  742. for (UINT i = 0; i < cInputs; ++i)
  743. {
  744. TouchApi::TOUCHINPUT& ti = inputs[i];
  745. TouchTrace::Log(_T("WM_TOUCH input[%u] id=%u flags=0x%08X screen=%d,%d"),
  746. i, ti.dwID, ti.dwFlags, TOUCH_COORD_TO_PIXEL(ti.x), TOUCH_COORD_TO_PIXEL(ti.y));
  747. POINT pt;
  748. pt.x = TOUCH_COORD_TO_PIXEL(ti.x);
  749. pt.y = TOUCH_COORD_TO_PIXEL(ti.y);
  750. ::ScreenToClient(m_hWndPaint, &pt);
  751. if (ti.dwFlags & TouchApi::TOUCHEVTFLAG_DOWN)
  752. {
  753. TouchTrace::Log(_T("TOUCHDOWN begin id=%u client=%d,%d"), ti.dwID, pt.x, pt.y);
  754. // 这里【不再】用 `if (m_pEventClick != NULL) break;` 做通用守卫:Windows 会在 WM_TOUCH
  755. // 前后补发合成的 WM_LBUTTONDOWN(同样设置 m_pEventClick),通用守卫会让 m_pEventTouch
  756. // 永不设置 → 容器收不到 TOUCHMOVE,列表滚不动。
  757. //
  758. // 滚动条也不再“交回原生鼠标”:实测部分触摸收银机在手指拖拽期间【不补发合成的
  759. // WM_MOUSEMOVE】,导致滑块拖不动。现在改为由 WM_TOUCH 直接驱动——CScrollBarUI 已把
  760. // TOUCHDOWN/MOVE/UP 与鼠标事件等价处理,这里让滚动条走常规接管流程,
  761. // 后续 TOUCHMOVE 由它消费(不会冒泡到容器把内容滚走)。
  762. ::SetFocus(m_hWndPaint);
  763. m_ptLastTouchPos = pt;
  764. // 触摸没有“悬浮”语义。新手势开始前先清掉上一次鼠标路径遗留的 HOT 状态,
  765. // 防止首个触摸 DOWN 恰好发生在合成 WM_MOUSEMOVE 之前。
  766. ClearTouchHoverState(pt);
  767. // 先把手势状态复位:即使这一按没命中任何控件,也不能让上一轮的 m_pEventTouch 残留
  768. // 下去(否则后续 TOUCHMOVE 会发给一个早已松开的手指所对应的控件)。
  769. m_pEventTouch = NULL;
  770. m_pTouchScrollContainer = NULL;
  771. m_bTouchMoved = false;
  772. m_ptTouchStart = pt;
  773. CControlUI* pControl = FindControl(pt);
  774. if (pControl == NULL)
  775. {
  776. TouchTrace::Log(_T("TOUCHDOWN no control client=%d,%d"), pt.x, pt.y);
  777. break;
  778. }
  779. if (pControl->GetManager() != this)
  780. {
  781. TouchTrace::Log(_T("TOUCHDOWN foreign control=%s manager=%p self=%p"),
  782. pControl->GetName().GetData(), (void*)pControl->GetManager(), (void*)this);
  783. break;
  784. }
  785. // [触屏] 手指容差改判:直接命中的不是滚动条时,看看是不是“手指离滚动条滑块很近”。
  786. // 典型场景:列表右侧竖直滚动条仅 16px 宽,用户想拖滑块却按到了旁边的列表项上。
  787. if (pControl->GetInterface(DUI_CTR_SCROLLBAR) == NULL)
  788. {
  789. CControlUI* pBar = Touch_FindScrollBar(pControl, pt);
  790. if (pBar == NULL)
  791. pBar = Touch_ResolveScrollBarThumb(pControl, pt);
  792. if (pBar != NULL)
  793. {
  794. TouchTrace::Log(_T("TOUCHDOWN-FATFINGER hit=%s -> scrollbar=%s pt=%d,%d"),
  795. pControl->GetName().GetData(), pBar->GetName().GetData(), pt.x, pt.y);
  796. pControl = pBar;
  797. }
  798. }
  799. bool bTouchIsScrollBar = (pControl->GetInterface(DUI_CTR_SCROLLBAR) != NULL);
  800. TouchTrace::Log(_T("TOUCHDOWN hit name=%s class=%s scrollbar=%d scrollContainer=%p"),
  801. pControl->GetName().GetData(), pControl->GetClass(), (int)bTouchIsScrollBar,
  802. (void*)Touch_FindScrollContainer(pControl));
  803. // 命中滚动条时把触点收进滚动条 rect(横向按偏也能拖滑块);事件点用收敛后的,
  804. // 但 m_ptLastTouchPos / m_ptTouchStart 仍记真实坐标(去重与位移判定按真实手指轨迹走)。
  805. POINT ptEvent = bTouchIsScrollBar ? Touch_SnapIntoScrollBar(pControl, pt) : pt;
  806. if (bTouchIsScrollBar)
  807. {
  808. TouchTrace::Log(_T("TOUCHDOWN-SCROLLBAR touch-driven hit=%s pt=%d,%d -> %d,%d"),
  809. pControl->GetName().GetData(), pt.x, pt.y, ptEvent.x, ptEvent.y);
  810. }
  811. m_pEventTouch = pControl;
  812. m_pTouchScrollContainer = bTouchIsScrollBar ? NULL : Touch_FindScrollContainer(pControl);
  813. if (!bTouchIsScrollBar)
  814. m_pEventClick = pControl;
  815. m_ptTouchStart = pt;
  816. // 触摸没有悬浮语义;整个手势期间以及抬起后的合成鼠标移动都不应
  817. // 重建 HOT。真实鼠标移动时在 WM_MOUSEMOVE 中解除该抑制。
  818. m_bSuppressHoverAfterTouch = true;
  819. pControl->SetFocus();
  820. SetCapture(); // mgr 级:m_bMouseCapture=true + ::SetCapture
  821. TouchTrace::Log(_T("TOUCHDOWN hit=%s cls=%s pt=%d,%d click=%p capture=%d"),
  822. pControl->GetName().GetData(), pControl->GetClass(), pt.x, pt.y,
  823. (void*)m_pEventClick, (int)m_bMouseCapture);
  824. TEventUI event = { 0 };
  825. event.Type = UIEVENT_TOUCHDOWN;
  826. event.pSender = pControl;
  827. event.ptMouse = ptEvent;
  828. event.dwTimestamp = ::GetTickCount();
  829. event.wKeyState = MapKeyState();
  830. pControl->Event(event);
  831. // 触摸按下只建立候选点击,不立即发送 BUTTONDOWN。
  832. // 用户可能正在从商品项或空白区域开始拖动滚动容器;若此时先发送
  833. // BUTTONDOWN,控件会先进入按下态,随后很容易被提升的鼠标消息激活。
  834. // 轻点的 BUTTONDOWN/BUTTONUP 在 TOUCHUP 且未越过阈值时成对发送。
  835. }
  836. else if (ti.dwFlags & TouchApi::TOUCHEVTFLAG_MOVE)
  837. {
  838. TouchTrace::Log(_T("TOUCHMOVE begin id=%u client=%d,%d active=%s scrollContainer=%p"),
  839. ti.dwID, pt.x, pt.y,
  840. m_pEventTouch != NULL ? m_pEventTouch->GetName().GetData() : _T("null"),
  841. (void*)m_pTouchScrollContainer);
  842. // 【不要】在这里对滚动条让路。滚动条现在完全由 WM_TOUCH 驱动:DOWN 已把 m_pEventTouch
  843. // 设为"滚动条自身"(不再交回原生鼠标),CScrollBarUI 也已把 TOUCHDOWN/MOVE/UP 与鼠标
  844. // 事件等价处理。若此处仍 `m_pEventClick->GetInterface("ScrollBar") != NULL` 就 break,
  845. // TOUCHMOVE 永远到不了滚动条 → 滑块拖不动(历史症状)。滚动条收到 TOUCHMOVE 会自行消费
  846. // (CScrollBarUI::DoEvent 命中已捕获分支后 return true),不会冒泡到容器把内容滚走。
  847. // 与上一包完全相同的点 -> 去重,避免无谓派发(NIM 同款守卫)
  848. if (m_ptLastTouchPos.x == pt.x && m_ptLastTouchPos.y == pt.y) break;
  849. // 本次位移增量(相对上一包),随事件下发,滚动容器据此做 1:1 跟手平移
  850. LONG dx = pt.x - m_ptLastTouchPos.x;
  851. LONG dy = pt.y - m_ptLastTouchPos.y;
  852. m_ptLastTouchPos = pt;
  853. // 相对手势起点的累计位移超过阈值 -> 判定为拖拽(用于松开时抑制点击)。
  854. LONG ox = pt.x - m_ptTouchStart.x;
  855. LONG oy = pt.y - m_ptTouchStart.y;
  856. // 阈值取 8px:真实滚动位移远大于此,而指尖“按下去时的轻微滚动”通常在 8px 内。
  857. // 阈值过小会把抖动误判成拖拽 → 该次抬起的 BUTTONUP 被抑制 → 按钮停在按下态。
  858. if (ox > 8 || ox < -8 || oy > 8 || oy < -8)
  859. m_bTouchMoved = true;
  860. if (m_pEventTouch == NULL) break;
  861. TEventUI event = { 0 };
  862. event.Type = UIEVENT_TOUCHMOVE;
  863. CControlUI* pTouchTarget = m_pTouchScrollContainer != NULL
  864. ? static_cast<CControlUI*>(m_pTouchScrollContainer) : m_pEventTouch;
  865. event.pSender = pTouchTarget;
  866. // 滚动条:触点同样收进滚动条 rect(与 DOWN 保持一致,避免首帧跳变)。
  867. event.ptMouse = (m_pEventTouch->GetInterface(DUI_CTR_SCROLLBAR) != NULL)
  868. ? Touch_SnapIntoScrollBar(m_pEventTouch, pt) : pt;
  869. event.dwTimestamp = ::GetTickCount();
  870. event.wKeyState = MapKeyState();
  871. event.wParam = (WPARAM)(int)dx;
  872. event.lParam = (LPARAM)(int)dy;
  873. TouchTrace::Log(_T("TOUCHMOVE touch=%s cls=%s d=%d,%d moved=%d click=%s"),
  874. m_pEventTouch->GetName().GetData(), m_pEventTouch->GetClass(),
  875. (int)dx, (int)dy, (int)m_bTouchMoved,
  876. m_pEventClick != NULL ? m_pEventClick->GetName().GetData() : _T("null"));
  877. pTouchTarget->Event(event);
  878. }
  879. else if (ti.dwFlags & TouchApi::TOUCHEVTFLAG_UP)
  880. {
  881. TouchTrace::Log(_T("TOUCHUP begin id=%u client=%d,%d active=%s moved=%d"),
  882. ti.dwID, pt.x, pt.y,
  883. m_pEventTouch != NULL ? m_pEventTouch->GetName().GetData() : _T("null"),
  884. (int)m_bTouchMoved);
  885. m_ptLastTouchPos = pt;
  886. ClearTouchHoverState(pt);
  887. // 触屏抬起模拟“鼠标离开”:清除悬浮态。触摸永远不会生成 WM_MOUSELEAVE,
  888. // 若只清 m_pEventHover,m_aNeedMouseLeaveNeeded 数组里登记过的控件
  889. // (按钮/列表项/滚动条等曾收到 MOUSEENTER 并 AddMouseLeaveNeeded 的)会一直残留
  890. // 悬浮态,表现为“多个控件同时悬浮”。必须复刻 WM_MOUSEMOVE 里的离开广播:先遍历
  891. // 整个数组逐一发 MOUSELEAVE(控件自身在 MOUSELEAVE 里把自己从数组移除),再清 m_pEventHover。
  892. if (m_aNeedMouseLeaveNeeded.GetSize() > 0 || m_pEventHover != NULL)
  893. {
  894. TEventUI event = { 0 };
  895. event.Type = UIEVENT_MOUSELEAVE;
  896. event.ptMouse = pt;
  897. event.wKeyState = MapKeyState();
  898. event.dwTimestamp = ::GetTickCount();
  899. if (m_aNeedMouseLeaveNeeded.GetSize() > 0)
  900. {
  901. CStdPtrArray aNeedMouseLeaveNeeded(m_aNeedMouseLeaveNeeded.GetSize());
  902. aNeedMouseLeaveNeeded.Resize(m_aNeedMouseLeaveNeeded.GetSize());
  903. ::CopyMemory(aNeedMouseLeaveNeeded.GetData(), m_aNeedMouseLeaveNeeded.GetData(), m_aNeedMouseLeaveNeeded.GetSize() * sizeof(LPVOID));
  904. for (int i = 0; i < aNeedMouseLeaveNeeded.GetSize(); i++)
  905. {
  906. CControlUI* pHover = static_cast<CControlUI*>(aNeedMouseLeaveNeeded[i]);
  907. event.pSender = pHover;
  908. pHover->Event(event);
  909. }
  910. }
  911. if (m_pEventHover != NULL)
  912. {
  913. event.pSender = m_pEventHover;
  914. m_pEventHover->Event(event);
  915. m_pEventHover = NULL;
  916. }
  917. if (m_hwndTooltip != NULL) ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ToolTip);
  918. }
  919. // 关键:手指抬起后禁止“悬浮态复活”。
  920. // Windows 会为触摸补发合成的 WM_MOUSEMOVE;且真实 WM_MOUSELEAVE 在 m_bMouseTracking
  921. // 为真时会自己给自己重发 WM_MOUSEMOVE(见 WM_MOUSELEAVE 分支)→ 刚清掉的悬浮态立刻
  922. // 被重新点亮。这里置抑制位 + 主动撤下鼠标跟踪,直到用户真的移动鼠标才恢复。
  923. m_bSuppressHoverAfterTouch = true;
  924. m_bMouseTracking = false;
  925. // 本函数未接管该次触摸(如 DOWN 时 FindControl 落空)→ 沿旧路径交给原生鼠标,这里不释放。
  926. if (m_pEventTouch == NULL)
  927. {
  928. m_pTouchScrollContainer = NULL;
  929. m_bTouchMoved = false;
  930. break;
  931. }
  932. ReleaseCapture();
  933. TEventUI event = { 0 };
  934. event.Type = UIEVENT_TOUCHUP;
  935. event.pSender = m_pEventTouch;
  936. // 与 DOWN/MOVE 一致:滚动条用收敛点,保证 ptMouse 轨迹连续、位移计算不失真。
  937. event.ptMouse = (m_pEventTouch->GetInterface(DUI_CTR_SCROLLBAR) != NULL)
  938. ? Touch_SnapIntoScrollBar(m_pEventTouch, pt) : pt;
  939. event.dwTimestamp = ::GetTickCount();
  940. event.wKeyState = MapKeyState();
  941. TouchTrace::Log(_T("TOUCHUP touch=%s moved=%d hover=%p leaveArr=%d click=%p capture=%d"),
  942. m_pEventTouch->GetName().GetData(), (int)m_bTouchMoved, (void*)m_pEventHover,
  943. m_aNeedMouseLeaveNeeded.GetSize(), (void*)m_pEventClick, (int)m_bMouseCapture);
  944. m_pEventTouch->Event(event);
  945. bool bClickSynthesized = false;
  946. if (m_pEventClick != NULL && !m_bTouchMoved
  947. && m_pEventClick->GetInterface(DUI_CTR_SCROLLBAR) == NULL)
  948. {
  949. CControlUI* pClick = m_pEventClick;
  950. m_pEventClick = NULL;
  951. bClickSynthesized = true;
  952. TEventUI clickEvent = { 0 };
  953. clickEvent.Type = UIEVENT_BUTTONDOWN;
  954. clickEvent.pSender = pClick;
  955. clickEvent.ptMouse = pt;
  956. clickEvent.dwTimestamp = ::GetTickCount();
  957. clickEvent.wKeyState = MapKeyState();
  958. pClick->Event(clickEvent);
  959. clickEvent.Type = UIEVENT_BUTTONUP;
  960. pClick->Event(clickEvent);
  961. }
  962. else if (m_pEventClick != NULL)
  963. {
  964. // 拖动或滚动条触摸从未发送 BUTTONDOWN,直接丢弃候选点击。
  965. m_pEventClick = NULL;
  966. }
  967. m_pEventTouch = NULL;
  968. m_pTouchScrollContainer = NULL;
  969. // 只有在本次 TOUCHUP 真的合成了点击时才屏蔽随后提升的鼠标消息(避免重复触发)。
  970. // 若本次【没有】合成(拖动滚动 / 未命中控件 / 命中滚动条),必须放行提升的鼠标消息:
  971. // 这是 Win10 上弹框还能"点得动"的兜底;WM_LBUTTONUP 原生分支已有 bTouchDragged
  972. // 保护(ptMouse 置 (-1,-1)),拖拽结束不会误激活控件,所以放行是安全的。
  973. m_bSuppressPromotedTouchMouse = bClickSynthesized;
  974. TouchTrace::Log(_T("TOUCHUP end synthesized=%d suppress=%d"),
  975. (int)bClickSynthesized, (int)m_bSuppressPromotedTouchMouse);
  976. }
  977. }
  978. TouchApi::PFN_CloseTouchInputHandle pfnClose = TouchApi::GetCloseTouchInputHandle();
  979. if (pfnClose != NULL)
  980. pfnClose((HANDLE)lParam);
  981. }
  982. void CPaintManagerUI::ClearTouchHoverState(POINT pt)
  983. {
  984. TouchTrace::Log(_T("CLEAR-HOVER pt=%d,%d hover=%s leaveCount=%d"),
  985. pt.x, pt.y,
  986. m_pEventHover != NULL ? m_pEventHover->GetName().GetData() : _T("null"),
  987. m_aNeedMouseLeaveNeeded.GetSize());
  988. TEventUI event = { 0 };
  989. event.Type = UIEVENT_MOUSELEAVE;
  990. event.ptMouse = pt;
  991. event.wKeyState = MapKeyState();
  992. event.dwTimestamp = ::GetTickCount();
  993. CStdPtrArray controls(m_aNeedMouseLeaveNeeded.GetSize());
  994. controls.Resize(m_aNeedMouseLeaveNeeded.GetSize());
  995. ::CopyMemory(controls.GetData(), m_aNeedMouseLeaveNeeded.GetData(),
  996. m_aNeedMouseLeaveNeeded.GetSize() * sizeof(LPVOID));
  997. for (int i = 0; i < controls.GetSize(); ++i)
  998. {
  999. CControlUI* pControl = static_cast<CControlUI*>(controls[i]);
  1000. if (pControl != NULL)
  1001. {
  1002. event.pSender = pControl;
  1003. pControl->Event(event);
  1004. }
  1005. }
  1006. if (m_pEventHover != NULL)
  1007. {
  1008. event.pSender = m_pEventHover;
  1009. m_pEventHover->Event(event);
  1010. m_pEventHover = NULL;
  1011. }
  1012. m_aNeedMouseLeaveNeeded.Empty();
  1013. if (m_hwndTooltip != NULL)
  1014. ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ToolTip);
  1015. }
  1016. void CPaintManagerUI::DeletePtr(void* ptr)
  1017. {
  1018. if (ptr)
  1019. {
  1020. delete ptr; ptr = NULL;
  1021. }
  1022. }
  1023. HINSTANCE CPaintManagerUI::GetInstance()
  1024. {
  1025. return m_hInstance;
  1026. }
  1027. CDuiString CPaintManagerUI::GetInstancePath()
  1028. {
  1029. if (m_hInstance == NULL) return _T('\0');
  1030. TCHAR tszModule[MAX_PATH + 1] = { 0 };
  1031. ::GetModuleFileName(m_hInstance, tszModule, MAX_PATH);
  1032. CDuiString sInstancePath = tszModule;
  1033. int pos = sInstancePath.ReverseFind(_T('\\'));
  1034. if (pos >= 0) sInstancePath = sInstancePath.Left(pos + 1);
  1035. return sInstancePath;
  1036. }
  1037. CDuiString CPaintManagerUI::GetCurrentPath()
  1038. {
  1039. TCHAR tszModule[MAX_PATH + 1] = { 0 };
  1040. ::GetCurrentDirectory(MAX_PATH, tszModule);
  1041. return tszModule;
  1042. }
  1043. HINSTANCE CPaintManagerUI::GetResourceDll()
  1044. {
  1045. if (m_hResourceInstance == NULL) return m_hInstance;
  1046. return m_hResourceInstance;
  1047. }
  1048. const CDuiString& CPaintManagerUI::GetResourcePath()
  1049. {
  1050. return m_pStrResourcePath;
  1051. }
  1052. const CDuiString& CPaintManagerUI::GetResourceZip()
  1053. {
  1054. return m_pStrResourceZip;
  1055. }
  1056. const CDuiString& CPaintManagerUI::GetResourceZipPwd()
  1057. {
  1058. return m_pStrResourceZipPwd;
  1059. }
  1060. bool CPaintManagerUI::IsCachedResourceZip()
  1061. {
  1062. return m_bCachedResourceZip;
  1063. }
  1064. HANDLE CPaintManagerUI::GetResourceZipHandle()
  1065. {
  1066. return m_hResourceZip;
  1067. }
  1068. void CPaintManagerUI::SetInstance(HINSTANCE hInst)
  1069. {
  1070. m_hInstance = hInst;
  1071. }
  1072. void CPaintManagerUI::SetCurrentPath(LPCTSTR pStrPath)
  1073. {
  1074. ::SetCurrentDirectory(pStrPath);
  1075. }
  1076. void CPaintManagerUI::SetResourceDll(HINSTANCE hInst)
  1077. {
  1078. m_hResourceInstance = hInst;
  1079. }
  1080. void CPaintManagerUI::SetResourcePath(LPCTSTR pStrPath)
  1081. {
  1082. m_pStrResourcePath = pStrPath;
  1083. if (m_pStrResourcePath.IsEmpty()) return;
  1084. TCHAR cEnd = m_pStrResourcePath.GetAt(m_pStrResourcePath.GetLength() - 1);
  1085. if (cEnd != _T('\\') && cEnd != _T('/')) m_pStrResourcePath += _T('\\');
  1086. }
  1087. void CPaintManagerUI::SetResourceZip(LPVOID pVoid, unsigned int len, LPCTSTR password)
  1088. {
  1089. if (m_pStrResourceZip == _T("membuffer")) return;
  1090. if (m_bCachedResourceZip && m_hResourceZip != NULL)
  1091. {
  1092. CloseZip((HZIP)m_hResourceZip);
  1093. m_hResourceZip = NULL;
  1094. }
  1095. m_pStrResourceZip = _T("membuffer");
  1096. if (m_cbZipBuf)
  1097. {
  1098. delete[] m_cbZipBuf;
  1099. m_cbZipBuf = nullptr;
  1100. }
  1101. if (!m_cbZipBuf)
  1102. {
  1103. m_cbZipBuf = new BYTE[len];
  1104. memcpy(m_cbZipBuf, pVoid, len);
  1105. }
  1106. m_bCachedResourceZip = true;
  1107. m_pStrResourceZipPwd = password; //Garfield 20160325 带密码zip包解密
  1108. if (m_bCachedResourceZip)
  1109. {
  1110. #ifdef UNICODE
  1111. char* pwd = w2a((wchar_t*)password);
  1112. m_hResourceZip = (HANDLE)OpenZip(m_cbZipBuf, len, pwd);
  1113. if (pwd)
  1114. {
  1115. delete[] pwd;
  1116. pwd = NULL;
  1117. }
  1118. #else
  1119. m_hResourceZip = (HANDLE)OpenZip(m_cbZipBuf, len, password);
  1120. #endif
  1121. }
  1122. }
  1123. void CPaintManagerUI::SetResourceZip(LPCTSTR pStrPath, bool bCachedResourceZip, LPCTSTR password)
  1124. {
  1125. if (m_pStrResourceZip == pStrPath && m_bCachedResourceZip == bCachedResourceZip) return;
  1126. if (m_bCachedResourceZip && m_hResourceZip != NULL)
  1127. {
  1128. CloseZip((HZIP)m_hResourceZip);
  1129. m_hResourceZip = NULL;
  1130. }
  1131. m_pStrResourceZip = pStrPath;
  1132. m_bCachedResourceZip = bCachedResourceZip;
  1133. m_pStrResourceZipPwd = password;
  1134. if (m_bCachedResourceZip)
  1135. {
  1136. CDuiString sFile = CPaintManagerUI::GetResourcePath();
  1137. sFile += CPaintManagerUI::GetResourceZip();
  1138. #ifdef UNICODE
  1139. char* pwd = w2a((wchar_t*)password);
  1140. m_hResourceZip = (HANDLE)OpenZip(sFile.GetData(), pwd);
  1141. if (pwd)
  1142. {
  1143. delete[] pwd;
  1144. pwd = NULL;
  1145. }
  1146. #else
  1147. m_hResourceZip = (HANDLE)OpenZip(sFile.GetData(), password);
  1148. #endif
  1149. }
  1150. }
  1151. void CPaintManagerUI::SetResourceType(int nType)
  1152. {
  1153. m_nResType = nType;
  1154. }
  1155. int CPaintManagerUI::GetResourceType()
  1156. {
  1157. return m_nResType;
  1158. }
  1159. bool CPaintManagerUI::GetHSL(short* H, short* S, short* L)
  1160. {
  1161. *H = m_H;
  1162. *S = m_S;
  1163. *L = m_L;
  1164. return m_bUseHSL;
  1165. }
  1166. void CPaintManagerUI::SetHSL(bool bUseHSL, short H, short S, short L)
  1167. {
  1168. if (m_bUseHSL || m_bUseHSL != bUseHSL)
  1169. {
  1170. m_bUseHSL = bUseHSL;
  1171. if (H == m_H && S == m_S && L == m_L) return;
  1172. m_H = CLAMP(H, 0, 360);
  1173. m_S = CLAMP(S, 0, 200);
  1174. m_L = CLAMP(L, 0, 200);
  1175. AdjustSharedImagesHSL();
  1176. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  1177. {
  1178. CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  1179. if (pManager != NULL) pManager->AdjustImagesHSL();
  1180. }
  1181. }
  1182. }
  1183. void CPaintManagerUI::ReloadSkin()
  1184. {
  1185. ReloadSharedImages();
  1186. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  1187. {
  1188. CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  1189. pManager->ReloadImages();
  1190. }
  1191. }
  1192. CPaintManagerUI* CPaintManagerUI::GetPaintManager(LPCTSTR pstrName)
  1193. {
  1194. if (pstrName == NULL) return NULL;
  1195. CDuiString sName = pstrName;
  1196. if (sName.IsEmpty()) return NULL;
  1197. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  1198. {
  1199. CPaintManagerUI* pManager = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  1200. if (pManager != NULL && sName == pManager->GetName()) return pManager;
  1201. }
  1202. return NULL;
  1203. }
  1204. CStdPtrArray* CPaintManagerUI::GetPaintManagers()
  1205. {
  1206. return &m_aPreMessages;
  1207. }
  1208. bool CPaintManagerUI::LoadPlugin(LPCTSTR pstrModuleName)
  1209. {
  1210. ASSERT(!::IsBadStringPtr(pstrModuleName, -1) || pstrModuleName == NULL);
  1211. if (pstrModuleName == NULL) return false;
  1212. HMODULE hModule = ::LoadLibrary(pstrModuleName);
  1213. if (hModule != NULL)
  1214. {
  1215. LPCREATECONTROL lpCreateControl = (LPCREATECONTROL)::GetProcAddress(hModule, "CreateControl");
  1216. if (lpCreateControl != NULL)
  1217. {
  1218. if (m_aPlugins.Find(lpCreateControl) >= 0) return true;
  1219. m_aPlugins.Add(lpCreateControl);
  1220. return true;
  1221. }
  1222. }
  1223. return false;
  1224. }
  1225. CStdPtrArray* CPaintManagerUI::GetPlugins()
  1226. {
  1227. return &m_aPlugins;
  1228. }
  1229. HWND CPaintManagerUI::GetPaintWindow() const
  1230. {
  1231. return m_hWndPaint;
  1232. }
  1233. HWND CPaintManagerUI::GetTooltipWindow() const
  1234. {
  1235. return m_hwndTooltip;
  1236. }
  1237. int CPaintManagerUI::GetHoverTime() const
  1238. {
  1239. return m_iHoverTime;
  1240. }
  1241. void CPaintManagerUI::SetHoverTime(int iTime)
  1242. {
  1243. m_iHoverTime = iTime;
  1244. }
  1245. LPCTSTR CPaintManagerUI::GetName() const
  1246. {
  1247. return m_sName;
  1248. }
  1249. HDC CPaintManagerUI::GetPaintDC() const
  1250. {
  1251. return m_hDcPaint;
  1252. }
  1253. POINT CPaintManagerUI::GetMousePos() const
  1254. {
  1255. return m_ptLastMousePos;
  1256. }
  1257. SIZE CPaintManagerUI::GetClientSize() const
  1258. {
  1259. RECT rcClient = { 0 };
  1260. ::GetClientRect(m_hWndPaint, &rcClient);
  1261. return CDuiSize(rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
  1262. }
  1263. SIZE CPaintManagerUI::GetInitSize()
  1264. {
  1265. return m_szInitWindowSize;
  1266. }
  1267. void CPaintManagerUI::SetInitSize(int cx, int cy)
  1268. {
  1269. m_szInitWindowSize.cx = cx;
  1270. m_szInitWindowSize.cy = cy;
  1271. if (m_pRoot == NULL && m_hWndPaint != NULL)
  1272. {
  1273. ::SetWindowPos(m_hWndPaint, NULL, 0, 0, cx, cy, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
  1274. }
  1275. }
  1276. RECT CPaintManagerUI::GetSizeBox()
  1277. {
  1278. return GetDPIObj()->Scale(m_rcSizeBox);
  1279. }
  1280. void CPaintManagerUI::SetSizeBox(RECT& rcSizeBox)
  1281. {
  1282. m_rcSizeBox = rcSizeBox;
  1283. }
  1284. RECT CPaintManagerUI::GetCaptionRect()
  1285. {
  1286. return GetDPIObj()->Scale(m_rcCaption);
  1287. }
  1288. void CPaintManagerUI::SetCaptionRect(RECT& rcCaption)
  1289. {
  1290. m_rcCaption = rcCaption;
  1291. }
  1292. SIZE CPaintManagerUI::GetRoundCorner()
  1293. {
  1294. return GetDPIObj()->Scale(m_szRoundCorner);
  1295. }
  1296. void CPaintManagerUI::SetRoundCorner(int cx, int cy)
  1297. {
  1298. m_szRoundCorner.cx = cx;
  1299. m_szRoundCorner.cy = cy;
  1300. }
  1301. SIZE CPaintManagerUI::GetMinInfo()
  1302. {
  1303. return GetDPIObj()->Scale(m_szMinWindow);
  1304. }
  1305. void CPaintManagerUI::SetMinInfo(int cx, int cy)
  1306. {
  1307. ASSERT(cx >= 0 && cy >= 0);
  1308. m_szMinWindow.cx = cx;
  1309. m_szMinWindow.cy = cy;
  1310. }
  1311. SIZE CPaintManagerUI::GetMaxInfo()
  1312. {
  1313. return GetDPIObj()->Scale(m_szMaxWindow);
  1314. }
  1315. void CPaintManagerUI::SetMaxInfo(int cx, int cy)
  1316. {
  1317. ASSERT(cx >= 0 && cy >= 0);
  1318. m_szMaxWindow.cx = cx;
  1319. m_szMaxWindow.cy = cy;
  1320. }
  1321. bool CPaintManagerUI::IsShowUpdateRect()
  1322. {
  1323. return m_bShowUpdateRect;
  1324. }
  1325. void CPaintManagerUI::SetShowUpdateRect(bool show)
  1326. {
  1327. m_bShowUpdateRect = show;
  1328. }
  1329. bool CPaintManagerUI::IsNoActivate()
  1330. {
  1331. return m_bNoActivate;
  1332. }
  1333. void CPaintManagerUI::SetNoActivate(bool bNoActivate)
  1334. {
  1335. m_bNoActivate = bNoActivate;
  1336. }
  1337. BYTE CPaintManagerUI::GetOpacity() const
  1338. {
  1339. return m_nOpacity;
  1340. }
  1341. void CPaintManagerUI::SetOpacity(BYTE nOpacity)
  1342. {
  1343. m_nOpacity = nOpacity;
  1344. if (m_hWndPaint != NULL)
  1345. {
  1346. typedef BOOL(__stdcall* PFUNCSETLAYEREDWINDOWATTR)(HWND, COLORREF, BYTE, DWORD);
  1347. PFUNCSETLAYEREDWINDOWATTR fSetLayeredWindowAttributes = NULL;
  1348. HMODULE hUser32 = ::GetModuleHandle(_T("User32.dll"));
  1349. if (hUser32)
  1350. {
  1351. fSetLayeredWindowAttributes =
  1352. (PFUNCSETLAYEREDWINDOWATTR)::GetProcAddress(hUser32, "SetLayeredWindowAttributes");
  1353. if (fSetLayeredWindowAttributes == NULL) return;
  1354. }
  1355. DWORD dwStyle = ::GetWindowLong(m_hWndPaint, GWL_EXSTYLE);
  1356. DWORD dwNewStyle = dwStyle;
  1357. if (nOpacity >= 0 && nOpacity < 256) dwNewStyle |= WS_EX_LAYERED;
  1358. else dwNewStyle &= ~WS_EX_LAYERED;
  1359. if (dwStyle != dwNewStyle) ::SetWindowLong(m_hWndPaint, GWL_EXSTYLE, dwNewStyle);
  1360. fSetLayeredWindowAttributes(m_hWndPaint, 0, nOpacity, LWA_ALPHA);
  1361. }
  1362. }
  1363. bool CPaintManagerUI::IsLayered()
  1364. {
  1365. return m_bLayered;
  1366. }
  1367. void CPaintManagerUI::SetLayered(bool bLayered)
  1368. {
  1369. if (m_hWndPaint != NULL && bLayered != m_bLayered)
  1370. {
  1371. UINT uStyle = GetWindowStyle(m_hWndPaint);
  1372. if ((uStyle & WS_CHILD) != 0) return;
  1373. if (g_fUpdateLayeredWindow == NULL)
  1374. {
  1375. HMODULE hUser32 = ::GetModuleHandle(_T("User32.dll"));
  1376. if (hUser32)
  1377. {
  1378. g_fUpdateLayeredWindow =
  1379. (PFUNCUPDATELAYEREDWINDOW)::GetProcAddress(hUser32, "UpdateLayeredWindow");
  1380. if (g_fUpdateLayeredWindow == NULL) return;
  1381. }
  1382. }
  1383. m_bLayered = bLayered;
  1384. if (m_pRoot != NULL) m_pRoot->NeedUpdate();
  1385. Invalidate();
  1386. }
  1387. }
  1388. RECT& CPaintManagerUI::GetLayeredInset()
  1389. {
  1390. return m_rcLayeredInset;
  1391. }
  1392. void CPaintManagerUI::SetLayeredInset(RECT& rcLayeredInset)
  1393. {
  1394. m_rcLayeredInset = rcLayeredInset;
  1395. m_bLayeredChanged = true;
  1396. Invalidate();
  1397. }
  1398. BYTE CPaintManagerUI::GetLayeredOpacity()
  1399. {
  1400. return m_nOpacity;
  1401. }
  1402. void CPaintManagerUI::SetLayeredOpacity(BYTE nOpacity)
  1403. {
  1404. m_nOpacity = nOpacity;
  1405. m_bLayeredChanged = true;
  1406. Invalidate();
  1407. }
  1408. LPCTSTR CPaintManagerUI::GetLayeredImage()
  1409. {
  1410. return m_diLayered.sDrawString;
  1411. }
  1412. void CPaintManagerUI::SetLayeredImage(LPCTSTR pstrImage)
  1413. {
  1414. m_diLayered.sDrawString = pstrImage;
  1415. RECT rcNull = { 0 };
  1416. CRenderEngine::DrawImageInfo(NULL, this, rcNull, rcNull, &m_diLayered);
  1417. m_bLayeredChanged = true;
  1418. Invalidate();
  1419. }
  1420. CShadowUI* CPaintManagerUI::GetShadow()
  1421. {
  1422. return &m_shadow;
  1423. }
  1424. void CPaintManagerUI::SetUseGdiplusText(bool bUse)
  1425. {
  1426. m_bUseGdiplusText = bUse;
  1427. }
  1428. bool CPaintManagerUI::IsUseGdiplusText() const
  1429. {
  1430. return m_bUseGdiplusText;
  1431. }
  1432. void CPaintManagerUI::SetGdiplusTextRenderingHint(int trh)
  1433. {
  1434. m_trh = trh;
  1435. }
  1436. int CPaintManagerUI::GetGdiplusTextRenderingHint() const
  1437. {
  1438. return m_trh;
  1439. }
  1440. bool CPaintManagerUI::PreMessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lRes)
  1441. {
  1442. for (int i = 0; i < m_aPreMessageFilters.GetSize(); i++)
  1443. {
  1444. bool bHandled = false;
  1445. LRESULT lResult = static_cast<IMessageFilterUI*>(m_aPreMessageFilters[i])->MessageHandler(uMsg, wParam, lParam, bHandled);
  1446. if (bHandled)
  1447. {
  1448. lRes = lResult;
  1449. return true;
  1450. }
  1451. }
  1452. switch (uMsg)
  1453. {
  1454. case WM_KEYDOWN:
  1455. {
  1456. // Tabbing between controls
  1457. if (wParam == VK_TAB)
  1458. {
  1459. if (m_pFocus && m_pFocus->IsVisible() && m_pFocus->IsEnabled() && _tcsstr(m_pFocus->GetClass(), _T("RichEditUI")) != NULL)
  1460. {
  1461. if (static_cast<CRichEditUI*>(m_pFocus)->IsWantTab()) return false;
  1462. }
  1463. if (m_pFocus && m_pFocus->IsVisible() && m_pFocus->IsEnabled() && _tcsstr(m_pFocus->GetClass(), _T("WkeWebkitUI")) != NULL)
  1464. {
  1465. return false;
  1466. }
  1467. SetNextTabControl(::GetKeyState(VK_SHIFT) >= 0);
  1468. return false;
  1469. }
  1470. }
  1471. break;
  1472. case WM_SYSCHAR:
  1473. {
  1474. // Handle ALT-shortcut key-combinations
  1475. FINDSHORTCUT fs = { 0 };
  1476. fs.ch = toupper((int)wParam);
  1477. CControlUI* pControl = m_pRoot->FindControl(__FindControlFromShortcut, &fs, UIFIND_ENABLED | UIFIND_ME_FIRST | UIFIND_TOP_FIRST);
  1478. if (pControl != NULL)
  1479. {
  1480. pControl->SetFocus();
  1481. pControl->Activate();
  1482. return true;
  1483. }
  1484. }
  1485. break;
  1486. case WM_SYSKEYDOWN:
  1487. {
  1488. if (m_pFocus != NULL)
  1489. {
  1490. TEventUI event = { 0 };
  1491. event.Type = UIEVENT_SYSKEY;
  1492. event.chKey = (TCHAR)wParam;
  1493. event.ptMouse = m_ptLastMousePos;
  1494. event.wKeyState = MapKeyState();
  1495. event.dwTimestamp = ::GetTickCount();
  1496. m_pFocus->Event(event);
  1497. }
  1498. }
  1499. break;
  1500. }
  1501. return false;
  1502. }
  1503. bool CPaintManagerUI::MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT& lRes)
  1504. {
  1505. if (m_hWndPaint == NULL) return false;
  1506. if (uMsg == WM_TOUCH || uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONUP)
  1507. {
  1508. TouchTrace::Log(_T("MANAGER-MSG hwnd=%p manager=%p msg=0x%04X w=0x%p l=0x%p touch=%p suppress=%d"),
  1509. (void*)m_hWndPaint, (void*)this, uMsg, (void*)wParam, (void*)lParam,
  1510. (void*)m_pEventTouch, (int)m_bSuppressPromotedTouchMouse);
  1511. }
  1512. // 触摸点击优先由 WM_TOUCH 状态机驱动 BUTTONDOWN/BUTTONUP。
  1513. // 只有当前 manager 已经收到本次 WM_TOUCH,或刚刚完成了触摸点击,
  1514. // 才拦截随后提升的鼠标消息。不能仅凭 FROMTOUCH 标记无条件拦截,
  1515. // 因为部分模态窗口只收到 Windows 提升的鼠标消息而没有收到 WM_TOUCH,
  1516. // 无条件拦截会导致模态窗口完全无法触摸点击。
  1517. if (uMsg == WM_LBUTTONDOWN || uMsg == WM_LBUTTONUP || uMsg == WM_LBUTTONDBLCLK)
  1518. {
  1519. if (m_pEventTouch != NULL || m_bSuppressPromotedTouchMouse)
  1520. {
  1521. if (uMsg == WM_LBUTTONUP || uMsg == WM_LBUTTONDBLCLK)
  1522. m_bSuppressPromotedTouchMouse = false;
  1523. return true;
  1524. }
  1525. }
  1526. // [触屏兼容] 不再过滤 Windows 为触摸模拟的鼠标消息(MOUSEEVENTF_FROMTOUCH)。
  1527. // 触摸的“点击”正来自这些模拟的 WM_LBUTTONDOWN/UP,由原生鼠标路径交给 CButtonUI 等控件
  1528. // Activate() 触发——而 DuiLib 的点击状态机依赖 WM_LBUTTONDOWN 设置 m_pEventClick,故绝不能
  1529. // 在鼠标分支吞掉这些模拟消息(之前那样做会导致触摸与鼠标点击全失效)。
  1530. // 为避免“拖拽=滚动”又“误点按钮”,改用 m_bTouchMoved:若本次手势发生位移,在 WM_LBUTTONUP 抑制点击。
  1531. // Cycle through listeners
  1532. for (int i = 0; i < m_aMessageFilters.GetSize(); i++)
  1533. {
  1534. bool bHandled = false;
  1535. LRESULT lResult = static_cast<IMessageFilterUI*>(m_aMessageFilters[i])->MessageHandler(uMsg, wParam, lParam, bHandled);
  1536. if (bHandled)
  1537. {
  1538. lRes = lResult;
  1539. switch (uMsg)
  1540. {
  1541. case WM_MOUSEMOVE:
  1542. case WM_LBUTTONDOWN:
  1543. case WM_LBUTTONDBLCLK:
  1544. case WM_LBUTTONUP:
  1545. {
  1546. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1547. m_ptLastMousePos = pt;
  1548. }
  1549. break;
  1550. case WM_CONTEXTMENU:
  1551. case WM_MOUSEWHEEL:
  1552. {
  1553. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  1554. ::ScreenToClient(m_hWndPaint, &pt);
  1555. m_ptLastMousePos = pt;
  1556. }
  1557. break;
  1558. }
  1559. return true;
  1560. }
  1561. }
  1562. if (m_bLayered)
  1563. {
  1564. switch (uMsg)
  1565. {
  1566. case WM_NCACTIVATE:
  1567. if (!::IsIconic(m_hWndPaint))
  1568. {
  1569. lRes = (wParam == 0) ? TRUE : FALSE;
  1570. return true;
  1571. }
  1572. break;
  1573. case WM_NCCALCSIZE:
  1574. case WM_NCPAINT:
  1575. lRes = 0;
  1576. return true;
  1577. }
  1578. }
  1579. // Custom handling of events
  1580. switch (uMsg)
  1581. {
  1582. case WM_APP + 1:
  1583. {
  1584. for (int i = 0; i < m_aDelayedCleanup.GetSize(); i++)
  1585. delete static_cast<CControlUI*>(m_aDelayedCleanup[i]);
  1586. m_aDelayedCleanup.Empty();
  1587. m_bAsyncNotifyPosted = false;
  1588. TNotifyUI* pMsg = NULL;
  1589. while (pMsg = static_cast<TNotifyUI*>(m_aAsyncNotify.GetAt(0)))
  1590. {
  1591. m_aAsyncNotify.Remove(0);
  1592. if (pMsg->pSender != NULL)
  1593. {
  1594. if (pMsg->pSender->OnNotify) pMsg->pSender->OnNotify(pMsg);
  1595. }
  1596. for (int j = 0; j < m_aNotifiers.GetSize(); j++)
  1597. {
  1598. static_cast<INotifyUI*>(m_aNotifiers[j])->Notify(*pMsg);
  1599. }
  1600. delete pMsg;
  1601. }
  1602. }
  1603. break;
  1604. case WM_CLOSE:
  1605. {
  1606. // Make sure all matching "closing" events are sent
  1607. TEventUI event = { 0 };
  1608. event.ptMouse = m_ptLastMousePos;
  1609. event.wKeyState = MapKeyState();
  1610. event.dwTimestamp = ::GetTickCount();
  1611. if (m_pEventHover != NULL)
  1612. {
  1613. event.Type = UIEVENT_MOUSELEAVE;
  1614. event.pSender = m_pEventHover;
  1615. m_pEventHover->Event(event);
  1616. }
  1617. if (m_pEventClick != NULL)
  1618. {
  1619. event.Type = UIEVENT_BUTTONUP;
  1620. event.pSender = m_pEventClick;
  1621. m_pEventClick->Event(event);
  1622. }
  1623. if (m_pEventRClick != NULL)
  1624. {
  1625. event.Type = UIEVENT_RBUTTONUP;
  1626. event.pSender = m_pEventClick;
  1627. m_pEventRClick->Event(event);
  1628. }
  1629. SetFocus(NULL);
  1630. if (::GetActiveWindow() == m_hWndPaint)
  1631. {
  1632. HWND hwndParent = GetWindowOwner(m_hWndPaint);
  1633. if (hwndParent != NULL) ::SetFocus(hwndParent);
  1634. }
  1635. if (m_hwndTooltip != NULL)
  1636. {
  1637. ::DestroyWindow(m_hwndTooltip);
  1638. m_hwndTooltip = NULL;
  1639. }
  1640. }
  1641. break;
  1642. case WM_ERASEBKGND:
  1643. {
  1644. // We'll do the painting here...
  1645. lRes = 1;
  1646. }
  1647. return true;
  1648. case WM_PAINT:
  1649. {
  1650. if (m_pRoot == NULL)
  1651. {
  1652. PAINTSTRUCT ps = { 0 };
  1653. ::BeginPaint(m_hWndPaint, &ps);
  1654. CRenderEngine::DrawColor(m_hDcPaint, ps.rcPaint, 0xFF000000);
  1655. ::EndPaint(m_hWndPaint, &ps);
  1656. return true;
  1657. }
  1658. RECT rcClient = { 0 };
  1659. ::GetClientRect(m_hWndPaint, &rcClient);
  1660. RECT rcPaint = { 0 };
  1661. if (!::GetUpdateRect(m_hWndPaint, &rcPaint, FALSE)) return true;
  1662. //if( m_bLayered ) {
  1663. // m_bOffscreenPaint = true;
  1664. // rcPaint = m_rcLayeredUpdate;
  1665. // if( ::IsRectEmpty(&m_rcLayeredUpdate) ) {
  1666. // PAINTSTRUCT ps = { 0 };
  1667. // ::BeginPaint(m_hWndPaint, &ps);
  1668. // ::EndPaint(m_hWndPaint, &ps);
  1669. // return true;
  1670. // }
  1671. // if( rcPaint.right > rcClient.right ) rcPaint.right = rcClient.right;
  1672. // if( rcPaint.bottom > rcClient.bottom ) rcPaint.bottom = rcClient.bottom;
  1673. // ::ZeroMemory(&m_rcLayeredUpdate, sizeof(m_rcLayeredUpdate));
  1674. //}
  1675. //else {
  1676. // if( !::GetUpdateRect(m_hWndPaint, &rcPaint, FALSE) ) return true;
  1677. //}
  1678. // Set focus to first control?
  1679. if (m_bFocusNeeded)
  1680. {
  1681. //修复新窗口自动切换焦点的问题
  1682. //SetNextTabControl();
  1683. m_bFocusNeeded = false;
  1684. }
  1685. SetPainting(true);
  1686. bool bNeedSizeMsg = false;
  1687. DWORD dwWidth = rcClient.right - rcClient.left;
  1688. DWORD dwHeight = rcClient.bottom - rcClient.top;
  1689. SetPainting(true);
  1690. if (m_bUpdateNeeded)
  1691. {
  1692. m_bUpdateNeeded = false;
  1693. if (!::IsRectEmpty(&rcClient) && !::IsIconic(m_hWndPaint))
  1694. {
  1695. if (m_pRoot->IsUpdateNeeded())
  1696. {
  1697. RECT rcRoot = rcClient;
  1698. if (m_hDcOffscreen != NULL) ::DeleteDC(m_hDcOffscreen);
  1699. if (m_hDcBackground != NULL) ::DeleteDC(m_hDcBackground);
  1700. if (m_hbmpOffscreen != NULL) ::DeleteObject(m_hbmpOffscreen);
  1701. if (m_hbmpBackground != NULL) ::DeleteObject(m_hbmpBackground);
  1702. m_hDcOffscreen = NULL;
  1703. m_hDcBackground = NULL;
  1704. m_hbmpOffscreen = NULL;
  1705. m_hbmpBackground = NULL;
  1706. if (m_bLayered)
  1707. {
  1708. rcRoot.left += m_rcLayeredInset.left;
  1709. rcRoot.top += m_rcLayeredInset.top;
  1710. rcRoot.right -= m_rcLayeredInset.right;
  1711. rcRoot.bottom -= m_rcLayeredInset.bottom;
  1712. }
  1713. m_pRoot->SetPos(rcRoot, true);
  1714. bNeedSizeMsg = true;
  1715. }
  1716. else
  1717. {
  1718. CControlUI* pControl = NULL;
  1719. m_aFoundControls.Empty();
  1720. m_pRoot->FindControl(__FindControlsFromUpdate, NULL, UIFIND_VISIBLE | UIFIND_ME_FIRST | UIFIND_UPDATETEST);
  1721. for (int it = 0; it < m_aFoundControls.GetSize(); it++)
  1722. {
  1723. pControl = static_cast<CControlUI*>(m_aFoundControls[it]);
  1724. pControl->SetPos(pControl->GetPos(), true);
  1725. }
  1726. bNeedSizeMsg = true;
  1727. }
  1728. // We'll want to notify the window when it is first initialized
  1729. // with the correct layout. The window form would take the time
  1730. // to submit swipes/animations.
  1731. if (m_bFirstLayout)
  1732. {
  1733. m_bFirstLayout = false;
  1734. SendNotify(m_pRoot, DUI_MSGTYPE_WINDOWINIT, 0, 0, false);
  1735. if (m_bLayered && m_bLayeredChanged)
  1736. {
  1737. Invalidate();
  1738. SetPainting(false);
  1739. return true;
  1740. }
  1741. // 更新阴影窗口显示
  1742. m_shadow.Update(m_hWndPaint);
  1743. }
  1744. }
  1745. }
  1746. else if (m_bLayered && m_bLayeredChanged)
  1747. {
  1748. RECT rcRoot = rcClient;
  1749. if (m_pOffscreenBits) ::ZeroMemory(m_pOffscreenBits, (rcRoot.right - rcRoot.left)
  1750. * (rcRoot.bottom - rcRoot.top) * 4);
  1751. rcRoot.left += m_rcLayeredInset.left;
  1752. rcRoot.top += m_rcLayeredInset.top;
  1753. rcRoot.right -= m_rcLayeredInset.right;
  1754. rcRoot.bottom -= m_rcLayeredInset.bottom;
  1755. m_pRoot->SetPos(rcRoot, true);
  1756. }
  1757. if (m_bLayered)
  1758. {
  1759. DWORD dwExStyle = ::GetWindowLong(m_hWndPaint, GWL_EXSTYLE);
  1760. DWORD dwNewExStyle = dwExStyle | WS_EX_LAYERED;
  1761. if (dwExStyle != dwNewExStyle) ::SetWindowLong(m_hWndPaint, GWL_EXSTYLE, dwNewExStyle);
  1762. m_bOffscreenPaint = true;
  1763. UnionRect(&rcPaint, &rcPaint, &m_rcLayeredUpdate);
  1764. if (rcPaint.right > rcClient.right) rcPaint.right = rcClient.right;
  1765. if (rcPaint.bottom > rcClient.bottom) rcPaint.bottom = rcClient.bottom;
  1766. ::ZeroMemory(&m_rcLayeredUpdate, sizeof(m_rcLayeredUpdate));
  1767. }
  1768. //
  1769. // Render screen
  1770. //
  1771. // Prepare offscreen bitmap
  1772. if (m_bOffscreenPaint && m_hbmpOffscreen == NULL)
  1773. {
  1774. m_hDcOffscreen = ::CreateCompatibleDC(m_hDcPaint);
  1775. m_hbmpOffscreen = CRenderEngine::CreateARGB32Bitmap(m_hDcPaint, dwWidth, dwHeight, (LPBYTE*)&m_pOffscreenBits);
  1776. ASSERT(m_hDcOffscreen);
  1777. ASSERT(m_hbmpOffscreen);
  1778. }
  1779. // Begin Windows paint
  1780. PAINTSTRUCT ps = { 0 };
  1781. ::BeginPaint(m_hWndPaint, &ps);
  1782. if (m_bOffscreenPaint)
  1783. {
  1784. HBITMAP hOldBitmap = (HBITMAP) ::SelectObject(m_hDcOffscreen, m_hbmpOffscreen);
  1785. int iSaveDC = ::SaveDC(m_hDcOffscreen);
  1786. if (m_bLayered)
  1787. {
  1788. for (LONG y = rcClient.bottom - rcPaint.bottom; y < rcClient.bottom - rcPaint.top; ++y)
  1789. {
  1790. for (LONG x = rcPaint.left; x < rcPaint.right; ++x)
  1791. {
  1792. int i = (y * dwWidth + x) * 4;
  1793. *(DWORD*)(&m_pOffscreenBits[i]) = 0;
  1794. }
  1795. }
  1796. }
  1797. m_pRoot->Paint(m_hDcOffscreen, rcPaint, NULL);
  1798. if (m_bLayered)
  1799. {
  1800. for (int i = 0; i < m_aNativeWindow.GetSize(); )
  1801. {
  1802. HWND hChildWnd = static_cast<HWND>(m_aNativeWindow[i]);
  1803. if (!::IsWindow(hChildWnd))
  1804. {
  1805. m_aNativeWindow.Remove(i);
  1806. m_aNativeWindowControl.Remove(i);
  1807. continue;
  1808. }
  1809. ++i;
  1810. if (!::IsWindowVisible(hChildWnd)) continue;
  1811. RECT rcChildWnd = GetNativeWindowRect(hChildWnd);
  1812. RECT rcTemp = { 0 };
  1813. if (!::IntersectRect(&rcTemp, &rcPaint, &rcChildWnd)) continue;
  1814. COLORREF* pChildBitmapBits = NULL;
  1815. HDC hChildMemDC = ::CreateCompatibleDC(m_hDcOffscreen);
  1816. HBITMAP hChildBitmap = CRenderEngine::CreateARGB32Bitmap(hChildMemDC, rcChildWnd.right - rcChildWnd.left, rcChildWnd.bottom - rcChildWnd.top, (BYTE**)&pChildBitmapBits);
  1817. ::ZeroMemory(pChildBitmapBits, (rcChildWnd.right - rcChildWnd.left) * (rcChildWnd.bottom - rcChildWnd.top) * 4);
  1818. HBITMAP hOldChildBitmap = (HBITMAP) ::SelectObject(hChildMemDC, hChildBitmap);
  1819. ::SendMessage(hChildWnd, WM_PRINT, (WPARAM)hChildMemDC, (LPARAM)(PRF_CHECKVISIBLE | PRF_CHILDREN | PRF_CLIENT | PRF_OWNED));
  1820. COLORREF* pChildBitmapBit;
  1821. for (LONG y = 0; y < rcChildWnd.bottom - rcChildWnd.top; y++)
  1822. {
  1823. for (LONG x = 0; x < rcChildWnd.right - rcChildWnd.left; x++)
  1824. {
  1825. pChildBitmapBit = pChildBitmapBits + y * (rcChildWnd.right - rcChildWnd.left) + x;
  1826. if (*pChildBitmapBit != 0x00000000) *pChildBitmapBit |= 0xff000000;
  1827. }
  1828. }
  1829. ::BitBlt(m_hDcOffscreen, rcChildWnd.left, rcChildWnd.top, rcChildWnd.right - rcChildWnd.left,
  1830. rcChildWnd.bottom - rcChildWnd.top, hChildMemDC, 0, 0, SRCCOPY);
  1831. ::SelectObject(hChildMemDC, hOldChildBitmap);
  1832. ::DeleteObject(hChildBitmap);
  1833. ::DeleteDC(hChildMemDC);
  1834. }
  1835. }
  1836. for (int i = 0; i < m_aPostPaintControls.GetSize(); i++)
  1837. {
  1838. CControlUI* pPostPaintControl = static_cast<CControlUI*>(m_aPostPaintControls[i]);
  1839. pPostPaintControl->DoPostPaint(m_hDcOffscreen, rcPaint);
  1840. }
  1841. ::RestoreDC(m_hDcOffscreen, iSaveDC);
  1842. if (m_bLayered)
  1843. {
  1844. RECT rcWnd = { 0 };
  1845. ::GetWindowRect(m_hWndPaint, &rcWnd);
  1846. if (!m_diLayered.sDrawString.IsEmpty())
  1847. {
  1848. DWORD dwWidth = rcClient.right - rcClient.left;
  1849. DWORD dwHeight = rcClient.bottom - rcClient.top;
  1850. RECT rcLayeredClient = rcClient;
  1851. rcLayeredClient.left += m_rcLayeredInset.left;
  1852. rcLayeredClient.top += m_rcLayeredInset.top;
  1853. rcLayeredClient.right -= m_rcLayeredInset.right;
  1854. rcLayeredClient.bottom -= m_rcLayeredInset.bottom;
  1855. COLORREF* pOffscreenBits = (COLORREF*)m_pOffscreenBits;
  1856. COLORREF* pBackgroundBits = m_pBackgroundBits;
  1857. BYTE A = 0;
  1858. BYTE R = 0;
  1859. BYTE G = 0;
  1860. BYTE B = 0;
  1861. if (!m_diLayered.sDrawString.IsEmpty())
  1862. {
  1863. if (m_hbmpBackground == NULL)
  1864. {
  1865. m_hDcBackground = ::CreateCompatibleDC(m_hDcPaint);
  1866. m_hbmpBackground = CRenderEngine::CreateARGB32Bitmap(m_hDcPaint, dwWidth, dwHeight, (BYTE**)&m_pBackgroundBits);
  1867. ::ZeroMemory(m_pBackgroundBits, dwWidth * dwHeight * 4);
  1868. ::SelectObject(m_hDcBackground, m_hbmpBackground);
  1869. CRenderClip clip;
  1870. CRenderClip::GenerateClip(m_hDcBackground, rcLayeredClient, clip);
  1871. CRenderEngine::DrawImageInfo(m_hDcBackground, this, rcLayeredClient, rcLayeredClient, &m_diLayered);
  1872. }
  1873. else if (m_bLayeredChanged)
  1874. {
  1875. ::ZeroMemory(m_pBackgroundBits, dwWidth * dwHeight * 4);
  1876. CRenderClip clip;
  1877. CRenderClip::GenerateClip(m_hDcBackground, rcLayeredClient, clip);
  1878. CRenderEngine::DrawImageInfo(m_hDcBackground, this, rcLayeredClient, rcLayeredClient, &m_diLayered);
  1879. }
  1880. for (LONG y = rcClient.bottom - rcPaint.bottom; y < rcClient.bottom - rcPaint.top; ++y)
  1881. {
  1882. for (LONG x = rcPaint.left; x < rcPaint.right; ++x)
  1883. {
  1884. pOffscreenBits = (COLORREF*)(m_pOffscreenBits + y * dwWidth + x);
  1885. pBackgroundBits = m_pBackgroundBits + y * dwWidth + x;
  1886. A = (BYTE)((*pBackgroundBits) >> 24);
  1887. R = (BYTE)((*pOffscreenBits) >> 16) * A / 255;
  1888. G = (BYTE)((*pOffscreenBits) >> 8) * A / 255;
  1889. B = (BYTE)(*pOffscreenBits) * A / 255;
  1890. *pOffscreenBits = RGB(B, G, R) + ((DWORD)A << 24);
  1891. }
  1892. }
  1893. }
  1894. }
  1895. else
  1896. {
  1897. for (LONG y = rcClient.bottom - rcPaint.bottom; y < rcClient.bottom - rcPaint.top; ++y)
  1898. {
  1899. for (LONG x = rcPaint.left; x < rcPaint.right; ++x)
  1900. {
  1901. int i = (y * dwWidth + x) * 4;
  1902. if ((m_pOffscreenBits[i + 3] == 0) && (m_pOffscreenBits[i + 0] != 0 || m_pOffscreenBits[i + 1] != 0 || m_pOffscreenBits[i + 2] != 0))
  1903. m_pOffscreenBits[i + 3] = 255;
  1904. }
  1905. }
  1906. }
  1907. BLENDFUNCTION bf = { AC_SRC_OVER, 0, m_nOpacity, AC_SRC_ALPHA };
  1908. POINT ptPos = { rcWnd.left, rcWnd.top };
  1909. SIZE sizeWnd = { dwWidth, dwHeight };
  1910. POINT ptSrc = { 0, 0 };
  1911. g_fUpdateLayeredWindow(m_hWndPaint, m_hDcPaint, &ptPos, &sizeWnd, m_hDcOffscreen, &ptSrc, 0, &bf, ULW_ALPHA);
  1912. }
  1913. else
  1914. {
  1915. ::BitBlt(m_hDcPaint, rcPaint.left, rcPaint.top, rcPaint.right - rcPaint.left, rcPaint.bottom - rcPaint.top, m_hDcOffscreen, rcPaint.left, rcPaint.top, SRCCOPY);
  1916. }
  1917. ::SelectObject(m_hDcOffscreen, hOldBitmap);
  1918. if (m_bShowUpdateRect && !m_bLayered)
  1919. {
  1920. HPEN hOldPen = (HPEN)::SelectObject(m_hDcPaint, m_hUpdateRectPen);
  1921. ::SelectObject(m_hDcPaint, ::GetStockObject(HOLLOW_BRUSH));
  1922. ::Rectangle(m_hDcPaint, rcPaint.left, rcPaint.top, rcPaint.right, rcPaint.bottom);
  1923. ::SelectObject(m_hDcPaint, hOldPen);
  1924. }
  1925. }
  1926. else
  1927. {
  1928. // A standard paint job
  1929. int iSaveDC = ::SaveDC(m_hDcPaint);
  1930. m_pRoot->Paint(m_hDcPaint, rcPaint, NULL);
  1931. for (int i = 0; i < m_aPostPaintControls.GetSize(); i++)
  1932. {
  1933. CControlUI* pPostPaintControl = static_cast<CControlUI*>(m_aPostPaintControls[i]);
  1934. pPostPaintControl->DoPostPaint(m_hDcPaint, rcPaint);
  1935. }
  1936. ::RestoreDC(m_hDcPaint, iSaveDC);
  1937. }
  1938. // All Done!
  1939. ::EndPaint(m_hWndPaint, &ps);
  1940. // 绘制结束
  1941. SetPainting(false);
  1942. m_bLayeredChanged = false;
  1943. if (m_bUpdateNeeded) Invalidate();
  1944. // 发送窗口大小改变消息
  1945. if (bNeedSizeMsg)
  1946. {
  1947. this->SendNotify(m_pRoot, DUI_MSGTYPE_WINDOWSIZE, 0, 0, true);
  1948. }
  1949. return true;
  1950. }
  1951. case WM_PRINTCLIENT:
  1952. {
  1953. if (m_pRoot == NULL) break;
  1954. RECT rcClient;
  1955. ::GetClientRect(m_hWndPaint, &rcClient);
  1956. HDC hDC = (HDC)wParam;
  1957. int save = ::SaveDC(hDC);
  1958. m_pRoot->Paint(hDC, rcClient, NULL);
  1959. if ((lParam & PRF_CHILDREN) != 0)
  1960. {
  1961. HWND hWndChild = ::GetWindow(m_hWndPaint, GW_CHILD);
  1962. while (hWndChild != NULL)
  1963. {
  1964. RECT rcPos = { 0 };
  1965. ::GetWindowRect(hWndChild, &rcPos);
  1966. ::MapWindowPoints(HWND_DESKTOP, m_hWndPaint, reinterpret_cast<LPPOINT>(&rcPos), 2);
  1967. ::SetWindowOrgEx(hDC, -rcPos.left, -rcPos.top, NULL);
  1968. ::SendMessage(hWndChild, WM_PRINT, wParam, lParam | PRF_NONCLIENT);
  1969. hWndChild = ::GetWindow(hWndChild, GW_HWNDNEXT);
  1970. }
  1971. }
  1972. ::RestoreDC(hDC, save);
  1973. }
  1974. break;
  1975. case WM_GETMINMAXINFO:
  1976. {
  1977. MONITORINFO Monitor = {};
  1978. Monitor.cbSize = sizeof(Monitor);
  1979. ::GetMonitorInfo(::MonitorFromWindow(m_hWndPaint, MONITOR_DEFAULTTOPRIMARY), &Monitor);
  1980. RECT rcWork = Monitor.rcWork;
  1981. if (Monitor.dwFlags != MONITORINFOF_PRIMARY)
  1982. {
  1983. ::OffsetRect(&rcWork, -rcWork.left, -rcWork.top);
  1984. }
  1985. LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
  1986. if (m_szMinWindow.cx > 0) lpMMI->ptMinTrackSize.x = m_szMinWindow.cx;
  1987. if (m_szMinWindow.cy > 0) lpMMI->ptMinTrackSize.y = m_szMinWindow.cy;
  1988. if (m_szMaxWindow.cx > 0) lpMMI->ptMaxTrackSize.x = m_szMaxWindow.cx;
  1989. if (m_szMaxWindow.cy > 0) lpMMI->ptMaxTrackSize.y = m_szMaxWindow.cy;
  1990. if (m_szMaxWindow.cx > 0) lpMMI->ptMaxSize.x = m_szMaxWindow.cx;
  1991. if (m_szMaxWindow.cy > 0) lpMMI->ptMaxSize.y = m_szMaxWindow.cy;
  1992. }
  1993. break;
  1994. case WM_SIZE:
  1995. {
  1996. if (m_pFocus != NULL)
  1997. {
  1998. TEventUI event = { 0 };
  1999. event.Type = UIEVENT_WINDOWSIZE;
  2000. event.pSender = m_pFocus;
  2001. event.dwTimestamp = ::GetTickCount();
  2002. m_pFocus->Event(event);
  2003. }
  2004. if (m_pRoot != NULL) m_pRoot->NeedUpdate();
  2005. }
  2006. return true;
  2007. case WM_TIMER:
  2008. {
  2009. for (int i = 0; i < m_aTimers.GetSize(); i++)
  2010. {
  2011. const TIMERINFO* pTimer = static_cast<TIMERINFO*>(m_aTimers[i]);
  2012. if (pTimer->hWnd == m_hWndPaint &&
  2013. pTimer->uWinTimer == LOWORD(wParam) &&
  2014. pTimer->bKilled == false)
  2015. {
  2016. TEventUI event = { 0 };
  2017. event.Type = UIEVENT_TIMER;
  2018. event.pSender = pTimer->pSender;
  2019. event.dwTimestamp = ::GetTickCount();
  2020. event.ptMouse = m_ptLastMousePos;
  2021. event.wKeyState = MapKeyState();
  2022. event.wParam = pTimer->nLocalID;
  2023. event.lParam = lParam;
  2024. pTimer->pSender->Event(event);
  2025. break;
  2026. }
  2027. }
  2028. }
  2029. break;
  2030. case WM_MOUSEHOVER:
  2031. {
  2032. if (m_bSuppressHoverAfterTouch) return true;
  2033. m_bMouseTracking = false;
  2034. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  2035. CControlUI* pHover = FindControl(pt);
  2036. if (pHover == NULL) break;
  2037. // Generate mouse hover event
  2038. if (m_pEventHover != NULL)
  2039. {
  2040. TEventUI event = { 0 };
  2041. event.Type = UIEVENT_MOUSEHOVER;
  2042. event.pSender = m_pEventHover;
  2043. event.wParam = wParam;
  2044. event.lParam = lParam;
  2045. event.dwTimestamp = ::GetTickCount();
  2046. event.ptMouse = pt;
  2047. event.wKeyState = MapKeyState();
  2048. m_pEventHover->Event(event);
  2049. }
  2050. // Create tooltip information
  2051. CDuiString sToolTip = pHover->GetToolTip();
  2052. if (sToolTip.IsEmpty()) return true;
  2053. ::ZeroMemory(&m_ToolTip, sizeof(TOOLINFO));
  2054. m_ToolTip.cbSize = sizeof(TOOLINFO);
  2055. m_ToolTip.uFlags = TTF_IDISHWND;
  2056. m_ToolTip.hwnd = m_hWndPaint;
  2057. m_ToolTip.uId = (UINT_PTR)m_hWndPaint;
  2058. m_ToolTip.hinst = m_hInstance;
  2059. m_ToolTip.lpszText = const_cast<LPTSTR>((LPCTSTR)sToolTip);
  2060. m_ToolTip.rect = pHover->GetPos();
  2061. if (m_hwndTooltip == NULL)
  2062. {
  2063. 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);
  2064. ::SendMessage(m_hwndTooltip, TTM_ADDTOOL, 0, (LPARAM)&m_ToolTip);
  2065. ::SendMessage(m_hwndTooltip, TTM_SETMAXTIPWIDTH, 0, pHover->GetToolTipWidth());
  2066. }
  2067. if (!::IsWindowVisible(m_hwndTooltip))
  2068. {
  2069. ::SendMessage(m_hwndTooltip, TTM_SETTOOLINFO, 0, (LPARAM)&m_ToolTip);
  2070. ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, TRUE, (LPARAM)&m_ToolTip);
  2071. }
  2072. }
  2073. return true;
  2074. case WM_MOUSELEAVE:
  2075. {
  2076. TouchTrace::Log(_T("WM_MOUSELEAVE tracking=%d hover=%s leaveArr=%d click=%s"),
  2077. (int)m_bMouseTracking,
  2078. m_pEventHover != NULL ? m_pEventHover->GetName().GetData() : _T("null"),
  2079. m_aNeedMouseLeaveNeeded.GetSize(),
  2080. m_pEventClick != NULL ? m_pEventClick->GetName().GetData() : _T("null"));
  2081. if (m_hwndTooltip != NULL) ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ToolTip);
  2082. // 触屏抬起刚清过悬浮态时,不要执行下面这段“自回发 WM_MOUSEMOVE”:
  2083. // 光标通常仍在窗口内 → 会立刻重建悬浮态,导致“悬浮态不消失/多控件同时悬浮”。
  2084. if (!m_bSuppressHoverAfterTouch && m_bMouseTracking)
  2085. {
  2086. POINT pt = { 0 };
  2087. RECT rcWnd = { 0 };
  2088. ::GetCursorPos(&pt);
  2089. ::GetWindowRect(m_hWndPaint, &rcWnd);
  2090. if (!::IsIconic(m_hWndPaint) && ::GetActiveWindow() == m_hWndPaint && ::PtInRect(&rcWnd, pt))
  2091. {
  2092. if (::SendMessage(m_hWndPaint, WM_NCHITTEST, 0, MAKELPARAM(pt.x, pt.y)) == HTCLIENT)
  2093. {
  2094. ::ScreenToClient(m_hWndPaint, &pt);
  2095. ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, MAKELPARAM(pt.x, pt.y));
  2096. }
  2097. else
  2098. ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, (LPARAM)-1);
  2099. }
  2100. else
  2101. ::SendMessage(m_hWndPaint, WM_MOUSEMOVE, 0, (LPARAM)-1);
  2102. }
  2103. m_bMouseTracking = false;
  2104. }
  2105. break;
  2106. case WM_MOUSEMOVE:
  2107. {
  2108. const bool bPromotedTouchMove = IsPromotedTouchMouseMessage();
  2109. if (bPromotedTouchMove && m_pEventTouch != NULL)
  2110. {
  2111. POINT ptTouch = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  2112. const int kTouchSlop = 8;
  2113. const int dxTouch = ptTouch.x - m_ptTouchStart.x;
  2114. const int dyTouch = ptTouch.y - m_ptTouchStart.y;
  2115. if (dxTouch > kTouchSlop || dxTouch < -kTouchSlop
  2116. || dyTouch > kTouchSlop || dyTouch < -kTouchSlop)
  2117. m_bTouchMoved = true;
  2118. m_ptLastMousePos = ptTouch;
  2119. return true;
  2120. }
  2121. if (m_bSuppressHoverAfterTouch)
  2122. {
  2123. m_ptLastMousePos = POINT{ GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  2124. return true;
  2125. }
  2126. if ((INT_PTR)lParam == -1)
  2127. break;
  2128. POINT ptCheck = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  2129. if (m_bSuppressHoverAfterTouch)
  2130. {
  2131. // 合成消息通常仍停在触摸结束点;只有明显离开该点的鼠标移动
  2132. // 才能重新建立鼠标悬浮语义。
  2133. const int dx = ptCheck.x - m_ptLastTouchPos.x;
  2134. const int dy = ptCheck.y - m_ptLastTouchPos.y;
  2135. if (dx <= 8 && dx >= -8 && dy <= 8 && dy >= -8)
  2136. break;
  2137. m_bSuppressHoverAfterTouch = false;
  2138. }
  2139. // Start tracking this entire window again...
  2140. if (!m_bMouseTracking)
  2141. {
  2142. TRACKMOUSEEVENT tme = { 0 };
  2143. tme.cbSize = sizeof(TRACKMOUSEEVENT);
  2144. tme.dwFlags = TME_HOVER | TME_LEAVE;
  2145. tme.hwndTrack = m_hWndPaint;
  2146. tme.dwHoverTime = m_hwndTooltip == NULL ? m_iHoverTime : (DWORD) ::SendMessage(m_hwndTooltip, TTM_GETDELAYTIME, TTDT_INITIAL, 0L);
  2147. _TrackMouseEvent(&tme);
  2148. m_bMouseTracking = true;
  2149. }
  2150. // Generate the appropriate mouse messages
  2151. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  2152. // 是否移动
  2153. bool bNeedDrag = true;
  2154. if (m_ptLastMousePos.x == pt.x && m_ptLastMousePos.y == pt.y)
  2155. {
  2156. bNeedDrag = false;
  2157. }
  2158. // 记录鼠标位置
  2159. m_ptLastMousePos = pt;
  2160. CControlUI* pNewHover = FindControl(pt);
  2161. if (pNewHover != NULL && pNewHover->GetManager() != this) break;
  2162. // 拖拽事件
  2163. if (bNeedDrag && m_bDragMode && wParam == MK_LBUTTON)
  2164. {
  2165. // 释放Capture
  2166. ::ReleaseCapture();
  2167. // 接口
  2168. if (m_pDragDrop != NULL && m_pDragDrop->OnDragDrop(m_pEventClick))
  2169. {
  2170. m_bDragMode = false;
  2171. break;
  2172. }
  2173. CIDropSource* pdsrc = new CIDropSource;
  2174. if (pdsrc == NULL) return 0;
  2175. pdsrc->AddRef();
  2176. CIDataObject* pdobj = new CIDataObject(pdsrc);
  2177. if (pdobj == NULL) return 0;
  2178. pdobj->AddRef();
  2179. FORMATETC fmtetc = { 0 };
  2180. STGMEDIUM medium = { 0 };
  2181. fmtetc.dwAspect = DVASPECT_CONTENT;
  2182. fmtetc.lindex = -1;
  2183. fmtetc.cfFormat = CF_BITMAP;
  2184. fmtetc.tymed = TYMED_GDI;
  2185. //////////////////////////////////////
  2186. HBITMAP hBitmap = (HBITMAP)OleDuplicateData(m_hDragBitmap, fmtetc.cfFormat, NULL);
  2187. medium.hBitmap = hBitmap;
  2188. pdobj->SetData(&fmtetc, &medium, FALSE);
  2189. //////////////////////////////////////
  2190. BITMAP bmap;
  2191. GetObject(hBitmap, sizeof(BITMAP), &bmap);
  2192. RECT rc = { 0, 0, bmap.bmWidth, bmap.bmHeight };
  2193. fmtetc.cfFormat = CF_ENHMETAFILE;
  2194. fmtetc.tymed = TYMED_ENHMF;
  2195. HDC hMetaDC = CreateEnhMetaFile(m_hDcPaint, NULL, NULL, NULL);
  2196. HDC hdcMem = CreateCompatibleDC(m_hDcPaint);
  2197. HGDIOBJ hOldBmp = ::SelectObject(hdcMem, hBitmap);
  2198. ::BitBlt(hMetaDC, 0, 0, rc.right, rc.bottom, hdcMem, 0, 0, SRCCOPY);
  2199. ::SelectObject(hdcMem, hOldBmp);
  2200. medium.hEnhMetaFile = CloseEnhMetaFile(hMetaDC);
  2201. DeleteDC(hdcMem);
  2202. medium.tymed = TYMED_ENHMF;
  2203. pdobj->SetData(&fmtetc, &medium, TRUE);
  2204. //////////////////////////////////////
  2205. CDragSourceHelper dragSrcHelper;
  2206. POINT ptDrag = { 0 };
  2207. ptDrag.x = bmap.bmWidth / 2;
  2208. ptDrag.y = bmap.bmHeight / 2;
  2209. dragSrcHelper.InitializeFromBitmap(hBitmap, ptDrag, rc, pdobj); //will own the bmp
  2210. DWORD dwEffect;
  2211. HRESULT hr = ::DoDragDrop(pdobj, pdsrc, DROPEFFECT_COPY | DROPEFFECT_MOVE, &dwEffect);
  2212. if (dwEffect) pdsrc->Release();
  2213. else delete pdsrc;
  2214. pdobj->Release();
  2215. m_bDragMode = false;
  2216. break;
  2217. }
  2218. TEventUI event = { 0 };
  2219. event.ptMouse = pt;
  2220. event.wParam = wParam;
  2221. event.lParam = lParam;
  2222. event.dwTimestamp = ::GetTickCount();
  2223. event.wKeyState = MapKeyState();
  2224. // 悬浮态总闸:触摸提升出来的合成鼠标消息(MOUSEEVENTF_FROMTOUCH)不参与
  2225. // 悬浮 enter/leave。触摸没有“光标停留”语义,一旦让它参与,就会出现
  2226. // “点过的控件一直高亮 / 连点几个控件后多个同时高亮、怎么点都清不掉”的顽疾
  2227. // (按钮的 UISTATE_HOT、列表项的 UISTATE_HOT 全靠 MOUSEENTER/MOUSELEAVE 维护)。
  2228. // 该判断只影响悬浮段,消息依旧往下走 → 点击/拖拽状态机不受影响。
  2229. if (!IsCaptured() && !bPromotedTouchMove)
  2230. {
  2231. pNewHover = FindControl(pt);
  2232. if (pNewHover != NULL && pNewHover->GetManager() != this) break;
  2233. if (pNewHover != m_pEventHover && m_pEventHover != NULL)
  2234. {
  2235. event.Type = UIEVENT_MOUSELEAVE;
  2236. event.pSender = m_pEventHover;
  2237. CStdPtrArray aNeedMouseLeaveNeeded(m_aNeedMouseLeaveNeeded.GetSize());
  2238. aNeedMouseLeaveNeeded.Resize(m_aNeedMouseLeaveNeeded.GetSize());
  2239. ::CopyMemory(aNeedMouseLeaveNeeded.GetData(), m_aNeedMouseLeaveNeeded.GetData(), m_aNeedMouseLeaveNeeded.GetSize() * sizeof(LPVOID));
  2240. for (int i = 0; i < aNeedMouseLeaveNeeded.GetSize(); i++)
  2241. {
  2242. static_cast<CControlUI*>(aNeedMouseLeaveNeeded[i])->Event(event);
  2243. }
  2244. TouchTrace::Log(_T("MOUSEMOVE-LEAVE old=%s leaveArr=%d new=%s pt=%d,%d"),
  2245. m_pEventHover->GetName().GetData(), m_aNeedMouseLeaveNeeded.GetSize(),
  2246. pNewHover != NULL ? pNewHover->GetName().GetData() : _T("null"), pt.x, pt.y);
  2247. m_pEventHover->Event(event);
  2248. m_pEventHover = NULL;
  2249. if (m_hwndTooltip != NULL) ::SendMessage(m_hwndTooltip, TTM_TRACKACTIVATE, FALSE, (LPARAM)&m_ToolTip);
  2250. }
  2251. if (pNewHover != m_pEventHover && pNewHover != NULL)
  2252. {
  2253. event.Type = UIEVENT_MOUSEENTER;
  2254. event.pSender = pNewHover;
  2255. pNewHover->Event(event);
  2256. m_pEventHover = pNewHover;
  2257. TouchTrace::Log(_T("HOVER-ENTER %s cls=%s pt=%d,%d"),
  2258. pNewHover->GetName().GetData(), pNewHover->GetClass(), pt.x, pt.y);
  2259. }
  2260. }
  2261. if (m_pEventClick != NULL)
  2262. {
  2263. event.Type = UIEVENT_MOUSEMOVE;
  2264. event.pSender = m_pEventClick;
  2265. m_pEventClick->Event(event);
  2266. }
  2267. else if (pNewHover != NULL)
  2268. {
  2269. event.Type = UIEVENT_MOUSEMOVE;
  2270. event.pSender = pNewHover;
  2271. pNewHover->Event(event);
  2272. }
  2273. }
  2274. break;
  2275. case WM_LBUTTONDOWN:
  2276. {
  2277. TouchTrace::Log(_T("WM_LBUTTONDOWN promoted=%d pt=%d,%d eventTouch=%s eventClick=%s"),
  2278. (int)IsPromotedTouchMouseMessage(), GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam),
  2279. m_pEventTouch != NULL ? m_pEventTouch->GetName().GetData() : _T("null"),
  2280. m_pEventClick != NULL ? m_pEventClick->GetName().GetData() : _T("null"));
  2281. // WM_TOUCH 后 Windows 可能补发 WM_LBUTTONDOWN。触摸点击由 TOUCHUP
  2282. // 的 tap 判定统一处理,不能让这条合成消息提前建立鼠标点击状态。
  2283. if (m_pEventTouch != NULL || m_bSuppressPromotedTouchMouse)
  2284. break;
  2285. // We alway set focus back to our app (this helps
  2286. // when Win32 child windows are placed on the dialog
  2287. // and we need to remove them on focus change).
  2288. if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
  2289. if (m_pRoot == NULL) break;
  2290. // 查找控件
  2291. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  2292. m_ptLastMousePos = pt;
  2293. CControlUI* pControl = FindControl(pt);
  2294. if (pControl == NULL)
  2295. {
  2296. TouchTrace::Log(_T("LBUTTONDOWN no control hwnd=%p pt=%d,%d root=%p"),
  2297. (void*)m_hWndPaint, pt.x, pt.y, (void*)m_pRoot);
  2298. break;
  2299. }
  2300. if (pControl->GetManager() != this)
  2301. {
  2302. TouchTrace::Log(_T("LBUTTONDOWN foreign control hwnd=%p pt=%d,%d name=%s manager=%p self=%p"),
  2303. (void*)m_hWndPaint, pt.x, pt.y, pControl->GetName().GetData(),
  2304. (void*)pControl->GetManager(), (void*)this);
  2305. break;
  2306. }
  2307. TouchTrace::Log(_T("LBUTTONDOWN hit hwnd=%p name=%s class=%s pos=%d,%d,%d,%d enabled=%d visible=%d"),
  2308. (void*)m_hWndPaint, pControl->GetName().GetData(), pControl->GetClass(),
  2309. pControl->GetPos().left, pControl->GetPos().top, pControl->GetPos().right, pControl->GetPos().bottom,
  2310. (int)pControl->IsEnabled(), (int)pControl->IsVisible());
  2311. // 准备拖拽
  2312. if (m_bDragDrop && pControl->IsDragEnabled())
  2313. {
  2314. m_bDragMode = true;
  2315. if (m_hDragBitmap != NULL)
  2316. {
  2317. ::DeleteObject(m_hDragBitmap);
  2318. m_hDragBitmap = NULL;
  2319. }
  2320. m_hDragBitmap = CRenderEngine::GenerateBitmap(this, pControl, pControl->GetPos());
  2321. }
  2322. // 开启捕获
  2323. SetCapture();
  2324. // 事件处理
  2325. m_pEventClick = pControl;
  2326. pControl->SetFocus();
  2327. TEventUI event = { 0 };
  2328. event.Type = UIEVENT_BUTTONDOWN;
  2329. event.pSender = pControl;
  2330. event.wParam = wParam;
  2331. event.lParam = lParam;
  2332. event.ptMouse = pt;
  2333. event.wKeyState = (WORD)wParam;
  2334. event.dwTimestamp = ::GetTickCount();
  2335. pControl->Event(event);
  2336. }
  2337. break;
  2338. case WM_LBUTTONDBLCLK:
  2339. {
  2340. if (!m_bNoActivate) ::SetFocus(m_hWndPaint);
  2341. POINT pt = { GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) };
  2342. m_ptLastMousePos = pt;
  2343. CControlUI* pControl = FindControl(pt);
  2344. if (pControl == NULL) break;
  2345. if (pControl->GetManager() != this) break;
  2346. //SetCapture();
  2347. TEventUI event = { 0 };
  2348. event.Type = UIEVENT_DBLCLICK;
  2349. event.pSender = pControl;
  2350. event.ptMouse = pt;
  2351. event.wParam = wParam;
  2352. event.lParam = lParam;
  2353. event.wKeyState = (WORD)wParam;
  2354. event.dwTimestamp = ::GetTickCount();
  2355. pControl->Event(event);
  2356. m_pEventClick = pControl;
  2357. }
  2358. break;
  2359. case WM_LBUTTONUP:
  2360. {
  2361. TouchTrace::Log(_T("WM_LBUTTONUP promoted=%d pt=%d,%d eventTouch=%s eventClick=%s moved=%d"),
  2362. (int)IsPromotedTouchMouseMessage(), GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam),
  2363. m_pEventTouch != NULL ? m_pEventTouch->GetName().GetData() : _T("null"),
  2364. m_pEventClick != NULL ? m_pEventClick->GetName().GetData() : _T("null"),
  2365. (int)m_bTouchMoved);
  2366. const bool bPromotedTouchUp = IsPromotedTouchMouseMessage();
  2367. // TOUCHUP 已经负责 tap/drag 判定;若提升的鼠标抬起先到,不能
  2368. // 绕过该判定触发 BUTTONUP。
  2369. if (m_pEventTouch != NULL || m_bSuppressPromotedTouchMouse)
  2370. {
  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. if (uChildRes != 0)
  4609. {
  4610. HWND hWndParent = ::GetParent(pMsg->hwnd);
  4611. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  4612. {
  4613. CPaintManagerUI* pT = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  4614. HWND hTempParent = hWndParent;
  4615. while (hTempParent)
  4616. {
  4617. if (pMsg->hwnd == pT->GetPaintWindow() || hTempParent == pT->GetPaintWindow())
  4618. {
  4619. if (pT->TranslateAccelerator(pMsg))
  4620. return true;
  4621. pT->PreMessageHandler(pMsg->message, pMsg->wParam, pMsg->lParam, lRes);
  4622. }
  4623. hTempParent = GetParent(hTempParent);
  4624. }
  4625. }
  4626. }
  4627. else
  4628. {
  4629. for (int i = 0; i < m_aPreMessages.GetSize(); i++)
  4630. {
  4631. CPaintManagerUI* pT = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
  4632. if (pMsg->hwnd == pT->GetPaintWindow())
  4633. {
  4634. if (pT->TranslateAccelerator(pMsg))
  4635. return true;
  4636. if (pT->PreMessageHandler(pMsg->message, pMsg->wParam, pMsg->lParam, lRes))
  4637. return true;
  4638. return false;
  4639. }
  4640. }
  4641. }
  4642. return false;
  4643. }
  4644. bool CPaintManagerUI::AddTranslateAccelerator(ITranslateAccelerator* pTranslateAccelerator)
  4645. {
  4646. ASSERT(m_aTranslateAccelerator.Find(pTranslateAccelerator) < 0);
  4647. return m_aTranslateAccelerator.Add(pTranslateAccelerator);
  4648. }
  4649. bool CPaintManagerUI::RemoveTranslateAccelerator(ITranslateAccelerator* pTranslateAccelerator)
  4650. {
  4651. for (int i = 0; i < m_aTranslateAccelerator.GetSize(); i++)
  4652. {
  4653. if (static_cast<ITranslateAccelerator*>(m_aTranslateAccelerator[i]) == pTranslateAccelerator)
  4654. {
  4655. return m_aTranslateAccelerator.Remove(i);
  4656. }
  4657. }
  4658. return false;
  4659. }
  4660. void CPaintManagerUI::UsedVirtualWnd(bool bUsed)
  4661. {
  4662. m_bUsedVirtualWnd = bUsed;
  4663. }
  4664. // 样式管理
  4665. void CPaintManagerUI::AddStyle(LPCTSTR pName, LPCTSTR pDeclarationList, bool bShared)
  4666. {
  4667. CDuiString* pStyle = new CDuiString(pDeclarationList);
  4668. if (bShared || m_bForceUseSharedRes)
  4669. {
  4670. if (!m_SharedResInfo.m_StyleHash.Insert(pName, pStyle))
  4671. {
  4672. delete pStyle;
  4673. }
  4674. }
  4675. else
  4676. {
  4677. if (!m_ResInfo.m_StyleHash.Insert(pName, pStyle))
  4678. {
  4679. delete pStyle;
  4680. }
  4681. }
  4682. }
  4683. LPCTSTR CPaintManagerUI::GetStyle(LPCTSTR pName) const
  4684. {
  4685. CDuiString* pStyle = static_cast<CDuiString*>(m_ResInfo.m_StyleHash.Find(pName));
  4686. if (!pStyle) pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(pName));
  4687. if (pStyle) return pStyle->GetData();
  4688. else return NULL;
  4689. }
  4690. BOOL CPaintManagerUI::RemoveStyle(LPCTSTR pName, bool bShared)
  4691. {
  4692. CDuiString* pStyle = NULL;
  4693. if (bShared)
  4694. {
  4695. pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(pName));
  4696. if (pStyle)
  4697. {
  4698. delete pStyle;
  4699. m_SharedResInfo.m_StyleHash.Remove(pName);
  4700. }
  4701. }
  4702. else
  4703. {
  4704. pStyle = static_cast<CDuiString*>(m_ResInfo.m_StyleHash.Find(pName));
  4705. if (pStyle)
  4706. {
  4707. delete pStyle;
  4708. m_ResInfo.m_StyleHash.Remove(pName);
  4709. }
  4710. }
  4711. return true;
  4712. }
  4713. const CStdStringPtrMap& CPaintManagerUI::GetStyles(bool bShared) const
  4714. {
  4715. if (bShared) return m_SharedResInfo.m_StyleHash;
  4716. else return m_ResInfo.m_StyleHash;
  4717. }
  4718. void CPaintManagerUI::RemoveAllStyle(bool bShared)
  4719. {
  4720. if (bShared)
  4721. {
  4722. CDuiString* pStyle;
  4723. for (int i = 0; i < m_SharedResInfo.m_StyleHash.GetSize(); i++)
  4724. {
  4725. if (LPCTSTR key = m_SharedResInfo.m_StyleHash.GetAt(i))
  4726. {
  4727. pStyle = static_cast<CDuiString*>(m_SharedResInfo.m_StyleHash.Find(key));
  4728. delete pStyle;
  4729. }
  4730. }
  4731. m_SharedResInfo.m_StyleHash.RemoveAll();
  4732. }
  4733. else
  4734. {
  4735. CDuiString* pStyle;
  4736. for (int i = 0; i < m_ResInfo.m_StyleHash.GetSize(); i++)
  4737. {
  4738. if (LPCTSTR key = m_ResInfo.m_StyleHash.GetAt(i))
  4739. {
  4740. pStyle = static_cast<CDuiString*>(m_ResInfo.m_StyleHash.Find(key));
  4741. delete pStyle;
  4742. }
  4743. }
  4744. m_ResInfo.m_StyleHash.RemoveAll();
  4745. }
  4746. }
  4747. const TImageInfo* CPaintManagerUI::GetImageString(LPCTSTR pStrImage, LPCTSTR pStrModify)
  4748. {
  4749. CDuiString sImageName = pStrImage;
  4750. CDuiString sImageResType = _T("");
  4751. DWORD dwMask = 0;
  4752. CDuiString sItem;
  4753. CDuiString sValue;
  4754. LPTSTR pstr = NULL;
  4755. for (int i = 0; i < 2; ++i)
  4756. {
  4757. if (i == 1)
  4758. pStrImage = pStrModify;
  4759. if (!pStrImage) continue;
  4760. while (*pStrImage != _T('\0'))
  4761. {
  4762. sItem.Empty();
  4763. sValue.Empty();
  4764. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  4765. while (*pStrImage != _T('\0') && *pStrImage != _T('=') && *pStrImage > _T(' '))
  4766. {
  4767. LPTSTR pstrTemp = ::CharNext(pStrImage);
  4768. while (pStrImage < pstrTemp)
  4769. {
  4770. sItem += *pStrImage++;
  4771. }
  4772. }
  4773. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  4774. if (*pStrImage++ != _T('=')) break;
  4775. while (*pStrImage > _T('\0') && *pStrImage <= _T(' ')) pStrImage = ::CharNext(pStrImage);
  4776. if (*pStrImage++ != _T('\'')) break;
  4777. while (*pStrImage != _T('\0') && *pStrImage != _T('\''))
  4778. {
  4779. LPTSTR pstrTemp = ::CharNext(pStrImage);
  4780. while (pStrImage < pstrTemp)
  4781. {
  4782. sValue += *pStrImage++;
  4783. }
  4784. }
  4785. if (*pStrImage++ != _T('\'')) break;
  4786. if (!sValue.IsEmpty())
  4787. {
  4788. if (sItem == _T("file") || sItem == _T("res"))
  4789. {
  4790. sImageName = sValue;
  4791. }
  4792. else if (sItem == _T("restype"))
  4793. {
  4794. sImageResType = sValue;
  4795. }
  4796. else if (sItem == _T("mask"))
  4797. {
  4798. if (sValue[0] == _T('#')) dwMask = _tcstoul(sValue.GetData() + 1, &pstr, 16);
  4799. else dwMask = _tcstoul(sValue.GetData(), &pstr, 16);
  4800. }
  4801. }
  4802. if (*pStrImage++ != _T(' ')) break;
  4803. }
  4804. }
  4805. return GetImageEx(sImageName, sImageResType, dwMask);
  4806. }
  4807. bool CPaintManagerUI::EnableDragDrop(bool bEnable)
  4808. {
  4809. if (m_bDragDrop == bEnable) return false;
  4810. m_bDragDrop = bEnable;
  4811. if (bEnable)
  4812. {
  4813. AddRef();
  4814. if (FAILED(RegisterDragDrop(m_hWndPaint, this)))
  4815. {
  4816. return false;
  4817. }
  4818. SetTargetWnd(m_hWndPaint);
  4819. FORMATETC ftetc = { 0 };
  4820. ftetc.cfFormat = CF_BITMAP;
  4821. ftetc.dwAspect = DVASPECT_CONTENT;
  4822. ftetc.lindex = -1;
  4823. ftetc.tymed = TYMED_GDI;
  4824. AddSuportedFormat(ftetc);
  4825. ftetc.cfFormat = CF_DIB;
  4826. ftetc.tymed = TYMED_HGLOBAL;
  4827. AddSuportedFormat(ftetc);
  4828. ftetc.cfFormat = CF_HDROP;
  4829. ftetc.tymed = TYMED_HGLOBAL;
  4830. AddSuportedFormat(ftetc);
  4831. ftetc.cfFormat = CF_ENHMETAFILE;
  4832. ftetc.tymed = TYMED_ENHMF;
  4833. AddSuportedFormat(ftetc);
  4834. }
  4835. else
  4836. {
  4837. Release();
  4838. if (FAILED(RevokeDragDrop(m_hWndPaint)))
  4839. {
  4840. return false;
  4841. }
  4842. }
  4843. return true;
  4844. }
  4845. void CPaintManagerUI::SetDragDrop(IDragDropUI* pDragDrop)
  4846. {
  4847. m_pDragDrop = pDragDrop;
  4848. }
  4849. static WORD DIBNumColors(void* pv)
  4850. {
  4851. int bits;
  4852. LPBITMAPINFOHEADER lpbi;
  4853. LPBITMAPCOREHEADER lpbc;
  4854. lpbi = ((LPBITMAPINFOHEADER)pv);
  4855. lpbc = ((LPBITMAPCOREHEADER)pv);
  4856. /* With the BITMAPINFO format headers, the size of the palette
  4857. * is in biClrUsed, whereas in the BITMAPCORE - style headers, it
  4858. * is dependent on the bits per pixel ( = 2 raised to the power of
  4859. * bits/pixel).
  4860. */
  4861. if (lpbi->biSize != sizeof(BITMAPCOREHEADER))
  4862. {
  4863. if (lpbi->biClrUsed != 0)
  4864. return (WORD)lpbi->biClrUsed;
  4865. bits = lpbi->biBitCount;
  4866. }
  4867. else
  4868. bits = lpbc->bcBitCount;
  4869. switch (bits)
  4870. {
  4871. case 1:
  4872. return 2;
  4873. case 4:
  4874. return 16;
  4875. case 8:
  4876. return 256;
  4877. default:
  4878. /* A 24 bitcount DIB has no color table */
  4879. return 0;
  4880. }
  4881. }
  4882. //code taken from SEEDIB MSDN sample
  4883. static WORD ColorTableSize(LPVOID lpv)
  4884. {
  4885. LPBITMAPINFOHEADER lpbih = (LPBITMAPINFOHEADER)lpv;
  4886. if (lpbih->biSize != sizeof(BITMAPCOREHEADER))
  4887. {
  4888. if (((LPBITMAPINFOHEADER)(lpbih))->biCompression == BI_BITFIELDS)
  4889. /* Remember that 16/32bpp dibs can still have a color table */
  4890. return (sizeof(DWORD) * 3) + (DIBNumColors(lpbih) * sizeof(RGBQUAD));
  4891. else
  4892. return (WORD)(DIBNumColors(lpbih) * sizeof(RGBQUAD));
  4893. }
  4894. else
  4895. return (WORD)(DIBNumColors(lpbih) * sizeof(RGBTRIPLE));
  4896. }
  4897. bool CPaintManagerUI::OnDrop(FORMATETC* pFmtEtc, STGMEDIUM& medium, DWORD* pdwEffect)
  4898. {
  4899. POINT ptMouse = { 0 };
  4900. GetCursorPos(&ptMouse);
  4901. ::SendMessage(m_hTargetWnd, WM_LBUTTONUP, NULL, MAKELPARAM(ptMouse.x, ptMouse.y));
  4902. if (pFmtEtc->cfFormat == CF_DIB && medium.tymed == TYMED_HGLOBAL)
  4903. {
  4904. if (medium.hGlobal != NULL)
  4905. {
  4906. LPBITMAPINFOHEADER lpbi = (BITMAPINFOHEADER*)GlobalLock(medium.hGlobal);
  4907. if (lpbi != NULL)
  4908. {
  4909. HBITMAP hbm = NULL;
  4910. HDC hdc = GetDC(NULL);
  4911. if (hdc != NULL)
  4912. {
  4913. int i = ((BITMAPFILEHEADER*)lpbi)->bfOffBits;
  4914. hbm = CreateDIBitmap(hdc, (LPBITMAPINFOHEADER)lpbi,
  4915. (LONG)CBM_INIT,
  4916. (LPSTR)lpbi + lpbi->biSize + ColorTableSize(lpbi),
  4917. (LPBITMAPINFO)lpbi, DIB_RGB_COLORS);
  4918. ::ReleaseDC(NULL, hdc);
  4919. }
  4920. GlobalUnlock(medium.hGlobal);
  4921. if (hbm != NULL)
  4922. hbm = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbm);
  4923. if (hbm != NULL)
  4924. DeleteObject(hbm);
  4925. return true; //release the medium
  4926. }
  4927. }
  4928. }
  4929. if (pFmtEtc->cfFormat == CF_BITMAP && medium.tymed == TYMED_GDI)
  4930. {
  4931. if (medium.hBitmap != NULL)
  4932. {
  4933. HBITMAP hBmp = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)medium.hBitmap);
  4934. if (hBmp != NULL)
  4935. DeleteObject(hBmp);
  4936. return false; //don't free the bitmap
  4937. }
  4938. }
  4939. if (pFmtEtc->cfFormat == CF_ENHMETAFILE && medium.tymed == TYMED_ENHMF)
  4940. {
  4941. ENHMETAHEADER emh;
  4942. GetEnhMetaFileHeader(medium.hEnhMetaFile, sizeof(ENHMETAHEADER), &emh);
  4943. RECT rc;//={0,0,EnhMetaHdr.rclBounds.right-EnhMetaHdr.rclBounds.left, EnhMetaHdr.rclBounds.bottom-EnhMetaHdr.rclBounds.top};
  4944. HDC hDC = GetDC(m_hTargetWnd);
  4945. //start code: taken from ENHMETA.EXE MSDN Sample
  4946. //*ALSO NEED to GET the pallete (select and RealizePalette it, but i was too lazy*
  4947. // Get the characteristics of the output device
  4948. float PixelsX = (float)GetDeviceCaps(hDC, HORZRES);
  4949. float PixelsY = (float)GetDeviceCaps(hDC, VERTRES);
  4950. float MMX = (float)GetDeviceCaps(hDC, HORZSIZE);
  4951. float MMY = (float)GetDeviceCaps(hDC, VERTSIZE);
  4952. // Calculate the rect in which to draw the metafile based on the
  4953. // intended size and the current output device resolution
  4954. // Remember that the intended size is given in 0.01mm units, so
  4955. // convert those to device units on the target device
  4956. rc.top = (int)((float)(emh.rclFrame.top) * PixelsY / (MMY * 100.0f));
  4957. rc.left = (int)((float)(emh.rclFrame.left) * PixelsX / (MMX * 100.0f));
  4958. rc.right = (int)((float)(emh.rclFrame.right) * PixelsX / (MMX * 100.0f));
  4959. rc.bottom = (int)((float)(emh.rclFrame.bottom) * PixelsY / (MMY * 100.0f));
  4960. //end code: taken from ENHMETA.EXE MSDN Sample
  4961. HDC hdcMem = CreateCompatibleDC(hDC);
  4962. HGDIOBJ hBmpMem = CreateCompatibleBitmap(hDC, emh.rclBounds.right, emh.rclBounds.bottom);
  4963. HGDIOBJ hOldBmp = ::SelectObject(hdcMem, hBmpMem);
  4964. PlayEnhMetaFile(hdcMem, medium.hEnhMetaFile, &rc);
  4965. HBITMAP hBmp = (HBITMAP)::SelectObject(hdcMem, hOldBmp);
  4966. DeleteDC(hdcMem);
  4967. ReleaseDC(m_hTargetWnd, hDC);
  4968. hBmp = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBmp);
  4969. if (hBmp != NULL)
  4970. DeleteObject(hBmp);
  4971. return true;
  4972. }
  4973. if (pFmtEtc->cfFormat == CF_HDROP && medium.tymed == TYMED_HGLOBAL)
  4974. {
  4975. HDROP hDrop = (HDROP)GlobalLock(medium.hGlobal);
  4976. if (hDrop != NULL)
  4977. {
  4978. TCHAR szFileName[MAX_PATH];
  4979. UINT cFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
  4980. if (cFiles > 0)
  4981. {
  4982. DragQueryFile(hDrop, 0, szFileName, sizeof(szFileName));
  4983. HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
  4984. if (hBitmap)
  4985. {
  4986. HBITMAP hBmp = (HBITMAP)SendMessage(m_hTargetWnd, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
  4987. if (hBmp != NULL)
  4988. DeleteObject(hBmp);
  4989. }
  4990. }
  4991. }
  4992. GlobalUnlock(medium.hGlobal);
  4993. }
  4994. return true; //let base free the medium
  4995. }
  4996. } // namespace DuiLib
  4997. #pragma warning(pop)