UIManager.cpp 139 KB

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