UIManager.cpp 148 KB

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