UIManager.cpp 153 KB

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