LayoutBox.cs 222 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033
  1. /*******************************************************************************
  2. * Copyright(c) 2012 dongke All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:LayoutBox.cs
  5. * 2.功能描述:票据主控件
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 欧阳涛 2012/09/11 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Drawing;
  16. using System.Drawing.Printing;
  17. using System.IO;
  18. using System.Runtime.InteropServices;
  19. using System.Runtime.Serialization.Formatters.Binary;
  20. using System.Security.Cryptography;
  21. using System.Windows.Forms;
  22. namespace Dongke.WinForm.Controls.InvoiceLayout
  23. {
  24. /// <summary>
  25. /// 票据模板控件
  26. /// </summary>
  27. public partial class LayoutBox : UserControl
  28. {
  29. #region 常量
  30. internal const string ITEMNAME_FIX = "Free";
  31. #region 保存、读取相关
  32. // TODO 加密保存 解密读取
  33. private static readonly byte[] KEY = new byte[] { 106, 240, 243, 74, 29, 163, 145, 29, 39, 65, 118, 96, 247, 237, 40, 121, 116, 10, 14, 50, 139, 117, 244, 163, 193, 190, 184, 66, 8, 185, 78, 73 };
  34. private static readonly byte[] IV = new byte[] { 26, 122, 25, 28, 97, 211, 152, 32, 223, 110, 120, 242, 170, 35, 96, 93 };
  35. #endregion 保存、读取相关
  36. #endregion 常量
  37. #region 成员变量
  38. #region Layout相关
  39. private bool _isDirty; // Layout是否变更
  40. private short _updateCount; // 更新数量
  41. private int _newItemID; // 生成新的ItemID
  42. private LayoutBoxDataBinding _dataBinding; // 数据源绑定
  43. private object _dataSource; // 数据源
  44. private string _dataMember; // 数据源名
  45. private LayoutBoxPrintDataBinding _printDataBinding; // 打印的数据源绑定
  46. private object _printDataSource; // 打印的数据源
  47. private string _printDataMember; // 打印的数据源名
  48. /* Item的显示循序
  49. * Items PaperArea.Controls
  50. * Item的显示循序 Index Index
  51. * 最前面 count-1 0
  52. * ↓ ↑ ↓
  53. * ↓ ↑ ↓
  54. * ↓ ↑ ↓
  55. * 最背面 0 count-1
  56. */
  57. private Comparison<LayoutItem> _comparisonItems; // 排序Item
  58. private List<LayoutItem> _items; // LayoutBox中的Item集合
  59. private List<LayoutItem> _selectedItems; // 选择状态的Item集合
  60. private LayoutMode _layoutMode; // Layout编辑模式
  61. private ZoomType _zoomType; // Layout缩放模式
  62. private int _zoom; // Layout缩放比例
  63. private float _paperHeight; // Layout的纸张的高
  64. private float _paperWidth; // Layout的纸张的宽
  65. private DataFormats.Format _clipboardFormat; // 剪贴板数据格式
  66. private string _backgroundImageName; // 背景图片名
  67. #endregion Layout相关
  68. #region Item选择
  69. private bool _isMouseDownInPaperArea; // 鼠标按下
  70. private bool _isMouseMoveInPaperArea; // 鼠标移动
  71. private Rectangle _mouseRect; // 鼠标框选范围
  72. #endregion Item选择
  73. #region Item移动
  74. private int _startX; // 鼠标按下位置X
  75. private int _startY; // 鼠标按下位置Y
  76. private int _mouseMoveX; // 鼠标移动位置X
  77. private int _mouseMoveY; // 鼠标移动位置Y
  78. private bool _isMouseDown; // 鼠标按下
  79. private bool _isMouseMove; // 鼠标移动
  80. private Rectangle[] _downRectangles; // 鼠标按下Item范围
  81. private Rectangle[] _moveRectangles; // 鼠标移动Item范围
  82. private bool _isShiftDown = false; // Shift按下
  83. private bool _isCtrlDown = false; // Ctrl按下
  84. #endregion Item移动
  85. #region 打印相关
  86. private bool _isPrintAreaVisible; // 是否标识打印范围
  87. private bool _isPrintBackground; // 是否打印背景
  88. private bool _landscape; // true:横向打印;false:纵向打印
  89. private float _printOffsetX; // 打印横向偏移
  90. private float _printOffsetY; // 打印纵向偏移
  91. //private PrintPreviewDialogEx printPreviewDialogEx;
  92. private int _numberOfCopies = 1; // 打印份数
  93. private int _allocationSkipCount = 0; // 分配打印开头跳过的位置数
  94. private int _allocationNumX = 1; // 分配打印横方向的分配数
  95. private int _allocationNumY = 1; // 分配打印纵方向的分配数
  96. private float _allocationVerticalSpace; // 分配打印横方向的间隔
  97. private float _allocationHorizontalSpace; // 分配打印纵方向的间隔
  98. private float _marginLeft; // 分配打印左侧余白
  99. private float _marginRight; // 分配打印右侧余白
  100. private float _marginTop; // 分配打印上侧余白
  101. private float _marginBottom; // 分配打印下侧余白
  102. private PrintType _printType; // 打印类别
  103. private int _printDataPosition = -1; // 打印数据源位置
  104. private int _printDataTotal = 0; // 打印数据总数
  105. private int _printDataCopies = -1; // 打印数据份数
  106. private int _allocationNum = 0; // 分配打印每页的分配数
  107. private PrintAction _printAction; // 打印种类
  108. private RectangleF _rectangleFGreed; // 打印范围
  109. private float _allOffsetX; // 打印横向偏移
  110. private float _allOffsetY; // 打印纵向偏移
  111. private float _printerOffsetX; // 打印横向偏移
  112. private float _printerOffsetY; // 打印纵向偏移
  113. #endregion 打印相关
  114. #region Item相关
  115. private int _maxItemLeft = 0; // 位置X上限(Pixel)
  116. private int _maxItemTop = 0; // 位置Y上限(Pixel)
  117. private int _maxItemWidth = 0; // 尺寸宽上限(Pixel)
  118. private int _maxItemHeight = 0; // 尺寸高上限(Pixel)
  119. private int _minItemWidth = 0; // 尺寸幅下限(Pixel)
  120. private int _minItemHeight = 0; // 尺寸高下限(Pixel)
  121. private int _pasteCount = 0;
  122. private RectangleF _lastItemRF = RectangleF.Empty;
  123. #endregion Item相关
  124. #region GridItem相关
  125. private int _currentPageNum = 1; // 当前页码
  126. private int _printPageNum = 1; // 打印页的当前页码
  127. private int _totalPageNum = 1; // 总页数
  128. private DataSet _gridDataSet = null; // GridItem利用的数据源
  129. #endregion GridItem相关
  130. #endregion 成员变量
  131. #region 构造函数
  132. /// <summary>
  133. /// 构造函数
  134. /// </summary>
  135. public LayoutBox(LayoutMode layoutMode = InvoiceLayout.LayoutMode.Preview)
  136. {
  137. this._layoutMode = layoutMode;
  138. InitializeComponent();
  139. InitializeLayoutBox();
  140. //printPreviewDialogEx = new PrintPreviewDialogEx();
  141. //this.printPreviewDialogEx.AutoScrollMargin = new System.Drawing.Size(0, 0);
  142. //this.printPreviewDialogEx.AutoScrollMinSize = new System.Drawing.Size(0, 0);
  143. //this.printPreviewDialogEx.ClientSize = new System.Drawing.Size(400, 300);
  144. //this.printPreviewDialogEx.Document = this.printDocument;
  145. //this.printPreviewDialogEx.Enabled = true;
  146. //this.printPreviewDialogEx.Name = "printPreviewDialogEx";
  147. //this.printPreviewDialogEx.Visible = false;
  148. }
  149. #endregion 构造函数
  150. #region 属性
  151. #region Layout
  152. /// <summary>
  153. /// Item选择状态变化中
  154. /// </summary>
  155. public bool ItemSelectionChanging
  156. {
  157. get;
  158. private set;
  159. }
  160. /// <summary>
  161. /// 一个GridItem
  162. /// </summary>
  163. public GridItem OneGridItem
  164. {
  165. get;
  166. private set;
  167. }
  168. /// <summary>
  169. /// 打印纸张
  170. /// </summary>
  171. [Browsable(false)]
  172. public Control PaperArea
  173. {
  174. get
  175. {
  176. return paperArea;
  177. }
  178. }
  179. /// <summary>
  180. /// 打印纸张图形范围
  181. /// </summary>
  182. [Browsable(false)]
  183. internal Microsoft.VisualBasic.PowerPacks.ShapeContainer ItemShapeContainer
  184. {
  185. get
  186. {
  187. return paperArea.ShapeContainer;
  188. }
  189. }
  190. /// <summary>
  191. /// Item图形集合
  192. /// </summary>
  193. [Browsable(false)]
  194. internal Microsoft.VisualBasic.PowerPacks.ShapeCollection ItemShapes
  195. {
  196. get
  197. {
  198. return paperArea.ShapeContainer.Shapes;
  199. }
  200. }
  201. /// <summary>
  202. /// 获得新的ItemID
  203. /// </summary>
  204. [Browsable(false)]
  205. internal int NewItemID
  206. {
  207. get
  208. {
  209. lock (this)
  210. {
  211. return _newItemID++;
  212. }
  213. }
  214. }
  215. /// <summary>
  216. /// 获取或设置数据源
  217. /// </summary>
  218. [Browsable(true)]
  219. [Description(" 获取或设置数据源"), Category("LayoutBox")]
  220. [DefaultValue((string)null), RefreshProperties(RefreshProperties.Repaint), AttributeProvider(typeof(IListSource))]
  221. public object DataSource
  222. {
  223. get
  224. {
  225. return _dataSource;
  226. }
  227. set
  228. {
  229. if (value != _dataSource)
  230. {
  231. _dataSource = value;
  232. if (_layoutMode == LayoutMode.Preview)
  233. {
  234. SetDataSource();
  235. }
  236. }
  237. }
  238. }
  239. /// <summary>
  240. /// 获取或设置被用作模块的基表中的数据源名称
  241. /// </summary>
  242. [Description("获取或设置被用作模块的基表中的数据源名称"), Category("LayoutBox")]
  243. [DefaultValue("")]
  244. public string DataMember
  245. {
  246. get
  247. {
  248. return _dataMember;
  249. }
  250. set
  251. {
  252. if (string.IsNullOrEmpty(value))
  253. {
  254. value = string.Empty;
  255. }
  256. if (!value.Equals(_dataMember))
  257. {
  258. _dataMember = value;
  259. if (_layoutMode == LayoutMode.Preview)
  260. {
  261. SetDataMember();
  262. }
  263. }
  264. }
  265. }
  266. /// <summary>
  267. /// 获取数据绑定源,从外部刷新数据源
  268. /// </summary>
  269. [Browsable(false)]
  270. [Description("获取数据绑定源,从外部刷新数据源"), Category("LayoutBox")]
  271. [DefaultValue((string)null)]
  272. public BindingSource BindingSource
  273. {
  274. get
  275. {
  276. if (_layoutMode == LayoutMode.Preview)
  277. {
  278. if (_dataBinding != null)
  279. {
  280. return _dataBinding.BindingSource;
  281. }
  282. }
  283. return null;
  284. }
  285. }
  286. /// <summary>
  287. /// 获取LayoutBox的全部Item集合
  288. /// </summary>
  289. [Description("获取LayoutBox的全部Item集合"), Category("LayoutBox")]
  290. [Browsable(false)]
  291. public List<LayoutItem> Items
  292. {
  293. get
  294. {
  295. return _items;
  296. }
  297. }
  298. /// <summary>
  299. /// 获取LayoutBox的全部Item集合
  300. /// </summary>
  301. [Description("获取剪贴板的内容"), Category("LayoutBox")]
  302. public DataFormats.Format ClipboardFormat
  303. {
  304. get
  305. {
  306. return GetCipboardFormat();
  307. //return _clipboardFormat;
  308. }
  309. }
  310. /// <summary>
  311. /// 获取LayoutBox的选择状态的Item集合
  312. /// </summary>
  313. [Description("获取LayoutBox的选择状态的Item集合"), Category("LayoutBox")]
  314. [Browsable(false)]
  315. public List<LayoutItem> SelectedItems
  316. {
  317. get
  318. {
  319. return _selectedItems;
  320. }
  321. }
  322. /// <summary>
  323. /// 获取或设置LayoutBox的编辑模式
  324. /// </summary>
  325. [Description("获取或设置LayoutBox的编辑模式"), Category("LayoutBox")]
  326. [DefaultValue(LayoutMode.Preview)]
  327. public LayoutMode LayoutMode
  328. {
  329. get
  330. {
  331. return _layoutMode;
  332. }
  333. set
  334. {
  335. if (_layoutMode != value)
  336. {
  337. if (_layoutMode == LayoutMode.Edit)
  338. {
  339. ClearSelection();
  340. }
  341. _layoutMode = value;
  342. if (_layoutMode == LayoutMode.Edit)
  343. {
  344. //// TODO
  345. //if (this._clipboardFormat == null)
  346. //{
  347. // _clipboardFormat =
  348. // DataFormats.GetFormat(typeof(LayoutItem).FullName
  349. // + "{"
  350. // + Guid.NewGuid().ToString()
  351. // + "}");
  352. //}
  353. tsmiAllSelect.Enabled = true;
  354. tsmiBackgroundImage.Enabled = true;
  355. tsmiPaperSize.Enabled = true;
  356. paperArea.Click += new System.EventHandler(this.paperArea_Click);
  357. paperArea.MouseDown += new System.Windows.Forms.MouseEventHandler(this.paperArea_MouseDown);
  358. paperArea.MouseMove += new System.Windows.Forms.MouseEventHandler(this.paperArea_MouseMove);
  359. paperArea.MouseUp += new System.Windows.Forms.MouseEventHandler(this.paperArea_MouseUp);
  360. // TODO Item范围内移动
  361. paperArea.ShapeContainer.KeyDown += new KeyEventHandler(RectTracker_KeyDown);
  362. paperArea.ShapeContainer.KeyUp += new KeyEventHandler(RectTracker_KeyUp);
  363. paperArea.ShapeContainer.MouseDown += new MouseEventHandler(RectTracker_MouseDown);
  364. paperArea.ShapeContainer.MouseMove += new MouseEventHandler(RectTracker_MouseMove);
  365. paperArea.ShapeContainer.MouseUp += new MouseEventHandler(RectTracker_MouseUp);
  366. paperArea.ShapeContainer.LostFocus += new EventHandler(RectTracker_LostFocus);
  367. }
  368. else
  369. {
  370. tsmiAllSelect.Enabled = false;
  371. tsmiBackgroundImage.Enabled = false;
  372. tsmiPaperSize.Enabled = false;
  373. paperArea.Click -= new System.EventHandler(this.paperArea_Click);
  374. paperArea.MouseDown -= new System.Windows.Forms.MouseEventHandler(this.paperArea_MouseDown);
  375. paperArea.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.paperArea_MouseMove);
  376. paperArea.MouseUp -= new System.Windows.Forms.MouseEventHandler(this.paperArea_MouseUp);
  377. // TODO Item范围内移动
  378. paperArea.ShapeContainer.KeyDown -= new KeyEventHandler(RectTracker_KeyDown);
  379. paperArea.ShapeContainer.KeyUp -= new KeyEventHandler(RectTracker_KeyUp);
  380. paperArea.ShapeContainer.MouseDown -= new MouseEventHandler(RectTracker_MouseDown);
  381. paperArea.ShapeContainer.MouseMove -= new MouseEventHandler(RectTracker_MouseMove);
  382. paperArea.ShapeContainer.MouseUp -= new MouseEventHandler(RectTracker_MouseUp);
  383. paperArea.ShapeContainer.LostFocus -= new EventHandler(RectTracker_LostFocus);
  384. if (_layoutMode == LayoutMode.Preview)
  385. {
  386. SetDataSource();
  387. SetDataMember();
  388. SetPrintDataSource();
  389. SetPrintDataMember();
  390. }
  391. }
  392. }
  393. }
  394. }
  395. /// <summary>
  396. /// 获取或设置LayoutBox的缩放模式
  397. /// </summary>
  398. [Description("获取或设置LayoutBox的缩放模式"), Category("LayoutBox")]
  399. [DefaultValue(ZoomType.Specify)]
  400. public ZoomType ZoomType
  401. {
  402. get
  403. {
  404. return _zoomType;
  405. }
  406. set
  407. {
  408. if (_zoomType != value)
  409. {
  410. _zoomType = value;
  411. if (value != ZoomType.Whole)
  412. {
  413. paperBox.AutoScroll = true;
  414. // 改变纸张大小
  415. ChangePaperSize();
  416. // 设置全部Item的比例
  417. ZoomAllItems();
  418. }
  419. else
  420. {
  421. paperBox.AutoScroll = false;
  422. // 全部Item显示
  423. InitializeZoom();
  424. }
  425. if (ZoomChanged != null)
  426. {
  427. ZoomChanged(this, EventArgs.Empty);
  428. }
  429. }
  430. }
  431. }
  432. /// <summary>
  433. /// 获取或设置LayoutBox的缩放比例(%)
  434. /// </summary>
  435. [Description("获取或设置LayoutBox的缩放比例"), Category("LayoutBox")]
  436. [DefaultValue(100)]
  437. public int Zoom
  438. {
  439. get
  440. {
  441. return _zoom;
  442. }
  443. set
  444. {
  445. if (0 <= value && _zoom != value)
  446. {
  447. _zoom = value;
  448. if (_zoomType == ZoomType.Specify)
  449. {
  450. // 改变纸张大小
  451. ChangePaperSize();
  452. // 设置全部Item的比例
  453. ZoomAllItems();
  454. if (ZoomChanged != null)
  455. {
  456. ZoomChanged(this, EventArgs.Empty);
  457. }
  458. }
  459. else
  460. {
  461. ZoomType = ZoomType.Specify;
  462. }
  463. }
  464. }
  465. }
  466. /// <summary>
  467. /// 获取或设置纸张的高(mm)
  468. /// </summary>
  469. [Description("获取或设置纸张的高"), Category("LayoutBox")]
  470. [DefaultValue(297f)]
  471. public float PaperHeight
  472. {
  473. get
  474. {
  475. return _paperHeight;
  476. }
  477. set
  478. {
  479. if (_layoutMode != LayoutMode.Edit)
  480. {
  481. return;
  482. }
  483. SetPaperAreaSize(_paperWidth, value);
  484. }
  485. }
  486. /// <summary>
  487. /// 获取或设置纸张的宽(mm)
  488. /// </summary>
  489. [Description("获取或设置纸张的宽"), Category("LayoutBox")]
  490. [DefaultValue(210f)]
  491. public float PaperWidth
  492. {
  493. get
  494. {
  495. return _paperWidth;
  496. }
  497. set
  498. {
  499. if (_layoutMode != LayoutMode.Edit)
  500. {
  501. return;
  502. }
  503. SetPaperAreaSize(value, _paperHeight);
  504. }
  505. }
  506. /// <summary>
  507. /// 获取纸张的背景图片名
  508. /// </summary>
  509. [Description("获取或设置纸张的背景图片"), Category("LayoutBox")]
  510. [DefaultValue("")]
  511. public string BackgroundImageName
  512. {
  513. get
  514. {
  515. return _backgroundImageName;
  516. }
  517. }
  518. /// <summary>
  519. /// 获取或设置纸张的背景图片
  520. /// </summary>
  521. [Description("获取或设置纸张的背景图片"), Category("LayoutBox")]
  522. [DefaultValue((string)null)]
  523. public override Image BackgroundImage
  524. {
  525. get
  526. {
  527. return paperArea.BackgroundImage;
  528. }
  529. set
  530. {
  531. paperArea.BackgroundImage = value;
  532. _backgroundImageName = string.Empty;
  533. }
  534. }
  535. /// <summary>
  536. /// 获取LayoutBox的布局是否已经改变
  537. /// </summary>
  538. //[Description("获取LayoutBox的布局是否已经改变"), Category("LayoutBox")]
  539. [Browsable(false)]
  540. public bool Dirty
  541. {
  542. get
  543. {
  544. return _isDirty;
  545. }
  546. internal set
  547. {
  548. SetDitry(value);
  549. }
  550. }
  551. /// <summary>
  552. /// 获取或设置是否加密保存、读取流
  553. /// </summary>
  554. [Description("获取或设置是否加密保存、读取流"), Category("LayoutBox")]
  555. [DefaultValue(true)]
  556. public bool CryptoStream = true;
  557. #endregion Layout
  558. #region 打印相关
  559. /// <summary>
  560. /// 获取或设置打印数据源
  561. /// </summary>
  562. [Browsable(true)]
  563. [Description("获取或设置打印数据源"), Category("打印相关")]
  564. [DefaultValue((string)null), RefreshProperties(RefreshProperties.Repaint), AttributeProvider(typeof(IListSource))]
  565. public object PrintDataSource
  566. {
  567. get
  568. {
  569. return _printDataSource;
  570. }
  571. set
  572. {
  573. if (value != _printDataSource)
  574. {
  575. _printDataSource = value;
  576. if (_layoutMode == LayoutMode.Preview)
  577. {
  578. SetPrintDataSource();
  579. }
  580. }
  581. }
  582. }
  583. /// <summary>
  584. /// 获取或设置打印数据源
  585. /// </summary>
  586. [Description("获取或设置打印数据源"), Category("打印相关")]
  587. [DefaultValue("")]
  588. public string PrintDataMember
  589. {
  590. get
  591. {
  592. return _printDataMember;
  593. }
  594. set
  595. {
  596. if (string.IsNullOrEmpty(value))
  597. {
  598. value = string.Empty;
  599. }
  600. if (!value.Equals(_printDataMember))
  601. {
  602. _printDataMember = value;
  603. if (_layoutMode == LayoutMode.Preview)
  604. {
  605. SetPrintDataMember();
  606. }
  607. }
  608. }
  609. }
  610. /// <summary>
  611. /// 获取或设置是否用绿色虚线标识出打印区域
  612. /// </summary>
  613. [Description("获取或设置是否用绿色虚线标识出打印区域"), Category("打印相关")]
  614. [DefaultValue(true)]
  615. public bool PrintAreaVisible
  616. {
  617. get
  618. {
  619. return _isPrintAreaVisible;
  620. }
  621. set
  622. {
  623. if (_isPrintAreaVisible != value)
  624. {
  625. _isPrintAreaVisible = value;
  626. _isDirty = true;
  627. }
  628. }
  629. }
  630. /// <summary>
  631. /// 获取或设置一个值,该值指示是否打印背景
  632. /// </summary>
  633. [Description("获取或设置一个值,该值指示是否打印背景"), Category("打印相关")]
  634. [DefaultValue(false)]
  635. public bool PrintBackground
  636. {
  637. get
  638. {
  639. return _isPrintBackground;
  640. }
  641. set
  642. {
  643. if (_isPrintBackground != value)
  644. {
  645. _isPrintBackground = value;
  646. _isDirty = true;
  647. }
  648. }
  649. }
  650. /// <summary>
  651. /// 获取或设置一个值,该值指示是否在横向或纵向的纸张打印页面
  652. /// </summary>
  653. [Description("获取或设置一个值,该值指示是否在横向或纵向的纸张时打印页面\r\n打印横向的页面为true;否则为false。默认是由打印机决定。"), Category("打印相关")]
  654. [DefaultValue(false)]
  655. public bool Landscape
  656. {
  657. get
  658. {
  659. return _landscape;
  660. }
  661. set
  662. {
  663. printDocument.DefaultPageSettings.Landscape = value;
  664. _landscape = value;
  665. }
  666. }
  667. /// <summary>
  668. /// 获取或设置打印纸张的大小
  669. /// </summary>
  670. [Description("获取或设置打印纸张的大小"), Category("LayoutBox")]
  671. [AmbientValue(typeof(Size), "0, 0")]
  672. [Browsable(false)]
  673. [DefaultValue((string)null)]
  674. public PaperSize PaperSize
  675. {
  676. get
  677. {
  678. return printDocument.PrinterSettings.DefaultPageSettings.PaperSize;
  679. }
  680. set
  681. {
  682. printDocument.PrinterSettings.DefaultPageSettings.PaperSize = value;
  683. }
  684. }
  685. /// <summary>
  686. /// Defines a reusable object that sends output to a printer, when printing from a Windows Forms application
  687. /// </summary>
  688. [Description("Defines a reusable object that sends output to a printer, when printing from a Windows Forms application"), Category("打印相关")]
  689. [Browsable(false)]
  690. public PrintDocument PrintDocument
  691. {
  692. get
  693. {
  694. return printDocument;
  695. }
  696. }
  697. /// <summary>
  698. /// 获取或设置打印机的配置信息
  699. /// </summary>
  700. [Description("获取或设置打印机的配置信息"), Category("打印相关")]
  701. [Browsable(false)]
  702. [DefaultValue((string)null)]
  703. public PrinterSettings PrinterSettings
  704. {
  705. get
  706. {
  707. return printDocument.PrinterSettings;
  708. }
  709. set
  710. {
  711. printDocument.PrinterSettings = value;
  712. _landscape = printDocument.PrinterSettings.DefaultPageSettings.Landscape;
  713. }
  714. }
  715. /// <summary>
  716. /// 获取或设置水平方向打印偏移(mm)
  717. /// </summary>
  718. [Description("获取或设置水平方向打印偏移"), Category("打印相关")]
  719. [DefaultValue(0f)]
  720. public float PrintOffsetX
  721. {
  722. get
  723. {
  724. return _printOffsetX;
  725. }
  726. set
  727. {
  728. _printOffsetX = value;
  729. }
  730. }
  731. /// <summary>
  732. /// 获取或设置垂直方向打印偏移(mm)
  733. /// </summary>
  734. [Description("获取或设置垂直方向打印偏移"), Category("打印相关")]
  735. [DefaultValue(0f)]
  736. public float PrintOffsetY
  737. {
  738. get
  739. {
  740. return _printOffsetY;
  741. }
  742. set
  743. {
  744. _printOffsetY = value;
  745. }
  746. }
  747. /// <summary>
  748. /// 获取或设置分配打印时,水平方向的分配数
  749. /// </summary>
  750. [Description("获取或设置分配打印时,水平方向的分配数"), Category("打印相关")]
  751. [DefaultValue(1)]
  752. public int AllocationNumX
  753. {
  754. get
  755. {
  756. return _allocationNumX;
  757. }
  758. set
  759. {
  760. if (0 < value)
  761. {
  762. _allocationNumX = value;
  763. }
  764. else
  765. {
  766. _allocationNumX = 1;
  767. }
  768. }
  769. }
  770. /// <summary>
  771. /// 获取或设置分配打印时,垂直方向的分配数
  772. /// </summary>
  773. [Description("获取或设置分配打印时,垂直方向的分配数"), Category("打印相关")]
  774. [DefaultValue(1)]
  775. public int AllocationNumY
  776. {
  777. get
  778. {
  779. return _allocationNumY;
  780. }
  781. set
  782. {
  783. if (0 < value)
  784. {
  785. _allocationNumY = value;
  786. }
  787. else
  788. {
  789. _allocationNumY = 1;
  790. }
  791. }
  792. }
  793. /// <summary>
  794. /// 获取或设置分配打印时,左侧余白(mm)
  795. /// </summary>
  796. [Description("获取或设置分配打印时,左侧余白(mm)"), Category("打印相关")]
  797. [DefaultValue(0f)]
  798. public float MarginLeft
  799. {
  800. get
  801. {
  802. return _marginLeft;
  803. }
  804. set
  805. {
  806. _marginLeft = value;
  807. }
  808. }
  809. /// <summary>
  810. /// 获取或设置分配打印时,右侧余白(mm)
  811. /// </summary>
  812. [Description("获取或设置分配打印时,右侧余白(mm)"), Category("打印相关")]
  813. [DefaultValue(0f)]
  814. public float MarginRight
  815. {
  816. get
  817. {
  818. return _marginRight;
  819. }
  820. set
  821. {
  822. _marginRight = value;
  823. }
  824. }
  825. /// <summary>
  826. /// 获取或设置分配打印时,上侧余白(mm)
  827. /// </summary>
  828. [Description("获取或设置分配打印时,上侧余白(mm)"), Category("打印相关")]
  829. [DefaultValue(0f)]
  830. public float MarginTop
  831. {
  832. get
  833. {
  834. return _marginTop;
  835. }
  836. set
  837. {
  838. _marginTop = value;
  839. }
  840. }
  841. /// <summary>
  842. /// 获取或设置分配打印时,下侧余白(mm)
  843. /// </summary>
  844. [Description("获取或设置分配打印时,下侧余白(mm)"), Category("打印相关")]
  845. [DefaultValue(0f)]
  846. public float MarginBottom
  847. {
  848. get
  849. {
  850. return _marginBottom;
  851. }
  852. set
  853. {
  854. _marginBottom = value;
  855. }
  856. }
  857. /// <summary>
  858. /// 获取或设置分配打印时,上下间隔(mm)
  859. /// </summary>
  860. [Description("获取或设置分配打印时,上下间隔(mm)"), Category("打印相关")]
  861. [DefaultValue(0f)]
  862. public float AllocationVirticalSpace
  863. {
  864. get
  865. {
  866. return _allocationVerticalSpace;
  867. }
  868. set
  869. {
  870. if (value < 0)
  871. {
  872. _allocationVerticalSpace = 0;
  873. }
  874. else
  875. {
  876. _allocationVerticalSpace = value;
  877. }
  878. }
  879. }
  880. /// <summary>
  881. /// 获取或设置分配打印时,左右间隔(mm)
  882. /// </summary>
  883. [Description("获取或设置分配打印时,左右间隔(mm)"), Category("打印相关")]
  884. [DefaultValue(0f)]
  885. public float AllocationHorizontalSpace
  886. {
  887. get
  888. {
  889. return _allocationHorizontalSpace;
  890. }
  891. set
  892. {
  893. if (value < 0)
  894. {
  895. _allocationHorizontalSpace = 0;
  896. }
  897. else
  898. {
  899. _allocationHorizontalSpace = value;
  900. }
  901. }
  902. }
  903. /// <summary>
  904. /// 获取或设置打印份数
  905. /// </summary>
  906. [Description("获取或设置打印份数"), Category("打印相关")]
  907. [DefaultValue(1)]
  908. public int NumberOfCopies
  909. {
  910. get
  911. {
  912. return _numberOfCopies;
  913. }
  914. set
  915. {
  916. if (0 < value)
  917. {
  918. _numberOfCopies = value;
  919. }
  920. else
  921. {
  922. _numberOfCopies = 1;
  923. }
  924. }
  925. }
  926. /// <summary>
  927. /// 获取或设置分配打印时,开头跳过的位置数
  928. /// </summary>
  929. [Description("获取或设置分配打印时,开头跳过的位置数"), Category("打印相关")]
  930. [DefaultValue(0)]
  931. public int AllocationSkipCount
  932. {
  933. get
  934. {
  935. return _allocationSkipCount;
  936. }
  937. set
  938. {
  939. if (0 < value)
  940. {
  941. _allocationSkipCount = value;
  942. }
  943. else
  944. {
  945. _allocationSkipCount = 0;
  946. }
  947. }
  948. }
  949. #endregion 打印相关
  950. #region Item相关
  951. /// <summary>
  952. /// 位置X上限(Pixel)
  953. /// </summary>
  954. [Browsable(false)]
  955. internal int MaxItemLeft
  956. {
  957. get
  958. {
  959. return _maxItemLeft;
  960. }
  961. }
  962. /// <summary>
  963. /// 位置Y上限(Pixel)
  964. /// </summary>
  965. [Browsable(false)]
  966. internal int MaxItemTop
  967. {
  968. get
  969. {
  970. return _maxItemTop;
  971. }
  972. }
  973. /// <summary>
  974. /// 尺寸宽上限(Pixel)
  975. /// </summary>
  976. [Browsable(false)]
  977. internal int MaxItemWidth
  978. {
  979. get
  980. {
  981. return _maxItemWidth;
  982. }
  983. }
  984. /// <summary>
  985. /// 尺寸高上限(Pixel)
  986. /// </summary>
  987. [Browsable(false)]
  988. internal int MaxItemHeight
  989. {
  990. get
  991. {
  992. return _maxItemHeight;
  993. }
  994. }
  995. /// <summary>
  996. ///尺寸宽下限(Pixel)
  997. /// </summary>
  998. [Browsable(false)]
  999. internal int MinItemWidth
  1000. {
  1001. get
  1002. {
  1003. return _minItemWidth;
  1004. }
  1005. }
  1006. /// <summary>
  1007. /// 尺寸高下限(Pixel)
  1008. /// </summary>
  1009. [Browsable(false)]
  1010. internal int MinItemHeight
  1011. {
  1012. get
  1013. {
  1014. return _minItemHeight;
  1015. }
  1016. }
  1017. #endregion Item相关
  1018. #endregion 属性
  1019. #region public函数
  1020. #region 保存、读取相关
  1021. /// <summary>
  1022. /// 从模版中读取Layout
  1023. /// </summary>
  1024. /// <param name="bytes">布局模版</param>
  1025. /// <returns>读取成功true;读取失败false</returns>
  1026. public bool ReadLayout(byte[] bytes)
  1027. {
  1028. MemoryStream memoryStream = null;
  1029. if (bytes != null && 0 < bytes.Length)
  1030. {
  1031. memoryStream = new MemoryStream(bytes);
  1032. memoryStream.Position = 0;
  1033. }
  1034. return ReadLayout(memoryStream);
  1035. }
  1036. /// <summary>
  1037. /// 保存Layout到布局模版
  1038. /// </summary>
  1039. /// <returns>布局模版</returns>
  1040. public byte[] WriteLayout()
  1041. {
  1042. MemoryStream memoryStream = new MemoryStream();
  1043. if (WriteLayout(memoryStream))
  1044. {
  1045. return memoryStream.ToArray();
  1046. }
  1047. return null;
  1048. }
  1049. /// <summary>
  1050. /// 从模版中读取Layout
  1051. /// </summary>
  1052. /// <param name="stream">布局模版</param>
  1053. /// <returns>读取成功true;读取失败false</returns>
  1054. public bool ReadLayout(Stream stream)
  1055. {
  1056. // TODO 异常分析
  1057. int err_level = 0;
  1058. try
  1059. {
  1060. if (stream == null)
  1061. {
  1062. // 清除Layout布局
  1063. ClearLayoutBox();
  1064. //paperArea.Refresh();
  1065. return true;
  1066. }
  1067. err_level = 1;
  1068. // TODO
  1069. if (_clipboardFormat != null)
  1070. {
  1071. this.ClearClipboard();
  1072. //if (Clipboard.ContainsData(_clipboardFormat.Name))
  1073. //{
  1074. // CopyToClipboard(_clipboardFormat.Name, null);
  1075. //}
  1076. //_clipboardFormat =
  1077. // DataFormats.GetFormat(typeof(LayoutItem).FullName
  1078. // + "{"
  1079. // + Guid.NewGuid().ToString()
  1080. // + "}");
  1081. }
  1082. err_level = 2;
  1083. _lastItemRF = RectangleF.Empty;
  1084. _mouseRect = Rectangle.Empty;
  1085. SetDitry(false);
  1086. _selectedItems.Clear();
  1087. err_level = 3;
  1088. foreach (LayoutItem item in _items)
  1089. {
  1090. if (item.Frame != null)
  1091. {
  1092. item.Frame.Dispose();
  1093. item.Frame = null;
  1094. }
  1095. item.Selected = false;
  1096. item.Dispose();
  1097. }
  1098. _items.Clear();
  1099. err_level = 4;
  1100. // 反序列化
  1101. BinaryFormatter formatter = new BinaryFormatter();
  1102. PropertyOfLayoutBox propertyOflayoutBox = null;
  1103. // TODO 解密读取
  1104. if (CryptoStream)
  1105. {
  1106. Rijndael age = Rijndael.Create();
  1107. CryptoStream cStream = new CryptoStream(stream,
  1108. age.CreateDecryptor(KEY, IV),
  1109. CryptoStreamMode.Read);
  1110. propertyOflayoutBox = (PropertyOfLayoutBox)formatter.Deserialize(cStream);
  1111. }
  1112. else
  1113. {
  1114. propertyOflayoutBox = (PropertyOfLayoutBox)formatter.Deserialize(stream);
  1115. }
  1116. err_level = 5;
  1117. _lastItemRF = propertyOflayoutBox.LastItemRF;
  1118. _newItemID = propertyOflayoutBox.NewItemID;
  1119. _paperHeight = propertyOflayoutBox.PaperHeight;
  1120. _paperWidth = propertyOflayoutBox.PaperWidth;
  1121. if (_zoomType != ZoomType.Whole)
  1122. {
  1123. ChangePaperSize();
  1124. }
  1125. else
  1126. {
  1127. InitializeZoom();
  1128. }
  1129. err_level = 6;
  1130. paperArea.BackgroundImage = propertyOflayoutBox.BackgroundImage;
  1131. _backgroundImageName = propertyOflayoutBox.BackgroundImageName;
  1132. _isPrintBackground = propertyOflayoutBox.PrintBackground;
  1133. _isPrintAreaVisible = propertyOflayoutBox.PrintAreaVisible;
  1134. ArrayList items = propertyOflayoutBox.Items;
  1135. if (items != null && 0 < items.Count)
  1136. {
  1137. foreach (LayoutItem item in items)
  1138. {
  1139. item.Init(this, false);
  1140. if (item.ItemType == ItemType.Grid)
  1141. {
  1142. this.OneGridItem = item as GridItem;
  1143. }
  1144. }
  1145. }
  1146. err_level = 7;
  1147. paperArea.Select();
  1148. RefreshItemsBound();
  1149. //paperArea.Refresh();
  1150. return true;
  1151. }
  1152. catch (Exception ex)
  1153. {
  1154. Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
  1155. "PrintBarcode --- " + err_level, ex.ToString(),
  1156. DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
  1157. Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
  1158. throw ex;
  1159. }
  1160. }
  1161. /// <summary>
  1162. /// 保存Layout到布局模版
  1163. /// </summary>
  1164. /// <param name="stream">布局模版</param>
  1165. /// <returns>保存成功true;保存失败false</returns>
  1166. public bool WriteLayout(Stream stream)
  1167. {
  1168. PropertyOfLayoutBox propertyOflayoutBox = new PropertyOfLayoutBox();
  1169. propertyOflayoutBox.LastItemRF = _lastItemRF;
  1170. propertyOflayoutBox.NewItemID = _newItemID;
  1171. propertyOflayoutBox.PaperHeight = _paperHeight;
  1172. propertyOflayoutBox.PaperWidth = _paperWidth;
  1173. propertyOflayoutBox.BackgroundImage = paperArea.BackgroundImage;
  1174. propertyOflayoutBox.BackgroundImageName = _backgroundImageName;
  1175. propertyOflayoutBox.PrintBackground = _isPrintBackground;
  1176. propertyOflayoutBox.PrintAreaVisible = _isPrintAreaVisible;
  1177. propertyOflayoutBox.Items = new ArrayList(_items);
  1178. // 序列化
  1179. BinaryFormatter formatter = new BinaryFormatter();
  1180. // TODO 加密保存
  1181. if (CryptoStream)
  1182. {
  1183. Rijndael age = Rijndael.Create();
  1184. CryptoStream cStream = new CryptoStream(stream,
  1185. age.CreateEncryptor(KEY, IV),
  1186. CryptoStreamMode.Write);
  1187. formatter.Serialize(cStream, propertyOflayoutBox);
  1188. cStream.FlushFinalBlock();
  1189. }
  1190. else
  1191. {
  1192. formatter.Serialize(stream, propertyOflayoutBox);
  1193. }
  1194. return true;
  1195. }
  1196. /// <summary>
  1197. /// 根据指定的ItemType,创建一个新Item对象
  1198. /// </summary>
  1199. /// <param name="itemType">ItemType的枚举值,表示创建的项目类型</param>
  1200. /// <param name="isShowDialog">是否显示属性设置对话框</param>
  1201. /// <returns>创建的新Item对象</returns>
  1202. public LayoutItem NewItem(ItemType itemType, bool isShowDialog)
  1203. {
  1204. if (_layoutMode != LayoutMode.Edit)
  1205. {
  1206. return null;
  1207. }
  1208. LayoutItem item = null;
  1209. switch (itemType)
  1210. {
  1211. case ItemType.Text:
  1212. item = NewTextItem(isShowDialog);
  1213. break;
  1214. case ItemType.Image:
  1215. item = NewImageItem(isShowDialog);
  1216. break;
  1217. case ItemType.Rectangle:
  1218. item = NewRectangleItem(isShowDialog);
  1219. break;
  1220. case ItemType.Ellipse:
  1221. item = NewEllipseItem(isShowDialog);
  1222. break;
  1223. case ItemType.Grid:
  1224. if (OneGridItem != null)
  1225. {
  1226. return null;
  1227. }
  1228. item = NewGridItem(isShowDialog);
  1229. OneGridItem = item as GridItem;
  1230. break;
  1231. case ItemType.PageNum:
  1232. item = NewPageNumItem(isShowDialog);
  1233. break;
  1234. case ItemType.TotalText:
  1235. item = NewTotalTextItem(isShowDialog);
  1236. break;
  1237. case ItemType.Barcode:
  1238. item = NewBarcodeItem(isShowDialog);
  1239. break;
  1240. case ItemType.QRcode:
  1241. item = NewQRcodeItem(isShowDialog);
  1242. break;
  1243. default:
  1244. return null;
  1245. }
  1246. if (item != null)
  1247. {
  1248. _lastItemRF.X = item.Left;
  1249. _lastItemRF.Y = item.Top;
  1250. if (_lastItemRF.Width < item.Width)
  1251. {
  1252. _lastItemRF.Width = item.Width;
  1253. }
  1254. _lastItemRF.Height = item.Height;
  1255. _isDirty = true;
  1256. SetItemChangedArgs(item, ItemChangeType.Added);
  1257. }
  1258. return item;
  1259. }
  1260. #endregion 保存、读取相关
  1261. #region Item操作相关
  1262. /// <summary>
  1263. /// 根据ItemID,获取Item
  1264. /// </summary>
  1265. /// <param name="itemID">ItemID</param>
  1266. /// <returns>Item</returns>
  1267. public LayoutItem GetItem(int itemID)
  1268. {
  1269. foreach (LayoutItem item in _items)
  1270. {
  1271. if (item.ID == itemID)
  1272. {
  1273. return item;
  1274. }
  1275. }
  1276. return null;
  1277. }
  1278. /// <summary>
  1279. /// 根据指定的ItemType,获取Item
  1280. /// </summary>
  1281. /// <param name="itemType">ItemType</param>
  1282. /// <returns>Item</returns>
  1283. public LayoutItem[] GetItems(ItemType itemType)
  1284. {
  1285. //var q = from item in _items where item.ItemType ==itemType select item;
  1286. Predicate<LayoutItem> isPrime = delegate(LayoutItem item)
  1287. {
  1288. return (item.ItemType == itemType) ? true : false;
  1289. };
  1290. return _items.FindAll(isPrime).ToArray();
  1291. }
  1292. /// <summary>
  1293. /// 改变指定Item的选择状态
  1294. /// </summary>
  1295. /// <param name="index">Item在Item集合中的索引</param>
  1296. /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
  1297. public void SelectItem(int index, bool value)
  1298. {
  1299. if (_layoutMode != LayoutMode.Edit)
  1300. {
  1301. return;
  1302. }
  1303. if (index < 0 || index >= Items.Count)
  1304. {
  1305. return;
  1306. }
  1307. LayoutItem item = _items[index];
  1308. SelectItemMultiple(item, value);
  1309. }
  1310. /// <summary>
  1311. /// 改变指定Item的选择状态
  1312. /// </summary>
  1313. /// <param name="indexes">Item在Item集合中的索引</param>
  1314. /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
  1315. public void SelectItems(int[] indexes, bool value)
  1316. {
  1317. if (_layoutMode != LayoutMode.Edit)
  1318. {
  1319. return;
  1320. }
  1321. List<LayoutItem> items = new List<LayoutItem>();
  1322. foreach (int index in indexes)
  1323. {
  1324. if (index < 0 || Items.Count <= index)
  1325. {
  1326. continue;
  1327. }
  1328. items.Add(Items[index]);
  1329. }
  1330. if (0 < items.Count)
  1331. {
  1332. SelectItemsSingle(items.ToArray(), value);
  1333. }
  1334. }
  1335. /// <summary>
  1336. /// 改变指定Item的选择状态
  1337. /// </summary>
  1338. /// <param name="item">Item</param>
  1339. /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
  1340. public void SelectItemSingle(LayoutItem item, bool value)
  1341. {
  1342. if (_layoutMode != LayoutMode.Edit || item == null)
  1343. {
  1344. return;
  1345. }
  1346. bool isChanging = false;
  1347. ItemSelectionChanging = true;
  1348. if (value)
  1349. {
  1350. if (0 < _selectedItems.Count)
  1351. {
  1352. LayoutItem[] selectedItems = _selectedItems.ToArray();
  1353. foreach (LayoutItem selectedItem in selectedItems)
  1354. {
  1355. if (item == null || item.ID == selectedItem.ID)
  1356. {
  1357. continue;
  1358. }
  1359. isChanging = true;
  1360. selectedItem.Selected = false;
  1361. _selectedItems.Remove(selectedItem);
  1362. if (selectedItem.Frame != null)
  1363. {
  1364. selectedItem.Frame.Dispose();
  1365. selectedItem.Frame = null;
  1366. }
  1367. SetItemSelectionChangedArgs(selectedItem, false);
  1368. }
  1369. }
  1370. }
  1371. if (value != item.Selected)
  1372. {
  1373. if (value)
  1374. {
  1375. item.Frame = new ItemFrame(item);
  1376. item.Selected = true;
  1377. _selectedItems.Add(item);
  1378. _selectedItems.Sort(_comparisonItems);
  1379. paperArea.Controls.Add(item.Frame);
  1380. item.Frame.BringToFront();
  1381. }
  1382. else
  1383. {
  1384. item.Selected = false;
  1385. _selectedItems.Remove(item);
  1386. if (item.Frame != null)
  1387. {
  1388. item.Frame.Dispose();
  1389. item.Frame = null;
  1390. }
  1391. }
  1392. isChanging = true;
  1393. SetItemSelectionChangedArgs(item, value);
  1394. }
  1395. ItemSelectionChanging = false;
  1396. if (isChanging)
  1397. {
  1398. if (UserSelectionChanged != null)
  1399. {
  1400. UserSelectionChanged(this, EventArgs.Empty);
  1401. }
  1402. }
  1403. }
  1404. /// <summary>
  1405. /// 改变指定Item的选择状态
  1406. /// </summary>
  1407. /// <param name="items">Item</param>
  1408. /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
  1409. public void SelectItemsSingle(LayoutItem[] items, bool value)
  1410. {
  1411. if (_layoutMode != LayoutMode.Edit)
  1412. {
  1413. return;
  1414. }
  1415. if (items == null || items.Length <= 0)
  1416. {
  1417. return;
  1418. }
  1419. bool isChanging = false;
  1420. ItemSelectionChanging = true;
  1421. if (value)
  1422. {
  1423. if (0 < _selectedItems.Count)
  1424. {
  1425. LayoutItem[] selecteditems = _selectedItems.ToArray();
  1426. foreach (LayoutItem selecteditem in selecteditems)
  1427. {
  1428. if (selecteditem == null)
  1429. {
  1430. continue;
  1431. }
  1432. foreach (LayoutItem item in items)
  1433. {
  1434. if (item == null || item.ID == selecteditem.ID)
  1435. {
  1436. continue;
  1437. }
  1438. isChanging = true;
  1439. selecteditem.Selected = false;
  1440. _selectedItems.Remove(selecteditem);
  1441. if (selecteditem.Frame != null)
  1442. {
  1443. selecteditem.Frame.Dispose();
  1444. selecteditem.Frame = null;
  1445. }
  1446. SetItemSelectionChangedArgs(selecteditem, false);
  1447. }
  1448. }
  1449. }
  1450. }
  1451. foreach (LayoutItem item in items)
  1452. {
  1453. SelectItemMultiple(item, value, ref isChanging);
  1454. }
  1455. ItemSelectionChanging = false;
  1456. if (isChanging)
  1457. {
  1458. if (UserSelectionChanged != null)
  1459. {
  1460. UserSelectionChanged(this, EventArgs.Empty);
  1461. }
  1462. }
  1463. }
  1464. /// <summary>
  1465. /// 改变指定Item的选择状态
  1466. /// </summary>
  1467. /// <param name="item">Item</param>
  1468. /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
  1469. public void SelectItemMultiple(LayoutItem item, bool value)
  1470. {
  1471. bool isChanging = false;
  1472. ItemSelectionChanging = true;
  1473. SelectItemMultiple(item, value, ref isChanging);
  1474. ItemSelectionChanging = false;
  1475. if (isChanging)
  1476. {
  1477. if (UserSelectionChanged != null)
  1478. {
  1479. UserSelectionChanged(this, EventArgs.Empty);
  1480. }
  1481. }
  1482. }
  1483. /// <summary>
  1484. /// 改变指定Item的选择状态
  1485. /// </summary>
  1486. /// <param name="item">Item</param>
  1487. /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
  1488. /// <param name="isChanging">是否改变</param>
  1489. private void SelectItemMultiple(LayoutItem item, bool value, ref bool isChanging)
  1490. {
  1491. if (_layoutMode != LayoutMode.Edit || item == null)
  1492. {
  1493. return;
  1494. }
  1495. if (value != item.Selected)
  1496. {
  1497. if (value)
  1498. {
  1499. item.Frame = new ItemFrame(item);
  1500. item.Selected = true;
  1501. _selectedItems.Add(item);
  1502. _selectedItems.Sort(_comparisonItems);
  1503. paperArea.Controls.Add(item.Frame);
  1504. item.Frame.BringToFront();
  1505. }
  1506. else
  1507. {
  1508. item.Selected = false;
  1509. _selectedItems.Remove(item);
  1510. if (item.Frame != null)
  1511. {
  1512. item.Frame.Dispose();
  1513. item.Frame = null;
  1514. }
  1515. }
  1516. isChanging = true;
  1517. SetItemSelectionChangedArgs(item, value);
  1518. }
  1519. }
  1520. /// <summary>
  1521. /// 改变指定Item的选择状态
  1522. /// </summary>
  1523. /// <param name="items">Item</param>
  1524. /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
  1525. public void SelectItemsMultiple(LayoutItem[] items, bool value)
  1526. {
  1527. if (_layoutMode != LayoutMode.Edit)
  1528. {
  1529. return;
  1530. }
  1531. if (items == null || items.Length <= 0)
  1532. {
  1533. return;
  1534. }
  1535. bool isChanging = false;
  1536. ItemSelectionChanging = true;
  1537. foreach (LayoutItem item in items)
  1538. {
  1539. SelectItemMultiple(item, value, ref isChanging);
  1540. }
  1541. ItemSelectionChanging = false;
  1542. if (isChanging)
  1543. {
  1544. if (UserSelectionChanged != null)
  1545. {
  1546. UserSelectionChanged(this, EventArgs.Empty);
  1547. }
  1548. }
  1549. }
  1550. /// <summary>
  1551. /// 取消所有选择的项目
  1552. /// </summary>
  1553. public void ClearSelection()
  1554. {
  1555. if (_layoutMode != LayoutMode.Edit)
  1556. {
  1557. return;
  1558. }
  1559. bool isChanging = false;
  1560. ItemSelectionChanging = true;
  1561. if (0 < _selectedItems.Count)
  1562. {
  1563. LayoutItem[] selectedItems = _selectedItems.ToArray();
  1564. foreach (LayoutItem selectedItem in selectedItems)
  1565. {
  1566. if (selectedItem == null)
  1567. {
  1568. continue;
  1569. }
  1570. isChanging = true;
  1571. selectedItem.Selected = false;
  1572. _selectedItems.Remove(selectedItem);
  1573. if (selectedItem.Frame != null)
  1574. {
  1575. selectedItem.Frame.Dispose();
  1576. selectedItem.Frame = null;
  1577. }
  1578. SetItemSelectionChangedArgs(selectedItem, false);
  1579. }
  1580. }
  1581. ItemSelectionChanging = false;
  1582. if (isChanging)
  1583. {
  1584. if (UserSelectionChanged != null)
  1585. {
  1586. UserSelectionChanged(this, EventArgs.Empty);
  1587. }
  1588. }
  1589. }
  1590. /// <summary>
  1591. /// 剪切选中的Item到剪切板
  1592. /// </summary>
  1593. public void CutItem()
  1594. {
  1595. if (_layoutMode != LayoutMode.Edit)
  1596. {
  1597. return;
  1598. }
  1599. _pasteCount = 0;
  1600. if (0 < _selectedItems.Count)
  1601. {
  1602. CopyToClipboard(new ArrayList(_selectedItems));
  1603. DeleteItem();
  1604. _isDirty = true;
  1605. }
  1606. }
  1607. /// <summary>
  1608. /// 粘贴Item到LayoutBox
  1609. /// </summary>
  1610. public void PasteItem()
  1611. {
  1612. if (_layoutMode != LayoutMode.Edit)
  1613. {
  1614. return;
  1615. }
  1616. object objectItems = GetFromClipboard();
  1617. if (objectItems == null)
  1618. {
  1619. _pasteCount = 0;
  1620. return;
  1621. }
  1622. ArrayList items = objectItems as ArrayList;
  1623. if (items == null || items.Count <= 0)
  1624. {
  1625. _pasteCount = 0;
  1626. return;
  1627. }
  1628. List<LayoutItem> layoutItems = new List<LayoutItem>();
  1629. _pasteCount++;
  1630. foreach (LayoutItem item in items)
  1631. {
  1632. if (item == null || item is GridItem)
  1633. {
  1634. continue;
  1635. }
  1636. int id = item.ID;
  1637. TextItem textItem = null;
  1638. string dataMember = null;
  1639. string defaultItemName = null;
  1640. string textItemName = null;
  1641. if (item.ItemType == ItemType.Text || item.ItemType == ItemType.TotalText)
  1642. {
  1643. textItem = item as TextItem;
  1644. if (textItem != null)
  1645. {
  1646. dataMember = textItem.DataMember;
  1647. defaultItemName = textItem.DefaultItemName;
  1648. textItemName = textItem.TextItemName;
  1649. }
  1650. }
  1651. item.Init(this, true);
  1652. if (textItem != null)
  1653. {
  1654. textItem.DataMember = dataMember;
  1655. textItem.DefaultItemName = defaultItemName;
  1656. textItem.TextItemName = textItemName;
  1657. }
  1658. //int top = item.ShapeBounds.Top + (item.ShapeBounds.Height + 2) * _pasteCount;
  1659. // 下移60mm
  1660. //int top = item.ShapeBounds.Top + 227;
  1661. ////item.ResetItemLocation(item.ShapeBounds.Left, top);
  1662. //设置图形像素坐标时刷新毫米坐标
  1663. //item.MoveItem(item.ShapeBounds.Left, top);
  1664. // 下移5mm
  1665. item.Top += 5;
  1666. _isDirty = true;
  1667. layoutItems.Add(item);
  1668. if (this.UserAddedItem != null)
  1669. {
  1670. ItemChangedEventArgs e = new ItemChangedEventArgs();
  1671. e.Item = item;
  1672. e.ChangedType = ItemChangeType.Added;
  1673. e.ItemID = item.ID;
  1674. this.UserAddedItem(this, e);
  1675. }
  1676. SetItemChangedArgs(item, ItemChangeType.Added);
  1677. }
  1678. // 粘贴的新Item为选择状态
  1679. if (1 == layoutItems.Count)
  1680. {
  1681. SelectItemSingle(layoutItems[0], true);
  1682. }
  1683. else if (1 < layoutItems.Count)
  1684. {
  1685. ClearSelection();
  1686. SelectItemsMultiple(layoutItems.ToArray(), true);
  1687. }
  1688. }
  1689. /// <summary>
  1690. /// 复制选中的Item到剪切板
  1691. /// </summary>
  1692. public void CopyItem()
  1693. {
  1694. if (_layoutMode != LayoutMode.Edit)
  1695. {
  1696. return;
  1697. }
  1698. _pasteCount = 0;
  1699. if (0 < _selectedItems.Count)
  1700. {
  1701. CopyToClipboard(new ArrayList(_selectedItems));
  1702. }
  1703. }
  1704. /// <summary>
  1705. /// 删除选中的Item
  1706. /// </summary>
  1707. public void DeleteItem()
  1708. {
  1709. if (_layoutMode != LayoutMode.Edit)
  1710. {
  1711. return;
  1712. }
  1713. if (0 < _selectedItems.Count)
  1714. {
  1715. LayoutItem[] items = _selectedItems.ToArray();
  1716. foreach (LayoutItem item in items)
  1717. {
  1718. ItemChangedEventArgs e = new ItemChangedEventArgs();
  1719. e.Item = item;
  1720. e.ChangedType = ItemChangeType.Deleted;
  1721. e.ItemID = item.ID;
  1722. DeletetItem(item);
  1723. if (this.UserDeletedItem != null)
  1724. {
  1725. this.UserDeletedItem(this, e);
  1726. }
  1727. }
  1728. }
  1729. }
  1730. /// <summary>
  1731. /// 根据ItemID删除Item
  1732. /// </summary>
  1733. /// <param name="itemID">ItemID</param>
  1734. public void DeletetItem(int itemID)
  1735. {
  1736. if (_layoutMode != LayoutMode.Edit)
  1737. {
  1738. return;
  1739. }
  1740. LayoutItem item = GetItem(itemID);
  1741. DeletetItem(item);
  1742. }
  1743. /// <summary>
  1744. /// 删除指定的Item
  1745. /// </summary>
  1746. /// <param name="item">item</param>
  1747. public void DeletetItem(LayoutItem item)
  1748. {
  1749. if (_layoutMode != LayoutMode.Edit || item == null)
  1750. {
  1751. return;
  1752. }
  1753. if (item.Selected)
  1754. {
  1755. item.Selected = false;
  1756. _selectedItems.Remove(item);
  1757. if (item.Frame != null)
  1758. {
  1759. item.Frame.Dispose();
  1760. item.Frame = null;
  1761. }
  1762. SetItemSelectionChangedArgs(item, false);
  1763. }
  1764. _items.Remove(item);
  1765. item.Dispose();
  1766. _isDirty = true;
  1767. SetItemChangedArgs(item, ItemChangeType.Deleted);
  1768. }
  1769. /// <summary>
  1770. /// 重绘Item的内容
  1771. /// </summary>
  1772. public void RefreshItems()
  1773. {
  1774. if (_layoutMode == LayoutMode.Preview)
  1775. {
  1776. RefreshItemsCurrentPositionValue();
  1777. }
  1778. }
  1779. /// <summary>
  1780. /// 对齐选定的Item
  1781. /// </summary>
  1782. /// <param name="arrangeType">对齐方式</param>
  1783. public void ArrangeItem(ArrangeType arrangeType)
  1784. {
  1785. if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
  1786. {
  1787. return;
  1788. }
  1789. switch (arrangeType)
  1790. {
  1791. case ArrangeType.Left:
  1792. ArrangeItemsLeft();
  1793. break;
  1794. case ArrangeType.Right:
  1795. ArrangeItemsRight();
  1796. break;
  1797. case ArrangeType.HorizontalCenter:
  1798. ArrangeItemsHorizontalCenter();
  1799. break;
  1800. case ArrangeType.Top:
  1801. ArrangeItemsTop();
  1802. break;
  1803. case ArrangeType.Bottom:
  1804. ArrangeItemsBottom();
  1805. break;
  1806. case ArrangeType.VerticalCenter:
  1807. ArrangeItemsVerticalCenter();
  1808. break;
  1809. //case ArrangeType.LeftRight:
  1810. // ArrangeItemsLeftRight();
  1811. // break;
  1812. //case ArrangeType.TopBottom:
  1813. // ArrangeItemsTopBottom();
  1814. // break;
  1815. default:
  1816. break;
  1817. }
  1818. }
  1819. /// <summary>
  1820. /// 所有选中的Item的顶部对齐
  1821. /// </summary>
  1822. public void ArrangeItemsTop()
  1823. {
  1824. if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
  1825. {
  1826. return;
  1827. }
  1828. float minTop = paperArea.Height;
  1829. foreach (LayoutItem item in _selectedItems)
  1830. {
  1831. minTop = Math.Min(item.Top, minTop);
  1832. }
  1833. foreach (LayoutItem item in _selectedItems)
  1834. {
  1835. item.Top = minTop;
  1836. }
  1837. _isDirty = true;
  1838. }
  1839. /// <summary>
  1840. /// 所有选中的Item的底部对齐
  1841. /// </summary>
  1842. public void ArrangeItemsBottom()
  1843. {
  1844. if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
  1845. {
  1846. return;
  1847. }
  1848. float maxBottom = 0;
  1849. foreach (LayoutItem item in _selectedItems)
  1850. {
  1851. maxBottom = Math.Max(item.Bottom, maxBottom);
  1852. }
  1853. foreach (LayoutItem item in _selectedItems)
  1854. {
  1855. item.Top = maxBottom - item.Height;
  1856. }
  1857. _isDirty = true;
  1858. }
  1859. /// <summary>
  1860. /// 所有选中的Item的左侧对齐
  1861. /// </summary>
  1862. public void ArrangeItemsLeft()
  1863. {
  1864. if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
  1865. {
  1866. return;
  1867. }
  1868. float minLeft = paperArea.Width;
  1869. foreach (LayoutItem item in _selectedItems)
  1870. {
  1871. minLeft = Math.Min(item.Left, minLeft);
  1872. }
  1873. foreach (LayoutItem item in _selectedItems)
  1874. {
  1875. item.Left = minLeft;
  1876. }
  1877. _isDirty = true;
  1878. }
  1879. /// <summary>
  1880. /// 所有选中的Item的右侧对齐
  1881. /// </summary>
  1882. public void ArrangeItemsRight()
  1883. {
  1884. if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
  1885. {
  1886. return;
  1887. }
  1888. float maxRight = 0;
  1889. foreach (LayoutItem item in _selectedItems)
  1890. {
  1891. maxRight = Math.Max(item.Right, maxRight);
  1892. }
  1893. foreach (LayoutItem item in _selectedItems)
  1894. {
  1895. item.Left = maxRight - item.Width;
  1896. }
  1897. _isDirty = true;
  1898. }
  1899. /// <summary>
  1900. /// 所有选中的Item的垂直方向中心对齐
  1901. /// </summary>
  1902. public void ArrangeItemsVerticalCenter()
  1903. {
  1904. if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
  1905. {
  1906. return;
  1907. }
  1908. float minTop = paperArea.Height;
  1909. float maxBottom = 0;
  1910. foreach (LayoutItem item in _selectedItems)
  1911. {
  1912. minTop = Math.Min(item.Top, minTop);
  1913. maxBottom = Math.Max(item.Bottom, maxBottom);
  1914. }
  1915. float center = (minTop + maxBottom) / 2;
  1916. foreach (LayoutItem item in _selectedItems)
  1917. {
  1918. item.Top = center - item.Height / 2;
  1919. }
  1920. _isDirty = true;
  1921. }
  1922. /// <summary>
  1923. /// 所有选中的Item的水平方向中心对齐
  1924. /// </summary>
  1925. public void ArrangeItemsHorizontalCenter()
  1926. {
  1927. if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
  1928. {
  1929. return;
  1930. }
  1931. float minLeft = paperArea.Width;
  1932. float maxRight = 0;
  1933. foreach (LayoutItem item in _selectedItems)
  1934. {
  1935. minLeft = Math.Min(item.Left, minLeft);
  1936. maxRight = Math.Max(item.Right, maxRight);
  1937. }
  1938. float center = (minLeft + maxRight) / 2;
  1939. foreach (LayoutItem item in _selectedItems)
  1940. {
  1941. item.Left = center - item.Width / 2;
  1942. }
  1943. _isDirty = true;
  1944. }
  1945. /// <summary>
  1946. /// 更改所选Item(Z顺序)的顺序
  1947. /// </summary>
  1948. /// <param name="position">指定的位置MovingPosition的枚举值,Z-顺序</param>
  1949. public void ChangeItemOrder(MovingPosition position)
  1950. {
  1951. if (_layoutMode != LayoutMode.Edit || 0 == _selectedItems.Count)
  1952. {
  1953. return;
  1954. }
  1955. switch (position)
  1956. {
  1957. case MovingPosition.Foremost:
  1958. ChangeItemsOrderForemost();
  1959. break;
  1960. case MovingPosition.Aftermost:
  1961. ChangeItemsOrderAftermost();
  1962. break;
  1963. case MovingPosition.Front:
  1964. ChangeItemsOrderFront();
  1965. break;
  1966. case MovingPosition.Back:
  1967. ChangeItemsOrderBack();
  1968. break;
  1969. default:
  1970. break;
  1971. }
  1972. }
  1973. /// <summary>
  1974. /// 将选定的Item移动到最前面
  1975. /// </summary>
  1976. public void ChangeItemsOrderForemost()
  1977. {
  1978. if (_layoutMode != LayoutMode.Edit)
  1979. {
  1980. return;
  1981. }
  1982. int count = _selectedItems.Count;
  1983. if (0 < count)
  1984. {
  1985. foreach (LayoutItem item in _selectedItems)
  1986. {
  1987. paperArea.ShapeContainer.Shapes.SetChildIndex(item.Shape, 0);
  1988. }
  1989. paperArea.ShapeContainer.Refresh();
  1990. _items.Sort(_comparisonItems);
  1991. _isDirty = true;
  1992. }
  1993. }
  1994. /// <summary>
  1995. /// 将选定的Item上移一层
  1996. /// </summary>
  1997. public void ChangeItemsOrderFront()
  1998. {
  1999. if (_layoutMode != LayoutMode.Edit)
  2000. {
  2001. return;
  2002. }
  2003. int count = _selectedItems.Count;
  2004. if (0 < count)
  2005. {
  2006. int childIndex = paperArea.ShapeContainer.Shapes.
  2007. GetChildIndex(_selectedItems[count - 1].Shape) - 1;
  2008. childIndex = (childIndex < 0) ? 0 : childIndex;
  2009. foreach (LayoutItem item in _selectedItems)
  2010. {
  2011. paperArea.ShapeContainer.Shapes.SetChildIndex(item.Shape, childIndex);
  2012. }
  2013. paperArea.ShapeContainer.Refresh();
  2014. _items.Sort(_comparisonItems);
  2015. _isDirty = true;
  2016. }
  2017. }
  2018. /// <summary>
  2019. /// 将选定的Item移动到最后面
  2020. /// </summary>
  2021. public void ChangeItemsOrderBack()
  2022. {
  2023. if (_layoutMode != LayoutMode.Edit)
  2024. {
  2025. return;
  2026. }
  2027. int count = _selectedItems.Count;
  2028. if (0 < count)
  2029. {
  2030. int childIndex = paperArea.ShapeContainer.Shapes.
  2031. GetChildIndex(_selectedItems[0].Shape) + 1;
  2032. for (int i = count - 1; 0 <= i; i--)
  2033. {
  2034. paperArea.ShapeContainer.Shapes.SetChildIndex(_selectedItems[i].Shape, childIndex);
  2035. }
  2036. paperArea.ShapeContainer.Refresh();
  2037. _items.Sort(_comparisonItems);
  2038. _isDirty = true;
  2039. }
  2040. }
  2041. /// <summary>
  2042. /// 将选定的Item下移一层
  2043. /// </summary>
  2044. public void ChangeItemsOrderAftermost()
  2045. {
  2046. if (_layoutMode != LayoutMode.Edit)
  2047. {
  2048. return;
  2049. }
  2050. int count = _selectedItems.Count;
  2051. if (0 < count)
  2052. {
  2053. for (int i = count - 1; 0 <= i; i--)
  2054. {
  2055. paperArea.ShapeContainer.Shapes.SetChildIndex(_selectedItems[i].Shape, int.MaxValue);
  2056. }
  2057. paperArea.ShapeContainer.Refresh();
  2058. _items.Sort(_comparisonItems);
  2059. _isDirty = true;
  2060. }
  2061. }
  2062. /// <summary>
  2063. /// 将LayoutBox的Dirty状态修改成false
  2064. /// 接受之前所有的改变
  2065. /// </summary>
  2066. public void AcceptChange()
  2067. {
  2068. this._isDirty = false;
  2069. }
  2070. #endregion Item操作相关
  2071. #region 设置对话框
  2072. /// <summary>
  2073. /// 显示背景设置的对话框
  2074. /// </summary>
  2075. /// <returns>
  2076. /// DialogResult.OK:设置成功<br/>
  2077. /// DialogResult.Cancel:取消设置<br/>
  2078. /// DialogResult.None:LayoutBox不是编辑模式
  2079. /// </returns>
  2080. public DialogResult ShowBackgroundSettingDialog()
  2081. {
  2082. if (_layoutMode != LayoutMode.Edit)
  2083. {
  2084. return DialogResult.None;
  2085. }
  2086. using (BackgroundSetting backgroundSetting = new BackgroundSetting(this))
  2087. {
  2088. DialogResult result = backgroundSetting.ShowDialog();
  2089. if (result == DialogResult.OK)
  2090. {
  2091. _backgroundImageName = backgroundSetting.BackgroundImageName;
  2092. paperArea.BackgroundImage = backgroundSetting.BackgroundImage;
  2093. _isDirty = true;
  2094. }
  2095. return result;
  2096. }
  2097. }
  2098. /// <summary>
  2099. /// 显示纸张设置的对话框
  2100. /// </summary>
  2101. /// <returns>
  2102. /// DialogResult.OK:设置成功<br/>
  2103. /// DialogResult.Cancel:取消设置<br/>
  2104. /// DialogResult.None:LayoutBox不是编辑模式
  2105. /// </returns>
  2106. public DialogResult ShowPaperPropertyDialog()
  2107. {
  2108. if (_layoutMode != LayoutMode.Edit)
  2109. {
  2110. return DialogResult.None;
  2111. }
  2112. using (PaperAreaSetting paperSizeSetting = new PaperAreaSetting())
  2113. {
  2114. paperSizeSetting.PaperWidth = _paperWidth;
  2115. paperSizeSetting.PaperHeight = _paperHeight;
  2116. DialogResult result = paperSizeSetting.ShowDialog();
  2117. if (result == DialogResult.OK)
  2118. {
  2119. SetPaperAreaSize(paperSizeSetting.PaperWidth,
  2120. paperSizeSetting.PaperHeight);
  2121. }
  2122. return result;
  2123. }
  2124. }
  2125. /// <summary>
  2126. /// 显示选中Item的属性设置画面
  2127. /// </summary>
  2128. /// <returns>
  2129. /// DialogResult.OK:选中的Item有效,设置成功<br/>
  2130. /// DialogResult.Cancel:选中的Item有效,取消设置<br/>
  2131. /// DialogResult.None:LayoutBox不是编辑模式,或选中的Item不是一个
  2132. /// </returns>
  2133. public DialogResult ShowItemPropertyDialog()
  2134. {
  2135. if (_layoutMode != LayoutMode.Edit || _selectedItems.Count != 1)
  2136. {
  2137. return DialogResult.None;
  2138. }
  2139. LayoutItem item = _selectedItems[0];
  2140. if (item != null)
  2141. {
  2142. return item.ShowItemPropertyDialog();
  2143. }
  2144. return DialogResult.None;
  2145. }
  2146. #endregion 设置对话框
  2147. #region 打印相关
  2148. /// <summary>
  2149. /// 使用打印机已设置的信息进行打印测试
  2150. /// </summary>
  2151. public bool PrintTest()
  2152. {
  2153. if (_layoutMode != LayoutMode.Edit)
  2154. {
  2155. return false;
  2156. }
  2157. _printType = PrintType.PrintTest;
  2158. if (CheckPrintSetting())
  2159. {
  2160. DialogResult result = printDialog.ShowDialog(this);
  2161. if (result == DialogResult.OK)
  2162. {
  2163. printDocument.PrinterSettings.PrinterName = printDialog.PrinterSettings.PrinterName;
  2164. if (printDocument.PrinterSettings.IsValid)
  2165. {
  2166. printDocument.Print();
  2167. return true;
  2168. }
  2169. else
  2170. {
  2171. MessageBox.Show(string.Format("打印机名({0}/{1})不正确。"
  2172. , printDocument.PrinterSettings.PrinterName
  2173. , printDialog.PrinterSettings.PrinterName),
  2174. "打印失败",
  2175. MessageBoxButtons.OK,
  2176. MessageBoxIcon.Error,
  2177. MessageBoxDefaultButton.Button1);
  2178. return false;
  2179. }
  2180. }
  2181. }
  2182. return false;
  2183. }
  2184. /// <summary>
  2185. /// 使用打印机已设置的信息进行打印
  2186. /// </summary>
  2187. public bool Print(bool checkPrintSetting = true)
  2188. {
  2189. //#if DEBUG
  2190. // Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
  2191. // "Print",
  2192. // "begin",
  2193. // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
  2194. // Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
  2195. //#endif
  2196. _printType = PrintType.Print;
  2197. if (!checkPrintSetting || CheckPrintSetting())
  2198. {
  2199. printDocument.Print();
  2200. //#if DEBUG
  2201. // Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
  2202. // "Print",
  2203. // "end",
  2204. // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
  2205. // Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
  2206. //#endif
  2207. return true;
  2208. }
  2209. return false;
  2210. }
  2211. /// <summary>
  2212. /// 使用打印机已设置的信息进行分配打印
  2213. /// </summary>
  2214. public bool AllocationPrint()
  2215. {
  2216. _printType = PrintType.AllocationPrint;
  2217. if (CheckPrintSetting())
  2218. {
  2219. printDocument.Print();
  2220. return true;
  2221. }
  2222. return false;
  2223. }
  2224. /// <summary>
  2225. /// 使用打印机已设置的信息进行打印预览
  2226. /// </summary>
  2227. public bool PrintPreview()
  2228. {
  2229. _printType = PrintType.PrintPreview;
  2230. if (CheckPrintSetting())
  2231. {
  2232. //printPreviewDialogEx.WindowState = FormWindowState.Maximized;
  2233. //printPreviewDialogEx.PrintPreviewControl.Columns = 1;
  2234. //printPreviewDialogEx.PrintPreviewControl.Rows = 1;
  2235. //printPreviewDialogEx.PrintPreviewControl.StartPage = 0;
  2236. //printPreviewDialogEx.ShowDialog();
  2237. printPreviewDialog1.WindowState = FormWindowState.Maximized;
  2238. printPreviewDialog1.PrintPreviewControl.Columns = 1;
  2239. printPreviewDialog1.PrintPreviewControl.Rows = 1;
  2240. printPreviewDialog1.PrintPreviewControl.StartPage = 0;
  2241. printPreviewDialog1.ShowDialog();
  2242. return true;
  2243. }
  2244. return false;
  2245. }
  2246. /// <summary>
  2247. /// 使用打印机已设置的信息进行分配打印预览
  2248. /// </summary>
  2249. public bool AllocationPrintPreview()
  2250. {
  2251. _printType = PrintType.AllocationPrintPreview;
  2252. if (CheckPrintSetting())
  2253. {
  2254. //printPreviewDialogEx.WindowState = FormWindowState.Maximized;
  2255. //printPreviewDialogEx.PrintPreviewControl.Columns = 1;
  2256. //printPreviewDialogEx.PrintPreviewControl.Rows = 1;
  2257. //printPreviewDialogEx.PrintPreviewControl.StartPage = 0;
  2258. //printPreviewDialogEx.ShowDialog();
  2259. printPreviewDialog1.WindowState = FormWindowState.Maximized;
  2260. printPreviewDialog1.PrintPreviewControl.Columns = 1;
  2261. printPreviewDialog1.PrintPreviewControl.Rows = 1;
  2262. printPreviewDialog1.PrintPreviewControl.StartPage = 0;
  2263. printPreviewDialog1.ShowDialog();
  2264. return true;
  2265. }
  2266. return false;
  2267. }
  2268. #endregion 打印相关
  2269. #endregion public函数
  2270. #region private函数
  2271. #region 保存、读取相关
  2272. /// <summary>
  2273. /// 创建一个新条码文本Item
  2274. /// </summary>
  2275. /// <param name="isShowDialog">是否显示属性设置对话框</param>
  2276. /// <returns>创建的新Item对象</returns>
  2277. private LayoutItem NewQRcodeItem(bool isShowDialog)
  2278. {
  2279. TextItem textItem = new TextItem(null);
  2280. textItem.IsNew = true;
  2281. PointF newItemLocation = GetNewItemLocation(LayoutCommon.DefaultTextItemSizeWidth,
  2282. LayoutCommon.DefaultTextItemSizeHeight);
  2283. if (isShowDialog)
  2284. {
  2285. using (TextItemSetting itemPropertySetting = new TextItemSetting())
  2286. {
  2287. itemPropertySetting.IsNew = true;
  2288. itemPropertySetting.IsDataBinding = false;
  2289. itemPropertySetting.TextPreview = "1234567890";
  2290. itemPropertySetting.TextWidth = 25;
  2291. itemPropertySetting.TextHeight = 25;
  2292. itemPropertySetting.TextFont = textItem.Font;
  2293. itemPropertySetting.TextLocationX = newItemLocation.X;
  2294. itemPropertySetting.TextLocationY = newItemLocation.Y;
  2295. itemPropertySetting.ItemStyle = ItemStyle.QRCode;
  2296. itemPropertySetting.ItemName = LayoutBox.ITEMNAME_FIX + _newItemID;
  2297. DialogResult result = itemPropertySetting.ShowDialog();
  2298. if (result == DialogResult.OK)
  2299. {
  2300. textItem.Init(this, true);
  2301. textItem.IsDataBinding = false;
  2302. textItem.Left = itemPropertySetting.TextLocationX;
  2303. textItem.Top = itemPropertySetting.TextLocationY;
  2304. textItem.Width = itemPropertySetting.TextWidth;
  2305. textItem.Height = itemPropertySetting.TextHeight;
  2306. textItem.LineSpace = itemPropertySetting.LineSpace;
  2307. textItem.CharacterSpace = itemPropertySetting.CharacterSpace;
  2308. textItem.CharacterCount = itemPropertySetting.CharacterCount;
  2309. textItem.Font = itemPropertySetting.TextFont;
  2310. textItem.Wrap = itemPropertySetting.Wrap;
  2311. textItem.TextColor = itemPropertySetting.TextColor;
  2312. textItem.TextAlign = itemPropertySetting.TextAlign;
  2313. textItem.TextAlignVertical = itemPropertySetting.TextAlignVertical;
  2314. textItem.DisplayValue = itemPropertySetting.TextPreview;
  2315. textItem.ItemStyle = itemPropertySetting.ItemStyle;
  2316. textItem.TextItemName = itemPropertySetting.ItemName;
  2317. textItem.Format = itemPropertySetting.TextFormat;
  2318. textItem.Culture = itemPropertySetting.Culture;
  2319. textItem.DefaultValue = itemPropertySetting.DefaultValue;
  2320. textItem.IsNew = false;
  2321. textItem.PicColumn = false;
  2322. textItem.PicMargin = itemPropertySetting.chkM.Checked;
  2323. textItem.FixedRatio = itemPropertySetting.chkFixedRatio.Checked;
  2324. textItem.SetDrawProperty();
  2325. return textItem;
  2326. }
  2327. else
  2328. {
  2329. return null;
  2330. }
  2331. }
  2332. }
  2333. else
  2334. {
  2335. textItem.Init(this, true);
  2336. textItem.Left = newItemLocation.X;
  2337. textItem.Top = newItemLocation.Y;
  2338. textItem.ItemStyle = ItemStyle.QRCode;
  2339. textItem.Width = 25;
  2340. textItem.Height = 25;
  2341. textItem.DisplayValue = "1234567890";
  2342. textItem.IsNew = false;
  2343. textItem.PicColumn = false;
  2344. textItem.PicMargin = false;
  2345. textItem.FixedRatio = false;
  2346. textItem.SetDrawProperty();
  2347. return textItem;
  2348. }
  2349. }
  2350. /// <summary>
  2351. /// 创建一个新条码文本Item
  2352. /// </summary>
  2353. /// <param name="isShowDialog">是否显示属性设置对话框</param>
  2354. /// <returns>创建的新Item对象</returns>
  2355. private LayoutItem NewBarcodeItem(bool isShowDialog)
  2356. {
  2357. TextItem textItem = new TextItem(null);
  2358. textItem.IsNew = true;
  2359. PointF newItemLocation = GetNewItemLocation(LayoutCommon.DefaultTextItemSizeWidth,
  2360. LayoutCommon.DefaultTextItemSizeHeight);
  2361. if (isShowDialog)
  2362. {
  2363. using (TextItemSetting itemPropertySetting = new TextItemSetting())
  2364. {
  2365. itemPropertySetting.IsNew = true;
  2366. itemPropertySetting.IsDataBinding = false;
  2367. itemPropertySetting.TextPreview = "12345678901";
  2368. itemPropertySetting.TextWidth = 50;
  2369. itemPropertySetting.TextHeight = 20;
  2370. itemPropertySetting.TextFont = textItem.Font;
  2371. itemPropertySetting.TextLocationX = newItemLocation.X;
  2372. itemPropertySetting.TextLocationY = newItemLocation.Y;
  2373. itemPropertySetting.ItemStyle = ItemStyle.Barcode;
  2374. itemPropertySetting.ItemName = LayoutBox.ITEMNAME_FIX + _newItemID;
  2375. DialogResult result = itemPropertySetting.ShowDialog();
  2376. if (result == DialogResult.OK)
  2377. {
  2378. textItem.Init(this, true);
  2379. textItem.IsDataBinding = false;
  2380. textItem.Left = itemPropertySetting.TextLocationX;
  2381. textItem.Top = itemPropertySetting.TextLocationY;
  2382. textItem.Width = itemPropertySetting.TextWidth;
  2383. textItem.Height = itemPropertySetting.TextHeight;
  2384. textItem.LineSpace = itemPropertySetting.LineSpace;
  2385. textItem.CharacterSpace = itemPropertySetting.CharacterSpace;
  2386. textItem.CharacterCount = itemPropertySetting.CharacterCount;
  2387. textItem.Font = itemPropertySetting.TextFont;
  2388. textItem.Wrap = itemPropertySetting.Wrap;
  2389. textItem.TextColor = itemPropertySetting.TextColor;
  2390. textItem.TextAlign = itemPropertySetting.TextAlign;
  2391. textItem.TextAlignVertical = itemPropertySetting.TextAlignVertical;
  2392. textItem.DisplayValue = itemPropertySetting.TextPreview;
  2393. textItem.ItemStyle = itemPropertySetting.ItemStyle;
  2394. textItem.TextItemName = itemPropertySetting.ItemName;
  2395. textItem.Format = itemPropertySetting.TextFormat;
  2396. textItem.Culture = itemPropertySetting.Culture;
  2397. textItem.DefaultValue = itemPropertySetting.DefaultValue;
  2398. textItem.IsNew = false;
  2399. textItem.PicColumn = false;
  2400. textItem.PicMargin = itemPropertySetting.chkM.Checked;
  2401. textItem.FixedRatio = itemPropertySetting.chkFixedRatio.Checked;
  2402. textItem.SetDrawProperty();
  2403. return textItem;
  2404. }
  2405. else
  2406. {
  2407. return null;
  2408. }
  2409. }
  2410. }
  2411. else
  2412. {
  2413. textItem.Init(this, true);
  2414. textItem.Left = newItemLocation.X;
  2415. textItem.Top = newItemLocation.Y;
  2416. textItem.ItemStyle = ItemStyle.Barcode;
  2417. textItem.Width = 50;
  2418. textItem.Height = 20;
  2419. textItem.DisplayValue = "12345678901";
  2420. textItem.IsNew = false;
  2421. textItem.PicColumn = false;
  2422. textItem.PicMargin = false;
  2423. textItem.FixedRatio = false;
  2424. textItem.SetDrawProperty();
  2425. return textItem;
  2426. }
  2427. }
  2428. /// <summary>
  2429. /// 创建一个新文本Item
  2430. /// </summary>
  2431. /// <param name="isShowDialog">是否显示属性设置对话框</param>
  2432. /// <returns>创建的新Item对象</returns>
  2433. private LayoutItem NewTextItem(bool isShowDialog)
  2434. {
  2435. TextItem textItem = new TextItem(null);
  2436. textItem.IsNew = true;
  2437. PointF newItemLocation = GetNewItemLocation(LayoutCommon.DefaultTextItemSizeWidth,
  2438. LayoutCommon.DefaultTextItemSizeHeight);
  2439. if (isShowDialog)
  2440. {
  2441. using (TextItemSetting itemPropertySetting = new TextItemSetting())
  2442. {
  2443. itemPropertySetting.IsNew = true;
  2444. itemPropertySetting.IsDataBinding = false;
  2445. itemPropertySetting.TextFont = textItem.Font;
  2446. itemPropertySetting.TextLocationX = newItemLocation.X;
  2447. itemPropertySetting.TextLocationY = newItemLocation.Y;
  2448. itemPropertySetting.ItemStyle = ItemStyle.Other;
  2449. itemPropertySetting.ItemName = LayoutBox.ITEMNAME_FIX + _newItemID;
  2450. DialogResult result = itemPropertySetting.ShowDialog();
  2451. if (result == DialogResult.OK)
  2452. {
  2453. textItem.Init(this, true);
  2454. textItem.IsDataBinding = false;
  2455. textItem.Left = itemPropertySetting.TextLocationX;
  2456. textItem.Top = itemPropertySetting.TextLocationY;
  2457. textItem.Width = itemPropertySetting.TextWidth;
  2458. textItem.Height = itemPropertySetting.TextHeight;
  2459. textItem.LineSpace = itemPropertySetting.LineSpace;
  2460. textItem.CharacterSpace = itemPropertySetting.CharacterSpace;
  2461. textItem.CharacterCount = itemPropertySetting.CharacterCount;
  2462. textItem.Font = itemPropertySetting.TextFont;
  2463. textItem.Wrap = itemPropertySetting.Wrap;
  2464. textItem.TextColor = itemPropertySetting.TextColor;
  2465. textItem.TextAlign = itemPropertySetting.TextAlign;
  2466. textItem.TextAlignVertical = itemPropertySetting.TextAlignVertical;
  2467. textItem.DisplayValue = itemPropertySetting.TextPreview;
  2468. textItem.ItemStyle = itemPropertySetting.ItemStyle;
  2469. textItem.TextItemName = itemPropertySetting.ItemName;
  2470. textItem.Format = itemPropertySetting.TextFormat;
  2471. textItem.Culture = itemPropertySetting.Culture;
  2472. textItem.DefaultValue = itemPropertySetting.DefaultValue;
  2473. textItem.IsNew = false;
  2474. textItem.SetDrawProperty();
  2475. return textItem;
  2476. }
  2477. else
  2478. {
  2479. return null;
  2480. }
  2481. }
  2482. }
  2483. else
  2484. {
  2485. textItem.Init(this, true);
  2486. textItem.Left = newItemLocation.X;
  2487. textItem.Top = newItemLocation.Y;
  2488. textItem.IsNew = false;
  2489. textItem.SetDrawProperty();
  2490. return textItem;
  2491. }
  2492. }
  2493. /// <summary>
  2494. /// 创建一个新图片Item
  2495. /// </summary>
  2496. /// <param name="isShowDialog">是否显示属性设置对话框</param>
  2497. /// <returns>创建的新Item对象</returns>
  2498. private LayoutItem NewImageItem(bool isShowDialog)
  2499. {
  2500. ImageItem imageItem = new ImageItem(null);
  2501. imageItem.IsNew = true;
  2502. openFileDialog.FileName = string.Empty;
  2503. if (openFileDialog.ShowDialog() == DialogResult.OK)
  2504. {
  2505. FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
  2506. // 图片尺寸超过200kbyte
  2507. if (LayoutConsts.FILE_DIALOG_IMAGE_MAX_SIZE < fileInfo.Length)
  2508. {
  2509. DialogResult dr = MessageBox.Show(
  2510. "图片太大,影响显示和打印速度,请准备200KByte以下的图片。",
  2511. "LayoutBox",
  2512. MessageBoxButtons.YesNo,
  2513. MessageBoxIcon.Warning,
  2514. MessageBoxDefaultButton.Button1);
  2515. if (dr == DialogResult.Yes)
  2516. {
  2517. return null;
  2518. }
  2519. }
  2520. Image image = null;
  2521. float imageWidth = LayoutConsts.SHAPE_ITEM_SIZE_DEFAULT_WIDTH;
  2522. float imageHeigh = LayoutConsts.SHAPE_ITEM_SIZE_DEFAULT_HEIGHT;
  2523. if (fileInfo.Length > 0)
  2524. {
  2525. Stream imageStream = null;
  2526. try
  2527. {
  2528. imageStream = fileInfo.Open(FileMode.Open,
  2529. FileAccess.Read,
  2530. FileShare.ReadWrite | FileShare.Delete);
  2531. image = Image.FromStream(imageStream);
  2532. MemoryStream mstream = new MemoryStream();
  2533. image.Save(mstream, image.RawFormat);
  2534. image = Image.FromStream(mstream);
  2535. }
  2536. finally
  2537. {
  2538. if (imageStream != null)
  2539. {
  2540. imageStream.Dispose();
  2541. }
  2542. }
  2543. imageWidth = LayoutCommon.PixelToMillimeter(image.Width, _zoom, image.HorizontalResolution);
  2544. imageHeigh = LayoutCommon.PixelToMillimeter(image.Height, _zoom, image.VerticalResolution);
  2545. }
  2546. else
  2547. {
  2548. MessageBox.Show("不能设置大小为0的图片。",
  2549. "LayoutBox",
  2550. MessageBoxButtons.OK,
  2551. MessageBoxIcon.Error,
  2552. MessageBoxDefaultButton.Button1);
  2553. return null;
  2554. }
  2555. PointF newItemLocation = GetNewItemLocation(imageWidth, imageHeigh);
  2556. if (isShowDialog)
  2557. {
  2558. ImageItemSetting itemPropertySetting = new ImageItemSetting();
  2559. itemPropertySetting.ImageLocationX = newItemLocation.X;
  2560. itemPropertySetting.ImageLocationY = newItemLocation.Y;
  2561. itemPropertySetting.ImageWidth = imageWidth;
  2562. itemPropertySetting.ImageHeight = imageHeigh;
  2563. DialogResult result = itemPropertySetting.ShowDialog();
  2564. if (result == DialogResult.OK)
  2565. {
  2566. imageItem.Init(this, true);
  2567. imageItem.Width = itemPropertySetting.ImageWidth;
  2568. imageItem.Height = itemPropertySetting.ImageHeight;
  2569. imageItem.Left = itemPropertySetting.ImageLocationX;
  2570. imageItem.Top = itemPropertySetting.ImageLocationY;
  2571. imageItem.SideRatioFixed = itemPropertySetting.SideRatioFixed;
  2572. imageItem.Image = image;
  2573. imageItem.FileName = openFileDialog.FileName;
  2574. imageItem.IsNew = false;
  2575. imageItem.SetDrawProperty();
  2576. return imageItem;
  2577. }
  2578. else
  2579. {
  2580. if (image != null)
  2581. {
  2582. image.Dispose();
  2583. }
  2584. return null;
  2585. }
  2586. }
  2587. else
  2588. {
  2589. if (imageWidth < LayoutConsts.ITEM_SIZE_MIN)
  2590. {
  2591. imageWidth = LayoutConsts.ITEM_SIZE_MIN;
  2592. }
  2593. else if (LayoutConsts.ITEM_SIZE_WIDTH_MAX < imageWidth)
  2594. {
  2595. imageWidth = LayoutConsts.ITEM_SIZE_WIDTH_MAX;
  2596. }
  2597. if (imageHeigh < LayoutConsts.ITEM_SIZE_MIN)
  2598. {
  2599. imageHeigh = LayoutConsts.ITEM_SIZE_MIN;
  2600. }
  2601. else if (LayoutConsts.ITEM_SIZE_HEIGHT_MAX < imageHeigh)
  2602. {
  2603. imageHeigh = LayoutConsts.ITEM_SIZE_HEIGHT_MAX;
  2604. }
  2605. imageItem.Init(this, true);
  2606. imageItem.Left = newItemLocation.X;
  2607. imageItem.Top = newItemLocation.Y;
  2608. imageItem.Width = imageWidth;
  2609. imageItem.Height = imageHeigh;
  2610. imageItem.Image = image;
  2611. imageItem.FileName = openFileDialog.FileName;
  2612. imageItem.SideRatioFixed = true;
  2613. imageItem.IsNew = false;
  2614. imageItem.SetDrawProperty();
  2615. return imageItem;
  2616. }
  2617. }
  2618. else
  2619. {
  2620. return null;
  2621. }
  2622. }
  2623. /// <summary>
  2624. /// 创建一个新矩形Item
  2625. /// </summary>
  2626. /// <param name="isShowDialog">是否显示属性设置对话框</param>
  2627. /// <returns>创建的新Item对象</returns>
  2628. private LayoutItem NewRectangleItem(bool isShowDialog)
  2629. {
  2630. RectangleItem rectangleItem = new RectangleItem(null);
  2631. rectangleItem.IsNew = true;
  2632. PointF newItemLocation = GetNewItemLocation(50, 50);
  2633. if (isShowDialog)
  2634. {
  2635. using (GraphicsItemSetting itemPropertySetting = new GraphicsItemSetting())
  2636. {
  2637. itemPropertySetting.ShapeLocationX = newItemLocation.X;
  2638. itemPropertySetting.ShapeLocationY = newItemLocation.Y;
  2639. itemPropertySetting.ShapeWidth = rectangleItem.Width;
  2640. itemPropertySetting.ShapeHeight = rectangleItem.Height;
  2641. itemPropertySetting.LineColor = rectangleItem.LineColor;
  2642. itemPropertySetting.FillColor = rectangleItem.FillColor;
  2643. itemPropertySetting.LineWidth = rectangleItem.LineWidth;
  2644. itemPropertySetting.Transparent = rectangleItem.Transparent;
  2645. DialogResult result = itemPropertySetting.ShowDialog();
  2646. if (result == DialogResult.OK)
  2647. {
  2648. rectangleItem.Init(this, true);
  2649. rectangleItem.Left = itemPropertySetting.ShapeLocationX;
  2650. rectangleItem.Top = itemPropertySetting.ShapeLocationY;
  2651. rectangleItem.Width = itemPropertySetting.ShapeWidth;
  2652. rectangleItem.Height = itemPropertySetting.ShapeHeight;
  2653. rectangleItem.LineColor = itemPropertySetting.LineColor;
  2654. rectangleItem.FillColor = itemPropertySetting.FillColor;
  2655. rectangleItem.LineWidth = itemPropertySetting.LineWidth;
  2656. rectangleItem.Transparent = itemPropertySetting.Transparent;
  2657. rectangleItem.IsNew = false;
  2658. rectangleItem.SetDrawProperty();
  2659. return rectangleItem;
  2660. }
  2661. else
  2662. {
  2663. return null;
  2664. }
  2665. }
  2666. }
  2667. else
  2668. {
  2669. rectangleItem.Init(this, true);
  2670. rectangleItem.Left = newItemLocation.X;
  2671. rectangleItem.Top = newItemLocation.Y;
  2672. rectangleItem.IsNew = false;
  2673. rectangleItem.SetDrawProperty();
  2674. return rectangleItem;
  2675. }
  2676. }
  2677. /// <summary>
  2678. /// 创建一个新椭圆Item
  2679. /// </summary>
  2680. /// <param name="isShowDialog">是否显示属性设置对话框</param>
  2681. /// <returns>创建的新Item对象</returns>
  2682. private LayoutItem NewEllipseItem(bool isShowDialog)
  2683. {
  2684. EllipseItem ellipseItem = new EllipseItem(null);
  2685. ellipseItem.IsNew = true;
  2686. PointF newItemLocation = GetNewItemLocation(50, 50);
  2687. if (isShowDialog)
  2688. {
  2689. using (GraphicsItemSetting itemPropertySetting = new GraphicsItemSetting())
  2690. {
  2691. itemPropertySetting.ShapeLocationX = newItemLocation.X;
  2692. itemPropertySetting.ShapeLocationY = newItemLocation.Y;
  2693. itemPropertySetting.ShapeWidth = ellipseItem.Width;
  2694. itemPropertySetting.ShapeHeight = ellipseItem.Height;
  2695. itemPropertySetting.LineColor = ellipseItem.LineColor;
  2696. itemPropertySetting.FillColor = ellipseItem.FillColor;
  2697. itemPropertySetting.LineWidth = ellipseItem.LineWidth;
  2698. itemPropertySetting.Transparent = ellipseItem.Transparent;
  2699. DialogResult result = itemPropertySetting.ShowDialog();
  2700. if (result == DialogResult.OK)
  2701. {
  2702. ellipseItem.Init(this, true);
  2703. ellipseItem.Left = itemPropertySetting.ShapeLocationX;
  2704. ellipseItem.Top = itemPropertySetting.ShapeLocationY;
  2705. ellipseItem.Width = itemPropertySetting.ShapeWidth;
  2706. ellipseItem.Height = itemPropertySetting.ShapeHeight;
  2707. ellipseItem.LineColor = itemPropertySetting.LineColor;
  2708. ellipseItem.FillColor = itemPropertySetting.FillColor;
  2709. ellipseItem.LineWidth = itemPropertySetting.LineWidth;
  2710. ellipseItem.Transparent = itemPropertySetting.Transparent;
  2711. ellipseItem.IsNew = false;
  2712. ellipseItem.SetDrawProperty();
  2713. return ellipseItem;
  2714. }
  2715. else
  2716. {
  2717. return null;
  2718. }
  2719. }
  2720. }
  2721. else
  2722. {
  2723. ellipseItem.Init(this, true);
  2724. ellipseItem.Left = newItemLocation.X;
  2725. ellipseItem.Top = newItemLocation.Y;
  2726. ellipseItem.IsNew = false;
  2727. ellipseItem.SetDrawProperty();
  2728. return ellipseItem;
  2729. }
  2730. }
  2731. /// <summary>
  2732. /// 创建一个新表格Item
  2733. /// </summary>
  2734. /// <param name="isShowDialog">是否显示属性设置对话框</param>
  2735. /// <returns>创建的新Item对象</returns>
  2736. private LayoutItem NewGridItem(bool isShowDialog)
  2737. {
  2738. GridItem gridItem = new GridItem(null);
  2739. gridItem.IsNew = true;
  2740. PointF newItemLocation = GetNewItemLocation(50, 50);
  2741. if (isShowDialog)
  2742. {
  2743. using (GridItemSetting itemPropertySetting = new GridItemSetting())
  2744. {
  2745. itemPropertySetting.ShapeLocationX = newItemLocation.X;
  2746. itemPropertySetting.ShapeLocationY = newItemLocation.Y;
  2747. itemPropertySetting.ShapeWidth = gridItem.Width;
  2748. itemPropertySetting.ShapeHeight = gridItem.Height;
  2749. itemPropertySetting.LineColor = gridItem.LineColor;
  2750. itemPropertySetting.FillColor = gridItem.FillColor;
  2751. itemPropertySetting.LineWidth = gridItem.LineWidth;
  2752. itemPropertySetting.Transparent = gridItem.Transparent;
  2753. itemPropertySetting.TitleVisible = gridItem.TitleVisible;
  2754. itemPropertySetting.AlwaysTitleVisible = gridItem.AlwaysTitleVisible;
  2755. itemPropertySetting.RowsHeight = gridItem.RowsHeight;
  2756. itemPropertySetting.HeadRowsHeight = gridItem.HeadRowsHeight;
  2757. itemPropertySetting.RowsCount = gridItem.DisplayRowCount;
  2758. itemPropertySetting.Columns = gridItem.Columns.CopyTo();
  2759. DialogResult result = itemPropertySetting.ShowDialog();
  2760. if (result == DialogResult.OK)
  2761. {
  2762. gridItem.Init(this, true);
  2763. gridItem.Left = itemPropertySetting.ShapeLocationX;
  2764. gridItem.Top = itemPropertySetting.ShapeLocationY;
  2765. gridItem.Width = itemPropertySetting.ShapeWidth;
  2766. gridItem.Height = itemPropertySetting.ShapeHeight;
  2767. gridItem.LineColor = itemPropertySetting.LineColor;
  2768. gridItem.FillColor = itemPropertySetting.FillColor;
  2769. gridItem.LineWidth = itemPropertySetting.LineWidth;
  2770. gridItem.Transparent = itemPropertySetting.Transparent;
  2771. gridItem.TitleVisible = itemPropertySetting.TitleVisible;
  2772. gridItem.AlwaysTitleVisible = itemPropertySetting.AlwaysTitleVisible;
  2773. gridItem.DisplayRowCount = itemPropertySetting.RowsCount;
  2774. gridItem.RowsHeight = itemPropertySetting.RowsHeight;
  2775. gridItem.HeadRowsHeight = itemPropertySetting.HeadRowsHeight;
  2776. if (itemPropertySetting.ColumnsChanged)
  2777. {
  2778. itemPropertySetting.Columns.GridItem = gridItem;
  2779. gridItem.Columns = itemPropertySetting.Columns;
  2780. }
  2781. gridItem.IsNew = false;
  2782. gridItem.SetDrawProperty();
  2783. return gridItem;
  2784. }
  2785. else
  2786. {
  2787. return null;
  2788. }
  2789. }
  2790. }
  2791. else
  2792. {
  2793. gridItem.Init(this, true);
  2794. gridItem.Left = newItemLocation.X;
  2795. gridItem.Top = newItemLocation.Y;
  2796. gridItem.IsNew = false;
  2797. gridItem.SetDrawProperty();
  2798. return gridItem;
  2799. }
  2800. }
  2801. /// <summary>
  2802. /// 创建一个Grid页码Item
  2803. /// </summary>
  2804. /// <param name="isShowDialog">是否显示属性设置对话框</param>
  2805. /// <returns>创建的新Item对象</returns>
  2806. private LayoutItem NewPageNumItem(bool isShowDialog)
  2807. {
  2808. PageNumItem pageNumItem = new PageNumItem(null);
  2809. pageNumItem.IsNew = true;
  2810. PointF newItemLocation = GetNewItemLocation(50, 50);
  2811. if (isShowDialog)
  2812. {
  2813. using (PageNumItemSetting itemPropertySetting = new PageNumItemSetting())
  2814. {
  2815. itemPropertySetting.TextLocationX = newItemLocation.X;
  2816. itemPropertySetting.TextLocationY = newItemLocation.Y;
  2817. itemPropertySetting.TextWidth = pageNumItem.Width;
  2818. itemPropertySetting.TextHeight = pageNumItem.Height;
  2819. itemPropertySetting.TextAlign = pageNumItem.TextAlign;
  2820. itemPropertySetting.TextAlignVertical = pageNumItem.TextAlignVertical;
  2821. itemPropertySetting.TextColor = pageNumItem.TextColor;
  2822. itemPropertySetting.TextFormat = pageNumItem.Format;
  2823. itemPropertySetting.TextFont = pageNumItem.Font;
  2824. DialogResult result = itemPropertySetting.ShowDialog();
  2825. if (result == DialogResult.OK)
  2826. {
  2827. pageNumItem.Init(this, true);
  2828. pageNumItem.Left = itemPropertySetting.TextLocationX;
  2829. pageNumItem.Top = itemPropertySetting.TextLocationY;
  2830. pageNumItem.Width = itemPropertySetting.TextWidth;
  2831. pageNumItem.Height = itemPropertySetting.TextHeight;
  2832. pageNumItem.Font = itemPropertySetting.TextFont;
  2833. pageNumItem.TextAlign = itemPropertySetting.TextAlign;
  2834. pageNumItem.TextAlignVertical = itemPropertySetting.TextAlignVertical;
  2835. pageNumItem.TextColor = itemPropertySetting.TextColor;
  2836. pageNumItem.Format = itemPropertySetting.TextFormat;
  2837. pageNumItem.IsNew = false;
  2838. pageNumItem.SetDrawProperty();
  2839. return pageNumItem;
  2840. }
  2841. else
  2842. {
  2843. return null;
  2844. }
  2845. }
  2846. }
  2847. else
  2848. {
  2849. pageNumItem.Init(this, true);
  2850. pageNumItem.Left = newItemLocation.X;
  2851. pageNumItem.Top = newItemLocation.Y;
  2852. pageNumItem.IsNew = false;
  2853. pageNumItem.SetDrawProperty();
  2854. return pageNumItem;
  2855. }
  2856. }
  2857. /// <summary>
  2858. /// 创建一个合计文本Item
  2859. /// </summary>
  2860. /// <param name="isShowDialog">是否显示属性设置对话框</param>
  2861. /// <returns>创建的新Item对象</returns>
  2862. private LayoutItem NewTotalTextItem(bool isShowDialog)
  2863. {
  2864. TotalTextItem textItem = new TotalTextItem(null);
  2865. textItem.IsNew = true;
  2866. PointF newItemLocation = GetNewItemLocation(LayoutCommon.DefaultTextItemSizeWidth,
  2867. LayoutCommon.DefaultTextItemSizeHeight);
  2868. if (isShowDialog)
  2869. {
  2870. using (TotalTextItemSetting itemPropertySetting = new TotalTextItemSetting())
  2871. {
  2872. itemPropertySetting.IsNew = true;
  2873. itemPropertySetting.IsDataBinding = false;
  2874. itemPropertySetting.TextFont = textItem.Font;
  2875. itemPropertySetting.TextLocationX = newItemLocation.X;
  2876. itemPropertySetting.TextLocationY = newItemLocation.Y;
  2877. itemPropertySetting.ItemStyle = ItemStyle.Other;
  2878. itemPropertySetting.ItemName = LayoutBox.ITEMNAME_FIX + _newItemID;
  2879. itemPropertySetting.NotTotalFont = textItem.NotTotalFont;
  2880. itemPropertySetting.NotTotalColor = textItem.NotTotalColor;
  2881. itemPropertySetting.NotTotalValue = textItem.NotTotalValue;
  2882. itemPropertySetting.NotTotalAlign = textItem.NotTotalAlign;
  2883. itemPropertySetting.NotTotalAlignVertical = textItem.NotTotalAlignVertical;
  2884. DialogResult result = itemPropertySetting.ShowDialog();
  2885. if (result == DialogResult.OK)
  2886. {
  2887. textItem.Init(this, true);
  2888. textItem.IsDataBinding = false;
  2889. textItem.Left = itemPropertySetting.TextLocationX;
  2890. textItem.Top = itemPropertySetting.TextLocationY;
  2891. textItem.Width = itemPropertySetting.TextWidth;
  2892. textItem.Height = itemPropertySetting.TextHeight;
  2893. textItem.LineSpace = itemPropertySetting.LineSpace;
  2894. textItem.CharacterSpace = itemPropertySetting.CharacterSpace;
  2895. textItem.CharacterCount = itemPropertySetting.CharacterCount;
  2896. textItem.Font = itemPropertySetting.TextFont;
  2897. textItem.Wrap = itemPropertySetting.Wrap;
  2898. textItem.TextColor = itemPropertySetting.TextColor;
  2899. textItem.TextAlign = itemPropertySetting.TextAlign;
  2900. textItem.TextAlignVertical = itemPropertySetting.TextAlignVertical;
  2901. textItem.DisplayValue = itemPropertySetting.TextPreview;
  2902. textItem.ItemStyle = itemPropertySetting.ItemStyle;
  2903. textItem.TextItemName = itemPropertySetting.ItemName;
  2904. textItem.Format = itemPropertySetting.TextFormat;
  2905. textItem.Culture = itemPropertySetting.Culture;
  2906. textItem.DefaultValue = itemPropertySetting.DefaultValue;
  2907. textItem.NotTotalFont = itemPropertySetting.NotTotalFont;
  2908. textItem.NotTotalColor = itemPropertySetting.NotTotalColor;
  2909. textItem.NotTotalValue = itemPropertySetting.NotTotalValue;
  2910. textItem.NotTotalAlign = itemPropertySetting.NotTotalAlign;
  2911. textItem.NotTotalAlignVertical = itemPropertySetting.NotTotalAlignVertical;
  2912. textItem.IsNew = false;
  2913. textItem.SetDrawProperty();
  2914. return textItem;
  2915. }
  2916. else
  2917. {
  2918. return null;
  2919. }
  2920. }
  2921. }
  2922. else
  2923. {
  2924. textItem.Init(this, true);
  2925. textItem.Left = newItemLocation.X;
  2926. textItem.Top = newItemLocation.Y;
  2927. textItem.IsNew = false;
  2928. textItem.SetDrawProperty();
  2929. return textItem;
  2930. }
  2931. }
  2932. /// <summary>
  2933. /// 清除LayoutBox的设置
  2934. /// </summary>
  2935. private void ClearLayoutBox()
  2936. {
  2937. // TODO
  2938. if (this._clipboardFormat != null)
  2939. {
  2940. this.ClearClipboard();
  2941. //if (Clipboard.ContainsData(_clipboardFormat.Name))
  2942. //{
  2943. // CopyToClipboard(_clipboardFormat.Name, null);
  2944. //}
  2945. //_clipboardFormat =
  2946. // DataFormats.GetFormat(typeof(LayoutItem).FullName
  2947. // + "{"
  2948. // + Guid.NewGuid().ToString()
  2949. // + "}");
  2950. }
  2951. _lastItemRF = RectangleF.Empty;
  2952. _mouseRect = Rectangle.Empty;
  2953. SetDitry(false);
  2954. _selectedItems.Clear();
  2955. foreach (LayoutItem item in _items)
  2956. {
  2957. if (item.Frame != null)
  2958. {
  2959. item.Frame.Dispose();
  2960. item.Frame = null;
  2961. }
  2962. item.Selected = false;
  2963. item.Dispose();
  2964. }
  2965. _items.Clear();
  2966. _updateCount = 0;
  2967. _newItemID = 1;
  2968. _paperHeight = 297.0f;
  2969. _paperWidth = 210.0f;
  2970. _backgroundImageName = string.Empty;
  2971. paperArea.BackgroundImage = null;
  2972. _isPrintAreaVisible = true;
  2973. _isPrintBackground = false;
  2974. if (_zoomType != ZoomType.Whole)
  2975. {
  2976. ChangePaperSize();
  2977. }
  2978. else
  2979. {
  2980. InitializeZoom();
  2981. }
  2982. }
  2983. /// <summary>
  2984. /// 初始化LayoutBox
  2985. /// </summary>
  2986. private void InitializeLayoutBox()
  2987. {
  2988. openFileDialog.Filter = LayoutConsts.FILE_DIALOG_IMAGE_FILTER;
  2989. openFileDialog.FilterIndex = 6;
  2990. string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
  2991. if (Directory.Exists(directory))
  2992. {
  2993. openFileDialog.InitialDirectory = directory;
  2994. }
  2995. else
  2996. {
  2997. openFileDialog.InitialDirectory =
  2998. Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  2999. }
  3000. _comparisonItems = new Comparison<LayoutItem>(SortItemsWithChildIndex);
  3001. //// TODO
  3002. //if (this._layoutMode == InvoiceLayout.LayoutMode.Edit)
  3003. //{
  3004. // _clipboardFormat =
  3005. // DataFormats.GetFormat(typeof(LayoutItem).FullName
  3006. // + "{"
  3007. // + Guid.NewGuid().ToString()
  3008. // + "}");
  3009. //}
  3010. tsmiPaste.Enabled = true;
  3011. _lastItemRF = RectangleF.Empty;
  3012. _mouseRect = Rectangle.Empty;
  3013. _updateCount = 0;
  3014. _newItemID = 1;
  3015. _dataMember = string.Empty;
  3016. _printDataMember = string.Empty;
  3017. //_layoutMode = LayoutMode.Display;
  3018. _paperHeight = 297.0f;
  3019. _paperWidth = 210.0f;
  3020. _zoom = 100;
  3021. _backgroundImageName = string.Empty;
  3022. _isDirty = false;
  3023. _isPrintAreaVisible = true;
  3024. _isPrintBackground = false;
  3025. _selectedItems = new List<LayoutItem>();
  3026. _items = new List<LayoutItem>();
  3027. _zoomType = ZoomType.Specify;
  3028. ChangePaperSize();
  3029. InitItemZoom();
  3030. }
  3031. /// <summary>
  3032. /// 获取新建Item的位置
  3033. /// </summary>
  3034. /// <returns>新建Item的位置</returns>
  3035. private PointF GetNewItemLocation(float itemWidth, float itemHeight)
  3036. {
  3037. PointF newItemLocation = new PointF(LayoutConsts.ITEM_LOCATION_DEF, LayoutConsts.ITEM_LOCATION_DEF);
  3038. if (paperArea.Left < 0)
  3039. {
  3040. newItemLocation.X += LayoutCommon.PixelToMillimeter(Math.Abs(paperArea.Left), _zoom);
  3041. }
  3042. if (paperArea.Top < 0)
  3043. {
  3044. newItemLocation.Y += LayoutCommon.PixelToMillimeter(Math.Abs(paperArea.Top), _zoom);
  3045. }
  3046. return newItemLocation;
  3047. }
  3048. /// <summary>
  3049. /// 复制到剪贴板中
  3050. /// </summary>
  3051. /// <param name="format">格式</param>
  3052. /// <param name="items">选定的Item</param>
  3053. private void CopyToClipboard(object items)
  3054. {
  3055. this.GetCipboardFormat();
  3056. if (this._clipboardFormat != null)
  3057. {
  3058. IDataObject dataObj = new DataObject();
  3059. dataObj.SetData(this._clipboardFormat.Name, false, items);
  3060. Clipboard.SetDataObject(dataObj, false);
  3061. }
  3062. }
  3063. /// <summary>
  3064. /// 获取剪贴板中的数据
  3065. /// </summary>
  3066. /// <param name="format">格式</param>
  3067. /// <returns>Item</returns>
  3068. private object GetFromClipboard()
  3069. {
  3070. this.GetCipboardFormat();
  3071. if (this._clipboardFormat != null)
  3072. {
  3073. if (Clipboard.ContainsData(this._clipboardFormat.Name))
  3074. {
  3075. return Clipboard.GetData(this._clipboardFormat.Name);
  3076. }
  3077. }
  3078. return null;
  3079. }
  3080. private void ClearClipboard()
  3081. {
  3082. if (this._clipboardFormat != null)
  3083. {
  3084. if (Clipboard.ContainsData(_clipboardFormat.Name))
  3085. {
  3086. IDataObject dataObj = new DataObject();
  3087. dataObj.SetData(this._clipboardFormat.Name, false, null);
  3088. Clipboard.SetDataObject(dataObj, false);
  3089. }
  3090. }
  3091. this._clipboardFormat = null;
  3092. }
  3093. private DataFormats.Format GetCipboardFormat()
  3094. {
  3095. if (_clipboardFormat != null)
  3096. {
  3097. return _clipboardFormat;
  3098. }
  3099. // TODO
  3100. if (this._layoutMode == InvoiceLayout.LayoutMode.Edit)
  3101. {
  3102. _clipboardFormat =
  3103. DataFormats.GetFormat(typeof(LayoutItem).FullName
  3104. + "{"
  3105. + Guid.NewGuid().ToString()
  3106. + "}");
  3107. }
  3108. return _clipboardFormat;
  3109. }
  3110. #endregion 保存、读取相关
  3111. #region 缩放比例和位置
  3112. /// <summary>
  3113. /// 设置纸张的高和宽
  3114. /// </summary>
  3115. /// <param name="paperWidth">纸张的宽</param>
  3116. /// <param name="paperHeight">纸张的高</param>
  3117. public void SetPaperAreaSize(float paperWidth, float paperHeight)
  3118. {
  3119. if (paperWidth < LayoutConsts.PAPER_SIZE_MIN
  3120. || LayoutConsts.PAPER_SIZE_MAX < paperWidth)
  3121. {
  3122. return;
  3123. }
  3124. if (paperHeight < LayoutConsts.PAPER_SIZE_MIN
  3125. || LayoutConsts.PAPER_SIZE_MAX < paperHeight)
  3126. {
  3127. return;
  3128. }
  3129. if (_paperHeight != paperHeight || _paperWidth != paperWidth)
  3130. {
  3131. foreach (LayoutItem item in _items)
  3132. {
  3133. if (paperHeight < item.Top + item.Height
  3134. || paperWidth < item.Left + item.Width)
  3135. {
  3136. MessageBox.Show("在纸张范围外有Item,请设置纸张大小包含所有Item",
  3137. "LayoutBox",
  3138. MessageBoxButtons.OK,
  3139. MessageBoxIcon.Error,
  3140. MessageBoxDefaultButton.Button1);
  3141. return;
  3142. }
  3143. }
  3144. _paperHeight = paperHeight;
  3145. _paperWidth = paperWidth;
  3146. if (_zoomType != ZoomType.Whole)
  3147. {
  3148. ChangePaperSize();
  3149. }
  3150. else
  3151. {
  3152. InitializeZoom();
  3153. }
  3154. if (PaperSizeChanged != null)
  3155. {
  3156. PaperSizeChanged(this, EventArgs.Empty);
  3157. }
  3158. _isDirty = true;
  3159. }
  3160. }
  3161. /// <summary>
  3162. /// 设置所有Item的缩放比例
  3163. /// </summary>
  3164. private void ZoomAllItems()
  3165. {
  3166. InitItemZoom();
  3167. if (0 < _items.Count)
  3168. {
  3169. foreach (LayoutItem item in _items)
  3170. {
  3171. item.ResetShapeLocationAndSize(false);
  3172. item.SetDrawProperty();
  3173. }
  3174. }
  3175. }
  3176. /// <summary>
  3177. /// 整页显示
  3178. /// </summary>
  3179. private void InitializeZoom()
  3180. {
  3181. int paperHeight = LayoutCommon.MillimeterToPixel(_paperHeight);
  3182. int paperWidth = LayoutCommon.MillimeterToPixel(_paperWidth);
  3183. int marginHeight = Height - LayoutConsts.LAYOUT_MARGIN - LayoutConsts.LAYOUT_MARGIN;
  3184. int marginWidth = Width - LayoutConsts.LAYOUT_MARGIN - LayoutConsts.LAYOUT_MARGIN;
  3185. double zoomHeight = 100.0 * marginHeight / paperHeight;
  3186. double zoomWidth = 100.0 * marginWidth / paperWidth;
  3187. int zoom = System.Convert.ToInt32(Math.Truncate(Math.Min(zoomHeight, zoomWidth)));
  3188. if (zoom < 0)
  3189. {
  3190. _zoom = 0;
  3191. }
  3192. else
  3193. {
  3194. _zoom = zoom;
  3195. }
  3196. ChangePaperSize();
  3197. ZoomAllItems();
  3198. }
  3199. /// <summary>
  3200. /// 纸张的显示比率
  3201. /// </summary>
  3202. private void ChangePaperSize()
  3203. {
  3204. int width = LayoutCommon.MillimeterToPixel(_paperWidth, _zoom);
  3205. int height = LayoutCommon.MillimeterToPixel(_paperHeight, _zoom);
  3206. paperArea.Size = new Size(width, height);
  3207. AdjustPaperArea();
  3208. }
  3209. /// <summary>
  3210. /// 缩放比例变更
  3211. /// </summary>
  3212. private void InitItemZoom()
  3213. {
  3214. _maxItemLeft = LayoutCommon.MillimeterToPixel(LayoutConsts.ITEM_LOCATION_MAX, _zoom);
  3215. _maxItemTop = _maxItemLeft;
  3216. _maxItemWidth = LayoutCommon.MillimeterToPixel(LayoutConsts.ITEM_SIZE_WIDTH_MAX, _zoom);
  3217. _maxItemHeight = LayoutCommon.MillimeterToPixel(LayoutConsts.ITEM_SIZE_HEIGHT_MAX, _zoom);
  3218. _minItemWidth = LayoutCommon.MillimeterToPixel(LayoutConsts.ITEM_SIZE_MIN, _zoom);
  3219. _minItemHeight = _minItemWidth;
  3220. }
  3221. /// <summary>
  3222. /// 设置纸张范围和位置
  3223. /// </summary>
  3224. private void AdjustPaperArea()
  3225. {
  3226. int top = AdjustPaperAreaTop();
  3227. int left = AdjustPaperAreaLeft();
  3228. paperArea.Location = new Point(left, top);
  3229. lblRightBottomPoint.Location = new Point(paperArea.Right, paperArea.Bottom);
  3230. }
  3231. /// <summary>
  3232. /// 设置纸张位置Y
  3233. /// </summary>
  3234. /// <returns>位置Y</returns>
  3235. private int AdjustPaperAreaTop()
  3236. {
  3237. int margin = paperBox.ClientSize.Height
  3238. - paperArea.Height
  3239. - LayoutConsts.LAYOUT_MARGIN
  3240. - LayoutConsts.LAYOUT_MARGIN;
  3241. if (0 < margin)
  3242. {
  3243. return lblLeftTopPoint.Top + LayoutConsts.LAYOUT_MARGIN + (margin) / 2;
  3244. }
  3245. else
  3246. {
  3247. return lblLeftTopPoint.Top + LayoutConsts.LAYOUT_MARGIN;
  3248. }
  3249. }
  3250. /// <summary>
  3251. /// 设置纸张位置X
  3252. /// </summary>
  3253. /// <returns>位置X</returns>
  3254. private int AdjustPaperAreaLeft()
  3255. {
  3256. int margin = paperBox.ClientSize.Width
  3257. - paperArea.Width
  3258. - LayoutConsts.LAYOUT_MARGIN
  3259. - LayoutConsts.LAYOUT_MARGIN;
  3260. if (0 < margin)
  3261. {
  3262. return lblLeftTopPoint.Left + LayoutConsts.LAYOUT_MARGIN + (margin) / 2;
  3263. }
  3264. else
  3265. {
  3266. return lblLeftTopPoint.Left + LayoutConsts.LAYOUT_MARGIN;
  3267. }
  3268. }
  3269. #endregion 缩放比例和位置
  3270. #region Item操作相关
  3271. /// <summary>
  3272. /// 设置Ditry
  3273. /// </summary>
  3274. /// <param name="vaule"></param>
  3275. private void SetDitry(bool vaule)
  3276. {
  3277. _isDirty = vaule;
  3278. }
  3279. /// <summary>
  3280. /// 计算滚动到指定子控制的偏移
  3281. /// </summary>
  3282. /// <param name="activeControl">滚动显示的子控制</param>
  3283. /// <returns>滚动到指定子控制的偏移</returns>
  3284. protected override Point ScrollToControl(Control activeControl)
  3285. {
  3286. // 选定Item时,滚动条不动
  3287. return DisplayRectangle.Location;
  3288. }
  3289. /// <summary>
  3290. /// 代表一个函数来比较两个相同类型的对象
  3291. /// </summary>
  3292. /// <param name="itemX">第一个比较对象</param>
  3293. /// <param name="itemY">第二个比较对象</param>
  3294. /// <returns>
  3295. /// 小于0 x小于y。
  3296. /// 等于0 x等于y。
  3297. /// 大于0 x大于y。
  3298. /// </returns>
  3299. private int SortItemsWithChildIndex(LayoutItem itemX, LayoutItem itemY)
  3300. {
  3301. try
  3302. {
  3303. int x = paperArea.ShapeContainer.Shapes.GetChildIndex(itemX.Shape);
  3304. int y = paperArea.ShapeContainer.Shapes.GetChildIndex(itemY.Shape);
  3305. return y - x;
  3306. }
  3307. catch
  3308. {
  3309. return 0;
  3310. }
  3311. }
  3312. /// <summary>
  3313. /// 调整拖动范围
  3314. /// </summary>
  3315. /// <param name="p">鼠标所在位置</param>
  3316. private void ResizeToRectangle(Point p)
  3317. {
  3318. Rectangle rect = _mouseRect;
  3319. _mouseRect.Width = p.X - _mouseRect.Left;
  3320. _mouseRect.Height = p.Y - _mouseRect.Top;
  3321. DrawReversibleFrame(_mouseRect);
  3322. DrawReversibleFrame(rect);
  3323. }
  3324. /// <summary>
  3325. /// 显示拖动范围
  3326. /// </summary>
  3327. /// <param name="rect">范围</param>
  3328. internal void DrawReversibleFrame(Rectangle rect)
  3329. {
  3330. ControlPaint.DrawReversibleFrame(paperArea.RectangleToScreen(rect),
  3331. Color.Black, FrameStyle.Dashed);
  3332. }
  3333. /// <summary>
  3334. /// 当前选择的对象已经改变
  3335. /// </summary>
  3336. private void SetTSMIOnSelectionChanged()
  3337. {
  3338. if (0 < _selectedItems.Count)
  3339. {
  3340. tsmiCut.Enabled = true;
  3341. tsmiCopy.Enabled = true;
  3342. tsmiDelete.Enabled = true;
  3343. tsmiOrder.Enabled = true;
  3344. if (_selectedItems.Count == 1)
  3345. {
  3346. tsmiProperties.Enabled = true;
  3347. tsmiArrange.Enabled = false;
  3348. }
  3349. else
  3350. {
  3351. tsmiProperties.Enabled = false;
  3352. tsmiArrange.Enabled = true;
  3353. }
  3354. }
  3355. else
  3356. {
  3357. tsmiCut.Enabled = false;
  3358. tsmiCopy.Enabled = false;
  3359. tsmiDelete.Enabled = false;
  3360. tsmiProperties.Enabled = false;
  3361. tsmiArrange.Enabled = false;
  3362. tsmiOrder.Enabled = false;
  3363. }
  3364. }
  3365. /// <summary>
  3366. /// 调整Item范围
  3367. /// </summary>
  3368. /// <param name="p">鼠标所在位置</param>
  3369. /// <param name="index">Item索引</param>
  3370. private void ResizeToRectangle(Point p, int index)
  3371. {
  3372. Rectangle rect = _moveRectangles[index];
  3373. if (_isShiftDown)
  3374. {
  3375. // 长宽比例
  3376. double sizeRatio = 1.0 * _downRectangles[index].Height / _downRectangles[index].Width;
  3377. int changedX = Math.Abs(p.X - _startX);
  3378. int changedY = Math.Abs(p.Y - _startY);
  3379. if (Math.Abs(p.X - _startX) < Math.Abs(p.Y - _startY))
  3380. {
  3381. rect.X = _downRectangles[index].Left;
  3382. rect.Y = _downRectangles[index].Top + p.Y - _startY;
  3383. }
  3384. else
  3385. {
  3386. rect.X = _downRectangles[index].Left + p.X - _startX;
  3387. rect.Y = _downRectangles[index].Top;
  3388. }
  3389. rect.Width = _downRectangles[index].Width;
  3390. rect.Height = _downRectangles[index].Height;
  3391. }
  3392. else if (_isCtrlDown)
  3393. {
  3394. rect.X = _downRectangles[index].Left + p.X - _startX;
  3395. rect.Y = _downRectangles[index].Top + p.Y - _startY;
  3396. rect.Width = _downRectangles[index].Width;
  3397. rect.Height = _downRectangles[index].Height;
  3398. }
  3399. else
  3400. {
  3401. rect.X = _downRectangles[index].Left + p.X - _startX;
  3402. rect.Y = _downRectangles[index].Top + p.Y - _startY;
  3403. rect.Width = _downRectangles[index].Width;
  3404. rect.Height = _downRectangles[index].Height;
  3405. }
  3406. {
  3407. DrawReversibleFrame(_selectedItems[index].ItemLockType, _moveRectangles[index], rect);
  3408. }
  3409. _moveRectangles[index] = rect;
  3410. }
  3411. /// <summary>
  3412. /// 绘制Item移动范围
  3413. /// </summary>
  3414. /// <param name="itemLock">Item锁定状态</param>
  3415. /// <param name="rectOriginal">源范围</param>
  3416. /// <param name="rectCurrent">新范围</param>
  3417. private void DrawReversibleFrame(ItemLock itemLock, Rectangle? rectOriginal, Rectangle? rectCurrent)
  3418. {
  3419. if (itemLock == ItemLock.LockLocationAndSize)
  3420. {
  3421. return;
  3422. }
  3423. if (rectCurrent.HasValue)
  3424. {
  3425. Rectangle rect = rectCurrent.Value;
  3426. rect.Width += 1;
  3427. rect.Height += 1;
  3428. DrawReversibleFrame(rect);
  3429. }
  3430. if (rectOriginal.HasValue && _isMouseMove)
  3431. {
  3432. Rectangle rect = rectOriginal.Value;
  3433. rect.Width += 1;
  3434. rect.Height += 1;
  3435. DrawReversibleFrame(rect);
  3436. }
  3437. }
  3438. /// <summary>
  3439. /// 调整控件的大小
  3440. /// </summary>
  3441. /// <param name="item">Item</param>
  3442. /// <param name="rect">范围</param>
  3443. private void DragHandler(LayoutItem item, Rectangle rect)
  3444. {
  3445. if (item.ItemLockType == ItemLock.LockLocationAndSize)
  3446. {
  3447. return;
  3448. }
  3449. int top = 0;
  3450. int left = 0;
  3451. int width = 0;
  3452. int height = 0;
  3453. if (rect.Width < 0)
  3454. {
  3455. left = rect.Right;
  3456. width -= rect.Width;
  3457. }
  3458. else
  3459. {
  3460. left = rect.Left;
  3461. width = rect.Width;
  3462. }
  3463. if (rect.Height < 0)
  3464. {
  3465. top = rect.Bottom;
  3466. height -= rect.Height;
  3467. }
  3468. else
  3469. {
  3470. top = rect.Top;
  3471. height = rect.Height;
  3472. }
  3473. if (left < 0
  3474. || _maxItemLeft < left
  3475. || top < 0
  3476. || _maxItemTop < top
  3477. || width < _minItemWidth
  3478. || _maxItemWidth < width
  3479. || height < _minItemHeight
  3480. || _maxItemHeight < height
  3481. || paperArea.Width - 1 < left
  3482. || paperArea.Height - 1 < top
  3483. )
  3484. {
  3485. //return;
  3486. }
  3487. item.MoveItem(left, top);
  3488. item.ChangeItemSize(width, height);
  3489. item.ResetItemFrame();
  3490. Dirty = true;
  3491. }
  3492. #endregion Item操作相关
  3493. #region 数据绑定相关
  3494. /// <summary>
  3495. /// 设置数据源
  3496. /// </summary>
  3497. private void SetDataSource()
  3498. {
  3499. if (_dataBinding == null)
  3500. {
  3501. _dataBinding = new LayoutBoxDataBinding(this);
  3502. _dataBinding.SetDataConnection(_dataSource, _dataMember);
  3503. }
  3504. else
  3505. {
  3506. if (_dataBinding.ContainsDataMember(_dataSource))
  3507. {
  3508. _dataMember = string.Empty;
  3509. }
  3510. _dataBinding.SetDataConnection(_dataSource, _dataMember);
  3511. if (_dataSource == null)
  3512. {
  3513. _dataBinding = null;
  3514. }
  3515. }
  3516. RefreshItemsBound();
  3517. }
  3518. /// <summary>
  3519. /// 设置数据成员
  3520. /// </summary>
  3521. private void SetDataMember()
  3522. {
  3523. if (_dataBinding == null)
  3524. {
  3525. _dataBinding = new LayoutBoxDataBinding(this);
  3526. }
  3527. _dataBinding.SetDataConnection(_dataSource, _dataMember);
  3528. RefreshItemsBound();
  3529. }
  3530. /// <summary>
  3531. /// 设置打印数据源
  3532. /// </summary>
  3533. private void SetPrintDataSource()
  3534. {
  3535. if (_printDataBinding == null)
  3536. {
  3537. _printDataBinding = new LayoutBoxPrintDataBinding(this);
  3538. _printDataBinding.SetDataConnection(_printDataSource, _printDataMember);
  3539. }
  3540. else
  3541. {
  3542. if (_printDataBinding.ContainsDataMember(_printDataSource))
  3543. {
  3544. _printDataMember = string.Empty;
  3545. }
  3546. _printDataBinding.SetDataConnection(_printDataSource, _printDataMember);
  3547. if (_printDataSource == null)
  3548. {
  3549. _printDataBinding = null;
  3550. }
  3551. }
  3552. RefreshItemsPrintBound();
  3553. }
  3554. /// <summary>
  3555. /// 设置打印数据成员
  3556. /// </summary>
  3557. private void SetPrintDataMember()
  3558. {
  3559. if (_printDataBinding == null)
  3560. {
  3561. _printDataBinding = new LayoutBoxPrintDataBinding(this);
  3562. }
  3563. _printDataBinding.SetDataConnection(_printDataSource, _printDataMember);
  3564. RefreshItemsPrintBound();
  3565. }
  3566. /// <summary>
  3567. /// 启动数据更新
  3568. /// </summary>
  3569. private void BeginUpdateInternal()
  3570. {
  3571. if (IsHandleCreated)
  3572. {
  3573. if (_updateCount == 0)
  3574. {
  3575. SendMessage(new HandleRef(this, Handle), 11, 0, 0);
  3576. }
  3577. _updateCount = (short)(_updateCount + 1);
  3578. }
  3579. }
  3580. /// <summary>
  3581. /// 结束数据更新
  3582. /// </summary>
  3583. private void EndUpdateInternal()
  3584. {
  3585. if (_updateCount <= 0)
  3586. {
  3587. return;
  3588. }
  3589. _updateCount = (short)(_updateCount - 1);
  3590. if (_updateCount == 0)
  3591. {
  3592. SendMessage(new HandleRef(this, Handle), 11, -1, 0);
  3593. }
  3594. }
  3595. /// <summary>
  3596. /// 刷新该项目的数据
  3597. /// </summary>
  3598. private void RefreshItemsCurrentPositionValue()
  3599. {
  3600. bool visible = base.Visible;
  3601. if (visible)
  3602. {
  3603. BeginUpdateInternal();
  3604. }
  3605. try
  3606. {
  3607. if (_dataBinding == null)
  3608. {
  3609. return;
  3610. }
  3611. foreach (LayoutItem item in _items)
  3612. {
  3613. if ((item.ItemType == ItemType.Text || item.ItemType == ItemType.TotalText) && _dataBinding != null)
  3614. {
  3615. _dataBinding.BoundDataValueToTextItem(item as TextItem);
  3616. }
  3617. else
  3618. {
  3619. continue;
  3620. }
  3621. }
  3622. }
  3623. finally
  3624. {
  3625. if (visible)
  3626. {
  3627. EndUpdateInternal();
  3628. base.Invalidate(true);
  3629. }
  3630. }
  3631. }
  3632. /// <summary>
  3633. /// 刷新该项目的数据绑定
  3634. /// </summary>
  3635. internal void RefreshItemsBound()
  3636. {
  3637. bool visible = base.Visible;
  3638. if (visible)
  3639. {
  3640. BeginUpdateInternal();
  3641. }
  3642. try
  3643. {
  3644. if (_dataBinding == null)
  3645. {
  3646. return;
  3647. }
  3648. foreach (LayoutItem item in _items)
  3649. {
  3650. if ((item.ItemType == ItemType.Text || item.ItemType == ItemType.TotalText) && _dataBinding != null)
  3651. {
  3652. _dataBinding.BoundFieldToTextItem(item as TextItem);
  3653. }
  3654. else
  3655. {
  3656. continue;
  3657. }
  3658. }
  3659. }
  3660. finally
  3661. {
  3662. if (visible)
  3663. {
  3664. EndUpdateInternal();
  3665. base.Invalidate(true);
  3666. }
  3667. }
  3668. }
  3669. /// <summary>
  3670. /// 刷新文本项目的数据绑定
  3671. /// </summary>
  3672. /// <param name="textItem">文本Item</param>
  3673. internal void RefreshTextItemBound(TextItem textItem)
  3674. {
  3675. bool visible = base.Visible;
  3676. if (visible)
  3677. {
  3678. BeginUpdateInternal();
  3679. }
  3680. try
  3681. {
  3682. if (_dataBinding != null)
  3683. {
  3684. _dataBinding.BoundFieldToTextItem(textItem);
  3685. }
  3686. }
  3687. finally
  3688. {
  3689. if (visible)
  3690. {
  3691. EndUpdateInternal();
  3692. base.Invalidate(true);
  3693. }
  3694. }
  3695. }
  3696. /// <summary>
  3697. /// 刷新该项目的打印数据
  3698. /// </summary>
  3699. internal void RefreshItemsCurrentPositionPrintValue()
  3700. {
  3701. bool visible = base.Visible;
  3702. if (visible)
  3703. {
  3704. BeginUpdateInternal();
  3705. }
  3706. try
  3707. {
  3708. if (_printDataBinding == null)
  3709. {
  3710. return;
  3711. }
  3712. foreach (LayoutItem item in _items)
  3713. {
  3714. if ((item.ItemType == ItemType.Text || item.ItemType == ItemType.TotalText) && _printDataBinding != null)
  3715. {
  3716. _printDataBinding.BoundPrintDataValueToTextItem(item as TextItem);
  3717. }
  3718. else
  3719. {
  3720. continue;
  3721. }
  3722. }
  3723. }
  3724. finally
  3725. {
  3726. if (visible)
  3727. {
  3728. EndUpdateInternal();
  3729. base.Invalidate(true);
  3730. }
  3731. }
  3732. }
  3733. /// <summary>
  3734. /// 刷新该项目的打印数据绑定
  3735. /// </summary>
  3736. internal void RefreshItemsPrintBound()
  3737. {
  3738. bool visible = base.Visible;
  3739. if (visible)
  3740. {
  3741. BeginUpdateInternal();
  3742. }
  3743. try
  3744. {
  3745. if (_printDataBinding == null)
  3746. {
  3747. return;
  3748. }
  3749. foreach (LayoutItem item in _items)
  3750. {
  3751. if ((item.ItemType == ItemType.Text || item.ItemType == ItemType.TotalText) && _printDataBinding != null)
  3752. {
  3753. _printDataBinding.BoundPrintFieldToTextItem(item as TextItem);
  3754. }
  3755. else
  3756. {
  3757. continue;
  3758. }
  3759. }
  3760. }
  3761. finally
  3762. {
  3763. if (visible)
  3764. {
  3765. EndUpdateInternal();
  3766. base.Invalidate(true);
  3767. }
  3768. }
  3769. }
  3770. /// <summary>
  3771. /// 刷新文本项目的打印数据绑定
  3772. /// </summary>
  3773. /// <param name="textItem">文本Item</param>
  3774. internal void RefreshTextItemPrintBound(TextItem textItem)
  3775. {
  3776. bool visible = base.Visible;
  3777. if (visible)
  3778. {
  3779. BeginUpdateInternal();
  3780. }
  3781. try
  3782. {
  3783. if (_printDataBinding != null)
  3784. {
  3785. _printDataBinding.BoundPrintFieldToTextItem(textItem);
  3786. }
  3787. }
  3788. finally
  3789. {
  3790. if (visible)
  3791. {
  3792. EndUpdateInternal();
  3793. base.Invalidate(true);
  3794. }
  3795. }
  3796. }
  3797. #endregion 数据绑定相关
  3798. #region 打印相关
  3799. /// <summary>
  3800. /// 检查打印设置
  3801. /// </summary>
  3802. /// <returns>可以打印true;其他为false</returns>
  3803. public bool CheckPrintSetting()
  3804. {
  3805. // 获取打印机设置非常慢,暂不校验
  3806. return true;
  3807. //string message = this.CheckPrintSettingIn(_printType);
  3808. //if (!string.IsNullOrWhiteSpace(message))
  3809. //{
  3810. // MessageBox.Show(message,
  3811. // "打印失败",
  3812. // MessageBoxButtons.OK,
  3813. // MessageBoxIcon.Error,
  3814. // MessageBoxDefaultButton.Button1);
  3815. // return false;
  3816. //}
  3817. //return true;
  3818. }
  3819. public string CheckPrintSettingIn(PrintType printType)
  3820. {
  3821. // 获取打印机设置非常慢,暂不校验
  3822. return null;
  3823. /*
  3824. if (System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count < 1)
  3825. {
  3826. return "请安装打印机驱动程序。";
  3827. }
  3828. if (PrinterSettings == null || PrinterSettings.DefaultPageSettings == null)
  3829. {
  3830. return "请设置打印机。";
  3831. }
  3832. if (PaperSize == null)
  3833. {
  3834. return "请设置打印纸张大小。";
  3835. }
  3836. if (!printDocument.PrinterSettings.IsValid)
  3837. {
  3838. return string.Format("打印机名称({0}/{1})不正确。"
  3839. , printDocument.PrinterSettings.PrinterName
  3840. , printDialog.PrinterSettings.PrinterName);
  3841. }
  3842. if (short.MaxValue < _numberOfCopies)
  3843. {
  3844. return string.Format("此打印机一次可以打印的最大份数是{0},不能打印{1}份。"
  3845. , short.MaxValue
  3846. , _numberOfCopies);
  3847. }
  3848. if (printType == PrintType.Print || printType == PrintType.PrintPreview)
  3849. {
  3850. if (PrinterSettings.MaximumCopies < _numberOfCopies)
  3851. {
  3852. return string.Format("此打印机一次可以打印的最大份数是{0},不能打印{1}份。"
  3853. , PrinterSettings.MaximumCopies
  3854. , _numberOfCopies);
  3855. }
  3856. }
  3857. else if (printType == PrintType.AllocationPrint || printType == PrintType.AllocationPrintPreview)
  3858. {
  3859. float paperWidth = 0;
  3860. float paperHeight = 0;
  3861. float horizontal = 0;
  3862. float vertical = 0;
  3863. if (_landscape)
  3864. {
  3865. paperWidth = (float)Math.Round(LayoutCommon.Inch100ToMillimeter(PaperSize.Height), 0);
  3866. paperHeight = (float)Math.Round(LayoutCommon.Inch100ToMillimeter(PaperSize.Width), 0);
  3867. horizontal = paperWidth + _allocationHorizontalSpace
  3868. - _marginLeft
  3869. - _marginRight
  3870. - (_allocationHorizontalSpace + _paperWidth)
  3871. * _allocationNumX;
  3872. vertical = paperHeight + _allocationVerticalSpace
  3873. - _marginTop
  3874. - _marginBottom
  3875. - (_allocationVerticalSpace + _paperHeight)
  3876. * _allocationNumY;
  3877. }
  3878. else
  3879. {
  3880. paperWidth = (float)Math.Round(LayoutCommon.Inch100ToMillimeter(PaperSize.Width), 0);
  3881. paperHeight = (float)Math.Round(LayoutCommon.Inch100ToMillimeter(PaperSize.Height), 0);
  3882. horizontal = paperWidth + _allocationHorizontalSpace
  3883. - _marginLeft
  3884. - _marginRight
  3885. - (_allocationHorizontalSpace + _paperWidth)
  3886. * _allocationNumX;
  3887. vertical = paperHeight + _allocationVerticalSpace
  3888. - _marginTop
  3889. - _marginBottom
  3890. - (_allocationVerticalSpace + _paperHeight)
  3891. * _allocationNumY;
  3892. }
  3893. if (horizontal < 0 && vertical < 0)
  3894. {
  3895. return "设置值大于纸张尺寸。请检查纸张横纵个数,间距,和余白。";
  3896. }
  3897. if (horizontal < 0)
  3898. {
  3899. return string.Format("分配的纸张宽度不能打印{0}列,请确认。"
  3900. , _allocationNumX);
  3901. }
  3902. if (vertical < 0)
  3903. {
  3904. return string.Format("分配的纸张高度不能打印{0}行,请确认。"
  3905. , _allocationNumY);
  3906. }
  3907. }
  3908. return null;
  3909. */
  3910. }
  3911. /// <summary>
  3912. /// 打印背景图片
  3913. /// </summary>
  3914. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  3915. private void PrintBackgroundImage(Graphics graphics)
  3916. {
  3917. if (BackgroundImage != null)
  3918. {
  3919. // 背景图片的高宽比例
  3920. float imgWHRate = (float)BackgroundImage.Width / (float)BackgroundImage.Height;
  3921. // 打印纸张的高宽比例
  3922. float paperWHRate = PaperWidth / PaperHeight;
  3923. // 背景图片打印范围
  3924. RectangleF newImgRectangleF = RectangleF.Empty;
  3925. // 缩放背景图片,但保持图片高宽比例
  3926. if (imgWHRate > paperWHRate)
  3927. {
  3928. newImgRectangleF.Width = PaperWidth;
  3929. newImgRectangleF.Height = PaperWidth / imgWHRate;
  3930. newImgRectangleF.X = _allOffsetX;
  3931. newImgRectangleF.Y = (PaperHeight - newImgRectangleF.Height) / 2 + _allOffsetY;
  3932. }
  3933. else
  3934. {
  3935. newImgRectangleF.Width = PaperHeight * imgWHRate;
  3936. newImgRectangleF.Height = PaperHeight;
  3937. newImgRectangleF.X = (PaperWidth - newImgRectangleF.Width) / 2 + _allOffsetX;
  3938. newImgRectangleF.Y = _allOffsetY;
  3939. }
  3940. // 在指定的范围绘制背景图片
  3941. graphics.DrawImage(BackgroundImage, newImgRectangleF);
  3942. }
  3943. }
  3944. /// <summary>
  3945. /// 打印文本Item
  3946. /// </summary>
  3947. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  3948. /// <param name="textItem">文本Item</param>
  3949. private void DrawTextItem(Graphics graphics, TextItem textItem)
  3950. {
  3951. // 文本Item打印范围
  3952. RectangleF rectangleFM = new RectangleF(
  3953. _allOffsetX + textItem.Left,
  3954. _allOffsetY + textItem.Top,
  3955. textItem.Width,
  3956. textItem.Height);
  3957. RectangleF rectangleF = rectangleFM;
  3958. rectangleF.Inflate(0 - LayoutConsts.TEXT_MARGIN, 0 - LayoutConsts.TEXT_MARGIN);
  3959. rectangleFM.Height -= LayoutConsts.TEXT_MARGIN;
  3960. /* Edit For Draw Pic
  3961. // 在指定的范围绘制文本Item
  3962. LayoutUtility.DrawText(graphics,
  3963. rectangleFM,
  3964. rectangleF,
  3965. textItem.PrintText,
  3966. textItem.Font,
  3967. textItem.TextColor,
  3968. textItem.LineSpace,
  3969. textItem.CharacterSpace,
  3970. textItem.CharacterCount,
  3971. textItem.TextAlign,
  3972. textItem.Wrap,
  3973. textItem.Clip,
  3974. false);
  3975. */
  3976. textItem.DrawTextItem(textItem.PrintDataValue, textItem.PrintBoundField, graphics, rectangleFM, rectangleF, true);
  3977. }
  3978. /// <summary>
  3979. /// 打印图片Item
  3980. /// </summary>
  3981. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  3982. /// <param name="imageItem">图片Item</param>
  3983. private void DrawImageItem(Graphics graphics, ImageItem imageItem)
  3984. {
  3985. // 图片Item打印范围
  3986. RectangleF rectangleF = new RectangleF(
  3987. _allOffsetX + imageItem.Left,
  3988. _allOffsetY + imageItem.Top,
  3989. imageItem.Width,
  3990. imageItem.Height);
  3991. // 在指定的范围绘制图片Item
  3992. LayoutUtility.DrawImage(graphics, rectangleF, imageItem.Image);
  3993. }
  3994. /// <summary>
  3995. /// 打印矩形Item
  3996. /// </summary>
  3997. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  3998. /// <param name="rectangleItem">矩形Item</param>
  3999. private void DrawRectangleItem(Graphics graphics, RectangleItem rectangleItem)
  4000. {
  4001. // 矩形Item打印范围
  4002. RectangleF rectangleF = new RectangleF(
  4003. _allOffsetX + rectangleItem.Left,
  4004. _allOffsetY + rectangleItem.Top,
  4005. rectangleItem.Width,
  4006. rectangleItem.Height);
  4007. float lineWidthDraw = rectangleItem.LineWidth;
  4008. // 在指定的范围绘制矩形Item
  4009. LayoutUtility.DrawRectangle(graphics, rectangleF, rectangleItem.LineColor,
  4010. rectangleItem.FillColor, lineWidthDraw, rectangleItem.Transparent);
  4011. }
  4012. /// <summary>
  4013. /// 打印椭圆Item
  4014. /// </summary>
  4015. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  4016. /// <param name="ellipseItem">椭圆Item</param>
  4017. private void DrawEllipseItem(Graphics graphics, EllipseItem ellipseItem)
  4018. {
  4019. // 椭圆Item打印范围
  4020. RectangleF rectangleF = new RectangleF(
  4021. _allOffsetX + ellipseItem.Left,
  4022. _allOffsetY + ellipseItem.Top,
  4023. ellipseItem.Width,
  4024. ellipseItem.Height);
  4025. float lineWidthDraw = ellipseItem.LineWidth;
  4026. // 在指定的范围绘制椭圆Item
  4027. LayoutUtility.DrawEllipse(graphics, rectangleF, ellipseItem.LineColor,
  4028. ellipseItem.FillColor, lineWidthDraw, ellipseItem.Transparent);
  4029. }
  4030. /// <summary>
  4031. /// 打印表格Item
  4032. /// </summary>
  4033. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  4034. /// <param name="gridItem">表格Item</param>
  4035. private void DrawGridItem(Graphics graphics, GridItem gridItem)
  4036. {
  4037. // 表格Item打印范围
  4038. RectangleF rectangleF = new RectangleF(
  4039. _allOffsetX + gridItem.Left,
  4040. _allOffsetY + gridItem.Top,
  4041. gridItem.Width,
  4042. gridItem.Height);
  4043. //// 在指定的范围绘制表格Item
  4044. gridItem.DrawGridItem(graphics, rectangleF, 1, _printPageNum);
  4045. }
  4046. /// <summary>
  4047. /// 打印页码Item
  4048. /// </summary>
  4049. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  4050. /// <param name="pageNumItem">页码Item</param>
  4051. private void DrawPageNumItem(Graphics graphics, PageNumItem pageNumItem)
  4052. {
  4053. // 文本Item打印范围
  4054. RectangleF rectangleFM = new RectangleF(
  4055. _allOffsetX + pageNumItem.Left,
  4056. _allOffsetY + pageNumItem.Top,
  4057. pageNumItem.Width,
  4058. pageNumItem.Height);
  4059. RectangleF rectangleF = rectangleFM;
  4060. rectangleF.Inflate(0 - LayoutConsts.TEXT_MARGIN, 0 - LayoutConsts.TEXT_MARGIN);
  4061. rectangleFM.Height -= LayoutConsts.TEXT_MARGIN;
  4062. // 在指定的范围绘制文本Item
  4063. LayoutUtility.DrawText(graphics,
  4064. rectangleFM,
  4065. rectangleF,
  4066. pageNumItem.PrintText,
  4067. pageNumItem.Font,
  4068. pageNumItem.TextColor,
  4069. 0,
  4070. 0,
  4071. 0,
  4072. pageNumItem.TextAlign,
  4073. pageNumItem.TextAlignVertical,
  4074. false,
  4075. false,
  4076. false);
  4077. }
  4078. /// <summary>
  4079. /// 打印合计Item
  4080. /// </summary>
  4081. /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
  4082. /// <param name="totalTextItem">合计Item</param>
  4083. private void DrawTotalTextItem(Graphics graphics, TotalTextItem totalTextItem)
  4084. {
  4085. // 文本Item打印范围
  4086. RectangleF rectangleFM = new RectangleF(
  4087. _allOffsetX + totalTextItem.Left,
  4088. _allOffsetY + totalTextItem.Top,
  4089. totalTextItem.Width,
  4090. totalTextItem.Height);
  4091. RectangleF rectangleF = rectangleFM;
  4092. rectangleF.Inflate(0 - LayoutConsts.TEXT_MARGIN, 0 - LayoutConsts.TEXT_MARGIN);
  4093. rectangleFM.Height -= LayoutConsts.TEXT_MARGIN;
  4094. if (PrintPageNum != TotalPageNum)
  4095. {
  4096. LayoutUtility.DrawText(graphics,
  4097. rectangleFM,
  4098. rectangleF,
  4099. totalTextItem.NotTotalValue,
  4100. totalTextItem.NotTotalFont,
  4101. totalTextItem.NotTotalColor,
  4102. totalTextItem.LineSpace,
  4103. totalTextItem.CharacterSpace,
  4104. totalTextItem.CharacterCount,
  4105. totalTextItem.NotTotalAlign,
  4106. totalTextItem.NotTotalAlignVertical,
  4107. totalTextItem.Wrap,
  4108. totalTextItem.Clip,
  4109. false);
  4110. return;
  4111. }
  4112. // 在指定的范围绘制文本Item
  4113. string text = totalTextItem.ResetTextValue(totalTextItem.PrintDataValue, totalTextItem.BoundField.IsDataBound) as string;
  4114. LayoutUtility.DrawText(graphics,
  4115. rectangleFM,
  4116. rectangleF,
  4117. text,
  4118. totalTextItem.Font,
  4119. totalTextItem.TextColor,
  4120. totalTextItem.LineSpace,
  4121. totalTextItem.CharacterSpace,
  4122. totalTextItem.CharacterCount,
  4123. totalTextItem.TextAlign,
  4124. totalTextItem.NotTotalAlignVertical,
  4125. totalTextItem.Wrap,
  4126. totalTextItem.Clip,
  4127. false);
  4128. }
  4129. #endregion 打印相关
  4130. #region 事件
  4131. /// <summary>
  4132. /// 当一个项目被改变,移动,添加或删除引发此事件。
  4133. /// </summary>
  4134. /// <param name="layoutItem">Item</param>
  4135. /// <param name="itemChangeType">变更类型</param>
  4136. internal void SetItemChangedArgs(LayoutItem layoutItem, ItemChangeType itemChangeType)
  4137. {
  4138. if (ItemChanged != null && !layoutItem.IsNew)
  4139. {
  4140. ItemChangedEventArgs e = new ItemChangedEventArgs();
  4141. e.Item = layoutItem;
  4142. e.ChangedType = itemChangeType;
  4143. e.ItemID = layoutItem.ID;
  4144. _isDirty = true;
  4145. ItemChanged(this, e);
  4146. }
  4147. if (itemChangeType != ItemChangeType.Deleted)
  4148. {
  4149. layoutItem.Shape.Refresh();
  4150. }
  4151. else
  4152. {
  4153. if (layoutItem.ItemType == ItemType.Grid)
  4154. {
  4155. this.OneGridItem = null;
  4156. }
  4157. }
  4158. }
  4159. /// <summary>
  4160. /// 当一个项目被改变,移动,添加或删除引发此事件。
  4161. /// </summary>
  4162. /// <param name="layoutItem">Item</param>
  4163. /// <param name="itemChangeType">变更类型</param>
  4164. /// <param name="property">属性</param>
  4165. internal void SetItemChangedArgs(LayoutItem layoutItem, ItemChangeType itemChangeType, string property)
  4166. {
  4167. if (ItemChanged != null && !layoutItem.IsNew)
  4168. {
  4169. ItemChangedEventArgs e = new ItemChangedEventArgs();
  4170. e.Item = layoutItem;
  4171. e.ChangedType = itemChangeType;
  4172. e.ItemID = layoutItem.ID;
  4173. e.Property = property;
  4174. _isDirty = true;
  4175. ItemChanged(this, e);
  4176. }
  4177. if (itemChangeType != ItemChangeType.Deleted)
  4178. {
  4179. layoutItem.Shape.Refresh();
  4180. }
  4181. else
  4182. {
  4183. if (layoutItem.ItemType == ItemType.Grid)
  4184. {
  4185. this.OneGridItem = null;
  4186. }
  4187. }
  4188. }
  4189. /// <summary>
  4190. /// 项目的选择状态改变时引发此事件。
  4191. /// </summary>
  4192. /// <param name="layoutItem">Item</param>
  4193. /// <param name="isSelect">选择状态</param>
  4194. internal void SetItemSelectionChangedArgs(LayoutItem layoutItem, bool isSelect)
  4195. {
  4196. SetTSMIOnSelectionChanged();
  4197. if (ItemSelectionChanged != null && !layoutItem.IsNew)
  4198. {
  4199. ItemSelectionChangedEventArgs e = new ItemSelectionChangedEventArgs();
  4200. e.Item = layoutItem;
  4201. e.IsSelected = isSelect;
  4202. e.ItemID = layoutItem.ID;
  4203. ItemSelectionChanged(this, e);
  4204. }
  4205. }
  4206. /// <summary>
  4207. /// 打印或显示数据之前引发此事件。
  4208. /// </summary>
  4209. /// <param name="layoutItem">Item</param>
  4210. /// <param name="value">数据</param>
  4211. /// <param name="format">格式</param>
  4212. /// <returns>e.Result格式化后结果</returns>
  4213. internal object SetItemValueFormattingArgs(LayoutItem layoutItem, object value, string format)
  4214. {
  4215. if (ItemValueFormatting != null && !layoutItem.IsNew)
  4216. {
  4217. ItemValueFormattingEventArgs e = new ItemValueFormattingEventArgs();
  4218. e.Item = layoutItem;
  4219. e.ItemID = layoutItem.ID;
  4220. e.Value = value;
  4221. e.Format = format;
  4222. ItemValueFormatting(this, e);
  4223. return e.Result;
  4224. }
  4225. return null;
  4226. }
  4227. internal void SetUserChangedGridItemArgs(GridItem gItem, CollectionChangeEventArgs e)
  4228. {
  4229. if (this.UserChangedGridItem != null)
  4230. {
  4231. this.UserChangedGridItem(gItem, e);
  4232. }
  4233. }
  4234. #endregion 事件
  4235. #endregion private函数
  4236. #region 事件处理
  4237. #region 缩放比例和位置
  4238. /// <summary>
  4239. /// 调整纸张大小
  4240. /// </summary>
  4241. /// <param name="sender">指定的对象</param>
  4242. /// <param name="e">提供的事件数据</param>
  4243. private void PaperBox_SizeChanged(object sender, EventArgs e)
  4244. {
  4245. if (_zoomType == ZoomType.Whole)
  4246. {
  4247. InitializeZoom();
  4248. }
  4249. else
  4250. {
  4251. AdjustPaperArea();
  4252. }
  4253. }
  4254. #endregion 缩放比例和位置
  4255. #region 右键菜单
  4256. /// <summary>
  4257. /// 打开右键菜单
  4258. /// </summary>
  4259. /// <param name="sender">指定的对象</param>
  4260. /// <param name="e">提供的事件数据</param>
  4261. private void contextMenu_Opening(object sender, CancelEventArgs e)
  4262. {
  4263. //SetTSMIOnSelectionChanged();
  4264. tsmiPaste.Enabled = (_clipboardFormat != null && Clipboard.ContainsData(_clipboardFormat.Name));
  4265. }
  4266. /// <summary>
  4267. /// 打开对齐右键菜单
  4268. /// </summary>
  4269. /// <param name="sender">指定的对象</param>
  4270. /// <param name="e">提供的事件数据</param>
  4271. private void contextMenuArrange_Opening(object sender, CancelEventArgs e)
  4272. {
  4273. e.Cancel = !tsmiArrange.Enabled;
  4274. }
  4275. /// <summary>
  4276. /// 打开排序右键菜单
  4277. /// </summary>
  4278. /// <param name="sender">指定的对象</param>
  4279. /// <param name="e">提供的事件数据</param>
  4280. private void contextMenuOrder_Opening(object sender, CancelEventArgs e)
  4281. {
  4282. e.Cancel = !tsmiOrder.Enabled;
  4283. }
  4284. /// <summary>
  4285. /// 关闭右键菜单
  4286. /// </summary>
  4287. /// <param name="sender">指定的对象</param>
  4288. /// <param name="e">提供的事件数据</param>
  4289. private void contextMenu_Closing(object sender, ToolStripDropDownClosingEventArgs e)
  4290. {
  4291. tsmiPaste.Enabled = true;
  4292. }
  4293. /// <summary>
  4294. /// 删除
  4295. /// </summary>
  4296. /// <param name="sender">指定的对象</param>
  4297. /// <param name="e">提供的事件数据</param>
  4298. private void tsmiDelete_Click(object sender, EventArgs e)
  4299. {
  4300. DeleteItem();
  4301. }
  4302. /// <summary>
  4303. /// 属性
  4304. /// </summary>
  4305. /// <param name="sender">指定的对象</param>
  4306. /// <param name="e">提供的事件数据</param>
  4307. private void tsmiProperties_Click(object sender, EventArgs e)
  4308. {
  4309. ShowItemPropertyDialog();
  4310. }
  4311. /// <summary>
  4312. /// 选择全部Item
  4313. /// </summary>
  4314. /// <param name="sender">指定的对象</param>
  4315. /// <param name="e">提供的事件数据</param>
  4316. private void tsmiAllSelect_Click(object sender, EventArgs e)
  4317. {
  4318. SelectItemsMultiple(_items.ToArray(), true);
  4319. }
  4320. /// <summary>
  4321. /// 纸张大小
  4322. /// </summary>
  4323. /// <param name="sender">指定的对象</param>
  4324. /// <param name="e">提供的事件数据</param>
  4325. private void tsmiPaperSize_Click(object sender, EventArgs e)
  4326. {
  4327. ShowPaperPropertyDialog();
  4328. }
  4329. /// <summary>
  4330. /// 背景图片
  4331. /// </summary>
  4332. /// <param name="sender">指定的对象</param>
  4333. /// <param name="e">提供的事件数据</param>
  4334. private void tsmiBackgroundImage_Click(object sender, EventArgs e)
  4335. {
  4336. ShowBackgroundSettingDialog();
  4337. }
  4338. /// <summary>
  4339. /// 打开缩放比例右键菜单
  4340. /// </summary>
  4341. /// <param name="sender">指定的对象</param>
  4342. /// <param name="e">提供的事件数据</param>
  4343. private void contextMenuZoom_Opening(object sender, CancelEventArgs e)
  4344. {
  4345. tsmiZoom500.Checked = false;
  4346. tsmiZoom200.Checked = false;
  4347. tsmiZoom100.Checked = false;
  4348. tsmiZoom75.Checked = false;
  4349. tsmiZoom50.Checked = false;
  4350. tsmiZoom33.Checked = false;
  4351. tsmiZoom25.Checked = false;
  4352. tsmiZoom10.Checked = false;
  4353. tsmiZoomWhole.Checked = false;
  4354. switch (_zoom)
  4355. {
  4356. case 500:
  4357. tsmiZoom500.Checked = true;
  4358. break;
  4359. case 200:
  4360. tsmiZoom200.Checked = true;
  4361. break;
  4362. case 100:
  4363. tsmiZoom100.Checked = true;
  4364. break;
  4365. case 75:
  4366. tsmiZoom75.Checked = true;
  4367. break;
  4368. case 50:
  4369. tsmiZoom50.Checked = true;
  4370. break;
  4371. case 33:
  4372. tsmiZoom33.Checked = true;
  4373. break;
  4374. case 25:
  4375. tsmiZoom25.Checked = true;
  4376. break;
  4377. case 10:
  4378. tsmiZoom10.Checked = true;
  4379. break;
  4380. default:
  4381. tsmiZoomWhole.Checked = true;
  4382. break;
  4383. }
  4384. }
  4385. /// <summary>
  4386. /// 缩放比例
  4387. /// </summary>
  4388. /// <param name="sender">指定的对象</param>
  4389. /// <param name="e">提供的事件数据</param>
  4390. private void tsmiZoom_Click(object sender, EventArgs e)
  4391. {
  4392. ToolStripMenuItem itemZoom = sender as ToolStripMenuItem;
  4393. if (itemZoom == null)
  4394. {
  4395. return;
  4396. }
  4397. int zoom = System.Convert.ToInt32(itemZoom.Tag);
  4398. if (zoom > 0)
  4399. {
  4400. Zoom = zoom;
  4401. }
  4402. else
  4403. {
  4404. ZoomType = ZoomType.Whole;
  4405. }
  4406. }
  4407. /// <summary>
  4408. /// 置于顶层
  4409. /// </summary>
  4410. /// <param name="sender">指定的对象</param>
  4411. /// <param name="e">提供的事件数据</param>
  4412. private void tsmiForemost_Click(object sender, EventArgs e)
  4413. {
  4414. ChangeItemsOrderForemost();
  4415. }
  4416. /// <summary>
  4417. /// 上移一层
  4418. /// </summary>
  4419. /// <param name="sender">指定的对象</param>
  4420. /// <param name="e">提供的事件数据</param>
  4421. private void tsmiFront_Click(object sender, EventArgs e)
  4422. {
  4423. ChangeItemsOrderFront();
  4424. }
  4425. /// <summary>
  4426. /// 下移一层
  4427. /// </summary>
  4428. /// <param name="sender">指定的对象</param>
  4429. /// <param name="e">提供的事件数据</param>
  4430. private void tsmiBack_Click(object sender, EventArgs e)
  4431. {
  4432. ChangeItemsOrderBack();
  4433. }
  4434. /// <summary>
  4435. /// 置于底层
  4436. /// </summary>
  4437. /// <param name="sender">指定的对象</param>
  4438. /// <param name="e">提供的事件数据</param>
  4439. private void tsmiAftermost_Click(object sender, EventArgs e)
  4440. {
  4441. ChangeItemsOrderAftermost();
  4442. }
  4443. /// <summary>
  4444. /// 顶端对齐
  4445. /// </summary>
  4446. /// <param name="sender">指定的对象</param>
  4447. /// <param name="e">提供的事件数据</param>
  4448. private void tsmiTop_Click(object sender, EventArgs e)
  4449. {
  4450. ArrangeItemsTop();
  4451. }
  4452. /// <summary>
  4453. /// 底端对齐
  4454. /// </summary>
  4455. /// <param name="sender">指定的对象</param>
  4456. /// <param name="e">提供的事件数据</param>
  4457. private void tsmiBottom_Click(object sender, EventArgs e)
  4458. {
  4459. ArrangeItemsBottom();
  4460. }
  4461. /// <summary>
  4462. /// 垂直居中对齐
  4463. /// </summary>
  4464. /// <param name="sender">指定的对象</param>
  4465. /// <param name="e">提供的事件数据</param>
  4466. private void tsmiVerticalCenter_Click(object sender, EventArgs e)
  4467. {
  4468. ArrangeItemsVerticalCenter();
  4469. }
  4470. /// <summary>
  4471. /// 右侧对齐
  4472. /// </summary>
  4473. /// <param name="sender">指定的对象</param>
  4474. /// <param name="e">提供的事件数据</param>
  4475. private void tsmiRight_Click(object sender, EventArgs e)
  4476. {
  4477. ArrangeItemsRight();
  4478. }
  4479. /// <summary>
  4480. /// 左侧对齐
  4481. /// </summary>
  4482. /// <param name="sender">指定的对象</param>
  4483. /// <param name="e">提供的事件数据</param>
  4484. private void tsmiLeft_Click(object sender, EventArgs e)
  4485. {
  4486. ArrangeItemsLeft();
  4487. }
  4488. /// <summary>
  4489. /// 水平居中对齐
  4490. /// </summary>
  4491. /// <param name="sender">指定的对象</param>
  4492. /// <param name="e">提供的事件数据</param>
  4493. private void tsmiHorizontalCenter_Click(object sender, EventArgs e)
  4494. {
  4495. ArrangeItemsHorizontalCenter();
  4496. }
  4497. /// <summary>
  4498. /// 剪切
  4499. /// </summary>
  4500. /// <param name="sender">指定的对象</param>
  4501. /// <param name="e">提供的事件数据</param>
  4502. private void tsmiCut_Click(object sender, EventArgs e)
  4503. {
  4504. CutItem();
  4505. }
  4506. /// <summary>
  4507. /// 复制
  4508. /// </summary>
  4509. /// <param name="sender">指定的对象</param>
  4510. /// <param name="e">提供的事件数据</param>
  4511. private void tsmiCopy_Click(object sender, EventArgs e)
  4512. {
  4513. CopyItem();
  4514. }
  4515. /// <summary>
  4516. /// 粘贴
  4517. /// </summary>
  4518. /// <param name="sender">指定的对象</param>
  4519. /// <param name="e">提供的事件数据</param>
  4520. private void tsmiPaste_Click(object sender, EventArgs e)
  4521. {
  4522. PasteItem();
  4523. }
  4524. #endregion 右键菜单
  4525. #region Item操作相关
  4526. /// <summary>
  4527. /// Item选择边框失去焦点时发生。
  4528. /// </summary>
  4529. /// <param name="sender">指定的对象</param>
  4530. /// <param name="e">提供的事件数据</param>
  4531. private void RectTracker_LostFocus(object sender, EventArgs e)
  4532. {
  4533. if (_moveRectangles != null && _isMouseMove)
  4534. {
  4535. //if (!_isRealFollow)
  4536. {
  4537. int count = _moveRectangles.Length;
  4538. for (int i = 0; i < count; i++)
  4539. {
  4540. DrawReversibleFrame(_selectedItems[i].ItemLockType, _moveRectangles[i], null);
  4541. }
  4542. }
  4543. _isMouseDown = false;
  4544. _isMouseMove = false;
  4545. _moveRectangles = null;
  4546. }
  4547. }
  4548. /// <summary>
  4549. /// Item选择边框 KeyDown事件
  4550. /// </summary>
  4551. /// <param name="sender">指定的对象</param>
  4552. /// <param name="e">提供的事件数据</param>
  4553. private void RectTracker_KeyDown(object sender, KeyEventArgs e)
  4554. {
  4555. if (e.KeyCode == Keys.ShiftKey && !_isShiftDown)
  4556. {
  4557. _isShiftDown = true;
  4558. }
  4559. else if (e.KeyCode == Keys.ControlKey && !_isCtrlDown)
  4560. {
  4561. _isCtrlDown = true;
  4562. }
  4563. else
  4564. {
  4565. return;
  4566. }
  4567. if (_isMouseMove)
  4568. {
  4569. int count = _moveRectangles.Length;
  4570. for (int i = 0; i < count; i++)
  4571. {
  4572. ResizeToRectangle(new Point(_mouseMoveX, _mouseMoveY), i);
  4573. }
  4574. }
  4575. }
  4576. /// <summary>
  4577. /// Item选择边框 KeyUp事件
  4578. /// </summary>
  4579. /// <param name="sender">指定的对象</param>
  4580. /// <param name="e">提供的事件数据</param>
  4581. private void RectTracker_KeyUp(object sender, KeyEventArgs e)
  4582. {
  4583. if (e.KeyCode == Keys.ShiftKey)
  4584. {
  4585. _isShiftDown = false;
  4586. }
  4587. else if (e.KeyCode == Keys.ControlKey)
  4588. {
  4589. _isCtrlDown = false;
  4590. }
  4591. else
  4592. {
  4593. return;
  4594. }
  4595. if (_isMouseMove)
  4596. {
  4597. int count = _moveRectangles.Length;
  4598. for (int i = 0; i < count; i++)
  4599. {
  4600. ResizeToRectangle(new Point(_mouseMoveX, _mouseMoveY), i);
  4601. }
  4602. }
  4603. }
  4604. /// <summary>
  4605. /// Item选择边框 MouseDown事件
  4606. /// </summary>
  4607. /// <param name="sender">指定的对象</param>
  4608. /// <param name="e">提供的事件数据</param>
  4609. private void RectTracker_MouseDown(object sender, MouseEventArgs e)
  4610. {
  4611. if (e.Button == MouseButtons.Left)
  4612. {
  4613. if (ItemShapeContainer.ClientRectangle.Contains(e.Location))
  4614. {
  4615. _startX = e.X;
  4616. _startY = e.Y;
  4617. _mouseMoveX = e.X;
  4618. _mouseMoveY = e.Y;
  4619. int count = _selectedItems.Count;
  4620. _downRectangles = new Rectangle[count];
  4621. _moveRectangles = new Rectangle[count];
  4622. for (int i = 0; i < count; i++)
  4623. {
  4624. _downRectangles[i] = _selectedItems[i].ShapeBounds;
  4625. _moveRectangles[i] = _downRectangles[i];
  4626. }
  4627. _isMouseDown = true;
  4628. Cursor.Current = Cursors.SizeAll;
  4629. }
  4630. }
  4631. }
  4632. /// <summary>
  4633. /// Item选择边框 MouseMove事件
  4634. /// </summary>
  4635. /// <param name="sender">指定的对象</param>
  4636. /// <param name="e">提供的事件数据</param>
  4637. private void RectTracker_MouseMove(object sender, MouseEventArgs e)
  4638. {
  4639. if (e.Button == MouseButtons.Left && _isMouseDown)
  4640. {
  4641. Cursor.Current = Cursors.SizeAll;
  4642. int mx = e.X;
  4643. int my = e.Y;
  4644. if (_mouseMoveX != mx || _mouseMoveY != my)
  4645. {
  4646. int count = _moveRectangles.Length;
  4647. for (int i = 0; i < count; i++)
  4648. {
  4649. ResizeToRectangle(new Point(mx, my), i);
  4650. }
  4651. _isMouseMove = true;
  4652. _mouseMoveX = mx;
  4653. _mouseMoveY = my;
  4654. }
  4655. }
  4656. }
  4657. /// <summary>
  4658. /// Item选择边框 MouseUp事件
  4659. /// </summary>
  4660. /// <param name="sender">指定的对象</param>
  4661. /// <param name="e">提供的事件数据</param>
  4662. private void RectTracker_MouseUp(object sender, MouseEventArgs e)
  4663. {
  4664. if (e.Button == MouseButtons.Left && _isMouseDown)
  4665. {
  4666. if (_isMouseMove)
  4667. {
  4668. Capture = false;
  4669. int count = _moveRectangles.Length;
  4670. for (int i = 0; i < count; i++)
  4671. {
  4672. DrawReversibleFrame(_selectedItems[i].ItemLockType, _moveRectangles[i], null);
  4673. DragHandler(_selectedItems[i], _moveRectangles[i]);
  4674. }
  4675. }
  4676. _isMouseDown = false;
  4677. _isMouseMove = false;
  4678. _moveRectangles = null;
  4679. }
  4680. }
  4681. /// <summary>
  4682. /// 鼠标在纸张范围内移动
  4683. /// </summary>
  4684. /// <param name="sender">指定的对象</param>
  4685. /// <param name="e">提供的事件数据</param>
  4686. private void paperArea_MouseMove(object sender, MouseEventArgs e)
  4687. {
  4688. if (e.Button == MouseButtons.Left && _layoutMode == LayoutMode.Edit)
  4689. {
  4690. if (_isMouseDownInPaperArea)
  4691. {
  4692. _mouseRect.X = e.X;
  4693. _mouseRect.Y = e.Y;
  4694. _isMouseDownInPaperArea = false;
  4695. _isMouseMoveInPaperArea = true;
  4696. }
  4697. else if (_isMouseMoveInPaperArea)
  4698. {
  4699. ResizeToRectangle(e.Location);
  4700. }
  4701. }
  4702. }
  4703. /// <summary>
  4704. /// 鼠标在纸张范围内抬起
  4705. /// </summary>
  4706. /// <param name="sender">指定的对象</param>
  4707. /// <param name="e">提供的事件数据</param>
  4708. private void paperArea_MouseUp(object sender, MouseEventArgs e)
  4709. {
  4710. if (_layoutMode != LayoutMode.Edit || e.Button != MouseButtons.Left)
  4711. {
  4712. return;
  4713. }
  4714. if (_isMouseMoveInPaperArea)
  4715. {
  4716. Capture = false;
  4717. DrawReversibleFrame(_mouseRect);
  4718. bool isChanging = false;
  4719. if (Items.Count > 0)
  4720. {
  4721. if (_mouseRect.Height > 0 && _mouseRect.Width > 0)
  4722. {
  4723. //_mouseRect.X = _mouseRect.X - paperArea.Left;
  4724. //_mouseRect.Y = _mouseRect.Y - paperArea.Top;
  4725. }
  4726. else if (_mouseRect.Height <= 0 && _mouseRect.Width > 0)
  4727. {
  4728. //_mouseRect.X = _mouseRect.X - paperArea.Left;
  4729. _mouseRect.Y = e.Y;
  4730. _mouseRect.Height = Math.Abs(_mouseRect.Height);
  4731. //_mouseRect.Width = Math.Abs(_mouseRect.Width);
  4732. }
  4733. else if (_mouseRect.Height > 0 && _mouseRect.Width <= 0)
  4734. {
  4735. _mouseRect.X = e.X;
  4736. //_mouseRect.Y = _mouseRect.Y - paperArea.Top;
  4737. _mouseRect.Width = Math.Abs(_mouseRect.Width);
  4738. }
  4739. else if (_mouseRect.Height <= 0 && _mouseRect.Width <= 0)
  4740. {
  4741. _mouseRect.X = e.X;
  4742. _mouseRect.Y = e.Y;
  4743. _mouseRect.Height = Math.Abs(_mouseRect.Height);
  4744. _mouseRect.Width = Math.Abs(_mouseRect.Width);
  4745. }
  4746. ItemSelectionChanging = true;
  4747. if (Control.ModifierKeys == Keys.Control)
  4748. {
  4749. foreach (LayoutItem item in _items)
  4750. {
  4751. if (item.BoundsContainsBy(_mouseRect))
  4752. {
  4753. SelectItemMultiple(item, !item.Selected, ref isChanging);
  4754. }
  4755. }
  4756. }
  4757. else if (Control.ModifierKeys == Keys.Shift)
  4758. {
  4759. foreach (LayoutItem item in _items)
  4760. {
  4761. if (item.BoundsContainsBy(_mouseRect))
  4762. {
  4763. SelectItemMultiple(item, true, ref isChanging);
  4764. }
  4765. }
  4766. }
  4767. else
  4768. {
  4769. foreach (LayoutItem item in _items)
  4770. {
  4771. //if (item.BoundsContainsBy(_mouseRect))
  4772. //{
  4773. // SelectItemMultiple(item, true, ref isChanging);
  4774. //}
  4775. //else
  4776. //{
  4777. // SelectItemMultiple(item, false, ref isChanging);
  4778. //}
  4779. bool isSelet = false;
  4780. if (item.ShapeBounds.Height <= 1 ||
  4781. item.ShapeBounds.Width <= 1)
  4782. {
  4783. isSelet = item.IntersectsWith(_mouseRect);
  4784. }
  4785. else
  4786. {
  4787. isSelet = item.BoundsContainsBy(_mouseRect);
  4788. }
  4789. SelectItemMultiple(item, isSelet, ref isChanging);
  4790. }
  4791. }
  4792. }
  4793. ItemSelectionChanging = false;
  4794. if (isChanging)
  4795. {
  4796. if (UserSelectionChanged != null)
  4797. {
  4798. UserSelectionChanged(this, EventArgs.Empty);
  4799. }
  4800. }
  4801. _isMouseMoveInPaperArea = false;
  4802. _mouseRect = Rectangle.Empty;
  4803. }
  4804. }
  4805. /// <summary>
  4806. /// 鼠标在纸张范围内按下
  4807. /// </summary>
  4808. /// <param name="sender">指定的对象</param>
  4809. /// <param name="e">提供的事件数据</param>
  4810. private void paperArea_MouseDown(object sender, MouseEventArgs e)
  4811. {
  4812. if (_layoutMode != LayoutMode.Edit)
  4813. {
  4814. return;
  4815. }
  4816. if (e.Button == MouseButtons.Left)
  4817. {
  4818. _isMouseDownInPaperArea = true;
  4819. _isMouseMoveInPaperArea = false;
  4820. }
  4821. }
  4822. /// <summary>
  4823. /// 鼠标在纸张范围内点击
  4824. /// </summary>
  4825. /// <param name="sender">指定的对象</param>
  4826. /// <param name="e">提供的事件数据</param>
  4827. private void paperArea_Click(object sender, EventArgs e)
  4828. {
  4829. if (_layoutMode != LayoutMode.Edit)
  4830. {
  4831. return;
  4832. }
  4833. // 选择状态全部解除
  4834. if (Control.ModifierKeys == Keys.Control || Control.ModifierKeys == Keys.Shift)
  4835. {
  4836. return;
  4837. }
  4838. ClearSelection();
  4839. paperArea.Focus();
  4840. }
  4841. #endregion Item操作相关
  4842. #region 打印相关
  4843. //private PageSettings _defaultPageSettings = null;
  4844. //private PaperSize _defaultPaperSize = null;
  4845. //private RectangleF _defaultPrintableArea = RectangleF.Empty;
  4846. /// <summary>
  4847. /// 打印开始。
  4848. /// </summary>
  4849. /// <param name="sender">指定的对象</param>
  4850. /// <param name="e">提供的事件数据</param>
  4851. private void printDocument_BeginPrint(object sender, PrintEventArgs e)
  4852. {
  4853. //#if DEBUG
  4854. // Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
  4855. // "printDocument_BeginPrint",
  4856. // "begin",
  4857. // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
  4858. // Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
  4859. //#endif
  4860. // 选定打印页码 modify by chenxy 2014-03-27 begin
  4861. //if (PrinterSettings.PrintRange == PrintRange.SomePages)
  4862. //{
  4863. // if (PrinterSettings.ToPage == 0 || TotalPageNum < PrinterSettings.FromPage)
  4864. // {
  4865. // e.Cancel = true;
  4866. // return;
  4867. // }
  4868. //}
  4869. // 选定打印页码 modify by chenxy 2014-03-27 end
  4870. //if (_numberOfCopies < 1 || !CheckPrintSetting())
  4871. //{
  4872. // e.Cancel = true;
  4873. // return;
  4874. //}
  4875. // 获取打印机配置非常慢,
  4876. //_defaultPageSettings = PrinterSettings.DefaultPageSettings;
  4877. //_defaultPaperSize = _defaultPageSettings.PaperSize;
  4878. //_defaultPrintableArea = _defaultPageSettings.PrintableArea;
  4879. // 页面左上点的坐标(1/100英寸)
  4880. if (_printAction == PrintAction.PrintToPreview)
  4881. {
  4882. _printerOffsetX = 0;
  4883. _printerOffsetY = 0;
  4884. _allOffsetX = _printOffsetX;
  4885. _allOffsetY = _printOffsetY;
  4886. }
  4887. else
  4888. {
  4889. //if (_landscape)
  4890. //{
  4891. // _printerOffsetX = LayoutCommon.Inch100ToMillimeter(_defaultPaperSize.Height - _defaultPrintableArea.Bottom);
  4892. // _printerOffsetY = LayoutCommon.Inch100ToMillimeter(_defaultPrintableArea.Left);
  4893. //}
  4894. //else
  4895. //{
  4896. // _printerOffsetX = LayoutCommon.Inch100ToMillimeter(_defaultPrintableArea.Left);
  4897. // _printerOffsetY = LayoutCommon.Inch100ToMillimeter(_defaultPrintableArea.Top);
  4898. //}
  4899. _allOffsetX = _printOffsetX - _printerOffsetX;
  4900. _allOffsetY = _printOffsetY - _printerOffsetY;
  4901. }
  4902. _rectangleFGreed = RectangleF.Empty;
  4903. _printPageNum = 1;
  4904. if (_printType == PrintType.PrintTest)
  4905. {
  4906. //printDocument.PrinterSettings.Copies = 1;
  4907. }
  4908. else if (_printType == PrintType.Print || _printType == PrintType.PrintPreview)
  4909. {
  4910. if (_printDataBinding != null && _printDataBinding.BindingSource != null && 0 < _printDataBinding.BindingSource.Count)
  4911. {
  4912. _printDataBinding.BindingSource.MoveFirst();
  4913. _printDataTotal = _printDataBinding.BindingSource.Count * _numberOfCopies;
  4914. }
  4915. else
  4916. {
  4917. _printDataTotal = _numberOfCopies;
  4918. }
  4919. _printDataPosition = 0;
  4920. _printDataCopies = 0;
  4921. //if (_printType == PrintType.Print)
  4922. if (_printAction == PrintAction.PrintToPrinter)
  4923. {
  4924. printDocument.PrinterSettings.Copies = (short)_numberOfCopies;
  4925. }
  4926. else
  4927. {
  4928. printDocument.PrinterSettings.Copies = 1;
  4929. }
  4930. }
  4931. else if (_printType == PrintType.AllocationPrint || _printType == PrintType.AllocationPrintPreview)
  4932. {
  4933. if (_printDataBinding != null && _printDataBinding.BindingSource != null && 0 < _printDataBinding.BindingSource.Count)
  4934. {
  4935. _printDataBinding.BindingSource.MoveFirst();
  4936. _printDataTotal = _printDataBinding.BindingSource.Count * _numberOfCopies;
  4937. }
  4938. else
  4939. {
  4940. _printDataTotal = _numberOfCopies;
  4941. }
  4942. _printDataPosition = 0;
  4943. _printDataCopies = 0;
  4944. _allocationNum = _allocationNumY * _allocationNumX;
  4945. _printDataTotal = _printDataTotal + _allocationSkipCount;
  4946. printDocument.PrinterSettings.Copies = 1;
  4947. }
  4948. else
  4949. {
  4950. printDocument.PrinterSettings.Copies = 1;
  4951. }
  4952. //#if DEBUG
  4953. // Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
  4954. // "printDocument_BeginPrint",
  4955. // "end",
  4956. // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
  4957. // Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
  4958. //#endif
  4959. }
  4960. // 选定打印页码 modify by chenxy 2014-03-27 begin
  4961. /// <summary>
  4962. /// 打印每页
  4963. /// </summary>
  4964. /// <param name="sender">指定的对象</param>
  4965. /// <param name="e">提供的事件数据</param>
  4966. private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
  4967. {
  4968. //#if DEBUG
  4969. // Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
  4970. // "PrintDocument_PrintPage",
  4971. // "begin",
  4972. // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
  4973. // Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
  4974. //#endif
  4975. /*
  4976. if (PrinterSettings.PrintRange == PrintRange.SomePages)
  4977. {
  4978. bool isPrintCurrentPage = false;
  4979. do
  4980. {
  4981. if (_printPageNum < PrinterSettings.FromPage)
  4982. {
  4983. isPrintCurrentPage = false;
  4984. }
  4985. else if (PrinterSettings.ToPage < _printPageNum)
  4986. {
  4987. isPrintCurrentPage = false;
  4988. e.HasMorePages = false;
  4989. e.Cancel = true;
  4990. return;
  4991. }
  4992. else
  4993. {
  4994. isPrintCurrentPage = true;
  4995. }
  4996. if (isPrintCurrentPage)
  4997. {
  4998. e.Cancel = false;
  4999. this.PrintPage(e);
  5000. if (_printPageNum - 1 == PrinterSettings.ToPage)
  5001. {
  5002. e.HasMorePages = false;
  5003. }
  5004. }
  5005. else
  5006. {
  5007. e.Cancel = true;
  5008. if (_printType == PrintType.PrintTest)
  5009. {
  5010. e.HasMorePages = false;
  5011. return;
  5012. }
  5013. else if (_printType == PrintType.Print || _printType == PrintType.PrintPreview)
  5014. {
  5015. // 表格Item是否有下一页
  5016. if (_printPageNum < TotalPageNum)
  5017. {
  5018. _printPageNum++;
  5019. e.HasMorePages = true;
  5020. //return;
  5021. }
  5022. else
  5023. {
  5024. if (_printAction == PrintAction.PrintToPrinter)
  5025. {
  5026. // 纸张数是否增加判断
  5027. if (_printDataBinding != null && _printDataBinding.BindingSource != null && 0 < _printDataBinding.BindingSource.Count)
  5028. {
  5029. if (_printDataBinding.BindingSource.Position + 1 < _printDataBinding.BindingSource.Count)
  5030. {
  5031. // 打印数据源下移一条记录
  5032. _printDataBinding.BindingSource.MoveNext();
  5033. _printPageNum = 1;
  5034. e.HasMorePages = true;
  5035. }
  5036. else
  5037. {
  5038. e.HasMorePages = false;
  5039. return;
  5040. }
  5041. }
  5042. else
  5043. {
  5044. e.HasMorePages = false;
  5045. return;
  5046. }
  5047. }
  5048. else
  5049. {
  5050. // 纸张数是否增加判断
  5051. if (++_printDataPosition < _printDataTotal)
  5052. {
  5053. _printPageNum = 1;
  5054. e.HasMorePages = true;
  5055. // 同一数据一次印刷/分批印刷
  5056. if (true)
  5057. {
  5058. // 同一数据一次印刷
  5059. if (++_printDataCopies < _numberOfCopies)
  5060. {
  5061. return;
  5062. }
  5063. else
  5064. {
  5065. _printDataCopies = 0;
  5066. }
  5067. }
  5068. }
  5069. else
  5070. {
  5071. e.HasMorePages = false;
  5072. return;
  5073. }
  5074. if (_printDataBinding != null && _printDataBinding.BindingSource != null && 0 < _printDataBinding.BindingSource.Count)
  5075. {
  5076. // 打印数据源下移一条记录
  5077. if (_printDataBinding.BindingSource.Position + 1 < _printDataBinding.BindingSource.Count)
  5078. {
  5079. _printDataBinding.BindingSource.MoveNext();
  5080. }
  5081. else
  5082. {
  5083. _printDataBinding.BindingSource.MoveFirst();
  5084. }
  5085. }
  5086. }
  5087. }
  5088. }
  5089. else if (_printType == PrintType.AllocationPrint || _printType == PrintType.AllocationPrintPreview)
  5090. {
  5091. int allocationIndex = -1;
  5092. // 一张纸打印
  5093. for (int i = 0; i < _allocationNum; i++)
  5094. {
  5095. allocationIndex++;
  5096. if (_printDataPosition < _allocationSkipCount)
  5097. {
  5098. // 跳过的打印位置
  5099. e.HasMorePages = true;
  5100. continue;
  5101. }
  5102. // 表格Item是否有下一页
  5103. if (_printPageNum < TotalPageNum)
  5104. {
  5105. _printPageNum++;
  5106. continue;
  5107. }
  5108. // 纸张数是否增加判断
  5109. if (++_printDataPosition < _printDataTotal)
  5110. {
  5111. _printPageNum = 1;
  5112. e.HasMorePages = true;
  5113. // 同一数据一次印刷/分批印刷
  5114. if (true)
  5115. {
  5116. // 同一数据一次印刷
  5117. if (++_printDataCopies < _numberOfCopies)
  5118. {
  5119. continue;
  5120. }
  5121. else
  5122. {
  5123. _printDataCopies = 0;
  5124. }
  5125. }
  5126. }
  5127. else
  5128. {
  5129. e.HasMorePages = false;
  5130. break;
  5131. }
  5132. if (_printDataBinding != null && _printDataBinding.BindingSource != null && 1 < _printDataBinding.BindingSource.Count)
  5133. {
  5134. // 打印数据源下移一条记录
  5135. if (_printDataBinding.BindingSource.Position + 1 < _printDataBinding.BindingSource.Count)
  5136. {
  5137. _printDataBinding.BindingSource.MoveNext();
  5138. }
  5139. else
  5140. {
  5141. _printDataBinding.BindingSource.MoveFirst();
  5142. }
  5143. }
  5144. }
  5145. // 纸张数是否增加判断
  5146. if (e.HasMorePages && _printDataPosition < _printDataTotal)
  5147. {
  5148. e.HasMorePages = true;
  5149. }
  5150. else
  5151. {
  5152. e.HasMorePages = false;
  5153. return;
  5154. }
  5155. }
  5156. else
  5157. {
  5158. e.HasMorePages = false;
  5159. return;
  5160. }
  5161. //e.HasMorePages = false;
  5162. //e.Graphics.Clear(Color.White);
  5163. }
  5164. } while (!isPrintCurrentPage);
  5165. }
  5166. else
  5167. */
  5168. {
  5169. this.PrintPage(e);
  5170. }
  5171. //#if DEBUG
  5172. // Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
  5173. // "PrintDocument_PrintPage",
  5174. // "end",
  5175. // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
  5176. // Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
  5177. //#endif
  5178. }
  5179. private void PrintPage(PrintPageEventArgs e)
  5180. {
  5181. e.Graphics.PageUnit = GraphicsUnit.Millimeter;
  5182. if (_isPrintAreaVisible && _rectangleFGreed.IsEmpty)
  5183. {
  5184. //if (_landscape)
  5185. //{
  5186. // // 横向打印
  5187. // _rectangleFGreed = new RectangleF(
  5188. // (_defaultPaperSize.Height - _defaultPrintableArea.Bottom) / 100 * e.Graphics.DpiY,
  5189. // _defaultPrintableArea.Left / 100 * e.Graphics.DpiX,
  5190. // _defaultPrintableArea.Height / 100 * e.Graphics.DpiY,
  5191. // _defaultPrintableArea.Width / 100 * e.Graphics.DpiX);
  5192. //}
  5193. //else
  5194. //{
  5195. // // 纵向打印
  5196. // _rectangleFGreed = new RectangleF(
  5197. // _defaultPrintableArea.Left / 100 * e.Graphics.DpiX,
  5198. // _defaultPrintableArea.Top / 100 * e.Graphics.DpiY,
  5199. // _defaultPrintableArea.Width / 100 * e.Graphics.DpiX,
  5200. // _defaultPrintableArea.Height / 100 * e.Graphics.DpiY);
  5201. //}
  5202. _rectangleFGreed = new RectangleF(
  5203. 0,
  5204. 0,
  5205. LayoutCommon.MillimeterToInch100Single(this.PaperWidth) / 100 * e.Graphics.DpiX,
  5206. LayoutCommon.MillimeterToInch100Single(this.PaperHeight) / 100 * e.Graphics.DpiY);
  5207. }
  5208. if (_printType == PrintType.PrintTest)
  5209. {
  5210. // 打印范围
  5211. RectangleF rectangleF = new RectangleF(_allOffsetX,
  5212. _allOffsetY,
  5213. _paperWidth,
  5214. _paperHeight);
  5215. e.Graphics.SetClip(rectangleF);
  5216. // 背景打印
  5217. if (_isPrintBackground)
  5218. {
  5219. PrintBackgroundImage(e.Graphics);
  5220. }
  5221. // Item打印
  5222. foreach (LayoutItem layoutItem in _items)
  5223. {
  5224. switch (layoutItem.ItemType)
  5225. {
  5226. case ItemType.Text:
  5227. DrawTextItem(e.Graphics, (TextItem)layoutItem);
  5228. break;
  5229. case ItemType.Image:
  5230. DrawImageItem(e.Graphics, (ImageItem)layoutItem);
  5231. break;
  5232. case ItemType.Rectangle:
  5233. DrawRectangleItem(e.Graphics, (RectangleItem)layoutItem);
  5234. break;
  5235. case ItemType.Ellipse:
  5236. DrawEllipseItem(e.Graphics, (EllipseItem)layoutItem);
  5237. break;
  5238. //case ItemType.Line:
  5239. // DrawLineItem(e.Graphics, (LineItem)layoutItem);
  5240. // break;
  5241. case ItemType.Grid:
  5242. DrawGridItem(e.Graphics, (GridItem)layoutItem);
  5243. break;
  5244. case ItemType.PageNum:
  5245. DrawPageNumItem(e.Graphics, (PageNumItem)layoutItem);
  5246. break;
  5247. case ItemType.TotalText:
  5248. DrawTotalTextItem(e.Graphics, (TotalTextItem)layoutItem);
  5249. break;
  5250. default:
  5251. //throw new NotImplementedException();
  5252. break;
  5253. }
  5254. }
  5255. // 可以打印范围
  5256. if (_isPrintAreaVisible)
  5257. {
  5258. e.Graphics.PageUnit = GraphicsUnit.Pixel;
  5259. e.Graphics.ResetClip();
  5260. e.Graphics.DrawRectangle(LayoutCommon.GreedDotPen,
  5261. _rectangleFGreed.X,
  5262. _rectangleFGreed.Y,
  5263. _rectangleFGreed.Width,
  5264. _rectangleFGreed.Height);
  5265. }
  5266. e.HasMorePages = false;
  5267. }
  5268. else if (_printType == PrintType.Print || _printType == PrintType.PrintPreview)
  5269. {
  5270. // 打印范围
  5271. RectangleF rectangleF = new RectangleF(_allOffsetX,
  5272. _allOffsetY,
  5273. _paperWidth,
  5274. _paperHeight);
  5275. e.Graphics.SetClip(rectangleF);
  5276. // 背景打印
  5277. if (_isPrintBackground)
  5278. {
  5279. PrintBackgroundImage(e.Graphics);
  5280. }
  5281. // Item打印
  5282. foreach (LayoutItem layoutItem in _items)
  5283. {
  5284. switch (layoutItem.ItemType)
  5285. {
  5286. case ItemType.Text:
  5287. DrawTextItem(e.Graphics, (TextItem)layoutItem);
  5288. break;
  5289. case ItemType.Image:
  5290. DrawImageItem(e.Graphics, (ImageItem)layoutItem);
  5291. break;
  5292. case ItemType.Rectangle:
  5293. DrawRectangleItem(e.Graphics, (RectangleItem)layoutItem);
  5294. break;
  5295. case ItemType.Ellipse:
  5296. DrawEllipseItem(e.Graphics, (EllipseItem)layoutItem);
  5297. break;
  5298. //case ItemType.Line:
  5299. // DrawLineItem(e.Graphics, (LineItem)layoutItem);
  5300. // break;
  5301. case ItemType.Grid:
  5302. DrawGridItem(e.Graphics, (GridItem)layoutItem);
  5303. break;
  5304. case ItemType.PageNum:
  5305. DrawPageNumItem(e.Graphics, (PageNumItem)layoutItem);
  5306. break;
  5307. case ItemType.TotalText:
  5308. DrawTotalTextItem(e.Graphics, (TotalTextItem)layoutItem);
  5309. break;
  5310. default:
  5311. //throw new NotImplementedException();
  5312. break;
  5313. }
  5314. }
  5315. // 可以打印范围
  5316. if (_isPrintAreaVisible)
  5317. {
  5318. e.Graphics.PageUnit = GraphicsUnit.Pixel;
  5319. e.Graphics.ResetClip();
  5320. e.Graphics.DrawRectangle(LayoutCommon.GreedDotPen,
  5321. _rectangleFGreed.X,
  5322. _rectangleFGreed.Y,
  5323. _rectangleFGreed.Width,
  5324. _rectangleFGreed.Height);
  5325. }
  5326. // 表格Item是否有下一页
  5327. if (_printPageNum < TotalPageNum)
  5328. {
  5329. _printPageNum++;
  5330. e.HasMorePages = true;
  5331. return;
  5332. }
  5333. if (_printAction == PrintAction.PrintToPrinter)
  5334. {
  5335. // 纸张数是否增加判断
  5336. if (_printDataBinding != null && _printDataBinding.BindingSource != null && 0 < _printDataBinding.BindingSource.Count)
  5337. {
  5338. if (_printDataBinding.BindingSource.Position + 1 < _printDataBinding.BindingSource.Count)
  5339. {
  5340. // 打印数据源下移一条记录
  5341. _printDataBinding.BindingSource.MoveNext();
  5342. _printPageNum = 1;
  5343. e.HasMorePages = true;
  5344. }
  5345. else
  5346. {
  5347. e.HasMorePages = false;
  5348. }
  5349. }
  5350. else
  5351. {
  5352. e.HasMorePages = false;
  5353. }
  5354. }
  5355. else
  5356. {
  5357. // 纸张数是否增加判断
  5358. if (++_printDataPosition < _printDataTotal)
  5359. {
  5360. _printPageNum = 1;
  5361. e.HasMorePages = true;
  5362. // 同一数据一次印刷/分批印刷
  5363. if (true)
  5364. {
  5365. // 同一数据一次印刷
  5366. if (++_printDataCopies < _numberOfCopies)
  5367. {
  5368. return;
  5369. }
  5370. else
  5371. {
  5372. _printDataCopies = 0;
  5373. }
  5374. }
  5375. }
  5376. else
  5377. {
  5378. e.HasMorePages = false;
  5379. return;
  5380. }
  5381. if (_printDataBinding != null && _printDataBinding.BindingSource != null && 0 < _printDataBinding.BindingSource.Count)
  5382. {
  5383. // 打印数据源下移一条记录
  5384. if (_printDataBinding.BindingSource.Position + 1 < _printDataBinding.BindingSource.Count)
  5385. {
  5386. _printDataBinding.BindingSource.MoveNext();
  5387. }
  5388. else
  5389. {
  5390. _printDataBinding.BindingSource.MoveFirst();
  5391. }
  5392. }
  5393. }
  5394. }
  5395. else if (_printType == PrintType.AllocationPrint || _printType == PrintType.AllocationPrintPreview)
  5396. {
  5397. e.Graphics.SetClip(RectangleF.Empty);
  5398. int allocationIndex = -1;
  5399. // 一张纸打印
  5400. for (int i = 0; i < _allocationNum; i++)
  5401. {
  5402. allocationIndex++;
  5403. if (_printDataPosition < _allocationSkipCount)
  5404. {
  5405. // 跳过的打印位置
  5406. e.HasMorePages = true;
  5407. continue;
  5408. }
  5409. _allOffsetX = _marginLeft
  5410. + (allocationIndex % _allocationNumX)
  5411. * (_paperWidth + _allocationHorizontalSpace)
  5412. - _printerOffsetX;
  5413. _allOffsetY = _marginTop
  5414. + (allocationIndex / _allocationNumX)
  5415. * (_paperHeight + _allocationVerticalSpace)
  5416. - _printerOffsetY;
  5417. // 打印范围
  5418. RectangleF rectangleF = new RectangleF(_allOffsetX,
  5419. _allOffsetY,
  5420. _paperWidth,
  5421. _paperHeight);
  5422. e.Graphics.SetClip(rectangleF);
  5423. // 背景打印
  5424. if (_isPrintBackground)
  5425. {
  5426. PrintBackgroundImage(e.Graphics);
  5427. }
  5428. // Item打印
  5429. foreach (LayoutItem layoutItem in _items)
  5430. {
  5431. switch (layoutItem.ItemType)
  5432. {
  5433. case ItemType.Text:
  5434. DrawTextItem(e.Graphics, (TextItem)layoutItem);
  5435. break;
  5436. case ItemType.Image:
  5437. DrawImageItem(e.Graphics, (ImageItem)layoutItem);
  5438. break;
  5439. case ItemType.Rectangle:
  5440. DrawRectangleItem(e.Graphics, (RectangleItem)layoutItem);
  5441. break;
  5442. case ItemType.Ellipse:
  5443. DrawEllipseItem(e.Graphics, (EllipseItem)layoutItem);
  5444. break;
  5445. //case ItemType.Line:
  5446. // DrawLineItem(e.Graphics, (LineItem)layoutItem);
  5447. // break;
  5448. case ItemType.Grid:
  5449. DrawGridItem(e.Graphics, (GridItem)layoutItem);
  5450. break;
  5451. case ItemType.PageNum:
  5452. DrawPageNumItem(e.Graphics, (PageNumItem)layoutItem);
  5453. break;
  5454. case ItemType.TotalText:
  5455. DrawTotalTextItem(e.Graphics, (TotalTextItem)layoutItem);
  5456. break;
  5457. default:
  5458. break;
  5459. }
  5460. }
  5461. // 表格Item是否有下一页
  5462. if (_printPageNum < TotalPageNum)
  5463. {
  5464. _printPageNum++;
  5465. continue;
  5466. }
  5467. // 纸张数是否增加判断
  5468. if (++_printDataPosition < _printDataTotal)
  5469. {
  5470. _printPageNum = 1;
  5471. e.HasMorePages = true;
  5472. // 同一数据一次印刷/分批印刷
  5473. if (true)
  5474. {
  5475. // 同一数据一次印刷
  5476. if (++_printDataCopies < _numberOfCopies)
  5477. {
  5478. continue;
  5479. }
  5480. else
  5481. {
  5482. _printDataCopies = 0;
  5483. }
  5484. }
  5485. }
  5486. else
  5487. {
  5488. e.HasMorePages = false;
  5489. break;
  5490. }
  5491. if (_printDataBinding != null && _printDataBinding.BindingSource != null && 1 < _printDataBinding.BindingSource.Count)
  5492. {
  5493. // 打印数据源下移一条记录
  5494. if (_printDataBinding.BindingSource.Position + 1 < _printDataBinding.BindingSource.Count)
  5495. {
  5496. _printDataBinding.BindingSource.MoveNext();
  5497. }
  5498. else
  5499. {
  5500. _printDataBinding.BindingSource.MoveFirst();
  5501. }
  5502. }
  5503. }
  5504. // 可以打印范围
  5505. if (_isPrintAreaVisible)
  5506. {
  5507. e.Graphics.PageUnit = GraphicsUnit.Pixel;
  5508. e.Graphics.ResetClip();
  5509. e.Graphics.DrawRectangle(LayoutCommon.GreedDotPen,
  5510. _rectangleFGreed.X,
  5511. _rectangleFGreed.Y,
  5512. _rectangleFGreed.Width,
  5513. _rectangleFGreed.Height);
  5514. }
  5515. // 纸张数是否增加判断
  5516. if (e.HasMorePages && _printDataPosition < _printDataTotal)
  5517. {
  5518. e.HasMorePages = true;
  5519. }
  5520. else
  5521. {
  5522. e.HasMorePages = false;
  5523. }
  5524. }
  5525. else
  5526. {
  5527. e.HasMorePages = false;
  5528. }
  5529. }
  5530. // 选定打印页码 modify by chenxy 2014-03-27 end
  5531. #endregion 打印相关
  5532. #endregion 事件处理
  5533. #region 委托事件
  5534. /// <summary>
  5535. /// 用户操作数据项目完成时发生
  5536. /// </summary>
  5537. /// <param name="sender"></param>
  5538. /// <param name="e"></param>
  5539. public delegate void UserChangedItemEventHandler(object sender, ItemChangedEventArgs e);
  5540. /// <summary>
  5541. /// 用户操作数据项目完成时发生
  5542. /// </summary>
  5543. /// <param name="sender"></param>
  5544. /// <param name="e"></param>
  5545. public delegate void UserChangedGridItemEventHandler(object sender, CollectionChangeEventArgs e);
  5546. #endregion
  5547. #region 事件
  5548. /// <summary>
  5549. /// 改变纸张尺寸。
  5550. /// </summary>
  5551. public event EventHandler PaperSizeChanged;
  5552. /// <summary>
  5553. /// 改变缩放比例
  5554. /// </summary>
  5555. public event EventHandler ZoomChanged;
  5556. /// <summary>
  5557. /// 当一个项目被改变,移动,添加或删除引发此事件。
  5558. /// </summary>
  5559. public event ItemChangedEventHandler ItemChanged;
  5560. /// <summary>
  5561. /// 项目的选择状态改变时引发此事件。
  5562. /// </summary>
  5563. public event ItemSelectionChangedEventHandler ItemSelectionChanged;
  5564. /// <summary>
  5565. /// 打印或显示数据之前引发此事件。
  5566. /// </summary>
  5567. public event ItemValueFormattingEventHandler ItemValueFormatting;
  5568. /// <summary>
  5569. /// 用户追加Item完成时触发事件
  5570. /// </summary>
  5571. public event UserChangedItemEventHandler UserAddedItem;
  5572. /// <summary>
  5573. /// 用户删除Item完成时触发事件
  5574. /// </summary>
  5575. public event UserChangedItemEventHandler UserDeletedItem;
  5576. /// <summary>
  5577. /// 用户修改GridItem列时触发事件
  5578. /// </summary>
  5579. public event UserChangedGridItemEventHandler UserChangedGridItem;
  5580. /// <summary>
  5581. /// 改变项目的选中状态
  5582. /// </summary>
  5583. public event EventHandler UserSelectionChanged;
  5584. #endregion 事件
  5585. #region User32
  5586. /// <summary>
  5587. ///
  5588. /// </summary>
  5589. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  5590. public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, int lParam);
  5591. #endregion User32
  5592. #region Grid页码相关
  5593. /// <summary>
  5594. /// 当前页码
  5595. /// </summary>
  5596. [Browsable(false)]
  5597. public int CurrentPageNum
  5598. {
  5599. get
  5600. {
  5601. return _currentPageNum;
  5602. }
  5603. }
  5604. /// <summary>
  5605. /// 页数
  5606. /// </summary>
  5607. [Browsable(false)]
  5608. public int TotalPageNum
  5609. {
  5610. get
  5611. {
  5612. return _totalPageNum;
  5613. }
  5614. }
  5615. /// <summary>
  5616. /// 打印页码
  5617. /// </summary>
  5618. internal int PrintPageNum
  5619. {
  5620. get
  5621. {
  5622. return _printPageNum;
  5623. }
  5624. }
  5625. /// <summary>
  5626. /// 设置总页数
  5627. /// </summary>
  5628. internal void SetTotalPageNum()
  5629. {
  5630. LayoutItem[] grids = this.GetItems(ItemType.Grid);
  5631. int total = 1;
  5632. foreach (GridItem grid in grids)
  5633. {
  5634. total = Math.Max(total, grid.TotalPageNum);
  5635. }
  5636. if (total != _totalPageNum)
  5637. {
  5638. _totalPageNum = total;
  5639. LayoutItem[] pageNums = this.GetItems(ItemType.PageNum);
  5640. foreach (PageNumItem pageNum in pageNums)
  5641. {
  5642. pageNum.Refresh();
  5643. }
  5644. }
  5645. }
  5646. /// <summary>
  5647. /// 设定显示第几页
  5648. /// </summary>
  5649. /// <param name="pageNum"></param>
  5650. public void SetPageNum(int pageNum)
  5651. {
  5652. if (pageNum < 1)
  5653. {
  5654. pageNum = 1;
  5655. }
  5656. if (pageNum > _totalPageNum)
  5657. {
  5658. pageNum = _totalPageNum;
  5659. }
  5660. if (pageNum != _currentPageNum)
  5661. {
  5662. _currentPageNum = pageNum;
  5663. LayoutItem[] grids = this.GetItems(ItemType.Grid);
  5664. foreach (GridItem grid in grids)
  5665. {
  5666. grid.Refresh();
  5667. }
  5668. LayoutItem[] pageNums = this.GetItems(ItemType.PageNum);
  5669. foreach (PageNumItem pageNumItem in pageNums)
  5670. {
  5671. pageNumItem.Refresh();
  5672. }
  5673. LayoutItem[] totalTextItems = this.GetItems(ItemType.TotalText);
  5674. foreach (TotalTextItem totalTextItem in totalTextItems)
  5675. {
  5676. totalTextItem.Refresh();
  5677. }
  5678. }
  5679. }
  5680. /// <summary>
  5681. /// Grid使用的数据源
  5682. /// </summary>
  5683. [Browsable(false)]
  5684. public DataSet GridDataSource
  5685. {
  5686. get
  5687. {
  5688. return _gridDataSet;
  5689. }
  5690. set
  5691. {
  5692. if (value != _gridDataSet)
  5693. {
  5694. _gridDataSet = value;
  5695. LayoutItem[] grids = this.GetItems(ItemType.Grid);
  5696. if (grids != null && grids.Length > 0)
  5697. {
  5698. if (_gridDataSet == null || _gridDataSet.Tables.Count == 0)
  5699. {
  5700. foreach (GridItem grid in grids)
  5701. {
  5702. grid.GridSource = null;
  5703. }
  5704. }
  5705. else
  5706. {
  5707. foreach (GridItem grid in grids)
  5708. {
  5709. if (_gridDataSet.Tables.Contains(grid.GridDataMember))
  5710. {
  5711. grid.GridSource = _gridDataSet.Tables[grid.GridDataMember];
  5712. }
  5713. else
  5714. {
  5715. grid.GridSource = null;
  5716. }
  5717. }
  5718. }
  5719. }
  5720. }
  5721. }
  5722. }
  5723. #endregion Grid页码相关
  5724. }
  5725. }