UIManager.cpp 128 KB

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