LayoutBox.cs 222 KB

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