UIManager.cpp 138 KB

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