LayoutBox.cs 230 KB

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