UIManager.cpp 153 KB

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