| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041 |
- /*******************************************************************************
- * Copyright(c) 2012 dongke All rights reserved. / Confidential
- * 类的信息:
- * 1.程序名称:LayoutBox.cs
- * 2.功能描述:票据主控件
- * 编辑履历:
- * 作者 日期 版本 修改内容
- * 欧阳涛 2012/09/11 1.00 新建
- *******************************************************************************/
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Printing;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Security.Cryptography;
- using System.Windows.Forms;
- namespace Dongke.WinForm.Controls.InvoiceLayout
- {
- /// <summary>
- /// 票据模板控件
- /// </summary>
- public partial class LayoutBox : UserControl
- {
- #region 常量
- internal const string ITEMNAME_FIX = "Free";
- #region 保存、读取相关
- // TODO 加密保存 解密读取
- 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 };
- private static readonly byte[] IV = new byte[] { 26, 122, 25, 28, 97, 211, 152, 32, 223, 110, 120, 242, 170, 35, 96, 93 };
- #endregion 保存、读取相关
- #endregion 常量
- #region 成员变量
- #region Layout相关
- private bool _isDirty; // Layout是否变更
- private short _updateCount; // 更新数量
- private int _newItemID; // 生成新的ItemID
- private LayoutBoxDataBinding _dataBinding; // 数据源绑定
- private object _dataSource; // 数据源
- private string _dataMember; // 数据源名
- private LayoutBoxPrintDataBinding _printDataBinding; // 打印的数据源绑定
- private object _printDataSource; // 打印的数据源
- private string _printDataMember; // 打印的数据源名
- /* Item的显示循序
- * Items PaperArea.Controls
- * Item的显示循序 Index Index
- * 最前面 count-1 0
- * ↓ ↑ ↓
- * ↓ ↑ ↓
- * ↓ ↑ ↓
- * 最背面 0 count-1
- */
- private Comparison<LayoutItem> _comparisonItems; // 排序Item
- private List<LayoutItem> _items; // LayoutBox中的Item集合
- private List<LayoutItem> _selectedItems; // 选择状态的Item集合
- private LayoutMode _layoutMode; // Layout编辑模式
- private ZoomType _zoomType; // Layout缩放模式
- private int _zoom; // Layout缩放比例
- private float _paperHeight; // Layout的纸张的高
- private float _paperWidth; // Layout的纸张的宽
- private DataFormats.Format _clipboardFormat; // 剪贴板数据格式
- private string _backgroundImageName; // 背景图片名
- #endregion Layout相关
- #region Item选择
- private bool _isMouseDownInPaperArea; // 鼠标按下
- private bool _isMouseMoveInPaperArea; // 鼠标移动
- private Rectangle _mouseRect; // 鼠标框选范围
- #endregion Item选择
- #region Item移动
- private int _startX; // 鼠标按下位置X
- private int _startY; // 鼠标按下位置Y
- private int _mouseMoveX; // 鼠标移动位置X
- private int _mouseMoveY; // 鼠标移动位置Y
- private bool _isMouseDown; // 鼠标按下
- private bool _isMouseMove; // 鼠标移动
- private Rectangle[] _downRectangles; // 鼠标按下Item范围
- private Rectangle[] _moveRectangles; // 鼠标移动Item范围
- private bool _isShiftDown = false; // Shift按下
- private bool _isCtrlDown = false; // Ctrl按下
- #endregion Item移动
- #region 打印相关
- private bool _isPrintAreaVisible; // 是否标识打印范围
- private bool _isPrintBackground; // 是否打印背景
- private bool _landscape; // true:横向打印;false:纵向打印
- private float _printOffsetX; // 打印横向偏移
- private float _printOffsetY; // 打印纵向偏移
- //private PrintPreviewDialogEx printPreviewDialogEx;
- private int _numberOfCopies = 1; // 打印份数
- private int _allocationSkipCount = 0; // 分配打印开头跳过的位置数
- private int _allocationNumX = 1; // 分配打印横方向的分配数
- private int _allocationNumY = 1; // 分配打印纵方向的分配数
- private float _allocationVerticalSpace; // 分配打印横方向的间隔
- private float _allocationHorizontalSpace; // 分配打印纵方向的间隔
- private float _marginLeft; // 分配打印左侧余白
- private float _marginRight; // 分配打印右侧余白
- private float _marginTop; // 分配打印上侧余白
- private float _marginBottom; // 分配打印下侧余白
- private PrintType _printType; // 打印类别
- private int _printDataPosition = -1; // 打印数据源位置
- private int _printDataTotal = 0; // 打印数据总数
- private int _printDataCopies = -1; // 打印数据份数
- private int _allocationNum = 0; // 分配打印每页的分配数
- private PrintAction _printAction; // 打印种类
- private RectangleF _rectangleFGreed; // 打印范围
- private float _allOffsetX; // 打印横向偏移
- private float _allOffsetY; // 打印纵向偏移
- private float _printerOffsetX; // 打印横向偏移
- private float _printerOffsetY; // 打印纵向偏移
- #endregion 打印相关
- #region Item相关
- private int _maxItemLeft = 0; // 位置X上限(Pixel)
- private int _maxItemTop = 0; // 位置Y上限(Pixel)
- private int _maxItemWidth = 0; // 尺寸宽上限(Pixel)
- private int _maxItemHeight = 0; // 尺寸高上限(Pixel)
- private int _minItemWidth = 0; // 尺寸幅下限(Pixel)
- private int _minItemHeight = 0; // 尺寸高下限(Pixel)
- private int _pasteCount = 0;
- private RectangleF _lastItemRF = RectangleF.Empty;
- #endregion Item相关
- #region GridItem相关
- private int _currentPageNum = 1; // 当前页码
- private int _printPageNum = 1; // 打印页的当前页码
- private int _totalPageNum = 1; // 总页数
- private DataSet _gridDataSet = null; // GridItem利用的数据源
- #endregion GridItem相关
- #endregion 成员变量
- #region 构造函数
- /// <summary>
- /// 构造函数
- /// </summary>
- public LayoutBox()
- : this(InvoiceLayout.LayoutMode.Preview)
- {
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- public LayoutBox(LayoutMode layoutMode = InvoiceLayout.LayoutMode.Preview)
- {
- this._layoutMode = layoutMode;
- InitializeComponent();
- InitializeLayoutBox();
- //printPreviewDialogEx = new PrintPreviewDialogEx();
- //this.printPreviewDialogEx.AutoScrollMargin = new System.Drawing.Size(0, 0);
- //this.printPreviewDialogEx.AutoScrollMinSize = new System.Drawing.Size(0, 0);
- //this.printPreviewDialogEx.ClientSize = new System.Drawing.Size(400, 300);
- //this.printPreviewDialogEx.Document = this.printDocument;
- //this.printPreviewDialogEx.Enabled = true;
- //this.printPreviewDialogEx.Name = "printPreviewDialogEx";
- //this.printPreviewDialogEx.Visible = false;
- }
- #endregion 构造函数
- #region 属性
- #region Layout
- /// <summary>
- /// Item选择状态变化中
- /// </summary>
- public bool ItemSelectionChanging
- {
- get;
- private set;
- }
- /// <summary>
- /// 一个GridItem
- /// </summary>
- public GridItem OneGridItem
- {
- get;
- private set;
- }
- /// <summary>
- /// 打印纸张
- /// </summary>
- [Browsable(false)]
- public Control PaperArea
- {
- get
- {
- return paperArea;
- }
- }
- /// <summary>
- /// 打印纸张图形范围
- /// </summary>
- [Browsable(false)]
- internal Microsoft.VisualBasic.PowerPacks.ShapeContainer ItemShapeContainer
- {
- get
- {
- return paperArea.ShapeContainer;
- }
- }
- /// <summary>
- /// Item图形集合
- /// </summary>
- [Browsable(false)]
- internal Microsoft.VisualBasic.PowerPacks.ShapeCollection ItemShapes
- {
- get
- {
- return paperArea.ShapeContainer.Shapes;
- }
- }
- /// <summary>
- /// 获得新的ItemID
- /// </summary>
- [Browsable(false)]
- internal int NewItemID
- {
- get
- {
- lock (this)
- {
- return _newItemID++;
- }
- }
- }
- /// <summary>
- /// 获取或设置数据源
- /// </summary>
- [Browsable(true)]
- [Description(" 获取或设置数据源"), Category("LayoutBox")]
- [DefaultValue((string)null), RefreshProperties(RefreshProperties.Repaint), AttributeProvider(typeof(IListSource))]
- public object DataSource
- {
- get
- {
- return _dataSource;
- }
- set
- {
- if (value != _dataSource)
- {
- _dataSource = value;
- if (_layoutMode == LayoutMode.Preview)
- {
- SetDataSource();
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置被用作模块的基表中的数据源名称
- /// </summary>
- [Description("获取或设置被用作模块的基表中的数据源名称"), Category("LayoutBox")]
- [DefaultValue("")]
- public string DataMember
- {
- get
- {
- return _dataMember;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_dataMember))
- {
- _dataMember = value;
- if (_layoutMode == LayoutMode.Preview)
- {
- SetDataMember();
- }
- }
- }
- }
- /// <summary>
- /// 获取数据绑定源,从外部刷新数据源
- /// </summary>
- [Browsable(false)]
- [Description("获取数据绑定源,从外部刷新数据源"), Category("LayoutBox")]
- [DefaultValue((string)null)]
- public BindingSource BindingSource
- {
- get
- {
- if (_layoutMode == LayoutMode.Preview)
- {
- if (_dataBinding != null)
- {
- return _dataBinding.BindingSource;
- }
- }
- return null;
- }
- }
- /// <summary>
- /// 获取LayoutBox的全部Item集合
- /// </summary>
- [Description("获取LayoutBox的全部Item集合"), Category("LayoutBox")]
- [Browsable(false)]
- public List<LayoutItem> Items
- {
- get
- {
- return _items;
- }
- }
- /// <summary>
- /// 获取LayoutBox的全部Item集合
- /// </summary>
- [Description("获取剪贴板的内容"), Category("LayoutBox")]
- public DataFormats.Format ClipboardFormat
- {
- get
- {
- return GetCipboardFormat();
- //return _clipboardFormat;
- }
- }
- /// <summary>
- /// 获取LayoutBox的选择状态的Item集合
- /// </summary>
- [Description("获取LayoutBox的选择状态的Item集合"), Category("LayoutBox")]
- [Browsable(false)]
- public List<LayoutItem> SelectedItems
- {
- get
- {
- return _selectedItems;
- }
- }
- /// <summary>
- /// 获取或设置LayoutBox的编辑模式
- /// </summary>
- [Description("获取或设置LayoutBox的编辑模式"), Category("LayoutBox")]
- [DefaultValue(LayoutMode.Preview)]
- public LayoutMode LayoutMode
- {
- get
- {
- return _layoutMode;
- }
- set
- {
- if (_layoutMode != value)
- {
- if (_layoutMode == LayoutMode.Edit)
- {
- ClearSelection();
- }
- _layoutMode = value;
- if (_layoutMode == LayoutMode.Edit)
- {
- //// TODO
- //if (this._clipboardFormat == null)
- //{
- // _clipboardFormat =
- // DataFormats.GetFormat(typeof(LayoutItem).FullName
- // + "{"
- // + Guid.NewGuid().ToString()
- // + "}");
- //}
- tsmiAllSelect.Enabled = true;
- tsmiBackgroundImage.Enabled = true;
- tsmiPaperSize.Enabled = true;
- paperArea.Click += new System.EventHandler(this.paperArea_Click);
- paperArea.MouseDown += new System.Windows.Forms.MouseEventHandler(this.paperArea_MouseDown);
- paperArea.MouseMove += new System.Windows.Forms.MouseEventHandler(this.paperArea_MouseMove);
- paperArea.MouseUp += new System.Windows.Forms.MouseEventHandler(this.paperArea_MouseUp);
- // TODO Item范围内移动
- paperArea.ShapeContainer.KeyDown += new KeyEventHandler(RectTracker_KeyDown);
- paperArea.ShapeContainer.KeyUp += new KeyEventHandler(RectTracker_KeyUp);
- paperArea.ShapeContainer.MouseDown += new MouseEventHandler(RectTracker_MouseDown);
- paperArea.ShapeContainer.MouseMove += new MouseEventHandler(RectTracker_MouseMove);
- paperArea.ShapeContainer.MouseUp += new MouseEventHandler(RectTracker_MouseUp);
- paperArea.ShapeContainer.LostFocus += new EventHandler(RectTracker_LostFocus);
- }
- else
- {
- tsmiAllSelect.Enabled = false;
- tsmiBackgroundImage.Enabled = false;
- tsmiPaperSize.Enabled = false;
- paperArea.Click -= new System.EventHandler(this.paperArea_Click);
- paperArea.MouseDown -= new System.Windows.Forms.MouseEventHandler(this.paperArea_MouseDown);
- paperArea.MouseMove -= new System.Windows.Forms.MouseEventHandler(this.paperArea_MouseMove);
- paperArea.MouseUp -= new System.Windows.Forms.MouseEventHandler(this.paperArea_MouseUp);
- // TODO Item范围内移动
- paperArea.ShapeContainer.KeyDown -= new KeyEventHandler(RectTracker_KeyDown);
- paperArea.ShapeContainer.KeyUp -= new KeyEventHandler(RectTracker_KeyUp);
- paperArea.ShapeContainer.MouseDown -= new MouseEventHandler(RectTracker_MouseDown);
- paperArea.ShapeContainer.MouseMove -= new MouseEventHandler(RectTracker_MouseMove);
- paperArea.ShapeContainer.MouseUp -= new MouseEventHandler(RectTracker_MouseUp);
- paperArea.ShapeContainer.LostFocus -= new EventHandler(RectTracker_LostFocus);
- if (_layoutMode == LayoutMode.Preview)
- {
- SetDataSource();
- SetDataMember();
- SetPrintDataSource();
- SetPrintDataMember();
- }
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置LayoutBox的缩放模式
- /// </summary>
- [Description("获取或设置LayoutBox的缩放模式"), Category("LayoutBox")]
- [DefaultValue(ZoomType.Specify)]
- public ZoomType ZoomType
- {
- get
- {
- return _zoomType;
- }
- set
- {
- if (_zoomType != value)
- {
- _zoomType = value;
- if (value != ZoomType.Whole)
- {
- paperBox.AutoScroll = true;
- // 改变纸张大小
- ChangePaperSize();
- // 设置全部Item的比例
- ZoomAllItems();
- }
- else
- {
- paperBox.AutoScroll = false;
- // 全部Item显示
- InitializeZoom();
- }
- if (ZoomChanged != null)
- {
- ZoomChanged(this, EventArgs.Empty);
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置LayoutBox的缩放比例(%)
- /// </summary>
- [Description("获取或设置LayoutBox的缩放比例"), Category("LayoutBox")]
- [DefaultValue(100)]
- public int Zoom
- {
- get
- {
- return _zoom;
- }
- set
- {
- if (0 <= value && _zoom != value)
- {
- _zoom = value;
- if (_zoomType == ZoomType.Specify)
- {
- // 改变纸张大小
- ChangePaperSize();
- // 设置全部Item的比例
- ZoomAllItems();
- if (ZoomChanged != null)
- {
- ZoomChanged(this, EventArgs.Empty);
- }
- }
- else
- {
- ZoomType = ZoomType.Specify;
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置纸张的高(mm)
- /// </summary>
- [Description("获取或设置纸张的高"), Category("LayoutBox")]
- [DefaultValue(297f)]
- public float PaperHeight
- {
- get
- {
- return _paperHeight;
- }
- set
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- SetPaperAreaSize(_paperWidth, value);
- }
- }
- /// <summary>
- /// 获取或设置纸张的宽(mm)
- /// </summary>
- [Description("获取或设置纸张的宽"), Category("LayoutBox")]
- [DefaultValue(210f)]
- public float PaperWidth
- {
- get
- {
- return _paperWidth;
- }
- set
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- SetPaperAreaSize(value, _paperHeight);
- }
- }
- /// <summary>
- /// 获取纸张的背景图片名
- /// </summary>
- [Description("获取或设置纸张的背景图片"), Category("LayoutBox")]
- [DefaultValue("")]
- public string BackgroundImageName
- {
- get
- {
- return _backgroundImageName;
- }
- }
- /// <summary>
- /// 获取或设置纸张的背景图片
- /// </summary>
- [Description("获取或设置纸张的背景图片"), Category("LayoutBox")]
- [DefaultValue((string)null)]
- public override Image BackgroundImage
- {
- get
- {
- return paperArea.BackgroundImage;
- }
- set
- {
- paperArea.BackgroundImage = value;
- _backgroundImageName = string.Empty;
- }
- }
- /// <summary>
- /// 获取LayoutBox的布局是否已经改变
- /// </summary>
- //[Description("获取LayoutBox的布局是否已经改变"), Category("LayoutBox")]
- [Browsable(false)]
- public bool Dirty
- {
- get
- {
- return _isDirty;
- }
- internal set
- {
- SetDitry(value);
- }
- }
- /// <summary>
- /// 获取或设置是否加密保存、读取流
- /// </summary>
- [Description("获取或设置是否加密保存、读取流"), Category("LayoutBox")]
- [DefaultValue(true)]
- public bool CryptoStream = true;
- #endregion Layout
- #region 打印相关
- /// <summary>
- /// 获取或设置打印数据源
- /// </summary>
- [Browsable(true)]
- [Description("获取或设置打印数据源"), Category("打印相关")]
- [DefaultValue((string)null), RefreshProperties(RefreshProperties.Repaint), AttributeProvider(typeof(IListSource))]
- public object PrintDataSource
- {
- get
- {
- return _printDataSource;
- }
- set
- {
- if (value != _printDataSource)
- {
- _printDataSource = value;
- if (_layoutMode == LayoutMode.Preview)
- {
- SetPrintDataSource();
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置打印数据源
- /// </summary>
- [Description("获取或设置打印数据源"), Category("打印相关")]
- [DefaultValue("")]
- public string PrintDataMember
- {
- get
- {
- return _printDataMember;
- }
- set
- {
- if (string.IsNullOrEmpty(value))
- {
- value = string.Empty;
- }
- if (!value.Equals(_printDataMember))
- {
- _printDataMember = value;
- if (_layoutMode == LayoutMode.Preview)
- {
- SetPrintDataMember();
- }
- }
- }
- }
- /// <summary>
- /// 获取或设置是否用绿色虚线标识出打印区域
- /// </summary>
- [Description("获取或设置是否用绿色虚线标识出打印区域"), Category("打印相关")]
- [DefaultValue(true)]
- public bool PrintAreaVisible
- {
- get
- {
- return _isPrintAreaVisible;
- }
- set
- {
- if (_isPrintAreaVisible != value)
- {
- _isPrintAreaVisible = value;
- _isDirty = true;
- }
- }
- }
- /// <summary>
- /// 获取或设置一个值,该值指示是否打印背景
- /// </summary>
- [Description("获取或设置一个值,该值指示是否打印背景"), Category("打印相关")]
- [DefaultValue(false)]
- public bool PrintBackground
- {
- get
- {
- return _isPrintBackground;
- }
- set
- {
- if (_isPrintBackground != value)
- {
- _isPrintBackground = value;
- _isDirty = true;
- }
- }
- }
- /// <summary>
- /// 获取或设置一个值,该值指示是否在横向或纵向的纸张打印页面
- /// </summary>
- [Description("获取或设置一个值,该值指示是否在横向或纵向的纸张时打印页面\r\n打印横向的页面为true;否则为false。默认是由打印机决定。"), Category("打印相关")]
- [DefaultValue(false)]
- public bool Landscape
- {
- get
- {
- return _landscape;
- }
- set
- {
- printDocument.DefaultPageSettings.Landscape = value;
- _landscape = value;
- }
- }
- /// <summary>
- /// 获取或设置打印纸张的大小
- /// </summary>
- [Description("获取或设置打印纸张的大小"), Category("LayoutBox")]
- [AmbientValue(typeof(Size), "0, 0")]
- [Browsable(false)]
- [DefaultValue((string)null)]
- public PaperSize PaperSize
- {
- get
- {
- return printDocument.PrinterSettings.DefaultPageSettings.PaperSize;
- }
- set
- {
- printDocument.PrinterSettings.DefaultPageSettings.PaperSize = value;
- }
- }
- /// <summary>
- /// Defines a reusable object that sends output to a printer, when printing from a Windows Forms application
- /// </summary>
- [Description("Defines a reusable object that sends output to a printer, when printing from a Windows Forms application"), Category("打印相关")]
- [Browsable(false)]
- public PrintDocument PrintDocument
- {
- get
- {
- return printDocument;
- }
- }
- /// <summary>
- /// 获取或设置打印机的配置信息
- /// </summary>
- [Description("获取或设置打印机的配置信息"), Category("打印相关")]
- [Browsable(false)]
- [DefaultValue((string)null)]
- public PrinterSettings PrinterSettings
- {
- get
- {
- return printDocument.PrinterSettings;
- }
- set
- {
- printDocument.PrinterSettings = value;
- _landscape = printDocument.PrinterSettings.DefaultPageSettings.Landscape;
- }
- }
- /// <summary>
- /// 获取或设置水平方向打印偏移(mm)
- /// </summary>
- [Description("获取或设置水平方向打印偏移"), Category("打印相关")]
- [DefaultValue(0f)]
- public float PrintOffsetX
- {
- get
- {
- return _printOffsetX;
- }
- set
- {
- _printOffsetX = value;
- }
- }
- /// <summary>
- /// 获取或设置垂直方向打印偏移(mm)
- /// </summary>
- [Description("获取或设置垂直方向打印偏移"), Category("打印相关")]
- [DefaultValue(0f)]
- public float PrintOffsetY
- {
- get
- {
- return _printOffsetY;
- }
- set
- {
- _printOffsetY = value;
- }
- }
- /// <summary>
- /// 获取或设置分配打印时,水平方向的分配数
- /// </summary>
- [Description("获取或设置分配打印时,水平方向的分配数"), Category("打印相关")]
- [DefaultValue(1)]
- public int AllocationNumX
- {
- get
- {
- return _allocationNumX;
- }
- set
- {
- if (0 < value)
- {
- _allocationNumX = value;
- }
- else
- {
- _allocationNumX = 1;
- }
- }
- }
- /// <summary>
- /// 获取或设置分配打印时,垂直方向的分配数
- /// </summary>
- [Description("获取或设置分配打印时,垂直方向的分配数"), Category("打印相关")]
- [DefaultValue(1)]
- public int AllocationNumY
- {
- get
- {
- return _allocationNumY;
- }
- set
- {
- if (0 < value)
- {
- _allocationNumY = value;
- }
- else
- {
- _allocationNumY = 1;
- }
- }
- }
- /// <summary>
- /// 获取或设置分配打印时,左侧余白(mm)
- /// </summary>
- [Description("获取或设置分配打印时,左侧余白(mm)"), Category("打印相关")]
- [DefaultValue(0f)]
- public float MarginLeft
- {
- get
- {
- return _marginLeft;
- }
- set
- {
- _marginLeft = value;
- }
- }
- /// <summary>
- /// 获取或设置分配打印时,右侧余白(mm)
- /// </summary>
- [Description("获取或设置分配打印时,右侧余白(mm)"), Category("打印相关")]
- [DefaultValue(0f)]
- public float MarginRight
- {
- get
- {
- return _marginRight;
- }
- set
- {
- _marginRight = value;
- }
- }
- /// <summary>
- /// 获取或设置分配打印时,上侧余白(mm)
- /// </summary>
- [Description("获取或设置分配打印时,上侧余白(mm)"), Category("打印相关")]
- [DefaultValue(0f)]
- public float MarginTop
- {
- get
- {
- return _marginTop;
- }
- set
- {
- _marginTop = value;
- }
- }
- /// <summary>
- /// 获取或设置分配打印时,下侧余白(mm)
- /// </summary>
- [Description("获取或设置分配打印时,下侧余白(mm)"), Category("打印相关")]
- [DefaultValue(0f)]
- public float MarginBottom
- {
- get
- {
- return _marginBottom;
- }
- set
- {
- _marginBottom = value;
- }
- }
- /// <summary>
- /// 获取或设置分配打印时,上下间隔(mm)
- /// </summary>
- [Description("获取或设置分配打印时,上下间隔(mm)"), Category("打印相关")]
- [DefaultValue(0f)]
- public float AllocationVirticalSpace
- {
- get
- {
- return _allocationVerticalSpace;
- }
- set
- {
- if (value < 0)
- {
- _allocationVerticalSpace = 0;
- }
- else
- {
- _allocationVerticalSpace = value;
- }
- }
- }
- /// <summary>
- /// 获取或设置分配打印时,左右间隔(mm)
- /// </summary>
- [Description("获取或设置分配打印时,左右间隔(mm)"), Category("打印相关")]
- [DefaultValue(0f)]
- public float AllocationHorizontalSpace
- {
- get
- {
- return _allocationHorizontalSpace;
- }
- set
- {
- if (value < 0)
- {
- _allocationHorizontalSpace = 0;
- }
- else
- {
- _allocationHorizontalSpace = value;
- }
- }
- }
- /// <summary>
- /// 获取或设置打印份数
- /// </summary>
- [Description("获取或设置打印份数"), Category("打印相关")]
- [DefaultValue(1)]
- public int NumberOfCopies
- {
- get
- {
- return _numberOfCopies;
- }
- set
- {
- if (0 < value)
- {
- _numberOfCopies = value;
- }
- else
- {
- _numberOfCopies = 1;
- }
- }
- }
- /// <summary>
- /// 获取或设置分配打印时,开头跳过的位置数
- /// </summary>
- [Description("获取或设置分配打印时,开头跳过的位置数"), Category("打印相关")]
- [DefaultValue(0)]
- public int AllocationSkipCount
- {
- get
- {
- return _allocationSkipCount;
- }
- set
- {
- if (0 < value)
- {
- _allocationSkipCount = value;
- }
- else
- {
- _allocationSkipCount = 0;
- }
- }
- }
- #endregion 打印相关
- #region Item相关
- /// <summary>
- /// 位置X上限(Pixel)
- /// </summary>
- [Browsable(false)]
- internal int MaxItemLeft
- {
- get
- {
- return _maxItemLeft;
- }
- }
- /// <summary>
- /// 位置Y上限(Pixel)
- /// </summary>
- [Browsable(false)]
- internal int MaxItemTop
- {
- get
- {
- return _maxItemTop;
- }
- }
- /// <summary>
- /// 尺寸宽上限(Pixel)
- /// </summary>
- [Browsable(false)]
- internal int MaxItemWidth
- {
- get
- {
- return _maxItemWidth;
- }
- }
- /// <summary>
- /// 尺寸高上限(Pixel)
- /// </summary>
- [Browsable(false)]
- internal int MaxItemHeight
- {
- get
- {
- return _maxItemHeight;
- }
- }
- /// <summary>
- ///尺寸宽下限(Pixel)
- /// </summary>
- [Browsable(false)]
- internal int MinItemWidth
- {
- get
- {
- return _minItemWidth;
- }
- }
- /// <summary>
- /// 尺寸高下限(Pixel)
- /// </summary>
- [Browsable(false)]
- internal int MinItemHeight
- {
- get
- {
- return _minItemHeight;
- }
- }
- #endregion Item相关
- #endregion 属性
- #region public函数
- #region 保存、读取相关
- /// <summary>
- /// 从模版中读取Layout
- /// </summary>
- /// <param name="bytes">布局模版</param>
- /// <returns>读取成功true;读取失败false</returns>
- public bool ReadLayout(byte[] bytes)
- {
- MemoryStream memoryStream = null;
- if (bytes != null && 0 < bytes.Length)
- {
- memoryStream = new MemoryStream(bytes);
- memoryStream.Position = 0;
- }
- return ReadLayout(memoryStream);
- }
- /// <summary>
- /// 保存Layout到布局模版
- /// </summary>
- /// <returns>布局模版</returns>
- public byte[] WriteLayout()
- {
- MemoryStream memoryStream = new MemoryStream();
- if (WriteLayout(memoryStream))
- {
- return memoryStream.ToArray();
- }
- return null;
- }
- /// <summary>
- /// 从模版中读取Layout
- /// </summary>
- /// <param name="stream">布局模版</param>
- /// <returns>读取成功true;读取失败false</returns>
- public bool ReadLayout(Stream stream)
- {
- // TODO 异常分析
- int err_level = 0;
- try
- {
- if (stream == null)
- {
- // 清除Layout布局
- ClearLayoutBox();
- //paperArea.Refresh();
- return true;
- }
- err_level = 1;
- // TODO
- if (_clipboardFormat != null)
- {
- this.ClearClipboard();
- //if (Clipboard.ContainsData(_clipboardFormat.Name))
- //{
- // CopyToClipboard(_clipboardFormat.Name, null);
- //}
- //_clipboardFormat =
- // DataFormats.GetFormat(typeof(LayoutItem).FullName
- // + "{"
- // + Guid.NewGuid().ToString()
- // + "}");
- }
- err_level = 2;
- _lastItemRF = RectangleF.Empty;
- _mouseRect = Rectangle.Empty;
- SetDitry(false);
- _selectedItems.Clear();
- err_level = 3;
- foreach (LayoutItem item in _items)
- {
- if (item.Frame != null)
- {
- item.Frame.Dispose();
- item.Frame = null;
- }
- item.Selected = false;
- item.Dispose();
- }
- _items.Clear();
- err_level = 4;
- // 反序列化
- BinaryFormatter formatter = new BinaryFormatter();
- PropertyOfLayoutBox propertyOflayoutBox = null;
- // TODO 解密读取
- if (CryptoStream)
- {
- Rijndael age = Rijndael.Create();
- CryptoStream cStream = new CryptoStream(stream,
- age.CreateDecryptor(KEY, IV),
- CryptoStreamMode.Read);
- propertyOflayoutBox = (PropertyOfLayoutBox)formatter.Deserialize(cStream);
- }
- else
- {
- propertyOflayoutBox = (PropertyOfLayoutBox)formatter.Deserialize(stream);
- }
- err_level = 5;
- _lastItemRF = propertyOflayoutBox.LastItemRF;
- _newItemID = propertyOflayoutBox.NewItemID;
- _paperHeight = propertyOflayoutBox.PaperHeight;
- _paperWidth = propertyOflayoutBox.PaperWidth;
- if (_zoomType != ZoomType.Whole)
- {
- ChangePaperSize();
- }
- else
- {
- InitializeZoom();
- }
- err_level = 6;
- paperArea.BackgroundImage = propertyOflayoutBox.BackgroundImage;
- _backgroundImageName = propertyOflayoutBox.BackgroundImageName;
- _isPrintBackground = propertyOflayoutBox.PrintBackground;
- _isPrintAreaVisible = propertyOflayoutBox.PrintAreaVisible;
- ArrayList items = propertyOflayoutBox.Items;
- if (items != null && 0 < items.Count)
- {
- foreach (LayoutItem item in items)
- {
- item.Init(this, false);
- if (item.ItemType == ItemType.Grid)
- {
- this.OneGridItem = item as GridItem;
- }
- }
- }
- err_level = 7;
- paperArea.Select();
- RefreshItemsBound();
- //paperArea.Refresh();
- return true;
- }
- catch (Exception ex)
- {
- Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
- "PrintBarcode --- " + err_level, ex.ToString(),
- DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
- Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
- throw ex;
- }
- }
- /// <summary>
- /// 保存Layout到布局模版
- /// </summary>
- /// <param name="stream">布局模版</param>
- /// <returns>保存成功true;保存失败false</returns>
- public bool WriteLayout(Stream stream)
- {
- PropertyOfLayoutBox propertyOflayoutBox = new PropertyOfLayoutBox();
- propertyOflayoutBox.LastItemRF = _lastItemRF;
- propertyOflayoutBox.NewItemID = _newItemID;
- propertyOflayoutBox.PaperHeight = _paperHeight;
- propertyOflayoutBox.PaperWidth = _paperWidth;
- propertyOflayoutBox.BackgroundImage = paperArea.BackgroundImage;
- propertyOflayoutBox.BackgroundImageName = _backgroundImageName;
- propertyOflayoutBox.PrintBackground = _isPrintBackground;
- propertyOflayoutBox.PrintAreaVisible = _isPrintAreaVisible;
- propertyOflayoutBox.Items = new ArrayList(_items);
- // 序列化
- BinaryFormatter formatter = new BinaryFormatter();
- // TODO 加密保存
- if (CryptoStream)
- {
- Rijndael age = Rijndael.Create();
- CryptoStream cStream = new CryptoStream(stream,
- age.CreateEncryptor(KEY, IV),
- CryptoStreamMode.Write);
- formatter.Serialize(cStream, propertyOflayoutBox);
- cStream.FlushFinalBlock();
- }
- else
- {
- formatter.Serialize(stream, propertyOflayoutBox);
- }
- return true;
- }
- /// <summary>
- /// 根据指定的ItemType,创建一个新Item对象
- /// </summary>
- /// <param name="itemType">ItemType的枚举值,表示创建的项目类型</param>
- /// <param name="isShowDialog">是否显示属性设置对话框</param>
- /// <returns>创建的新Item对象</returns>
- public LayoutItem NewItem(ItemType itemType, bool isShowDialog)
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return null;
- }
- LayoutItem item = null;
- switch (itemType)
- {
- case ItemType.Text:
- item = NewTextItem(isShowDialog);
- break;
- case ItemType.Image:
- item = NewImageItem(isShowDialog);
- break;
- case ItemType.Rectangle:
- item = NewRectangleItem(isShowDialog);
- break;
- case ItemType.Ellipse:
- item = NewEllipseItem(isShowDialog);
- break;
- case ItemType.Grid:
- if (OneGridItem != null)
- {
- return null;
- }
- item = NewGridItem(isShowDialog);
- OneGridItem = item as GridItem;
- break;
- case ItemType.PageNum:
- item = NewPageNumItem(isShowDialog);
- break;
- case ItemType.TotalText:
- item = NewTotalTextItem(isShowDialog);
- break;
- case ItemType.Barcode:
- item = NewBarcodeItem(isShowDialog);
- break;
- case ItemType.QRcode:
- item = NewQRcodeItem(isShowDialog);
- break;
- default:
- return null;
- }
- if (item != null)
- {
- _lastItemRF.X = item.Left;
- _lastItemRF.Y = item.Top;
- if (_lastItemRF.Width < item.Width)
- {
- _lastItemRF.Width = item.Width;
- }
- _lastItemRF.Height = item.Height;
- _isDirty = true;
- SetItemChangedArgs(item, ItemChangeType.Added);
- }
- return item;
- }
- #endregion 保存、读取相关
- #region Item操作相关
- /// <summary>
- /// 根据ItemID,获取Item
- /// </summary>
- /// <param name="itemID">ItemID</param>
- /// <returns>Item</returns>
- public LayoutItem GetItem(int itemID)
- {
- foreach (LayoutItem item in _items)
- {
- if (item.ID == itemID)
- {
- return item;
- }
- }
- return null;
- }
- /// <summary>
- /// 根据指定的ItemType,获取Item
- /// </summary>
- /// <param name="itemType">ItemType</param>
- /// <returns>Item</returns>
- public LayoutItem[] GetItems(ItemType itemType)
- {
- //var q = from item in _items where item.ItemType ==itemType select item;
- Predicate<LayoutItem> isPrime = delegate(LayoutItem item)
- {
- return (item.ItemType == itemType) ? true : false;
- };
- return _items.FindAll(isPrime).ToArray();
- }
- /// <summary>
- /// 改变指定Item的选择状态
- /// </summary>
- /// <param name="index">Item在Item集合中的索引</param>
- /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
- public void SelectItem(int index, bool value)
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- if (index < 0 || index >= Items.Count)
- {
- return;
- }
- LayoutItem item = _items[index];
- SelectItemMultiple(item, value);
- }
- /// <summary>
- /// 改变指定Item的选择状态
- /// </summary>
- /// <param name="indexes">Item在Item集合中的索引</param>
- /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
- public void SelectItems(int[] indexes, bool value)
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- List<LayoutItem> items = new List<LayoutItem>();
- foreach (int index in indexes)
- {
- if (index < 0 || Items.Count <= index)
- {
- continue;
- }
- items.Add(Items[index]);
- }
- if (0 < items.Count)
- {
- SelectItemsSingle(items.ToArray(), value);
- }
- }
- /// <summary>
- /// 改变指定Item的选择状态
- /// </summary>
- /// <param name="item">Item</param>
- /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
- public void SelectItemSingle(LayoutItem item, bool value)
- {
- if (_layoutMode != LayoutMode.Edit || item == null)
- {
- return;
- }
- bool isChanging = false;
- ItemSelectionChanging = true;
- if (value)
- {
- if (0 < _selectedItems.Count)
- {
- LayoutItem[] selectedItems = _selectedItems.ToArray();
- foreach (LayoutItem selectedItem in selectedItems)
- {
- if (item == null || item.ID == selectedItem.ID)
- {
- continue;
- }
- isChanging = true;
- selectedItem.Selected = false;
- _selectedItems.Remove(selectedItem);
- if (selectedItem.Frame != null)
- {
- selectedItem.Frame.Dispose();
- selectedItem.Frame = null;
- }
- SetItemSelectionChangedArgs(selectedItem, false);
- }
- }
- }
- if (value != item.Selected)
- {
- if (value)
- {
- item.Frame = new ItemFrame(item);
- item.Selected = true;
- _selectedItems.Add(item);
- _selectedItems.Sort(_comparisonItems);
- paperArea.Controls.Add(item.Frame);
- item.Frame.BringToFront();
- }
- else
- {
- item.Selected = false;
- _selectedItems.Remove(item);
- if (item.Frame != null)
- {
- item.Frame.Dispose();
- item.Frame = null;
- }
- }
- isChanging = true;
- SetItemSelectionChangedArgs(item, value);
- }
- ItemSelectionChanging = false;
- if (isChanging)
- {
- if (UserSelectionChanged != null)
- {
- UserSelectionChanged(this, EventArgs.Empty);
- }
- }
- }
- /// <summary>
- /// 改变指定Item的选择状态
- /// </summary>
- /// <param name="items">Item</param>
- /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
- public void SelectItemsSingle(LayoutItem[] items, bool value)
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- if (items == null || items.Length <= 0)
- {
- return;
- }
- bool isChanging = false;
- ItemSelectionChanging = true;
- if (value)
- {
- if (0 < _selectedItems.Count)
- {
- LayoutItem[] selecteditems = _selectedItems.ToArray();
- foreach (LayoutItem selecteditem in selecteditems)
- {
- if (selecteditem == null)
- {
- continue;
- }
- foreach (LayoutItem item in items)
- {
- if (item == null || item.ID == selecteditem.ID)
- {
- continue;
- }
- isChanging = true;
- selecteditem.Selected = false;
- _selectedItems.Remove(selecteditem);
- if (selecteditem.Frame != null)
- {
- selecteditem.Frame.Dispose();
- selecteditem.Frame = null;
- }
- SetItemSelectionChangedArgs(selecteditem, false);
- }
- }
- }
- }
- foreach (LayoutItem item in items)
- {
- SelectItemMultiple(item, value, ref isChanging);
- }
- ItemSelectionChanging = false;
- if (isChanging)
- {
- if (UserSelectionChanged != null)
- {
- UserSelectionChanged(this, EventArgs.Empty);
- }
- }
- }
- /// <summary>
- /// 改变指定Item的选择状态
- /// </summary>
- /// <param name="item">Item</param>
- /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
- public void SelectItemMultiple(LayoutItem item, bool value)
- {
- bool isChanging = false;
- ItemSelectionChanging = true;
- SelectItemMultiple(item, value, ref isChanging);
- ItemSelectionChanging = false;
- if (isChanging)
- {
- if (UserSelectionChanged != null)
- {
- UserSelectionChanged(this, EventArgs.Empty);
- }
- }
- }
- /// <summary>
- /// 改变指定Item的选择状态
- /// </summary>
- /// <param name="item">Item</param>
- /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
- /// <param name="isChanging">是否改变</param>
- private void SelectItemMultiple(LayoutItem item, bool value, ref bool isChanging)
- {
- if (_layoutMode != LayoutMode.Edit || item == null)
- {
- return;
- }
- if (value != item.Selected)
- {
- if (value)
- {
- item.Frame = new ItemFrame(item);
- item.Selected = true;
- _selectedItems.Add(item);
- _selectedItems.Sort(_comparisonItems);
- paperArea.Controls.Add(item.Frame);
- item.Frame.BringToFront();
- }
- else
- {
- item.Selected = false;
- _selectedItems.Remove(item);
- if (item.Frame != null)
- {
- item.Frame.Dispose();
- item.Frame = null;
- }
- }
- isChanging = true;
- SetItemSelectionChangedArgs(item, value);
- }
- }
- /// <summary>
- /// 改变指定Item的选择状态
- /// </summary>
- /// <param name="items">Item</param>
- /// <param name="value">Item的选择状态,选中为true;未选中为true</param>
- public void SelectItemsMultiple(LayoutItem[] items, bool value)
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- if (items == null || items.Length <= 0)
- {
- return;
- }
- bool isChanging = false;
- ItemSelectionChanging = true;
- foreach (LayoutItem item in items)
- {
- SelectItemMultiple(item, value, ref isChanging);
- }
- ItemSelectionChanging = false;
- if (isChanging)
- {
- if (UserSelectionChanged != null)
- {
- UserSelectionChanged(this, EventArgs.Empty);
- }
- }
- }
- /// <summary>
- /// 取消所有选择的项目
- /// </summary>
- public void ClearSelection()
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- bool isChanging = false;
- ItemSelectionChanging = true;
- if (0 < _selectedItems.Count)
- {
- LayoutItem[] selectedItems = _selectedItems.ToArray();
- foreach (LayoutItem selectedItem in selectedItems)
- {
- if (selectedItem == null)
- {
- continue;
- }
- isChanging = true;
- selectedItem.Selected = false;
- _selectedItems.Remove(selectedItem);
- if (selectedItem.Frame != null)
- {
- selectedItem.Frame.Dispose();
- selectedItem.Frame = null;
- }
- SetItemSelectionChangedArgs(selectedItem, false);
- }
- }
- ItemSelectionChanging = false;
- if (isChanging)
- {
- if (UserSelectionChanged != null)
- {
- UserSelectionChanged(this, EventArgs.Empty);
- }
- }
- }
- /// <summary>
- /// 剪切选中的Item到剪切板
- /// </summary>
- public void CutItem()
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- _pasteCount = 0;
- if (0 < _selectedItems.Count)
- {
- CopyToClipboard(new ArrayList(_selectedItems));
- DeleteItem();
- _isDirty = true;
- }
- }
- /// <summary>
- /// 粘贴Item到LayoutBox
- /// </summary>
- public void PasteItem()
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- object objectItems = GetFromClipboard();
- if (objectItems == null)
- {
- _pasteCount = 0;
- return;
- }
- ArrayList items = objectItems as ArrayList;
- if (items == null || items.Count <= 0)
- {
- _pasteCount = 0;
- return;
- }
- List<LayoutItem> layoutItems = new List<LayoutItem>();
- _pasteCount++;
- foreach (LayoutItem item in items)
- {
- if (item == null || item is GridItem)
- {
- continue;
- }
- int id = item.ID;
- TextItem textItem = null;
- string dataMember = null;
- string defaultItemName = null;
- string textItemName = null;
- if (item.ItemType == ItemType.Text || item.ItemType == ItemType.TotalText)
- {
- textItem = item as TextItem;
- if (textItem != null)
- {
- dataMember = textItem.DataMember;
- defaultItemName = textItem.DefaultItemName;
- textItemName = textItem.TextItemName;
- }
- }
- item.Init(this, true);
- if (textItem != null)
- {
- textItem.DataMember = dataMember;
- textItem.DefaultItemName = defaultItemName;
- textItem.TextItemName = textItemName;
- }
- //int top = item.ShapeBounds.Top + (item.ShapeBounds.Height + 2) * _pasteCount;
- // 下移60mm
- //int top = item.ShapeBounds.Top + 227;
- ////item.ResetItemLocation(item.ShapeBounds.Left, top);
- //设置图形像素坐标时刷新毫米坐标
- //item.MoveItem(item.ShapeBounds.Left, top);
- // 下移5mm
- item.Top += 5;
- _isDirty = true;
- layoutItems.Add(item);
- if (this.UserAddedItem != null)
- {
- ItemChangedEventArgs e = new ItemChangedEventArgs();
- e.Item = item;
- e.ChangedType = ItemChangeType.Added;
- e.ItemID = item.ID;
- this.UserAddedItem(this, e);
- }
- SetItemChangedArgs(item, ItemChangeType.Added);
- }
- // 粘贴的新Item为选择状态
- if (1 == layoutItems.Count)
- {
- SelectItemSingle(layoutItems[0], true);
- }
- else if (1 < layoutItems.Count)
- {
- ClearSelection();
- SelectItemsMultiple(layoutItems.ToArray(), true);
- }
- }
- /// <summary>
- /// 复制选中的Item到剪切板
- /// </summary>
- public void CopyItem()
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- _pasteCount = 0;
- if (0 < _selectedItems.Count)
- {
- CopyToClipboard(new ArrayList(_selectedItems));
- }
- }
- /// <summary>
- /// 删除选中的Item
- /// </summary>
- public void DeleteItem()
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- if (0 < _selectedItems.Count)
- {
- LayoutItem[] items = _selectedItems.ToArray();
- foreach (LayoutItem item in items)
- {
- ItemChangedEventArgs e = new ItemChangedEventArgs();
- e.Item = item;
- e.ChangedType = ItemChangeType.Deleted;
- e.ItemID = item.ID;
- DeletetItem(item);
- if (this.UserDeletedItem != null)
- {
- this.UserDeletedItem(this, e);
- }
- }
- }
- }
- /// <summary>
- /// 根据ItemID删除Item
- /// </summary>
- /// <param name="itemID">ItemID</param>
- public void DeletetItem(int itemID)
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- LayoutItem item = GetItem(itemID);
- DeletetItem(item);
- }
- /// <summary>
- /// 删除指定的Item
- /// </summary>
- /// <param name="item">item</param>
- public void DeletetItem(LayoutItem item)
- {
- if (_layoutMode != LayoutMode.Edit || item == null)
- {
- return;
- }
- if (item.Selected)
- {
- item.Selected = false;
- _selectedItems.Remove(item);
- if (item.Frame != null)
- {
- item.Frame.Dispose();
- item.Frame = null;
- }
- SetItemSelectionChangedArgs(item, false);
- }
- _items.Remove(item);
- item.Dispose();
- _isDirty = true;
- SetItemChangedArgs(item, ItemChangeType.Deleted);
- }
- /// <summary>
- /// 重绘Item的内容
- /// </summary>
- public void RefreshItems()
- {
- if (_layoutMode == LayoutMode.Preview)
- {
- RefreshItemsCurrentPositionValue();
- }
- }
- /// <summary>
- /// 对齐选定的Item
- /// </summary>
- /// <param name="arrangeType">对齐方式</param>
- public void ArrangeItem(ArrangeType arrangeType)
- {
- if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
- {
- return;
- }
- switch (arrangeType)
- {
- case ArrangeType.Left:
- ArrangeItemsLeft();
- break;
- case ArrangeType.Right:
- ArrangeItemsRight();
- break;
- case ArrangeType.HorizontalCenter:
- ArrangeItemsHorizontalCenter();
- break;
- case ArrangeType.Top:
- ArrangeItemsTop();
- break;
- case ArrangeType.Bottom:
- ArrangeItemsBottom();
- break;
- case ArrangeType.VerticalCenter:
- ArrangeItemsVerticalCenter();
- break;
- //case ArrangeType.LeftRight:
- // ArrangeItemsLeftRight();
- // break;
- //case ArrangeType.TopBottom:
- // ArrangeItemsTopBottom();
- // break;
- default:
- break;
- }
- }
- /// <summary>
- /// 所有选中的Item的顶部对齐
- /// </summary>
- public void ArrangeItemsTop()
- {
- if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
- {
- return;
- }
- float minTop = paperArea.Height;
- foreach (LayoutItem item in _selectedItems)
- {
- minTop = Math.Min(item.Top, minTop);
- }
- foreach (LayoutItem item in _selectedItems)
- {
- item.Top = minTop;
- }
- _isDirty = true;
- }
- /// <summary>
- /// 所有选中的Item的底部对齐
- /// </summary>
- public void ArrangeItemsBottom()
- {
- if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
- {
- return;
- }
- float maxBottom = 0;
- foreach (LayoutItem item in _selectedItems)
- {
- maxBottom = Math.Max(item.Bottom, maxBottom);
- }
- foreach (LayoutItem item in _selectedItems)
- {
- item.Top = maxBottom - item.Height;
- }
- _isDirty = true;
- }
- /// <summary>
- /// 所有选中的Item的左侧对齐
- /// </summary>
- public void ArrangeItemsLeft()
- {
- if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
- {
- return;
- }
- float minLeft = paperArea.Width;
- foreach (LayoutItem item in _selectedItems)
- {
- minLeft = Math.Min(item.Left, minLeft);
- }
- foreach (LayoutItem item in _selectedItems)
- {
- item.Left = minLeft;
- }
- _isDirty = true;
- }
- /// <summary>
- /// 所有选中的Item的右侧对齐
- /// </summary>
- public void ArrangeItemsRight()
- {
- if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
- {
- return;
- }
- float maxRight = 0;
- foreach (LayoutItem item in _selectedItems)
- {
- maxRight = Math.Max(item.Right, maxRight);
- }
- foreach (LayoutItem item in _selectedItems)
- {
- item.Left = maxRight - item.Width;
- }
- _isDirty = true;
- }
- /// <summary>
- /// 所有选中的Item的垂直方向中心对齐
- /// </summary>
- public void ArrangeItemsVerticalCenter()
- {
- if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
- {
- return;
- }
- float minTop = paperArea.Height;
- float maxBottom = 0;
- foreach (LayoutItem item in _selectedItems)
- {
- minTop = Math.Min(item.Top, minTop);
- maxBottom = Math.Max(item.Bottom, maxBottom);
- }
- float center = (minTop + maxBottom) / 2;
- foreach (LayoutItem item in _selectedItems)
- {
- item.Top = center - item.Height / 2;
- }
- _isDirty = true;
- }
- /// <summary>
- /// 所有选中的Item的水平方向中心对齐
- /// </summary>
- public void ArrangeItemsHorizontalCenter()
- {
- if (_layoutMode != LayoutMode.Edit || _selectedItems.Count < 2)
- {
- return;
- }
- float minLeft = paperArea.Width;
- float maxRight = 0;
- foreach (LayoutItem item in _selectedItems)
- {
- minLeft = Math.Min(item.Left, minLeft);
- maxRight = Math.Max(item.Right, maxRight);
- }
- float center = (minLeft + maxRight) / 2;
- foreach (LayoutItem item in _selectedItems)
- {
- item.Left = center - item.Width / 2;
- }
- _isDirty = true;
- }
- /// <summary>
- /// 更改所选Item(Z顺序)的顺序
- /// </summary>
- /// <param name="position">指定的位置MovingPosition的枚举值,Z-顺序</param>
- public void ChangeItemOrder(MovingPosition position)
- {
- if (_layoutMode != LayoutMode.Edit || 0 == _selectedItems.Count)
- {
- return;
- }
- switch (position)
- {
- case MovingPosition.Foremost:
- ChangeItemsOrderForemost();
- break;
- case MovingPosition.Aftermost:
- ChangeItemsOrderAftermost();
- break;
- case MovingPosition.Front:
- ChangeItemsOrderFront();
- break;
- case MovingPosition.Back:
- ChangeItemsOrderBack();
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// 将选定的Item移动到最前面
- /// </summary>
- public void ChangeItemsOrderForemost()
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- int count = _selectedItems.Count;
- if (0 < count)
- {
- foreach (LayoutItem item in _selectedItems)
- {
- paperArea.ShapeContainer.Shapes.SetChildIndex(item.Shape, 0);
- }
- paperArea.ShapeContainer.Refresh();
- _items.Sort(_comparisonItems);
- _isDirty = true;
- }
- }
- /// <summary>
- /// 将选定的Item上移一层
- /// </summary>
- public void ChangeItemsOrderFront()
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- int count = _selectedItems.Count;
- if (0 < count)
- {
- int childIndex = paperArea.ShapeContainer.Shapes.
- GetChildIndex(_selectedItems[count - 1].Shape) - 1;
- childIndex = (childIndex < 0) ? 0 : childIndex;
- foreach (LayoutItem item in _selectedItems)
- {
- paperArea.ShapeContainer.Shapes.SetChildIndex(item.Shape, childIndex);
- }
- paperArea.ShapeContainer.Refresh();
- _items.Sort(_comparisonItems);
- _isDirty = true;
- }
- }
- /// <summary>
- /// 将选定的Item移动到最后面
- /// </summary>
- public void ChangeItemsOrderBack()
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- int count = _selectedItems.Count;
- if (0 < count)
- {
- int childIndex = paperArea.ShapeContainer.Shapes.
- GetChildIndex(_selectedItems[0].Shape) + 1;
- for (int i = count - 1; 0 <= i; i--)
- {
- paperArea.ShapeContainer.Shapes.SetChildIndex(_selectedItems[i].Shape, childIndex);
- }
- paperArea.ShapeContainer.Refresh();
- _items.Sort(_comparisonItems);
- _isDirty = true;
- }
- }
- /// <summary>
- /// 将选定的Item下移一层
- /// </summary>
- public void ChangeItemsOrderAftermost()
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- int count = _selectedItems.Count;
- if (0 < count)
- {
- for (int i = count - 1; 0 <= i; i--)
- {
- paperArea.ShapeContainer.Shapes.SetChildIndex(_selectedItems[i].Shape, int.MaxValue);
- }
- paperArea.ShapeContainer.Refresh();
- _items.Sort(_comparisonItems);
- _isDirty = true;
- }
- }
- /// <summary>
- /// 将LayoutBox的Dirty状态修改成false
- /// 接受之前所有的改变
- /// </summary>
- public void AcceptChange()
- {
- this._isDirty = false;
- }
- #endregion Item操作相关
- #region 设置对话框
- /// <summary>
- /// 显示背景设置的对话框
- /// </summary>
- /// <returns>
- /// DialogResult.OK:设置成功<br/>
- /// DialogResult.Cancel:取消设置<br/>
- /// DialogResult.None:LayoutBox不是编辑模式
- /// </returns>
- public DialogResult ShowBackgroundSettingDialog()
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return DialogResult.None;
- }
- using (BackgroundSetting backgroundSetting = new BackgroundSetting(this))
- {
- DialogResult result = backgroundSetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- _backgroundImageName = backgroundSetting.BackgroundImageName;
- paperArea.BackgroundImage = backgroundSetting.BackgroundImage;
- _isDirty = true;
- }
- return result;
- }
- }
- /// <summary>
- /// 显示纸张设置的对话框
- /// </summary>
- /// <returns>
- /// DialogResult.OK:设置成功<br/>
- /// DialogResult.Cancel:取消设置<br/>
- /// DialogResult.None:LayoutBox不是编辑模式
- /// </returns>
- public DialogResult ShowPaperPropertyDialog()
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return DialogResult.None;
- }
- using (PaperAreaSetting paperSizeSetting = new PaperAreaSetting())
- {
- paperSizeSetting.PaperWidth = _paperWidth;
- paperSizeSetting.PaperHeight = _paperHeight;
- DialogResult result = paperSizeSetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- SetPaperAreaSize(paperSizeSetting.PaperWidth,
- paperSizeSetting.PaperHeight);
- }
- return result;
- }
- }
- /// <summary>
- /// 显示选中Item的属性设置画面
- /// </summary>
- /// <returns>
- /// DialogResult.OK:选中的Item有效,设置成功<br/>
- /// DialogResult.Cancel:选中的Item有效,取消设置<br/>
- /// DialogResult.None:LayoutBox不是编辑模式,或选中的Item不是一个
- /// </returns>
- public DialogResult ShowItemPropertyDialog()
- {
- if (_layoutMode != LayoutMode.Edit || _selectedItems.Count != 1)
- {
- return DialogResult.None;
- }
- LayoutItem item = _selectedItems[0];
- if (item != null)
- {
- return item.ShowItemPropertyDialog();
- }
- return DialogResult.None;
- }
- #endregion 设置对话框
- #region 打印相关
- /// <summary>
- /// 使用打印机已设置的信息进行打印测试
- /// </summary>
- public bool PrintTest()
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return false;
- }
- _printType = PrintType.PrintTest;
- if (CheckPrintSetting())
- {
- DialogResult result = printDialog.ShowDialog(this);
- if (result == DialogResult.OK)
- {
- printDocument.PrinterSettings.PrinterName = printDialog.PrinterSettings.PrinterName;
- if (printDocument.PrinterSettings.IsValid)
- {
- printDocument.Print();
- return true;
- }
- else
- {
- MessageBox.Show(string.Format("打印机名({0}/{1})不正确。"
- , printDocument.PrinterSettings.PrinterName
- , printDialog.PrinterSettings.PrinterName),
- "打印失败",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error,
- MessageBoxDefaultButton.Button1);
- return false;
- }
- }
- }
- return false;
- }
- /// <summary>
- /// 使用打印机已设置的信息进行打印
- /// </summary>
- public bool Print(bool checkPrintSetting = true)
- {
- //#if DEBUG
- // Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
- // "Print",
- // "begin",
- // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
- // Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
- //#endif
- _printType = PrintType.Print;
- if (!checkPrintSetting || CheckPrintSetting())
- {
- printDocument.Print();
- //#if DEBUG
- // Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
- // "Print",
- // "end",
- // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
- // Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
- //#endif
- return true;
- }
- return false;
- }
- /// <summary>
- /// 使用打印机已设置的信息进行分配打印
- /// </summary>
- public bool AllocationPrint()
- {
- _printType = PrintType.AllocationPrint;
- if (CheckPrintSetting())
- {
- printDocument.Print();
- return true;
- }
- return false;
- }
- /// <summary>
- /// 使用打印机已设置的信息进行打印预览
- /// </summary>
- public bool PrintPreview()
- {
- _printType = PrintType.PrintPreview;
- if (CheckPrintSetting())
- {
- //printPreviewDialogEx.WindowState = FormWindowState.Maximized;
- //printPreviewDialogEx.PrintPreviewControl.Columns = 1;
- //printPreviewDialogEx.PrintPreviewControl.Rows = 1;
- //printPreviewDialogEx.PrintPreviewControl.StartPage = 0;
- //printPreviewDialogEx.ShowDialog();
- printPreviewDialog1.WindowState = FormWindowState.Maximized;
- printPreviewDialog1.PrintPreviewControl.Columns = 1;
- printPreviewDialog1.PrintPreviewControl.Rows = 1;
- printPreviewDialog1.PrintPreviewControl.StartPage = 0;
- printPreviewDialog1.ShowDialog();
- return true;
- }
- return false;
- }
- /// <summary>
- /// 使用打印机已设置的信息进行分配打印预览
- /// </summary>
- public bool AllocationPrintPreview()
- {
- _printType = PrintType.AllocationPrintPreview;
- if (CheckPrintSetting())
- {
- //printPreviewDialogEx.WindowState = FormWindowState.Maximized;
- //printPreviewDialogEx.PrintPreviewControl.Columns = 1;
- //printPreviewDialogEx.PrintPreviewControl.Rows = 1;
- //printPreviewDialogEx.PrintPreviewControl.StartPage = 0;
- //printPreviewDialogEx.ShowDialog();
- printPreviewDialog1.WindowState = FormWindowState.Maximized;
- printPreviewDialog1.PrintPreviewControl.Columns = 1;
- printPreviewDialog1.PrintPreviewControl.Rows = 1;
- printPreviewDialog1.PrintPreviewControl.StartPage = 0;
- printPreviewDialog1.ShowDialog();
- return true;
- }
- return false;
- }
- #endregion 打印相关
- #endregion public函数
- #region private函数
- #region 保存、读取相关
- /// <summary>
- /// 创建一个新条码文本Item
- /// </summary>
- /// <param name="isShowDialog">是否显示属性设置对话框</param>
- /// <returns>创建的新Item对象</returns>
- private LayoutItem NewQRcodeItem(bool isShowDialog)
- {
- TextItem textItem = new TextItem(null);
- textItem.IsNew = true;
- PointF newItemLocation = GetNewItemLocation(LayoutCommon.DefaultTextItemSizeWidth,
- LayoutCommon.DefaultTextItemSizeHeight);
- if (isShowDialog)
- {
- using (TextItemSetting itemPropertySetting = new TextItemSetting())
- {
- itemPropertySetting.IsNew = true;
- itemPropertySetting.IsDataBinding = false;
- itemPropertySetting.TextPreview = "1234567890";
- itemPropertySetting.TextWidth = 25;
- itemPropertySetting.TextHeight = 25;
- itemPropertySetting.TextFont = textItem.Font;
- itemPropertySetting.TextLocationX = newItemLocation.X;
- itemPropertySetting.TextLocationY = newItemLocation.Y;
- itemPropertySetting.ItemStyle = ItemStyle.QRCode;
- itemPropertySetting.ItemName = LayoutBox.ITEMNAME_FIX + _newItemID;
- DialogResult result = itemPropertySetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- textItem.Init(this, true);
- textItem.IsDataBinding = false;
- textItem.Left = itemPropertySetting.TextLocationX;
- textItem.Top = itemPropertySetting.TextLocationY;
- textItem.Width = itemPropertySetting.TextWidth;
- textItem.Height = itemPropertySetting.TextHeight;
- textItem.LineSpace = itemPropertySetting.LineSpace;
- textItem.CharacterSpace = itemPropertySetting.CharacterSpace;
- textItem.CharacterCount = itemPropertySetting.CharacterCount;
- textItem.Font = itemPropertySetting.TextFont;
- textItem.Wrap = itemPropertySetting.Wrap;
- textItem.TextColor = itemPropertySetting.TextColor;
- textItem.TextAlign = itemPropertySetting.TextAlign;
- textItem.TextAlignVertical = itemPropertySetting.TextAlignVertical;
- textItem.DisplayValue = itemPropertySetting.TextPreview;
- textItem.ItemStyle = itemPropertySetting.ItemStyle;
- textItem.TextItemName = itemPropertySetting.ItemName;
- textItem.Format = itemPropertySetting.TextFormat;
- textItem.Culture = itemPropertySetting.Culture;
- textItem.DefaultValue = itemPropertySetting.DefaultValue;
- textItem.IsNew = false;
- textItem.PicColumn = false;
- textItem.PicMargin = itemPropertySetting.chkM.Checked;
- textItem.FixedRatio = itemPropertySetting.chkFixedRatio.Checked;
- textItem.SetDrawProperty();
- return textItem;
- }
- else
- {
- return null;
- }
- }
- }
- else
- {
- textItem.Init(this, true);
- textItem.Left = newItemLocation.X;
- textItem.Top = newItemLocation.Y;
- textItem.ItemStyle = ItemStyle.QRCode;
- textItem.Width = 25;
- textItem.Height = 25;
- textItem.DisplayValue = "1234567890";
- textItem.IsNew = false;
- textItem.PicColumn = false;
- textItem.PicMargin = false;
- textItem.FixedRatio = false;
- textItem.SetDrawProperty();
- return textItem;
- }
- }
- /// <summary>
- /// 创建一个新条码文本Item
- /// </summary>
- /// <param name="isShowDialog">是否显示属性设置对话框</param>
- /// <returns>创建的新Item对象</returns>
- private LayoutItem NewBarcodeItem(bool isShowDialog)
- {
- TextItem textItem = new TextItem(null);
- textItem.IsNew = true;
- PointF newItemLocation = GetNewItemLocation(LayoutCommon.DefaultTextItemSizeWidth,
- LayoutCommon.DefaultTextItemSizeHeight);
- if (isShowDialog)
- {
- using (TextItemSetting itemPropertySetting = new TextItemSetting())
- {
- itemPropertySetting.IsNew = true;
- itemPropertySetting.IsDataBinding = false;
- itemPropertySetting.TextPreview = "12345678901";
- itemPropertySetting.TextWidth = 50;
- itemPropertySetting.TextHeight = 20;
- itemPropertySetting.TextFont = textItem.Font;
- itemPropertySetting.TextLocationX = newItemLocation.X;
- itemPropertySetting.TextLocationY = newItemLocation.Y;
- itemPropertySetting.ItemStyle = ItemStyle.Barcode;
- itemPropertySetting.ItemName = LayoutBox.ITEMNAME_FIX + _newItemID;
- DialogResult result = itemPropertySetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- textItem.Init(this, true);
- textItem.IsDataBinding = false;
- textItem.Left = itemPropertySetting.TextLocationX;
- textItem.Top = itemPropertySetting.TextLocationY;
- textItem.Width = itemPropertySetting.TextWidth;
- textItem.Height = itemPropertySetting.TextHeight;
- textItem.LineSpace = itemPropertySetting.LineSpace;
- textItem.CharacterSpace = itemPropertySetting.CharacterSpace;
- textItem.CharacterCount = itemPropertySetting.CharacterCount;
- textItem.Font = itemPropertySetting.TextFont;
- textItem.Wrap = itemPropertySetting.Wrap;
- textItem.TextColor = itemPropertySetting.TextColor;
- textItem.TextAlign = itemPropertySetting.TextAlign;
- textItem.TextAlignVertical = itemPropertySetting.TextAlignVertical;
- textItem.DisplayValue = itemPropertySetting.TextPreview;
- textItem.ItemStyle = itemPropertySetting.ItemStyle;
- textItem.TextItemName = itemPropertySetting.ItemName;
- textItem.Format = itemPropertySetting.TextFormat;
- textItem.Culture = itemPropertySetting.Culture;
- textItem.DefaultValue = itemPropertySetting.DefaultValue;
- textItem.IsNew = false;
- textItem.PicColumn = false;
- textItem.PicMargin = itemPropertySetting.chkM.Checked;
- textItem.FixedRatio = itemPropertySetting.chkFixedRatio.Checked;
- textItem.SetDrawProperty();
- return textItem;
- }
- else
- {
- return null;
- }
- }
- }
- else
- {
- textItem.Init(this, true);
- textItem.Left = newItemLocation.X;
- textItem.Top = newItemLocation.Y;
- textItem.ItemStyle = ItemStyle.Barcode;
- textItem.Width = 50;
- textItem.Height = 20;
- textItem.DisplayValue = "12345678901";
- textItem.IsNew = false;
- textItem.PicColumn = false;
- textItem.PicMargin = false;
- textItem.FixedRatio = false;
- textItem.SetDrawProperty();
- return textItem;
- }
- }
- /// <summary>
- /// 创建一个新文本Item
- /// </summary>
- /// <param name="isShowDialog">是否显示属性设置对话框</param>
- /// <returns>创建的新Item对象</returns>
- private LayoutItem NewTextItem(bool isShowDialog)
- {
- TextItem textItem = new TextItem(null);
- textItem.IsNew = true;
- PointF newItemLocation = GetNewItemLocation(LayoutCommon.DefaultTextItemSizeWidth,
- LayoutCommon.DefaultTextItemSizeHeight);
- if (isShowDialog)
- {
- using (TextItemSetting itemPropertySetting = new TextItemSetting())
- {
- itemPropertySetting.IsNew = true;
- itemPropertySetting.IsDataBinding = false;
- itemPropertySetting.TextFont = textItem.Font;
- itemPropertySetting.TextLocationX = newItemLocation.X;
- itemPropertySetting.TextLocationY = newItemLocation.Y;
- itemPropertySetting.ItemStyle = ItemStyle.Other;
- itemPropertySetting.ItemName = LayoutBox.ITEMNAME_FIX + _newItemID;
- DialogResult result = itemPropertySetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- textItem.Init(this, true);
- textItem.IsDataBinding = false;
- textItem.Left = itemPropertySetting.TextLocationX;
- textItem.Top = itemPropertySetting.TextLocationY;
- textItem.Width = itemPropertySetting.TextWidth;
- textItem.Height = itemPropertySetting.TextHeight;
- textItem.LineSpace = itemPropertySetting.LineSpace;
- textItem.CharacterSpace = itemPropertySetting.CharacterSpace;
- textItem.CharacterCount = itemPropertySetting.CharacterCount;
- textItem.Font = itemPropertySetting.TextFont;
- textItem.Wrap = itemPropertySetting.Wrap;
- textItem.TextColor = itemPropertySetting.TextColor;
- textItem.TextAlign = itemPropertySetting.TextAlign;
- textItem.TextAlignVertical = itemPropertySetting.TextAlignVertical;
- textItem.DisplayValue = itemPropertySetting.TextPreview;
- textItem.ItemStyle = itemPropertySetting.ItemStyle;
- textItem.TextItemName = itemPropertySetting.ItemName;
- textItem.Format = itemPropertySetting.TextFormat;
- textItem.Culture = itemPropertySetting.Culture;
- textItem.DefaultValue = itemPropertySetting.DefaultValue;
- textItem.IsNew = false;
- textItem.SetDrawProperty();
- return textItem;
- }
- else
- {
- return null;
- }
- }
- }
- else
- {
- textItem.Init(this, true);
- textItem.Left = newItemLocation.X;
- textItem.Top = newItemLocation.Y;
- textItem.IsNew = false;
- textItem.SetDrawProperty();
- return textItem;
- }
- }
- /// <summary>
- /// 创建一个新图片Item
- /// </summary>
- /// <param name="isShowDialog">是否显示属性设置对话框</param>
- /// <returns>创建的新Item对象</returns>
- private LayoutItem NewImageItem(bool isShowDialog)
- {
- ImageItem imageItem = new ImageItem(null);
- imageItem.IsNew = true;
- openFileDialog.FileName = string.Empty;
- if (openFileDialog.ShowDialog() == DialogResult.OK)
- {
- FileInfo fileInfo = new FileInfo(openFileDialog.FileName);
- // 图片尺寸超过200kbyte
- if (LayoutConsts.FILE_DIALOG_IMAGE_MAX_SIZE < fileInfo.Length)
- {
- DialogResult dr = MessageBox.Show(
- "图片太大,影响显示和打印速度,请准备200KByte以下的图片。",
- "LayoutBox",
- MessageBoxButtons.YesNo,
- MessageBoxIcon.Warning,
- MessageBoxDefaultButton.Button1);
- if (dr == DialogResult.Yes)
- {
- return null;
- }
- }
- Image image = null;
- float imageWidth = LayoutConsts.SHAPE_ITEM_SIZE_DEFAULT_WIDTH;
- float imageHeigh = LayoutConsts.SHAPE_ITEM_SIZE_DEFAULT_HEIGHT;
- if (fileInfo.Length > 0)
- {
- Stream imageStream = null;
- try
- {
- imageStream = fileInfo.Open(FileMode.Open,
- FileAccess.Read,
- FileShare.ReadWrite | FileShare.Delete);
- image = Image.FromStream(imageStream);
- MemoryStream mstream = new MemoryStream();
- image.Save(mstream, image.RawFormat);
- image = Image.FromStream(mstream);
- }
- finally
- {
- if (imageStream != null)
- {
- imageStream.Dispose();
- }
- }
- imageWidth = LayoutCommon.PixelToMillimeter(image.Width, _zoom, image.HorizontalResolution);
- imageHeigh = LayoutCommon.PixelToMillimeter(image.Height, _zoom, image.VerticalResolution);
- }
- else
- {
- MessageBox.Show("不能设置大小为0的图片。",
- "LayoutBox",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error,
- MessageBoxDefaultButton.Button1);
- return null;
- }
- PointF newItemLocation = GetNewItemLocation(imageWidth, imageHeigh);
- if (isShowDialog)
- {
- ImageItemSetting itemPropertySetting = new ImageItemSetting();
- itemPropertySetting.ImageLocationX = newItemLocation.X;
- itemPropertySetting.ImageLocationY = newItemLocation.Y;
- itemPropertySetting.ImageWidth = imageWidth;
- itemPropertySetting.ImageHeight = imageHeigh;
- DialogResult result = itemPropertySetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- imageItem.Init(this, true);
- imageItem.Width = itemPropertySetting.ImageWidth;
- imageItem.Height = itemPropertySetting.ImageHeight;
- imageItem.Left = itemPropertySetting.ImageLocationX;
- imageItem.Top = itemPropertySetting.ImageLocationY;
- imageItem.SideRatioFixed = itemPropertySetting.SideRatioFixed;
- imageItem.Image = image;
- imageItem.FileName = openFileDialog.FileName;
- imageItem.IsNew = false;
- imageItem.SetDrawProperty();
- return imageItem;
- }
- else
- {
- if (image != null)
- {
- image.Dispose();
- }
- return null;
- }
- }
- else
- {
- if (imageWidth < LayoutConsts.ITEM_SIZE_MIN)
- {
- imageWidth = LayoutConsts.ITEM_SIZE_MIN;
- }
- else if (LayoutConsts.ITEM_SIZE_WIDTH_MAX < imageWidth)
- {
- imageWidth = LayoutConsts.ITEM_SIZE_WIDTH_MAX;
- }
- if (imageHeigh < LayoutConsts.ITEM_SIZE_MIN)
- {
- imageHeigh = LayoutConsts.ITEM_SIZE_MIN;
- }
- else if (LayoutConsts.ITEM_SIZE_HEIGHT_MAX < imageHeigh)
- {
- imageHeigh = LayoutConsts.ITEM_SIZE_HEIGHT_MAX;
- }
- imageItem.Init(this, true);
- imageItem.Left = newItemLocation.X;
- imageItem.Top = newItemLocation.Y;
- imageItem.Width = imageWidth;
- imageItem.Height = imageHeigh;
- imageItem.Image = image;
- imageItem.FileName = openFileDialog.FileName;
- imageItem.SideRatioFixed = true;
- imageItem.IsNew = false;
- imageItem.SetDrawProperty();
- return imageItem;
- }
- }
- else
- {
- return null;
- }
- }
- /// <summary>
- /// 创建一个新矩形Item
- /// </summary>
- /// <param name="isShowDialog">是否显示属性设置对话框</param>
- /// <returns>创建的新Item对象</returns>
- private LayoutItem NewRectangleItem(bool isShowDialog)
- {
- RectangleItem rectangleItem = new RectangleItem(null);
- rectangleItem.IsNew = true;
- PointF newItemLocation = GetNewItemLocation(50, 50);
- if (isShowDialog)
- {
- using (GraphicsItemSetting itemPropertySetting = new GraphicsItemSetting())
- {
- itemPropertySetting.ShapeLocationX = newItemLocation.X;
- itemPropertySetting.ShapeLocationY = newItemLocation.Y;
- itemPropertySetting.ShapeWidth = rectangleItem.Width;
- itemPropertySetting.ShapeHeight = rectangleItem.Height;
- itemPropertySetting.LineColor = rectangleItem.LineColor;
- itemPropertySetting.FillColor = rectangleItem.FillColor;
- itemPropertySetting.LineWidth = rectangleItem.LineWidth;
- itemPropertySetting.Transparent = rectangleItem.Transparent;
- DialogResult result = itemPropertySetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- rectangleItem.Init(this, true);
- rectangleItem.Left = itemPropertySetting.ShapeLocationX;
- rectangleItem.Top = itemPropertySetting.ShapeLocationY;
- rectangleItem.Width = itemPropertySetting.ShapeWidth;
- rectangleItem.Height = itemPropertySetting.ShapeHeight;
- rectangleItem.LineColor = itemPropertySetting.LineColor;
- rectangleItem.FillColor = itemPropertySetting.FillColor;
- rectangleItem.LineWidth = itemPropertySetting.LineWidth;
- rectangleItem.Transparent = itemPropertySetting.Transparent;
- rectangleItem.IsNew = false;
- rectangleItem.SetDrawProperty();
- return rectangleItem;
- }
- else
- {
- return null;
- }
- }
- }
- else
- {
- rectangleItem.Init(this, true);
- rectangleItem.Left = newItemLocation.X;
- rectangleItem.Top = newItemLocation.Y;
- rectangleItem.IsNew = false;
- rectangleItem.SetDrawProperty();
- return rectangleItem;
- }
- }
- /// <summary>
- /// 创建一个新椭圆Item
- /// </summary>
- /// <param name="isShowDialog">是否显示属性设置对话框</param>
- /// <returns>创建的新Item对象</returns>
- private LayoutItem NewEllipseItem(bool isShowDialog)
- {
- EllipseItem ellipseItem = new EllipseItem(null);
- ellipseItem.IsNew = true;
- PointF newItemLocation = GetNewItemLocation(50, 50);
- if (isShowDialog)
- {
- using (GraphicsItemSetting itemPropertySetting = new GraphicsItemSetting())
- {
- itemPropertySetting.ShapeLocationX = newItemLocation.X;
- itemPropertySetting.ShapeLocationY = newItemLocation.Y;
- itemPropertySetting.ShapeWidth = ellipseItem.Width;
- itemPropertySetting.ShapeHeight = ellipseItem.Height;
- itemPropertySetting.LineColor = ellipseItem.LineColor;
- itemPropertySetting.FillColor = ellipseItem.FillColor;
- itemPropertySetting.LineWidth = ellipseItem.LineWidth;
- itemPropertySetting.Transparent = ellipseItem.Transparent;
- DialogResult result = itemPropertySetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- ellipseItem.Init(this, true);
- ellipseItem.Left = itemPropertySetting.ShapeLocationX;
- ellipseItem.Top = itemPropertySetting.ShapeLocationY;
- ellipseItem.Width = itemPropertySetting.ShapeWidth;
- ellipseItem.Height = itemPropertySetting.ShapeHeight;
- ellipseItem.LineColor = itemPropertySetting.LineColor;
- ellipseItem.FillColor = itemPropertySetting.FillColor;
- ellipseItem.LineWidth = itemPropertySetting.LineWidth;
- ellipseItem.Transparent = itemPropertySetting.Transparent;
- ellipseItem.IsNew = false;
- ellipseItem.SetDrawProperty();
- return ellipseItem;
- }
- else
- {
- return null;
- }
- }
- }
- else
- {
- ellipseItem.Init(this, true);
- ellipseItem.Left = newItemLocation.X;
- ellipseItem.Top = newItemLocation.Y;
- ellipseItem.IsNew = false;
- ellipseItem.SetDrawProperty();
- return ellipseItem;
- }
- }
- /// <summary>
- /// 创建一个新表格Item
- /// </summary>
- /// <param name="isShowDialog">是否显示属性设置对话框</param>
- /// <returns>创建的新Item对象</returns>
- private LayoutItem NewGridItem(bool isShowDialog)
- {
- GridItem gridItem = new GridItem(null);
- gridItem.IsNew = true;
- PointF newItemLocation = GetNewItemLocation(50, 50);
- if (isShowDialog)
- {
- using (GridItemSetting itemPropertySetting = new GridItemSetting())
- {
- itemPropertySetting.ShapeLocationX = newItemLocation.X;
- itemPropertySetting.ShapeLocationY = newItemLocation.Y;
- itemPropertySetting.ShapeWidth = gridItem.Width;
- itemPropertySetting.ShapeHeight = gridItem.Height;
- itemPropertySetting.LineColor = gridItem.LineColor;
- itemPropertySetting.FillColor = gridItem.FillColor;
- itemPropertySetting.LineWidth = gridItem.LineWidth;
- itemPropertySetting.Transparent = gridItem.Transparent;
- itemPropertySetting.TitleVisible = gridItem.TitleVisible;
- itemPropertySetting.AlwaysTitleVisible = gridItem.AlwaysTitleVisible;
- itemPropertySetting.RowsHeight = gridItem.RowsHeight;
- itemPropertySetting.HeadRowsHeight = gridItem.HeadRowsHeight;
- itemPropertySetting.RowsCount = gridItem.DisplayRowCount;
- itemPropertySetting.Columns = gridItem.Columns.CopyTo();
- DialogResult result = itemPropertySetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- gridItem.Init(this, true);
- gridItem.Left = itemPropertySetting.ShapeLocationX;
- gridItem.Top = itemPropertySetting.ShapeLocationY;
- gridItem.Width = itemPropertySetting.ShapeWidth;
- gridItem.Height = itemPropertySetting.ShapeHeight;
- gridItem.LineColor = itemPropertySetting.LineColor;
- gridItem.FillColor = itemPropertySetting.FillColor;
- gridItem.LineWidth = itemPropertySetting.LineWidth;
- gridItem.Transparent = itemPropertySetting.Transparent;
- gridItem.TitleVisible = itemPropertySetting.TitleVisible;
- gridItem.AlwaysTitleVisible = itemPropertySetting.AlwaysTitleVisible;
- gridItem.DisplayRowCount = itemPropertySetting.RowsCount;
- gridItem.RowsHeight = itemPropertySetting.RowsHeight;
- gridItem.HeadRowsHeight = itemPropertySetting.HeadRowsHeight;
- if (itemPropertySetting.ColumnsChanged)
- {
- itemPropertySetting.Columns.GridItem = gridItem;
- gridItem.Columns = itemPropertySetting.Columns;
- }
- gridItem.IsNew = false;
- gridItem.SetDrawProperty();
- return gridItem;
- }
- else
- {
- return null;
- }
- }
- }
- else
- {
- gridItem.Init(this, true);
- gridItem.Left = newItemLocation.X;
- gridItem.Top = newItemLocation.Y;
- gridItem.IsNew = false;
- gridItem.SetDrawProperty();
- return gridItem;
- }
- }
- /// <summary>
- /// 创建一个Grid页码Item
- /// </summary>
- /// <param name="isShowDialog">是否显示属性设置对话框</param>
- /// <returns>创建的新Item对象</returns>
- private LayoutItem NewPageNumItem(bool isShowDialog)
- {
- PageNumItem pageNumItem = new PageNumItem(null);
- pageNumItem.IsNew = true;
- PointF newItemLocation = GetNewItemLocation(50, 50);
- if (isShowDialog)
- {
- using (PageNumItemSetting itemPropertySetting = new PageNumItemSetting())
- {
- itemPropertySetting.TextLocationX = newItemLocation.X;
- itemPropertySetting.TextLocationY = newItemLocation.Y;
- itemPropertySetting.TextWidth = pageNumItem.Width;
- itemPropertySetting.TextHeight = pageNumItem.Height;
- itemPropertySetting.TextAlign = pageNumItem.TextAlign;
- itemPropertySetting.TextAlignVertical = pageNumItem.TextAlignVertical;
- itemPropertySetting.TextColor = pageNumItem.TextColor;
- itemPropertySetting.TextFormat = pageNumItem.Format;
- itemPropertySetting.TextFont = pageNumItem.Font;
- DialogResult result = itemPropertySetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- pageNumItem.Init(this, true);
- pageNumItem.Left = itemPropertySetting.TextLocationX;
- pageNumItem.Top = itemPropertySetting.TextLocationY;
- pageNumItem.Width = itemPropertySetting.TextWidth;
- pageNumItem.Height = itemPropertySetting.TextHeight;
- pageNumItem.Font = itemPropertySetting.TextFont;
- pageNumItem.TextAlign = itemPropertySetting.TextAlign;
- pageNumItem.TextAlignVertical = itemPropertySetting.TextAlignVertical;
- pageNumItem.TextColor = itemPropertySetting.TextColor;
- pageNumItem.Format = itemPropertySetting.TextFormat;
- pageNumItem.IsNew = false;
- pageNumItem.SetDrawProperty();
- return pageNumItem;
- }
- else
- {
- return null;
- }
- }
- }
- else
- {
- pageNumItem.Init(this, true);
- pageNumItem.Left = newItemLocation.X;
- pageNumItem.Top = newItemLocation.Y;
- pageNumItem.IsNew = false;
- pageNumItem.SetDrawProperty();
- return pageNumItem;
- }
- }
- /// <summary>
- /// 创建一个合计文本Item
- /// </summary>
- /// <param name="isShowDialog">是否显示属性设置对话框</param>
- /// <returns>创建的新Item对象</returns>
- private LayoutItem NewTotalTextItem(bool isShowDialog)
- {
- TotalTextItem textItem = new TotalTextItem(null);
- textItem.IsNew = true;
- PointF newItemLocation = GetNewItemLocation(LayoutCommon.DefaultTextItemSizeWidth,
- LayoutCommon.DefaultTextItemSizeHeight);
- if (isShowDialog)
- {
- using (TotalTextItemSetting itemPropertySetting = new TotalTextItemSetting())
- {
- itemPropertySetting.IsNew = true;
- itemPropertySetting.IsDataBinding = false;
- itemPropertySetting.TextFont = textItem.Font;
- itemPropertySetting.TextLocationX = newItemLocation.X;
- itemPropertySetting.TextLocationY = newItemLocation.Y;
- itemPropertySetting.ItemStyle = ItemStyle.Other;
- itemPropertySetting.ItemName = LayoutBox.ITEMNAME_FIX + _newItemID;
- itemPropertySetting.NotTotalFont = textItem.NotTotalFont;
- itemPropertySetting.NotTotalColor = textItem.NotTotalColor;
- itemPropertySetting.NotTotalValue = textItem.NotTotalValue;
- itemPropertySetting.NotTotalAlign = textItem.NotTotalAlign;
- itemPropertySetting.NotTotalAlignVertical = textItem.NotTotalAlignVertical;
- DialogResult result = itemPropertySetting.ShowDialog();
- if (result == DialogResult.OK)
- {
- textItem.Init(this, true);
- textItem.IsDataBinding = false;
- textItem.Left = itemPropertySetting.TextLocationX;
- textItem.Top = itemPropertySetting.TextLocationY;
- textItem.Width = itemPropertySetting.TextWidth;
- textItem.Height = itemPropertySetting.TextHeight;
- textItem.LineSpace = itemPropertySetting.LineSpace;
- textItem.CharacterSpace = itemPropertySetting.CharacterSpace;
- textItem.CharacterCount = itemPropertySetting.CharacterCount;
- textItem.Font = itemPropertySetting.TextFont;
- textItem.Wrap = itemPropertySetting.Wrap;
- textItem.TextColor = itemPropertySetting.TextColor;
- textItem.TextAlign = itemPropertySetting.TextAlign;
- textItem.TextAlignVertical = itemPropertySetting.TextAlignVertical;
- textItem.DisplayValue = itemPropertySetting.TextPreview;
- textItem.ItemStyle = itemPropertySetting.ItemStyle;
- textItem.TextItemName = itemPropertySetting.ItemName;
- textItem.Format = itemPropertySetting.TextFormat;
- textItem.Culture = itemPropertySetting.Culture;
- textItem.DefaultValue = itemPropertySetting.DefaultValue;
- textItem.NotTotalFont = itemPropertySetting.NotTotalFont;
- textItem.NotTotalColor = itemPropertySetting.NotTotalColor;
- textItem.NotTotalValue = itemPropertySetting.NotTotalValue;
- textItem.NotTotalAlign = itemPropertySetting.NotTotalAlign;
- textItem.NotTotalAlignVertical = itemPropertySetting.NotTotalAlignVertical;
- textItem.IsNew = false;
- textItem.SetDrawProperty();
- return textItem;
- }
- else
- {
- return null;
- }
- }
- }
- else
- {
- textItem.Init(this, true);
- textItem.Left = newItemLocation.X;
- textItem.Top = newItemLocation.Y;
- textItem.IsNew = false;
- textItem.SetDrawProperty();
- return textItem;
- }
- }
- /// <summary>
- /// 清除LayoutBox的设置
- /// </summary>
- private void ClearLayoutBox()
- {
- // TODO
- if (this._clipboardFormat != null)
- {
- this.ClearClipboard();
- //if (Clipboard.ContainsData(_clipboardFormat.Name))
- //{
- // CopyToClipboard(_clipboardFormat.Name, null);
- //}
- //_clipboardFormat =
- // DataFormats.GetFormat(typeof(LayoutItem).FullName
- // + "{"
- // + Guid.NewGuid().ToString()
- // + "}");
- }
- _lastItemRF = RectangleF.Empty;
- _mouseRect = Rectangle.Empty;
- SetDitry(false);
- _selectedItems.Clear();
- foreach (LayoutItem item in _items)
- {
- if (item.Frame != null)
- {
- item.Frame.Dispose();
- item.Frame = null;
- }
- item.Selected = false;
- item.Dispose();
- }
- _items.Clear();
- _updateCount = 0;
- _newItemID = 1;
- _paperHeight = 297.0f;
- _paperWidth = 210.0f;
- _backgroundImageName = string.Empty;
- paperArea.BackgroundImage = null;
- _isPrintAreaVisible = true;
- _isPrintBackground = false;
- if (_zoomType != ZoomType.Whole)
- {
- ChangePaperSize();
- }
- else
- {
- InitializeZoom();
- }
- }
- /// <summary>
- /// 初始化LayoutBox
- /// </summary>
- private void InitializeLayoutBox()
- {
- openFileDialog.Filter = LayoutConsts.FILE_DIALOG_IMAGE_FILTER;
- openFileDialog.FilterIndex = 6;
- string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
- if (Directory.Exists(directory))
- {
- openFileDialog.InitialDirectory = directory;
- }
- else
- {
- openFileDialog.InitialDirectory =
- Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
- }
- _comparisonItems = new Comparison<LayoutItem>(SortItemsWithChildIndex);
- //// TODO
- //if (this._layoutMode == InvoiceLayout.LayoutMode.Edit)
- //{
- // _clipboardFormat =
- // DataFormats.GetFormat(typeof(LayoutItem).FullName
- // + "{"
- // + Guid.NewGuid().ToString()
- // + "}");
- //}
- tsmiPaste.Enabled = true;
- _lastItemRF = RectangleF.Empty;
- _mouseRect = Rectangle.Empty;
- _updateCount = 0;
- _newItemID = 1;
- _dataMember = string.Empty;
- _printDataMember = string.Empty;
- //_layoutMode = LayoutMode.Display;
- _paperHeight = 297.0f;
- _paperWidth = 210.0f;
- _zoom = 100;
- _backgroundImageName = string.Empty;
- _isDirty = false;
- _isPrintAreaVisible = true;
- _isPrintBackground = false;
- _selectedItems = new List<LayoutItem>();
- _items = new List<LayoutItem>();
- _zoomType = ZoomType.Specify;
- ChangePaperSize();
- InitItemZoom();
- }
- /// <summary>
- /// 获取新建Item的位置
- /// </summary>
- /// <returns>新建Item的位置</returns>
- private PointF GetNewItemLocation(float itemWidth, float itemHeight)
- {
- PointF newItemLocation = new PointF(LayoutConsts.ITEM_LOCATION_DEF, LayoutConsts.ITEM_LOCATION_DEF);
- if (paperArea.Left < 0)
- {
- newItemLocation.X += LayoutCommon.PixelToMillimeter(Math.Abs(paperArea.Left), _zoom);
- }
- if (paperArea.Top < 0)
- {
- newItemLocation.Y += LayoutCommon.PixelToMillimeter(Math.Abs(paperArea.Top), _zoom);
- }
- return newItemLocation;
- }
- /// <summary>
- /// 复制到剪贴板中
- /// </summary>
- /// <param name="format">格式</param>
- /// <param name="items">选定的Item</param>
- private void CopyToClipboard(object items)
- {
- this.GetCipboardFormat();
- if (this._clipboardFormat != null)
- {
- IDataObject dataObj = new DataObject();
- dataObj.SetData(this._clipboardFormat.Name, false, items);
- Clipboard.SetDataObject(dataObj, false);
- }
- }
- /// <summary>
- /// 获取剪贴板中的数据
- /// </summary>
- /// <param name="format">格式</param>
- /// <returns>Item</returns>
- private object GetFromClipboard()
- {
- this.GetCipboardFormat();
- if (this._clipboardFormat != null)
- {
- if (Clipboard.ContainsData(this._clipboardFormat.Name))
- {
- return Clipboard.GetData(this._clipboardFormat.Name);
- }
- }
- return null;
- }
- private void ClearClipboard()
- {
- if (this._clipboardFormat != null)
- {
- if (Clipboard.ContainsData(_clipboardFormat.Name))
- {
- IDataObject dataObj = new DataObject();
- dataObj.SetData(this._clipboardFormat.Name, false, null);
- Clipboard.SetDataObject(dataObj, false);
- }
- }
- this._clipboardFormat = null;
- }
- private DataFormats.Format GetCipboardFormat()
- {
- if (_clipboardFormat != null)
- {
- return _clipboardFormat;
- }
- // TODO
- if (this._layoutMode == InvoiceLayout.LayoutMode.Edit)
- {
- _clipboardFormat =
- DataFormats.GetFormat(typeof(LayoutItem).FullName
- + "{"
- + Guid.NewGuid().ToString()
- + "}");
- }
- return _clipboardFormat;
- }
- #endregion 保存、读取相关
- #region 缩放比例和位置
- /// <summary>
- /// 设置纸张的高和宽
- /// </summary>
- /// <param name="paperWidth">纸张的宽</param>
- /// <param name="paperHeight">纸张的高</param>
- public void SetPaperAreaSize(float paperWidth, float paperHeight)
- {
- if (paperWidth < LayoutConsts.PAPER_SIZE_MIN
- || LayoutConsts.PAPER_SIZE_MAX < paperWidth)
- {
- return;
- }
- if (paperHeight < LayoutConsts.PAPER_SIZE_MIN
- || LayoutConsts.PAPER_SIZE_MAX < paperHeight)
- {
- return;
- }
- if (_paperHeight != paperHeight || _paperWidth != paperWidth)
- {
- foreach (LayoutItem item in _items)
- {
- if (paperHeight < item.Top + item.Height
- || paperWidth < item.Left + item.Width)
- {
- MessageBox.Show("在纸张范围外有Item,请设置纸张大小包含所有Item",
- "LayoutBox",
- MessageBoxButtons.OK,
- MessageBoxIcon.Error,
- MessageBoxDefaultButton.Button1);
- return;
- }
- }
- _paperHeight = paperHeight;
- _paperWidth = paperWidth;
- if (_zoomType != ZoomType.Whole)
- {
- ChangePaperSize();
- }
- else
- {
- InitializeZoom();
- }
- if (PaperSizeChanged != null)
- {
- PaperSizeChanged(this, EventArgs.Empty);
- }
- _isDirty = true;
- }
- }
- /// <summary>
- /// 设置所有Item的缩放比例
- /// </summary>
- private void ZoomAllItems()
- {
- InitItemZoom();
- if (0 < _items.Count)
- {
- foreach (LayoutItem item in _items)
- {
- item.ResetShapeLocationAndSize(false);
- item.SetDrawProperty();
- }
- }
- }
- /// <summary>
- /// 整页显示
- /// </summary>
- private void InitializeZoom()
- {
- int paperHeight = LayoutCommon.MillimeterToPixel(_paperHeight);
- int paperWidth = LayoutCommon.MillimeterToPixel(_paperWidth);
- int marginHeight = Height - LayoutConsts.LAYOUT_MARGIN - LayoutConsts.LAYOUT_MARGIN;
- int marginWidth = Width - LayoutConsts.LAYOUT_MARGIN - LayoutConsts.LAYOUT_MARGIN;
- double zoomHeight = 100.0 * marginHeight / paperHeight;
- double zoomWidth = 100.0 * marginWidth / paperWidth;
- int zoom = System.Convert.ToInt32(Math.Truncate(Math.Min(zoomHeight, zoomWidth)));
- if (zoom < 0)
- {
- _zoom = 0;
- }
- else
- {
- _zoom = zoom;
- }
- ChangePaperSize();
- ZoomAllItems();
- }
- /// <summary>
- /// 纸张的显示比率
- /// </summary>
- private void ChangePaperSize()
- {
- int width = LayoutCommon.MillimeterToPixel(_paperWidth, _zoom);
- int height = LayoutCommon.MillimeterToPixel(_paperHeight, _zoom);
- paperArea.Size = new Size(width, height);
- AdjustPaperArea();
- }
- /// <summary>
- /// 缩放比例变更
- /// </summary>
- private void InitItemZoom()
- {
- _maxItemLeft = LayoutCommon.MillimeterToPixel(LayoutConsts.ITEM_LOCATION_MAX, _zoom);
- _maxItemTop = _maxItemLeft;
- _maxItemWidth = LayoutCommon.MillimeterToPixel(LayoutConsts.ITEM_SIZE_WIDTH_MAX, _zoom);
- _maxItemHeight = LayoutCommon.MillimeterToPixel(LayoutConsts.ITEM_SIZE_HEIGHT_MAX, _zoom);
- _minItemWidth = LayoutCommon.MillimeterToPixel(LayoutConsts.ITEM_SIZE_MIN, _zoom);
- _minItemHeight = _minItemWidth;
- }
- /// <summary>
- /// 设置纸张范围和位置
- /// </summary>
- private void AdjustPaperArea()
- {
- int top = AdjustPaperAreaTop();
- int left = AdjustPaperAreaLeft();
- paperArea.Location = new Point(left, top);
- lblRightBottomPoint.Location = new Point(paperArea.Right, paperArea.Bottom);
- }
- /// <summary>
- /// 设置纸张位置Y
- /// </summary>
- /// <returns>位置Y</returns>
- private int AdjustPaperAreaTop()
- {
- int margin = paperBox.ClientSize.Height
- - paperArea.Height
- - LayoutConsts.LAYOUT_MARGIN
- - LayoutConsts.LAYOUT_MARGIN;
- if (0 < margin)
- {
- return lblLeftTopPoint.Top + LayoutConsts.LAYOUT_MARGIN + (margin) / 2;
- }
- else
- {
- return lblLeftTopPoint.Top + LayoutConsts.LAYOUT_MARGIN;
- }
- }
- /// <summary>
- /// 设置纸张位置X
- /// </summary>
- /// <returns>位置X</returns>
- private int AdjustPaperAreaLeft()
- {
- int margin = paperBox.ClientSize.Width
- - paperArea.Width
- - LayoutConsts.LAYOUT_MARGIN
- - LayoutConsts.LAYOUT_MARGIN;
- if (0 < margin)
- {
- return lblLeftTopPoint.Left + LayoutConsts.LAYOUT_MARGIN + (margin) / 2;
- }
- else
- {
- return lblLeftTopPoint.Left + LayoutConsts.LAYOUT_MARGIN;
- }
- }
- #endregion 缩放比例和位置
- #region Item操作相关
- /// <summary>
- /// 设置Ditry
- /// </summary>
- /// <param name="vaule"></param>
- private void SetDitry(bool vaule)
- {
- _isDirty = vaule;
- }
- /// <summary>
- /// 计算滚动到指定子控制的偏移
- /// </summary>
- /// <param name="activeControl">滚动显示的子控制</param>
- /// <returns>滚动到指定子控制的偏移</returns>
- protected override Point ScrollToControl(Control activeControl)
- {
- // 选定Item时,滚动条不动
- return DisplayRectangle.Location;
- }
- /// <summary>
- /// 代表一个函数来比较两个相同类型的对象
- /// </summary>
- /// <param name="itemX">第一个比较对象</param>
- /// <param name="itemY">第二个比较对象</param>
- /// <returns>
- /// 小于0 x小于y。
- /// 等于0 x等于y。
- /// 大于0 x大于y。
- /// </returns>
- private int SortItemsWithChildIndex(LayoutItem itemX, LayoutItem itemY)
- {
- try
- {
- int x = paperArea.ShapeContainer.Shapes.GetChildIndex(itemX.Shape);
- int y = paperArea.ShapeContainer.Shapes.GetChildIndex(itemY.Shape);
- return y - x;
- }
- catch
- {
- return 0;
- }
- }
- /// <summary>
- /// 调整拖动范围
- /// </summary>
- /// <param name="p">鼠标所在位置</param>
- private void ResizeToRectangle(Point p)
- {
- Rectangle rect = _mouseRect;
- _mouseRect.Width = p.X - _mouseRect.Left;
- _mouseRect.Height = p.Y - _mouseRect.Top;
- DrawReversibleFrame(_mouseRect);
- DrawReversibleFrame(rect);
- }
- /// <summary>
- /// 显示拖动范围
- /// </summary>
- /// <param name="rect">范围</param>
- internal void DrawReversibleFrame(Rectangle rect)
- {
- ControlPaint.DrawReversibleFrame(paperArea.RectangleToScreen(rect),
- Color.Black, FrameStyle.Dashed);
- }
- /// <summary>
- /// 当前选择的对象已经改变
- /// </summary>
- private void SetTSMIOnSelectionChanged()
- {
- if (0 < _selectedItems.Count)
- {
- tsmiCut.Enabled = true;
- tsmiCopy.Enabled = true;
- tsmiDelete.Enabled = true;
- tsmiOrder.Enabled = true;
- if (_selectedItems.Count == 1)
- {
- tsmiProperties.Enabled = true;
- tsmiArrange.Enabled = false;
- }
- else
- {
- tsmiProperties.Enabled = false;
- tsmiArrange.Enabled = true;
- }
- }
- else
- {
- tsmiCut.Enabled = false;
- tsmiCopy.Enabled = false;
- tsmiDelete.Enabled = false;
- tsmiProperties.Enabled = false;
- tsmiArrange.Enabled = false;
- tsmiOrder.Enabled = false;
- }
- }
- /// <summary>
- /// 调整Item范围
- /// </summary>
- /// <param name="p">鼠标所在位置</param>
- /// <param name="index">Item索引</param>
- private void ResizeToRectangle(Point p, int index)
- {
- Rectangle rect = _moveRectangles[index];
- if (_isShiftDown)
- {
- // 长宽比例
- double sizeRatio = 1.0 * _downRectangles[index].Height / _downRectangles[index].Width;
- int changedX = Math.Abs(p.X - _startX);
- int changedY = Math.Abs(p.Y - _startY);
- if (Math.Abs(p.X - _startX) < Math.Abs(p.Y - _startY))
- {
- rect.X = _downRectangles[index].Left;
- rect.Y = _downRectangles[index].Top + p.Y - _startY;
- }
- else
- {
- rect.X = _downRectangles[index].Left + p.X - _startX;
- rect.Y = _downRectangles[index].Top;
- }
- rect.Width = _downRectangles[index].Width;
- rect.Height = _downRectangles[index].Height;
- }
- else if (_isCtrlDown)
- {
- rect.X = _downRectangles[index].Left + p.X - _startX;
- rect.Y = _downRectangles[index].Top + p.Y - _startY;
- rect.Width = _downRectangles[index].Width;
- rect.Height = _downRectangles[index].Height;
- }
- else
- {
- rect.X = _downRectangles[index].Left + p.X - _startX;
- rect.Y = _downRectangles[index].Top + p.Y - _startY;
- rect.Width = _downRectangles[index].Width;
- rect.Height = _downRectangles[index].Height;
- }
- {
- DrawReversibleFrame(_selectedItems[index].ItemLockType, _moveRectangles[index], rect);
- }
- _moveRectangles[index] = rect;
- }
- /// <summary>
- /// 绘制Item移动范围
- /// </summary>
- /// <param name="itemLock">Item锁定状态</param>
- /// <param name="rectOriginal">源范围</param>
- /// <param name="rectCurrent">新范围</param>
- private void DrawReversibleFrame(ItemLock itemLock, Rectangle? rectOriginal, Rectangle? rectCurrent)
- {
- if (itemLock == ItemLock.LockLocationAndSize)
- {
- return;
- }
- if (rectCurrent.HasValue)
- {
- Rectangle rect = rectCurrent.Value;
- rect.Width += 1;
- rect.Height += 1;
- DrawReversibleFrame(rect);
- }
- if (rectOriginal.HasValue && _isMouseMove)
- {
- Rectangle rect = rectOriginal.Value;
- rect.Width += 1;
- rect.Height += 1;
- DrawReversibleFrame(rect);
- }
- }
- /// <summary>
- /// 调整控件的大小
- /// </summary>
- /// <param name="item">Item</param>
- /// <param name="rect">范围</param>
- private void DragHandler(LayoutItem item, Rectangle rect)
- {
- if (item.ItemLockType == ItemLock.LockLocationAndSize)
- {
- return;
- }
- int top = 0;
- int left = 0;
- int width = 0;
- int height = 0;
- if (rect.Width < 0)
- {
- left = rect.Right;
- width -= rect.Width;
- }
- else
- {
- left = rect.Left;
- width = rect.Width;
- }
- if (rect.Height < 0)
- {
- top = rect.Bottom;
- height -= rect.Height;
- }
- else
- {
- top = rect.Top;
- height = rect.Height;
- }
- if (left < 0
- || _maxItemLeft < left
- || top < 0
- || _maxItemTop < top
- || width < _minItemWidth
- || _maxItemWidth < width
- || height < _minItemHeight
- || _maxItemHeight < height
- || paperArea.Width - 1 < left
- || paperArea.Height - 1 < top
- )
- {
- //return;
- }
- item.MoveItem(left, top);
- item.ChangeItemSize(width, height);
- item.ResetItemFrame();
- Dirty = true;
- }
- #endregion Item操作相关
- #region 数据绑定相关
- /// <summary>
- /// 设置数据源
- /// </summary>
- private void SetDataSource()
- {
- if (_dataBinding == null)
- {
- _dataBinding = new LayoutBoxDataBinding(this);
- _dataBinding.SetDataConnection(_dataSource, _dataMember);
- }
- else
- {
- if (_dataBinding.ContainsDataMember(_dataSource))
- {
- _dataMember = string.Empty;
- }
- _dataBinding.SetDataConnection(_dataSource, _dataMember);
- if (_dataSource == null)
- {
- _dataBinding = null;
- }
- }
- RefreshItemsBound();
- }
- /// <summary>
- /// 设置数据成员
- /// </summary>
- private void SetDataMember()
- {
- if (_dataBinding == null)
- {
- _dataBinding = new LayoutBoxDataBinding(this);
- }
- _dataBinding.SetDataConnection(_dataSource, _dataMember);
- RefreshItemsBound();
- }
- /// <summary>
- /// 设置打印数据源
- /// </summary>
- private void SetPrintDataSource()
- {
- if (_printDataBinding == null)
- {
- _printDataBinding = new LayoutBoxPrintDataBinding(this);
- _printDataBinding.SetDataConnection(_printDataSource, _printDataMember);
- }
- else
- {
- if (_printDataBinding.ContainsDataMember(_printDataSource))
- {
- _printDataMember = string.Empty;
- }
- _printDataBinding.SetDataConnection(_printDataSource, _printDataMember);
- if (_printDataSource == null)
- {
- _printDataBinding = null;
- }
- }
- RefreshItemsPrintBound();
- }
- /// <summary>
- /// 设置打印数据成员
- /// </summary>
- private void SetPrintDataMember()
- {
- if (_printDataBinding == null)
- {
- _printDataBinding = new LayoutBoxPrintDataBinding(this);
- }
- _printDataBinding.SetDataConnection(_printDataSource, _printDataMember);
- RefreshItemsPrintBound();
- }
- /// <summary>
- /// 启动数据更新
- /// </summary>
- private void BeginUpdateInternal()
- {
- if (IsHandleCreated)
- {
- if (_updateCount == 0)
- {
- SendMessage(new HandleRef(this, Handle), 11, 0, 0);
- }
- _updateCount = (short)(_updateCount + 1);
- }
- }
- /// <summary>
- /// 结束数据更新
- /// </summary>
- private void EndUpdateInternal()
- {
- if (_updateCount <= 0)
- {
- return;
- }
- _updateCount = (short)(_updateCount - 1);
- if (_updateCount == 0)
- {
- SendMessage(new HandleRef(this, Handle), 11, -1, 0);
- }
- }
- /// <summary>
- /// 刷新该项目的数据
- /// </summary>
- private void RefreshItemsCurrentPositionValue()
- {
- bool visible = base.Visible;
- if (visible)
- {
- BeginUpdateInternal();
- }
- try
- {
- if (_dataBinding == null)
- {
- return;
- }
- foreach (LayoutItem item in _items)
- {
- if ((item.ItemType == ItemType.Text || item.ItemType == ItemType.TotalText) && _dataBinding != null)
- {
- _dataBinding.BoundDataValueToTextItem(item as TextItem);
- }
- else
- {
- continue;
- }
- }
- }
- finally
- {
- if (visible)
- {
- EndUpdateInternal();
- base.Invalidate(true);
- }
- }
- }
- /// <summary>
- /// 刷新该项目的数据绑定
- /// </summary>
- internal void RefreshItemsBound()
- {
- bool visible = base.Visible;
- if (visible)
- {
- BeginUpdateInternal();
- }
- try
- {
- if (_dataBinding == null)
- {
- return;
- }
- foreach (LayoutItem item in _items)
- {
- if ((item.ItemType == ItemType.Text || item.ItemType == ItemType.TotalText) && _dataBinding != null)
- {
- _dataBinding.BoundFieldToTextItem(item as TextItem);
- }
- else
- {
- continue;
- }
- }
- }
- finally
- {
- if (visible)
- {
- EndUpdateInternal();
- base.Invalidate(true);
- }
- }
- }
- /// <summary>
- /// 刷新文本项目的数据绑定
- /// </summary>
- /// <param name="textItem">文本Item</param>
- internal void RefreshTextItemBound(TextItem textItem)
- {
- bool visible = base.Visible;
- if (visible)
- {
- BeginUpdateInternal();
- }
- try
- {
- if (_dataBinding != null)
- {
- _dataBinding.BoundFieldToTextItem(textItem);
- }
- }
- finally
- {
- if (visible)
- {
- EndUpdateInternal();
- base.Invalidate(true);
- }
- }
- }
- /// <summary>
- /// 刷新该项目的打印数据
- /// </summary>
- internal void RefreshItemsCurrentPositionPrintValue()
- {
- bool visible = base.Visible;
- if (visible)
- {
- BeginUpdateInternal();
- }
- try
- {
- if (_printDataBinding == null)
- {
- return;
- }
- foreach (LayoutItem item in _items)
- {
- if ((item.ItemType == ItemType.Text || item.ItemType == ItemType.TotalText) && _printDataBinding != null)
- {
- _printDataBinding.BoundPrintDataValueToTextItem(item as TextItem);
- }
- else
- {
- continue;
- }
- }
- }
- finally
- {
- if (visible)
- {
- EndUpdateInternal();
- base.Invalidate(true);
- }
- }
- }
- /// <summary>
- /// 刷新该项目的打印数据绑定
- /// </summary>
- internal void RefreshItemsPrintBound()
- {
- bool visible = base.Visible;
- if (visible)
- {
- BeginUpdateInternal();
- }
- try
- {
- if (_printDataBinding == null)
- {
- return;
- }
- foreach (LayoutItem item in _items)
- {
- if ((item.ItemType == ItemType.Text || item.ItemType == ItemType.TotalText) && _printDataBinding != null)
- {
- _printDataBinding.BoundPrintFieldToTextItem(item as TextItem);
- }
- else
- {
- continue;
- }
- }
- }
- finally
- {
- if (visible)
- {
- EndUpdateInternal();
- base.Invalidate(true);
- }
- }
- }
- /// <summary>
- /// 刷新文本项目的打印数据绑定
- /// </summary>
- /// <param name="textItem">文本Item</param>
- internal void RefreshTextItemPrintBound(TextItem textItem)
- {
- bool visible = base.Visible;
- if (visible)
- {
- BeginUpdateInternal();
- }
- try
- {
- if (_printDataBinding != null)
- {
- _printDataBinding.BoundPrintFieldToTextItem(textItem);
- }
- }
- finally
- {
- if (visible)
- {
- EndUpdateInternal();
- base.Invalidate(true);
- }
- }
- }
- #endregion 数据绑定相关
- #region 打印相关
- /// <summary>
- /// 检查打印设置
- /// </summary>
- /// <returns>可以打印true;其他为false</returns>
- public bool CheckPrintSetting()
- {
- // 获取打印机设置非常慢,暂不校验
- return true;
- //string message = this.CheckPrintSettingIn(_printType);
- //if (!string.IsNullOrWhiteSpace(message))
- //{
- // MessageBox.Show(message,
- // "打印失败",
- // MessageBoxButtons.OK,
- // MessageBoxIcon.Error,
- // MessageBoxDefaultButton.Button1);
- // return false;
- //}
- //return true;
- }
- public string CheckPrintSettingIn(PrintType printType)
- {
- // 获取打印机设置非常慢,暂不校验
- return null;
- /*
- if (System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count < 1)
- {
- return "请安装打印机驱动程序。";
- }
- if (PrinterSettings == null || PrinterSettings.DefaultPageSettings == null)
- {
- return "请设置打印机。";
- }
- if (PaperSize == null)
- {
- return "请设置打印纸张大小。";
- }
- if (!printDocument.PrinterSettings.IsValid)
- {
- return string.Format("打印机名称({0}/{1})不正确。"
- , printDocument.PrinterSettings.PrinterName
- , printDialog.PrinterSettings.PrinterName);
- }
- if (short.MaxValue < _numberOfCopies)
- {
- return string.Format("此打印机一次可以打印的最大份数是{0},不能打印{1}份。"
- , short.MaxValue
- , _numberOfCopies);
- }
- if (printType == PrintType.Print || printType == PrintType.PrintPreview)
- {
- if (PrinterSettings.MaximumCopies < _numberOfCopies)
- {
- return string.Format("此打印机一次可以打印的最大份数是{0},不能打印{1}份。"
- , PrinterSettings.MaximumCopies
- , _numberOfCopies);
- }
- }
- else if (printType == PrintType.AllocationPrint || printType == PrintType.AllocationPrintPreview)
- {
- float paperWidth = 0;
- float paperHeight = 0;
- float horizontal = 0;
- float vertical = 0;
- if (_landscape)
- {
- paperWidth = (float)Math.Round(LayoutCommon.Inch100ToMillimeter(PaperSize.Height), 0);
- paperHeight = (float)Math.Round(LayoutCommon.Inch100ToMillimeter(PaperSize.Width), 0);
- horizontal = paperWidth + _allocationHorizontalSpace
- - _marginLeft
- - _marginRight
- - (_allocationHorizontalSpace + _paperWidth)
- * _allocationNumX;
- vertical = paperHeight + _allocationVerticalSpace
- - _marginTop
- - _marginBottom
- - (_allocationVerticalSpace + _paperHeight)
- * _allocationNumY;
- }
- else
- {
- paperWidth = (float)Math.Round(LayoutCommon.Inch100ToMillimeter(PaperSize.Width), 0);
- paperHeight = (float)Math.Round(LayoutCommon.Inch100ToMillimeter(PaperSize.Height), 0);
- horizontal = paperWidth + _allocationHorizontalSpace
- - _marginLeft
- - _marginRight
- - (_allocationHorizontalSpace + _paperWidth)
- * _allocationNumX;
- vertical = paperHeight + _allocationVerticalSpace
- - _marginTop
- - _marginBottom
- - (_allocationVerticalSpace + _paperHeight)
- * _allocationNumY;
- }
- if (horizontal < 0 && vertical < 0)
- {
- return "设置值大于纸张尺寸。请检查纸张横纵个数,间距,和余白。";
- }
- if (horizontal < 0)
- {
- return string.Format("分配的纸张宽度不能打印{0}列,请确认。"
- , _allocationNumX);
- }
- if (vertical < 0)
- {
- return string.Format("分配的纸张高度不能打印{0}行,请确认。"
- , _allocationNumY);
- }
- }
- return null;
- */
- }
- /// <summary>
- /// 打印背景图片
- /// </summary>
- /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
- private void PrintBackgroundImage(Graphics graphics)
- {
- if (BackgroundImage != null)
- {
- // 背景图片的高宽比例
- float imgWHRate = (float)BackgroundImage.Width / (float)BackgroundImage.Height;
- // 打印纸张的高宽比例
- float paperWHRate = PaperWidth / PaperHeight;
- // 背景图片打印范围
- RectangleF newImgRectangleF = RectangleF.Empty;
- // 缩放背景图片,但保持图片高宽比例
- if (imgWHRate > paperWHRate)
- {
- newImgRectangleF.Width = PaperWidth;
- newImgRectangleF.Height = PaperWidth / imgWHRate;
- newImgRectangleF.X = _allOffsetX;
- newImgRectangleF.Y = (PaperHeight - newImgRectangleF.Height) / 2 + _allOffsetY;
- }
- else
- {
- newImgRectangleF.Width = PaperHeight * imgWHRate;
- newImgRectangleF.Height = PaperHeight;
- newImgRectangleF.X = (PaperWidth - newImgRectangleF.Width) / 2 + _allOffsetX;
- newImgRectangleF.Y = _allOffsetY;
- }
- // 在指定的范围绘制背景图片
- graphics.DrawImage(BackgroundImage, newImgRectangleF);
- }
- }
- /// <summary>
- /// 打印文本Item
- /// </summary>
- /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
- /// <param name="textItem">文本Item</param>
- private void DrawTextItem(Graphics graphics, TextItem textItem)
- {
- // 文本Item打印范围
- RectangleF rectangleFM = new RectangleF(
- _allOffsetX + textItem.Left,
- _allOffsetY + textItem.Top,
- textItem.Width,
- textItem.Height);
- RectangleF rectangleF = rectangleFM;
- rectangleF.Inflate(0 - LayoutConsts.TEXT_MARGIN, 0 - LayoutConsts.TEXT_MARGIN);
- rectangleFM.Height -= LayoutConsts.TEXT_MARGIN;
- /* Edit For Draw Pic
- // 在指定的范围绘制文本Item
- LayoutUtility.DrawText(graphics,
- rectangleFM,
- rectangleF,
- textItem.PrintText,
- textItem.Font,
- textItem.TextColor,
- textItem.LineSpace,
- textItem.CharacterSpace,
- textItem.CharacterCount,
- textItem.TextAlign,
- textItem.Wrap,
- textItem.Clip,
- false);
- */
- textItem.DrawTextItem(textItem.PrintDataValue, textItem.PrintBoundField, graphics, rectangleFM, rectangleF, true);
- }
- /// <summary>
- /// 打印图片Item
- /// </summary>
- /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
- /// <param name="imageItem">图片Item</param>
- private void DrawImageItem(Graphics graphics, ImageItem imageItem)
- {
- // 图片Item打印范围
- RectangleF rectangleF = new RectangleF(
- _allOffsetX + imageItem.Left,
- _allOffsetY + imageItem.Top,
- imageItem.Width,
- imageItem.Height);
- // 在指定的范围绘制图片Item
- LayoutUtility.DrawImage(graphics, rectangleF, imageItem.Image);
- }
- /// <summary>
- /// 打印矩形Item
- /// </summary>
- /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
- /// <param name="rectangleItem">矩形Item</param>
- private void DrawRectangleItem(Graphics graphics, RectangleItem rectangleItem)
- {
- // 矩形Item打印范围
- RectangleF rectangleF = new RectangleF(
- _allOffsetX + rectangleItem.Left,
- _allOffsetY + rectangleItem.Top,
- rectangleItem.Width,
- rectangleItem.Height);
- float lineWidthDraw = rectangleItem.LineWidth;
- // 在指定的范围绘制矩形Item
- LayoutUtility.DrawRectangle(graphics, rectangleF, rectangleItem.LineColor,
- rectangleItem.FillColor, lineWidthDraw, rectangleItem.Transparent);
- }
- /// <summary>
- /// 打印椭圆Item
- /// </summary>
- /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
- /// <param name="ellipseItem">椭圆Item</param>
- private void DrawEllipseItem(Graphics graphics, EllipseItem ellipseItem)
- {
- // 椭圆Item打印范围
- RectangleF rectangleF = new RectangleF(
- _allOffsetX + ellipseItem.Left,
- _allOffsetY + ellipseItem.Top,
- ellipseItem.Width,
- ellipseItem.Height);
- float lineWidthDraw = ellipseItem.LineWidth;
- // 在指定的范围绘制椭圆Item
- LayoutUtility.DrawEllipse(graphics, rectangleF, ellipseItem.LineColor,
- ellipseItem.FillColor, lineWidthDraw, ellipseItem.Transparent);
- }
- /// <summary>
- /// 打印表格Item
- /// </summary>
- /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
- /// <param name="gridItem">表格Item</param>
- private void DrawGridItem(Graphics graphics, GridItem gridItem)
- {
- // 表格Item打印范围
- RectangleF rectangleF = new RectangleF(
- _allOffsetX + gridItem.Left,
- _allOffsetY + gridItem.Top,
- gridItem.Width,
- gridItem.Height);
- //// 在指定的范围绘制表格Item
- gridItem.DrawGridItem(graphics, rectangleF, 1, _printPageNum);
- }
- /// <summary>
- /// 打印页码Item
- /// </summary>
- /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
- /// <param name="pageNumItem">页码Item</param>
- private void DrawPageNumItem(Graphics graphics, PageNumItem pageNumItem)
- {
- // 文本Item打印范围
- RectangleF rectangleFM = new RectangleF(
- _allOffsetX + pageNumItem.Left,
- _allOffsetY + pageNumItem.Top,
- pageNumItem.Width,
- pageNumItem.Height);
- RectangleF rectangleF = rectangleFM;
- rectangleF.Inflate(0 - LayoutConsts.TEXT_MARGIN, 0 - LayoutConsts.TEXT_MARGIN);
- rectangleFM.Height -= LayoutConsts.TEXT_MARGIN;
- // 在指定的范围绘制文本Item
- LayoutUtility.DrawText(graphics,
- rectangleFM,
- rectangleF,
- pageNumItem.PrintText,
- pageNumItem.Font,
- pageNumItem.TextColor,
- 0,
- 0,
- 0,
- pageNumItem.TextAlign,
- pageNumItem.TextAlignVertical,
- false,
- false,
- false);
- }
- /// <summary>
- /// 打印合计Item
- /// </summary>
- /// <param name="graphics">用于绘制的System.Drawing.Graphics</param>
- /// <param name="totalTextItem">合计Item</param>
- private void DrawTotalTextItem(Graphics graphics, TotalTextItem totalTextItem)
- {
- // 文本Item打印范围
- RectangleF rectangleFM = new RectangleF(
- _allOffsetX + totalTextItem.Left,
- _allOffsetY + totalTextItem.Top,
- totalTextItem.Width,
- totalTextItem.Height);
- RectangleF rectangleF = rectangleFM;
- rectangleF.Inflate(0 - LayoutConsts.TEXT_MARGIN, 0 - LayoutConsts.TEXT_MARGIN);
- rectangleFM.Height -= LayoutConsts.TEXT_MARGIN;
- if (PrintPageNum != TotalPageNum)
- {
- LayoutUtility.DrawText(graphics,
- rectangleFM,
- rectangleF,
- totalTextItem.NotTotalValue,
- totalTextItem.NotTotalFont,
- totalTextItem.NotTotalColor,
- totalTextItem.LineSpace,
- totalTextItem.CharacterSpace,
- totalTextItem.CharacterCount,
- totalTextItem.NotTotalAlign,
- totalTextItem.NotTotalAlignVertical,
- totalTextItem.Wrap,
- totalTextItem.Clip,
- false);
- return;
- }
- // 在指定的范围绘制文本Item
- string text = totalTextItem.ResetTextValue(totalTextItem.PrintDataValue, totalTextItem.BoundField.IsDataBound) as string;
- LayoutUtility.DrawText(graphics,
- rectangleFM,
- rectangleF,
- text,
- totalTextItem.Font,
- totalTextItem.TextColor,
- totalTextItem.LineSpace,
- totalTextItem.CharacterSpace,
- totalTextItem.CharacterCount,
- totalTextItem.TextAlign,
- totalTextItem.NotTotalAlignVertical,
- totalTextItem.Wrap,
- totalTextItem.Clip,
- false);
- }
- #endregion 打印相关
- #region 事件
- /// <summary>
- /// 当一个项目被改变,移动,添加或删除引发此事件。
- /// </summary>
- /// <param name="layoutItem">Item</param>
- /// <param name="itemChangeType">变更类型</param>
- internal void SetItemChangedArgs(LayoutItem layoutItem, ItemChangeType itemChangeType)
- {
- if (ItemChanged != null && !layoutItem.IsNew)
- {
- ItemChangedEventArgs e = new ItemChangedEventArgs();
- e.Item = layoutItem;
- e.ChangedType = itemChangeType;
- e.ItemID = layoutItem.ID;
- _isDirty = true;
- ItemChanged(this, e);
- }
- if (itemChangeType != ItemChangeType.Deleted)
- {
- layoutItem.Shape.Refresh();
- }
- else
- {
- if (layoutItem.ItemType == ItemType.Grid)
- {
- this.OneGridItem = null;
- }
- }
- }
- /// <summary>
- /// 当一个项目被改变,移动,添加或删除引发此事件。
- /// </summary>
- /// <param name="layoutItem">Item</param>
- /// <param name="itemChangeType">变更类型</param>
- /// <param name="property">属性</param>
- internal void SetItemChangedArgs(LayoutItem layoutItem, ItemChangeType itemChangeType, string property)
- {
- if (ItemChanged != null && !layoutItem.IsNew)
- {
- ItemChangedEventArgs e = new ItemChangedEventArgs();
- e.Item = layoutItem;
- e.ChangedType = itemChangeType;
- e.ItemID = layoutItem.ID;
- e.Property = property;
- _isDirty = true;
- ItemChanged(this, e);
- }
- if (itemChangeType != ItemChangeType.Deleted)
- {
- layoutItem.Shape.Refresh();
- }
- else
- {
- if (layoutItem.ItemType == ItemType.Grid)
- {
- this.OneGridItem = null;
- }
- }
- }
- /// <summary>
- /// 项目的选择状态改变时引发此事件。
- /// </summary>
- /// <param name="layoutItem">Item</param>
- /// <param name="isSelect">选择状态</param>
- internal void SetItemSelectionChangedArgs(LayoutItem layoutItem, bool isSelect)
- {
- SetTSMIOnSelectionChanged();
- if (ItemSelectionChanged != null && !layoutItem.IsNew)
- {
- ItemSelectionChangedEventArgs e = new ItemSelectionChangedEventArgs();
- e.Item = layoutItem;
- e.IsSelected = isSelect;
- e.ItemID = layoutItem.ID;
- ItemSelectionChanged(this, e);
- }
- }
- /// <summary>
- /// 打印或显示数据之前引发此事件。
- /// </summary>
- /// <param name="layoutItem">Item</param>
- /// <param name="value">数据</param>
- /// <param name="format">格式</param>
- /// <returns>e.Result格式化后结果</returns>
- internal object SetItemValueFormattingArgs(LayoutItem layoutItem, object value, string format)
- {
- if (ItemValueFormatting != null && !layoutItem.IsNew)
- {
- ItemValueFormattingEventArgs e = new ItemValueFormattingEventArgs();
- e.Item = layoutItem;
- e.ItemID = layoutItem.ID;
- e.Value = value;
- e.Format = format;
- ItemValueFormatting(this, e);
- return e.Result;
- }
- return null;
- }
- internal void SetUserChangedGridItemArgs(GridItem gItem, CollectionChangeEventArgs e)
- {
- if (this.UserChangedGridItem != null)
- {
- this.UserChangedGridItem(gItem, e);
- }
- }
- #endregion 事件
- #endregion private函数
- #region 事件处理
- #region 缩放比例和位置
- /// <summary>
- /// 调整纸张大小
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void PaperBox_SizeChanged(object sender, EventArgs e)
- {
- if (_zoomType == ZoomType.Whole)
- {
- InitializeZoom();
- }
- else
- {
- AdjustPaperArea();
- }
- }
- #endregion 缩放比例和位置
- #region 右键菜单
- /// <summary>
- /// 打开右键菜单
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void contextMenu_Opening(object sender, CancelEventArgs e)
- {
- //SetTSMIOnSelectionChanged();
- tsmiPaste.Enabled = (_clipboardFormat != null && Clipboard.ContainsData(_clipboardFormat.Name));
- }
- /// <summary>
- /// 打开对齐右键菜单
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void contextMenuArrange_Opening(object sender, CancelEventArgs e)
- {
- e.Cancel = !tsmiArrange.Enabled;
- }
- /// <summary>
- /// 打开排序右键菜单
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void contextMenuOrder_Opening(object sender, CancelEventArgs e)
- {
- e.Cancel = !tsmiOrder.Enabled;
- }
- /// <summary>
- /// 关闭右键菜单
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void contextMenu_Closing(object sender, ToolStripDropDownClosingEventArgs e)
- {
- tsmiPaste.Enabled = true;
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiDelete_Click(object sender, EventArgs e)
- {
- DeleteItem();
- }
- /// <summary>
- /// 属性
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiProperties_Click(object sender, EventArgs e)
- {
- ShowItemPropertyDialog();
- }
- /// <summary>
- /// 选择全部Item
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiAllSelect_Click(object sender, EventArgs e)
- {
- SelectItemsMultiple(_items.ToArray(), true);
- }
- /// <summary>
- /// 纸张大小
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiPaperSize_Click(object sender, EventArgs e)
- {
- ShowPaperPropertyDialog();
- }
- /// <summary>
- /// 背景图片
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiBackgroundImage_Click(object sender, EventArgs e)
- {
- ShowBackgroundSettingDialog();
- }
- /// <summary>
- /// 打开缩放比例右键菜单
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void contextMenuZoom_Opening(object sender, CancelEventArgs e)
- {
- tsmiZoom500.Checked = false;
- tsmiZoom200.Checked = false;
- tsmiZoom100.Checked = false;
- tsmiZoom75.Checked = false;
- tsmiZoom50.Checked = false;
- tsmiZoom33.Checked = false;
- tsmiZoom25.Checked = false;
- tsmiZoom10.Checked = false;
- tsmiZoomWhole.Checked = false;
- switch (_zoom)
- {
- case 500:
- tsmiZoom500.Checked = true;
- break;
- case 200:
- tsmiZoom200.Checked = true;
- break;
- case 100:
- tsmiZoom100.Checked = true;
- break;
- case 75:
- tsmiZoom75.Checked = true;
- break;
- case 50:
- tsmiZoom50.Checked = true;
- break;
- case 33:
- tsmiZoom33.Checked = true;
- break;
- case 25:
- tsmiZoom25.Checked = true;
- break;
- case 10:
- tsmiZoom10.Checked = true;
- break;
- default:
- tsmiZoomWhole.Checked = true;
- break;
- }
- }
- /// <summary>
- /// 缩放比例
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiZoom_Click(object sender, EventArgs e)
- {
- ToolStripMenuItem itemZoom = sender as ToolStripMenuItem;
- if (itemZoom == null)
- {
- return;
- }
- int zoom = System.Convert.ToInt32(itemZoom.Tag);
- if (zoom > 0)
- {
- Zoom = zoom;
- }
- else
- {
- ZoomType = ZoomType.Whole;
- }
- }
- /// <summary>
- /// 置于顶层
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiForemost_Click(object sender, EventArgs e)
- {
- ChangeItemsOrderForemost();
- }
- /// <summary>
- /// 上移一层
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiFront_Click(object sender, EventArgs e)
- {
- ChangeItemsOrderFront();
- }
- /// <summary>
- /// 下移一层
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiBack_Click(object sender, EventArgs e)
- {
- ChangeItemsOrderBack();
- }
- /// <summary>
- /// 置于底层
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiAftermost_Click(object sender, EventArgs e)
- {
- ChangeItemsOrderAftermost();
- }
- /// <summary>
- /// 顶端对齐
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiTop_Click(object sender, EventArgs e)
- {
- ArrangeItemsTop();
- }
- /// <summary>
- /// 底端对齐
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiBottom_Click(object sender, EventArgs e)
- {
- ArrangeItemsBottom();
- }
- /// <summary>
- /// 垂直居中对齐
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiVerticalCenter_Click(object sender, EventArgs e)
- {
- ArrangeItemsVerticalCenter();
- }
- /// <summary>
- /// 右侧对齐
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiRight_Click(object sender, EventArgs e)
- {
- ArrangeItemsRight();
- }
- /// <summary>
- /// 左侧对齐
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiLeft_Click(object sender, EventArgs e)
- {
- ArrangeItemsLeft();
- }
- /// <summary>
- /// 水平居中对齐
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiHorizontalCenter_Click(object sender, EventArgs e)
- {
- ArrangeItemsHorizontalCenter();
- }
- /// <summary>
- /// 剪切
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiCut_Click(object sender, EventArgs e)
- {
- CutItem();
- }
- /// <summary>
- /// 复制
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiCopy_Click(object sender, EventArgs e)
- {
- CopyItem();
- }
- /// <summary>
- /// 粘贴
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void tsmiPaste_Click(object sender, EventArgs e)
- {
- PasteItem();
- }
- #endregion 右键菜单
- #region Item操作相关
- /// <summary>
- /// Item选择边框失去焦点时发生。
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void RectTracker_LostFocus(object sender, EventArgs e)
- {
- if (_moveRectangles != null && _isMouseMove)
- {
- //if (!_isRealFollow)
- {
- int count = _moveRectangles.Length;
- for (int i = 0; i < count; i++)
- {
- DrawReversibleFrame(_selectedItems[i].ItemLockType, _moveRectangles[i], null);
- }
- }
- _isMouseDown = false;
- _isMouseMove = false;
- _moveRectangles = null;
- }
- }
- /// <summary>
- /// Item选择边框 KeyDown事件
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void RectTracker_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.ShiftKey && !_isShiftDown)
- {
- _isShiftDown = true;
- }
- else if (e.KeyCode == Keys.ControlKey && !_isCtrlDown)
- {
- _isCtrlDown = true;
- }
- else
- {
- return;
- }
- if (_isMouseMove)
- {
- int count = _moveRectangles.Length;
- for (int i = 0; i < count; i++)
- {
- ResizeToRectangle(new Point(_mouseMoveX, _mouseMoveY), i);
- }
- }
- }
- /// <summary>
- /// Item选择边框 KeyUp事件
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void RectTracker_KeyUp(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.ShiftKey)
- {
- _isShiftDown = false;
- }
- else if (e.KeyCode == Keys.ControlKey)
- {
- _isCtrlDown = false;
- }
- else
- {
- return;
- }
- if (_isMouseMove)
- {
- int count = _moveRectangles.Length;
- for (int i = 0; i < count; i++)
- {
- ResizeToRectangle(new Point(_mouseMoveX, _mouseMoveY), i);
- }
- }
- }
- /// <summary>
- /// Item选择边框 MouseDown事件
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void RectTracker_MouseDown(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- if (ItemShapeContainer.ClientRectangle.Contains(e.Location))
- {
- _startX = e.X;
- _startY = e.Y;
- _mouseMoveX = e.X;
- _mouseMoveY = e.Y;
- int count = _selectedItems.Count;
- _downRectangles = new Rectangle[count];
- _moveRectangles = new Rectangle[count];
- for (int i = 0; i < count; i++)
- {
- _downRectangles[i] = _selectedItems[i].ShapeBounds;
- _moveRectangles[i] = _downRectangles[i];
- }
- _isMouseDown = true;
- Cursor.Current = Cursors.SizeAll;
- }
- }
- }
- /// <summary>
- /// Item选择边框 MouseMove事件
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void RectTracker_MouseMove(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left && _isMouseDown)
- {
- Cursor.Current = Cursors.SizeAll;
- int mx = e.X;
- int my = e.Y;
- if (_mouseMoveX != mx || _mouseMoveY != my)
- {
- int count = _moveRectangles.Length;
- for (int i = 0; i < count; i++)
- {
- ResizeToRectangle(new Point(mx, my), i);
- }
- _isMouseMove = true;
- _mouseMoveX = mx;
- _mouseMoveY = my;
- }
- }
- }
- /// <summary>
- /// Item选择边框 MouseUp事件
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void RectTracker_MouseUp(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left && _isMouseDown)
- {
- if (_isMouseMove)
- {
- Capture = false;
- int count = _moveRectangles.Length;
- for (int i = 0; i < count; i++)
- {
- DrawReversibleFrame(_selectedItems[i].ItemLockType, _moveRectangles[i], null);
- DragHandler(_selectedItems[i], _moveRectangles[i]);
- }
- }
- _isMouseDown = false;
- _isMouseMove = false;
- _moveRectangles = null;
- }
- }
- /// <summary>
- /// 鼠标在纸张范围内移动
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void paperArea_MouseMove(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left && _layoutMode == LayoutMode.Edit)
- {
- if (_isMouseDownInPaperArea)
- {
- _mouseRect.X = e.X;
- _mouseRect.Y = e.Y;
- _isMouseDownInPaperArea = false;
- _isMouseMoveInPaperArea = true;
- }
- else if (_isMouseMoveInPaperArea)
- {
- ResizeToRectangle(e.Location);
- }
- }
- }
- /// <summary>
- /// 鼠标在纸张范围内抬起
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void paperArea_MouseUp(object sender, MouseEventArgs e)
- {
- if (_layoutMode != LayoutMode.Edit || e.Button != MouseButtons.Left)
- {
- return;
- }
- if (_isMouseMoveInPaperArea)
- {
- Capture = false;
- DrawReversibleFrame(_mouseRect);
- bool isChanging = false;
- if (Items.Count > 0)
- {
- if (_mouseRect.Height > 0 && _mouseRect.Width > 0)
- {
- //_mouseRect.X = _mouseRect.X - paperArea.Left;
- //_mouseRect.Y = _mouseRect.Y - paperArea.Top;
- }
- else if (_mouseRect.Height <= 0 && _mouseRect.Width > 0)
- {
- //_mouseRect.X = _mouseRect.X - paperArea.Left;
- _mouseRect.Y = e.Y;
- _mouseRect.Height = Math.Abs(_mouseRect.Height);
- //_mouseRect.Width = Math.Abs(_mouseRect.Width);
- }
- else if (_mouseRect.Height > 0 && _mouseRect.Width <= 0)
- {
- _mouseRect.X = e.X;
- //_mouseRect.Y = _mouseRect.Y - paperArea.Top;
- _mouseRect.Width = Math.Abs(_mouseRect.Width);
- }
- else if (_mouseRect.Height <= 0 && _mouseRect.Width <= 0)
- {
- _mouseRect.X = e.X;
- _mouseRect.Y = e.Y;
- _mouseRect.Height = Math.Abs(_mouseRect.Height);
- _mouseRect.Width = Math.Abs(_mouseRect.Width);
- }
- ItemSelectionChanging = true;
- if (Control.ModifierKeys == Keys.Control)
- {
- foreach (LayoutItem item in _items)
- {
- if (item.BoundsContainsBy(_mouseRect))
- {
- SelectItemMultiple(item, !item.Selected, ref isChanging);
- }
- }
- }
- else if (Control.ModifierKeys == Keys.Shift)
- {
- foreach (LayoutItem item in _items)
- {
- if (item.BoundsContainsBy(_mouseRect))
- {
- SelectItemMultiple(item, true, ref isChanging);
- }
- }
- }
- else
- {
- foreach (LayoutItem item in _items)
- {
- //if (item.BoundsContainsBy(_mouseRect))
- //{
- // SelectItemMultiple(item, true, ref isChanging);
- //}
- //else
- //{
- // SelectItemMultiple(item, false, ref isChanging);
- //}
- bool isSelet = false;
- if (item.ShapeBounds.Height <= 1 ||
- item.ShapeBounds.Width <= 1)
- {
- isSelet = item.IntersectsWith(_mouseRect);
- }
- else
- {
- isSelet = item.BoundsContainsBy(_mouseRect);
- }
- SelectItemMultiple(item, isSelet, ref isChanging);
- }
- }
- }
- ItemSelectionChanging = false;
- if (isChanging)
- {
- if (UserSelectionChanged != null)
- {
- UserSelectionChanged(this, EventArgs.Empty);
- }
- }
- _isMouseMoveInPaperArea = false;
- _mouseRect = Rectangle.Empty;
- }
- }
- /// <summary>
- /// 鼠标在纸张范围内按下
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void paperArea_MouseDown(object sender, MouseEventArgs e)
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- if (e.Button == MouseButtons.Left)
- {
- _isMouseDownInPaperArea = true;
- _isMouseMoveInPaperArea = false;
- }
- }
- /// <summary>
- /// 鼠标在纸张范围内点击
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void paperArea_Click(object sender, EventArgs e)
- {
- if (_layoutMode != LayoutMode.Edit)
- {
- return;
- }
- // 选择状态全部解除
- if (Control.ModifierKeys == Keys.Control || Control.ModifierKeys == Keys.Shift)
- {
- return;
- }
- ClearSelection();
- paperArea.Focus();
- }
- #endregion Item操作相关
- #region 打印相关
- //private PageSettings _defaultPageSettings = null;
- //private PaperSize _defaultPaperSize = null;
- //private RectangleF _defaultPrintableArea = RectangleF.Empty;
- /// <summary>
- /// 打印开始。
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void printDocument_BeginPrint(object sender, PrintEventArgs e)
- {
- //#if DEBUG
- // Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
- // "printDocument_BeginPrint",
- // "begin",
- // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
- // Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
- //#endif
- // 选定打印页码 modify by chenxy 2014-03-27 begin
- //if (PrinterSettings.PrintRange == PrintRange.SomePages)
- //{
- // if (PrinterSettings.ToPage == 0 || TotalPageNum < PrinterSettings.FromPage)
- // {
- // e.Cancel = true;
- // return;
- // }
- //}
- // 选定打印页码 modify by chenxy 2014-03-27 end
- //if (_numberOfCopies < 1 || !CheckPrintSetting())
- //{
- // e.Cancel = true;
- // return;
- //}
- // 获取打印机配置非常慢,
- //_defaultPageSettings = PrinterSettings.DefaultPageSettings;
- //_defaultPaperSize = _defaultPageSettings.PaperSize;
- //_defaultPrintableArea = _defaultPageSettings.PrintableArea;
- // 页面左上点的坐标(1/100英寸)
- if (_printAction == PrintAction.PrintToPreview)
- {
- _printerOffsetX = 0;
- _printerOffsetY = 0;
- _allOffsetX = _printOffsetX;
- _allOffsetY = _printOffsetY;
- }
- else
- {
- //if (_landscape)
- //{
- // _printerOffsetX = LayoutCommon.Inch100ToMillimeter(_defaultPaperSize.Height - _defaultPrintableArea.Bottom);
- // _printerOffsetY = LayoutCommon.Inch100ToMillimeter(_defaultPrintableArea.Left);
- //}
- //else
- //{
- // _printerOffsetX = LayoutCommon.Inch100ToMillimeter(_defaultPrintableArea.Left);
- // _printerOffsetY = LayoutCommon.Inch100ToMillimeter(_defaultPrintableArea.Top);
- //}
- _allOffsetX = _printOffsetX - _printerOffsetX;
- _allOffsetY = _printOffsetY - _printerOffsetY;
- }
- _rectangleFGreed = RectangleF.Empty;
- _printPageNum = 1;
- if (_printType == PrintType.PrintTest)
- {
- //printDocument.PrinterSettings.Copies = 1;
- }
- else if (_printType == PrintType.Print || _printType == PrintType.PrintPreview)
- {
- if (_printDataBinding != null && _printDataBinding.BindingSource != null && 0 < _printDataBinding.BindingSource.Count)
- {
- _printDataBinding.BindingSource.MoveFirst();
- _printDataTotal = _printDataBinding.BindingSource.Count * _numberOfCopies;
- }
- else
- {
- _printDataTotal = _numberOfCopies;
- }
- _printDataPosition = 0;
- _printDataCopies = 0;
- //if (_printType == PrintType.Print)
- if (_printAction == PrintAction.PrintToPrinter)
- {
- printDocument.PrinterSettings.Copies = (short)_numberOfCopies;
- }
- else
- {
- printDocument.PrinterSettings.Copies = 1;
- }
- }
- else if (_printType == PrintType.AllocationPrint || _printType == PrintType.AllocationPrintPreview)
- {
- if (_printDataBinding != null && _printDataBinding.BindingSource != null && 0 < _printDataBinding.BindingSource.Count)
- {
- _printDataBinding.BindingSource.MoveFirst();
- _printDataTotal = _printDataBinding.BindingSource.Count * _numberOfCopies;
- }
- else
- {
- _printDataTotal = _numberOfCopies;
- }
- _printDataPosition = 0;
- _printDataCopies = 0;
- _allocationNum = _allocationNumY * _allocationNumX;
- _printDataTotal = _printDataTotal + _allocationSkipCount;
- printDocument.PrinterSettings.Copies = 1;
- }
- else
- {
- printDocument.PrinterSettings.Copies = 1;
- }
- //#if DEBUG
- // Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
- // "printDocument_BeginPrint",
- // "end",
- // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
- // Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
- //#endif
- }
- // 选定打印页码 modify by chenxy 2014-03-27 begin
- /// <summary>
- /// 打印每页
- /// </summary>
- /// <param name="sender">指定的对象</param>
- /// <param name="e">提供的事件数据</param>
- private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
- {
- //#if DEBUG
- // Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
- // "PrintDocument_PrintPage",
- // "begin",
- // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
- // Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
- //#endif
- /*
- if (PrinterSettings.PrintRange == PrintRange.SomePages)
- {
- bool isPrintCurrentPage = false;
- do
- {
- if (_printPageNum < PrinterSettings.FromPage)
- {
- isPrintCurrentPage = false;
- }
- else if (PrinterSettings.ToPage < _printPageNum)
- {
- isPrintCurrentPage = false;
- e.HasMorePages = false;
- e.Cancel = true;
- return;
- }
- else
- {
- isPrintCurrentPage = true;
- }
- if (isPrintCurrentPage)
- {
- e.Cancel = false;
- this.PrintPage(e);
- if (_printPageNum - 1 == PrinterSettings.ToPage)
- {
- e.HasMorePages = false;
- }
- }
- else
- {
- e.Cancel = true;
- if (_printType == PrintType.PrintTest)
- {
- e.HasMorePages = false;
- return;
- }
- else if (_printType == PrintType.Print || _printType == PrintType.PrintPreview)
- {
- // 表格Item是否有下一页
- if (_printPageNum < TotalPageNum)
- {
- _printPageNum++;
- e.HasMorePages = true;
- //return;
- }
- else
- {
- if (_printAction == PrintAction.PrintToPrinter)
- {
- // 纸张数是否增加判断
- if (_printDataBinding != null && _printDataBinding.BindingSource != null && 0 < _printDataBinding.BindingSource.Count)
- {
- if (_printDataBinding.BindingSource.Position + 1 < _printDataBinding.BindingSource.Count)
- {
- // 打印数据源下移一条记录
- _printDataBinding.BindingSource.MoveNext();
- _printPageNum = 1;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- return;
- }
- }
- else
- {
- e.HasMorePages = false;
- return;
- }
- }
- else
- {
- // 纸张数是否增加判断
- if (++_printDataPosition < _printDataTotal)
- {
- _printPageNum = 1;
- e.HasMorePages = true;
- // 同一数据一次印刷/分批印刷
- if (true)
- {
- // 同一数据一次印刷
- if (++_printDataCopies < _numberOfCopies)
- {
- return;
- }
- else
- {
- _printDataCopies = 0;
- }
- }
- }
- else
- {
- e.HasMorePages = false;
- return;
- }
- if (_printDataBinding != null && _printDataBinding.BindingSource != null && 0 < _printDataBinding.BindingSource.Count)
- {
- // 打印数据源下移一条记录
- if (_printDataBinding.BindingSource.Position + 1 < _printDataBinding.BindingSource.Count)
- {
- _printDataBinding.BindingSource.MoveNext();
- }
- else
- {
- _printDataBinding.BindingSource.MoveFirst();
- }
- }
- }
- }
- }
- else if (_printType == PrintType.AllocationPrint || _printType == PrintType.AllocationPrintPreview)
- {
- int allocationIndex = -1;
- // 一张纸打印
- for (int i = 0; i < _allocationNum; i++)
- {
- allocationIndex++;
- if (_printDataPosition < _allocationSkipCount)
- {
- // 跳过的打印位置
- e.HasMorePages = true;
- continue;
- }
- // 表格Item是否有下一页
- if (_printPageNum < TotalPageNum)
- {
- _printPageNum++;
- continue;
- }
- // 纸张数是否增加判断
- if (++_printDataPosition < _printDataTotal)
- {
- _printPageNum = 1;
- e.HasMorePages = true;
- // 同一数据一次印刷/分批印刷
- if (true)
- {
- // 同一数据一次印刷
- if (++_printDataCopies < _numberOfCopies)
- {
- continue;
- }
- else
- {
- _printDataCopies = 0;
- }
- }
- }
- else
- {
- e.HasMorePages = false;
- break;
- }
- if (_printDataBinding != null && _printDataBinding.BindingSource != null && 1 < _printDataBinding.BindingSource.Count)
- {
- // 打印数据源下移一条记录
- if (_printDataBinding.BindingSource.Position + 1 < _printDataBinding.BindingSource.Count)
- {
- _printDataBinding.BindingSource.MoveNext();
- }
- else
- {
- _printDataBinding.BindingSource.MoveFirst();
- }
- }
- }
- // 纸张数是否增加判断
- if (e.HasMorePages && _printDataPosition < _printDataTotal)
- {
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- return;
- }
- }
- else
- {
- e.HasMorePages = false;
- return;
- }
- //e.HasMorePages = false;
- //e.Graphics.Clear(Color.White);
- }
- } while (!isPrintCurrentPage);
- }
- else
- */
- {
- this.PrintPage(e);
- }
-
- //#if DEBUG
- // Dongke.IBOSS.PRD.Basics.Library.OutputLog.TraceLog(Dongke.IBOSS.PRD.Basics.Library.LogPriority.Debug,
- // "PrintDocument_PrintPage",
- // "end",
- // DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"),
- // Dongke.IBOSS.PRD.Basics.Library.LocalPath.LogExePath + @"printdebug\");
- //#endif
- }
- private void PrintPage(PrintPageEventArgs e)
- {
- e.Graphics.PageUnit = GraphicsUnit.Millimeter;
- if (_isPrintAreaVisible && _rectangleFGreed.IsEmpty)
- {
- //if (_landscape)
- //{
- // // 横向打印
- // _rectangleFGreed = new RectangleF(
- // (_defaultPaperSize.Height - _defaultPrintableArea.Bottom) / 100 * e.Graphics.DpiY,
- // _defaultPrintableArea.Left / 100 * e.Graphics.DpiX,
- // _defaultPrintableArea.Height / 100 * e.Graphics.DpiY,
- // _defaultPrintableArea.Width / 100 * e.Graphics.DpiX);
- //}
- //else
- //{
- // // 纵向打印
- // _rectangleFGreed = new RectangleF(
- // _defaultPrintableArea.Left / 100 * e.Graphics.DpiX,
- // _defaultPrintableArea.Top / 100 * e.Graphics.DpiY,
- // _defaultPrintableArea.Width / 100 * e.Graphics.DpiX,
- // _defaultPrintableArea.Height / 100 * e.Graphics.DpiY);
- //}
- _rectangleFGreed = new RectangleF(
- 0,
- 0,
- LayoutCommon.MillimeterToInch100Single(this.PaperWidth) / 100 * e.Graphics.DpiX,
- LayoutCommon.MillimeterToInch100Single(this.PaperHeight) / 100 * e.Graphics.DpiY);
- }
- if (_printType == PrintType.PrintTest)
- {
- // 打印范围
- RectangleF rectangleF = new RectangleF(_allOffsetX,
- _allOffsetY,
- _paperWidth,
- _paperHeight);
- e.Graphics.SetClip(rectangleF);
- // 背景打印
- if (_isPrintBackground)
- {
- PrintBackgroundImage(e.Graphics);
- }
- // Item打印
- foreach (LayoutItem layoutItem in _items)
- {
- switch (layoutItem.ItemType)
- {
- case ItemType.Text:
- DrawTextItem(e.Graphics, (TextItem)layoutItem);
- break;
- case ItemType.Image:
- DrawImageItem(e.Graphics, (ImageItem)layoutItem);
- break;
- case ItemType.Rectangle:
- DrawRectangleItem(e.Graphics, (RectangleItem)layoutItem);
- break;
- case ItemType.Ellipse:
- DrawEllipseItem(e.Graphics, (EllipseItem)layoutItem);
- break;
- //case ItemType.Line:
- // DrawLineItem(e.Graphics, (LineItem)layoutItem);
- // break;
- case ItemType.Grid:
- DrawGridItem(e.Graphics, (GridItem)layoutItem);
- break;
- case ItemType.PageNum:
- DrawPageNumItem(e.Graphics, (PageNumItem)layoutItem);
- break;
- case ItemType.TotalText:
- DrawTotalTextItem(e.Graphics, (TotalTextItem)layoutItem);
- break;
- default:
- //throw new NotImplementedException();
- break;
- }
- }
- // 可以打印范围
- if (_isPrintAreaVisible)
- {
- e.Graphics.PageUnit = GraphicsUnit.Pixel;
- e.Graphics.ResetClip();
- e.Graphics.DrawRectangle(LayoutCommon.GreedDotPen,
- _rectangleFGreed.X,
- _rectangleFGreed.Y,
- _rectangleFGreed.Width,
- _rectangleFGreed.Height);
- }
- e.HasMorePages = false;
- }
- else if (_printType == PrintType.Print || _printType == PrintType.PrintPreview)
- {
- // 打印范围
- RectangleF rectangleF = new RectangleF(_allOffsetX,
- _allOffsetY,
- _paperWidth,
- _paperHeight);
- e.Graphics.SetClip(rectangleF);
- // 背景打印
- if (_isPrintBackground)
- {
- PrintBackgroundImage(e.Graphics);
- }
- // Item打印
- foreach (LayoutItem layoutItem in _items)
- {
- switch (layoutItem.ItemType)
- {
- case ItemType.Text:
- DrawTextItem(e.Graphics, (TextItem)layoutItem);
- break;
- case ItemType.Image:
- DrawImageItem(e.Graphics, (ImageItem)layoutItem);
- break;
- case ItemType.Rectangle:
- DrawRectangleItem(e.Graphics, (RectangleItem)layoutItem);
- break;
- case ItemType.Ellipse:
- DrawEllipseItem(e.Graphics, (EllipseItem)layoutItem);
- break;
- //case ItemType.Line:
- // DrawLineItem(e.Graphics, (LineItem)layoutItem);
- // break;
- case ItemType.Grid:
- DrawGridItem(e.Graphics, (GridItem)layoutItem);
- break;
- case ItemType.PageNum:
- DrawPageNumItem(e.Graphics, (PageNumItem)layoutItem);
- break;
- case ItemType.TotalText:
- DrawTotalTextItem(e.Graphics, (TotalTextItem)layoutItem);
- break;
- default:
- //throw new NotImplementedException();
- break;
- }
- }
- // 可以打印范围
- if (_isPrintAreaVisible)
- {
- e.Graphics.PageUnit = GraphicsUnit.Pixel;
- e.Graphics.ResetClip();
- e.Graphics.DrawRectangle(LayoutCommon.GreedDotPen,
- _rectangleFGreed.X,
- _rectangleFGreed.Y,
- _rectangleFGreed.Width,
- _rectangleFGreed.Height);
- }
- // 表格Item是否有下一页
- if (_printPageNum < TotalPageNum)
- {
- _printPageNum++;
- e.HasMorePages = true;
- return;
- }
- if (_printAction == PrintAction.PrintToPrinter)
- {
- // 纸张数是否增加判断
- if (_printDataBinding != null && _printDataBinding.BindingSource != null && 0 < _printDataBinding.BindingSource.Count)
- {
- if (_printDataBinding.BindingSource.Position + 1 < _printDataBinding.BindingSource.Count)
- {
- // 打印数据源下移一条记录
- _printDataBinding.BindingSource.MoveNext();
- _printPageNum = 1;
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- else
- {
- // 纸张数是否增加判断
- if (++_printDataPosition < _printDataTotal)
- {
- _printPageNum = 1;
- e.HasMorePages = true;
- // 同一数据一次印刷/分批印刷
- if (true)
- {
- // 同一数据一次印刷
- if (++_printDataCopies < _numberOfCopies)
- {
- return;
- }
- else
- {
- _printDataCopies = 0;
- }
- }
- }
- else
- {
- e.HasMorePages = false;
- return;
- }
- if (_printDataBinding != null && _printDataBinding.BindingSource != null && 0 < _printDataBinding.BindingSource.Count)
- {
- // 打印数据源下移一条记录
- if (_printDataBinding.BindingSource.Position + 1 < _printDataBinding.BindingSource.Count)
- {
- _printDataBinding.BindingSource.MoveNext();
- }
- else
- {
- _printDataBinding.BindingSource.MoveFirst();
- }
- }
- }
- }
- else if (_printType == PrintType.AllocationPrint || _printType == PrintType.AllocationPrintPreview)
- {
- e.Graphics.SetClip(RectangleF.Empty);
- int allocationIndex = -1;
- // 一张纸打印
- for (int i = 0; i < _allocationNum; i++)
- {
- allocationIndex++;
- if (_printDataPosition < _allocationSkipCount)
- {
- // 跳过的打印位置
- e.HasMorePages = true;
- continue;
- }
- _allOffsetX = _marginLeft
- + (allocationIndex % _allocationNumX)
- * (_paperWidth + _allocationHorizontalSpace)
- - _printerOffsetX;
- _allOffsetY = _marginTop
- + (allocationIndex / _allocationNumX)
- * (_paperHeight + _allocationVerticalSpace)
- - _printerOffsetY;
- // 打印范围
- RectangleF rectangleF = new RectangleF(_allOffsetX,
- _allOffsetY,
- _paperWidth,
- _paperHeight);
- e.Graphics.SetClip(rectangleF);
- // 背景打印
- if (_isPrintBackground)
- {
- PrintBackgroundImage(e.Graphics);
- }
- // Item打印
- foreach (LayoutItem layoutItem in _items)
- {
- switch (layoutItem.ItemType)
- {
- case ItemType.Text:
- DrawTextItem(e.Graphics, (TextItem)layoutItem);
- break;
- case ItemType.Image:
- DrawImageItem(e.Graphics, (ImageItem)layoutItem);
- break;
- case ItemType.Rectangle:
- DrawRectangleItem(e.Graphics, (RectangleItem)layoutItem);
- break;
- case ItemType.Ellipse:
- DrawEllipseItem(e.Graphics, (EllipseItem)layoutItem);
- break;
- //case ItemType.Line:
- // DrawLineItem(e.Graphics, (LineItem)layoutItem);
- // break;
- case ItemType.Grid:
- DrawGridItem(e.Graphics, (GridItem)layoutItem);
- break;
- case ItemType.PageNum:
- DrawPageNumItem(e.Graphics, (PageNumItem)layoutItem);
- break;
- case ItemType.TotalText:
- DrawTotalTextItem(e.Graphics, (TotalTextItem)layoutItem);
- break;
- default:
- break;
- }
- }
- // 表格Item是否有下一页
- if (_printPageNum < TotalPageNum)
- {
- _printPageNum++;
- continue;
- }
- // 纸张数是否增加判断
- if (++_printDataPosition < _printDataTotal)
- {
- _printPageNum = 1;
- e.HasMorePages = true;
- // 同一数据一次印刷/分批印刷
- if (true)
- {
- // 同一数据一次印刷
- if (++_printDataCopies < _numberOfCopies)
- {
- continue;
- }
- else
- {
- _printDataCopies = 0;
- }
- }
- }
- else
- {
- e.HasMorePages = false;
- break;
- }
- if (_printDataBinding != null && _printDataBinding.BindingSource != null && 1 < _printDataBinding.BindingSource.Count)
- {
- // 打印数据源下移一条记录
- if (_printDataBinding.BindingSource.Position + 1 < _printDataBinding.BindingSource.Count)
- {
- _printDataBinding.BindingSource.MoveNext();
- }
- else
- {
- _printDataBinding.BindingSource.MoveFirst();
- }
- }
- }
- // 可以打印范围
- if (_isPrintAreaVisible)
- {
- e.Graphics.PageUnit = GraphicsUnit.Pixel;
- e.Graphics.ResetClip();
- e.Graphics.DrawRectangle(LayoutCommon.GreedDotPen,
- _rectangleFGreed.X,
- _rectangleFGreed.Y,
- _rectangleFGreed.Width,
- _rectangleFGreed.Height);
- }
- // 纸张数是否增加判断
- if (e.HasMorePages && _printDataPosition < _printDataTotal)
- {
- e.HasMorePages = true;
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- else
- {
- e.HasMorePages = false;
- }
- }
- // 选定打印页码 modify by chenxy 2014-03-27 end
- #endregion 打印相关
- #endregion 事件处理
- #region 委托事件
- /// <summary>
- /// 用户操作数据项目完成时发生
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public delegate void UserChangedItemEventHandler(object sender, ItemChangedEventArgs e);
- /// <summary>
- /// 用户操作数据项目完成时发生
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- public delegate void UserChangedGridItemEventHandler(object sender, CollectionChangeEventArgs e);
- #endregion
- #region 事件
- /// <summary>
- /// 改变纸张尺寸。
- /// </summary>
- public event EventHandler PaperSizeChanged;
- /// <summary>
- /// 改变缩放比例
- /// </summary>
- public event EventHandler ZoomChanged;
- /// <summary>
- /// 当一个项目被改变,移动,添加或删除引发此事件。
- /// </summary>
- public event ItemChangedEventHandler ItemChanged;
- /// <summary>
- /// 项目的选择状态改变时引发此事件。
- /// </summary>
- public event ItemSelectionChangedEventHandler ItemSelectionChanged;
- /// <summary>
- /// 打印或显示数据之前引发此事件。
- /// </summary>
- public event ItemValueFormattingEventHandler ItemValueFormatting;
- /// <summary>
- /// 用户追加Item完成时触发事件
- /// </summary>
- public event UserChangedItemEventHandler UserAddedItem;
- /// <summary>
- /// 用户删除Item完成时触发事件
- /// </summary>
- public event UserChangedItemEventHandler UserDeletedItem;
- /// <summary>
- /// 用户修改GridItem列时触发事件
- /// </summary>
- public event UserChangedGridItemEventHandler UserChangedGridItem;
- /// <summary>
- /// 改变项目的选中状态
- /// </summary>
- public event EventHandler UserSelectionChanged;
- #endregion 事件
- #region User32
- /// <summary>
- ///
- /// </summary>
- [DllImport("user32.dll", CharSet = CharSet.Auto)]
- public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, int lParam);
- #endregion User32
- #region Grid页码相关
- /// <summary>
- /// 当前页码
- /// </summary>
- [Browsable(false)]
- public int CurrentPageNum
- {
- get
- {
- return _currentPageNum;
- }
- }
- /// <summary>
- /// 页数
- /// </summary>
- [Browsable(false)]
- public int TotalPageNum
- {
- get
- {
- return _totalPageNum;
- }
- }
- /// <summary>
- /// 打印页码
- /// </summary>
- internal int PrintPageNum
- {
- get
- {
- return _printPageNum;
- }
- }
- /// <summary>
- /// 设置总页数
- /// </summary>
- internal void SetTotalPageNum()
- {
- LayoutItem[] grids = this.GetItems(ItemType.Grid);
- int total = 1;
- foreach (GridItem grid in grids)
- {
- total = Math.Max(total, grid.TotalPageNum);
- }
- if (total != _totalPageNum)
- {
- _totalPageNum = total;
- LayoutItem[] pageNums = this.GetItems(ItemType.PageNum);
- foreach (PageNumItem pageNum in pageNums)
- {
- pageNum.Refresh();
- }
- }
- }
- /// <summary>
- /// 设定显示第几页
- /// </summary>
- /// <param name="pageNum"></param>
- public void SetPageNum(int pageNum)
- {
- if (pageNum < 1)
- {
- pageNum = 1;
- }
- if (pageNum > _totalPageNum)
- {
- pageNum = _totalPageNum;
- }
- if (pageNum != _currentPageNum)
- {
- _currentPageNum = pageNum;
- LayoutItem[] grids = this.GetItems(ItemType.Grid);
- foreach (GridItem grid in grids)
- {
- grid.Refresh();
- }
- LayoutItem[] pageNums = this.GetItems(ItemType.PageNum);
- foreach (PageNumItem pageNumItem in pageNums)
- {
- pageNumItem.Refresh();
- }
- LayoutItem[] totalTextItems = this.GetItems(ItemType.TotalText);
- foreach (TotalTextItem totalTextItem in totalTextItems)
- {
- totalTextItem.Refresh();
- }
- }
- }
- /// <summary>
- /// Grid使用的数据源
- /// </summary>
- [Browsable(false)]
- public DataSet GridDataSource
- {
- get
- {
- return _gridDataSet;
- }
- set
- {
- if (value != _gridDataSet)
- {
- _gridDataSet = value;
- LayoutItem[] grids = this.GetItems(ItemType.Grid);
- if (grids != null && grids.Length > 0)
- {
- if (_gridDataSet == null || _gridDataSet.Tables.Count == 0)
- {
- foreach (GridItem grid in grids)
- {
- grid.GridSource = null;
- }
- }
- else
- {
- foreach (GridItem grid in grids)
- {
- if (_gridDataSet.Tables.Contains(grid.GridDataMember))
- {
- grid.GridSource = _gridDataSet.Tables[grid.GridDataMember];
- }
- else
- {
- grid.GridSource = null;
- }
- }
- }
- }
- }
- }
- }
- #endregion Grid页码相关
- }
- }
|