UIManager.cpp 162 KB

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