LayoutBox.cs 223 KB

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