LayoutBox.cs 229 KB

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