DocumentView.cs 184 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148
  1. using PaintDotNet.Adjust;
  2. using PaintDotNet.Adjust.BaseImage;
  3. using PaintDotNet.Annotation;
  4. using PaintDotNet.Annotation.Command;
  5. using PaintDotNet.Annotation.DedicatedAnalysis;
  6. using PaintDotNet.Annotation.Enum;
  7. using PaintDotNet.Annotation.FieldView;
  8. using PaintDotNet.Annotation.ImageCollect;
  9. using PaintDotNet.Annotation.Label;
  10. using PaintDotNet.Annotation.Measure;
  11. using PaintDotNet.Annotation.Other;
  12. using PaintDotNet.Annotation.PhysicalPhaseAction;
  13. using PaintDotNet.Base.CommTool;
  14. using PaintDotNet.Base.Enum;
  15. using PaintDotNet.Base.Functionodel;
  16. using PaintDotNet.Base.SettingModel;
  17. using PaintDotNet.Base.SettingModel.LVMModel;
  18. using PaintDotNet.Base.XmlSaveModel;
  19. using PaintDotNet.DbOpreate.DbModel;
  20. using PaintDotNet.SystemLayer;
  21. using System;
  22. using System.Collections.Generic;
  23. using System.ComponentModel;
  24. using System.Drawing;
  25. using System.Drawing.Drawing2D;
  26. using System.Reflection;
  27. using System.Runtime.InteropServices;
  28. using System.Threading;
  29. using System.Windows.Forms;
  30. using static PaintDotNet.Base.SettingModel.LabelStyleModel;
  31. namespace PaintDotNet
  32. {
  33. /// <summary>
  34. /// 主面板
  35. /// </summary>
  36. public class DocumentView : UserControl2, ISurfaceBox, DocumentDirtyObserver
  37. {
  38. #region 控件
  39. private IContainer components = null;
  40. /// <summary>
  41. /// 左侧刻度标尺
  42. /// </summary>
  43. public Ruler leftRuler;
  44. /// <summary>
  45. /// 中心画布
  46. /// </summary>
  47. public PanelEx panel;
  48. /// <summary>
  49. /// 顶部刻度标尺
  50. /// </summary>
  51. public Ruler topRuler;
  52. /// <summary>
  53. /// 右键菜单
  54. /// </summary>
  55. private ContextMenuStrip contextMenuStrip1;
  56. private ToolStripMenuItem toolStripMenuItem1;
  57. private ToolStripMenuItem toolStripMenuItem2;
  58. private ToolStripMenuItem toolStripMenuItem3;
  59. private ToolStripMenuItem toolStripMenuItem4;
  60. #endregion
  61. private bool raiseFirstInputAfterGotFocus = false;
  62. private int refreshSuspended = 0;
  63. private bool hookedMouseEvents = false;
  64. public bool previewMeasure = false;
  65. private static WeakReference<Surface> doubleBufferSurfaceWeakRef = null;
  66. private Surface doubleBufferSurface = null;
  67. /// <summary>
  68. /// 绘制线程
  69. /// </summary>
  70. private Threading.ThreadPool threadPool = new Threading.ThreadPool();
  71. private RenderContext renderContext;
  72. /// <summary>
  73. /// 为了保证图片绘制在画布中心
  74. /// 设置的偏移量
  75. /// </summary>
  76. private int offsetW = 0;
  77. private int offsetH = 0;
  78. private int offsetHalfW = 0;
  79. private int offsetHalfH = 0;
  80. /*private int offsetW = 300;
  81. private int offsetH = 150;
  82. private int offsetHalfW = 150;
  83. private int offsetHalfH = 75;*/
  84. /// <summary>
  85. /// 相的集合
  86. /// </summary>
  87. public List<PhaseModel> phaseModels = new List<PhaseModel>();
  88. /// <summary>
  89. /// 满屏网格是否显示,默认不显示
  90. /// </summary>
  91. private bool gridLineFullEnabled = false;
  92. /// <summary>
  93. /// 网格是否显示,默认不显示
  94. /// </summary>
  95. private bool gridLineEnabled = false;
  96. /// <summary>
  97. /// 矩形网格是否显示,默认不显示
  98. /// </summary>
  99. private bool gridRectangleEnabled = false;
  100. /// <summary>
  101. /// 圆形网格是否显示,默认不显示
  102. /// </summary>
  103. private bool gridRoundEnabled = false;
  104. /// <summary>
  105. /// 十字线是否显示,默认不显示
  106. /// </summary>
  107. private bool gridCrossCurveEnabled = false;
  108. /// <summary>
  109. /// 辅助线是否显示,默认不显示
  110. /// </summary>
  111. private bool auxiliaryLineEnabled = false;
  112. /// <summary>
  113. /// 视图标尺是否显示,默认显示
  114. /// </summary>
  115. private bool rulersEnabled = false;
  116. /// <summary>
  117. /// 当前的视场行为标记,合并或删除
  118. /// </summary>
  119. private CombineMode combineMode = CombineMode.Union;
  120. /// <summary>
  121. /// 标记连续绘制
  122. /// </summary>
  123. private bool continuousDrawingLabel = false;
  124. /// <summary>
  125. /// 测量连续绘制
  126. /// </summary>
  127. private bool continuousDrawingMeasure = false;
  128. /// <summary>
  129. /// 二值提取\连续操作
  130. /// </summary>
  131. private bool continuousBinaryAction = false;
  132. /// <summary>
  133. /// 实际大小标记
  134. /// </summary>
  135. private bool actualsize = false;
  136. /// <summary>
  137. /// 合适大小标记
  138. /// </summary>
  139. private bool suitableSize = false;
  140. /// <summary>
  141. /// 合适高度标记
  142. /// </summary>
  143. private bool suitableHeight = false;
  144. /// <summary>
  145. /// 合适宽度标记
  146. /// </summary>
  147. private bool suitableWidth = false;
  148. /// <summary>
  149. /// 锁定扩缩
  150. /// </summary>
  151. private bool lockZoom = false;
  152. /// <summary>
  153. /// 定倍显示
  154. /// </summary>
  155. private bool fixedMultiple = false;
  156. /// <summary>
  157. /// 合并视场
  158. /// </summary>
  159. private bool mergeFieldOfView = false;
  160. /// <summary>
  161. /// 删除视场
  162. /// </summary>
  163. private bool deleteFieldOfView = false;
  164. /// <summary>
  165. /// 图层及文档信息
  166. /// </summary>
  167. private Document document;
  168. /// <summary>
  169. /// 图像内存块及其属性
  170. /// </summary>
  171. private Surface compositionSurface;
  172. /// <summary>
  173. /// 【暂留,未用到】
  174. /// </summary>
  175. private SurfaceBoxGridRenderer gridRenderer;
  176. /// <summary>
  177. /// 【标注、测量】用于保存测量、标注等的历史记录,用于撤销
  178. /// </summary>
  179. public UndoManager undoManager;
  180. /// <summary>
  181. /// 【标注、测量】list of draw objects
  182. /// </summary>
  183. private GraphicsList graphicsList;
  184. /// <summary>
  185. /// 【标注、测量、视场、其它】当前激活的工具
  186. /// </summary>
  187. public DrawToolType activeTool;
  188. /// <summary>
  189. /// 工具的集合
  190. /// </summary>
  191. public Type[] tools;
  192. /// <summary>
  193. /// 初始化标记
  194. /// </summary>
  195. private bool initialized;
  196. /// <summary>
  197. /// AppWorkspace接口
  198. /// </summary>
  199. private IAppWorkspaceForSurfaceBox appWorkspace;
  200. /// <summary>
  201. /// 目前用于修改视场,如果是鼠标保持按下的状态,比如按下不抬起状态,
  202. /// 按下拖动状态,则不响应修改视场界面的相应numericUpDown等控件的值变化事件,
  203. /// 以避免事件的冲突
  204. /// </summary>
  205. public bool mouseStatus;
  206. /// <summary>
  207. /// 辅助线的横线和竖线
  208. /// </summary>
  209. GraphicsPath path, path1;
  210. /// <summary>
  211. /// 待渲染的列表
  212. /// </summary>
  213. private SurfaceBoxRendererList rendererList;
  214. private SurfaceBoxBaseRenderer baseRenderer;
  215. /// <summary>
  216. /// 窗体状态
  217. /// </summary>
  218. private FormWindowState oldWindowState = FormWindowState.Minimized;
  219. /// <summary>
  220. /// 缩放比例
  221. /// </summary>
  222. private ScaleFactor scaleFactor = new ScaleFactor(1, 1);
  223. /// <summary>
  224. /// 每像素多少微米
  225. /// </summary>
  226. private double micronRatio;
  227. /// <summary>
  228. /// 是否允许像素跟踪
  229. /// </summary>
  230. public bool pixelTrackingEnabled = true;
  231. /// <summary>
  232. /// 是否允许刷新公共的底部的放大缩小的进度条和值
  233. /// </summary>
  234. public bool refueshZoomTrackValue = true;
  235. private int toolNumber = -1;
  236. public DrawAnalysisModel AnalysisStyleModel { get; set; }
  237. /// <summary>
  238. /// 如果是false,则是弹窗的DocumentWorkspaceWindow
  239. /// </summary>
  240. public bool rightDown = true;
  241. /// <summary>
  242. /// ctrl按下事件
  243. /// </summary>
  244. public bool controlPress = false;
  245. /// <summary>
  246. /// 在toolPointer的工具下,是否绘制鼠标划选的矩形
  247. /// </summary>
  248. public bool drawRectangle = false;
  249. /// <summary>
  250. /// 在toolPointer的工具下,鼠标划选的矩形
  251. /// </summary>
  252. public Rectangle rectangle;
  253. #region 直方图操作数据缓存
  254. /// <summary>
  255. /// 亮度
  256. /// </summary>
  257. private double histogramBeta = -0.5;
  258. /// <summary>
  259. /// 对比度
  260. /// </summary>
  261. private double histogramAlpha = 1.0;
  262. /// <summary>
  263. /// gamma值
  264. /// </summary>
  265. private double histogramGamma = 1.0;
  266. /// <summary>
  267. /// 最佳算法的数值,范围0-499
  268. /// </summary>
  269. private int histogramPercent = 200;
  270. /// <summary>
  271. /// 是否选中了log
  272. /// </summary>
  273. private bool histogramLogEnabled;
  274. /// <summary>
  275. /// 是否选中了skip
  276. /// </summary>
  277. private bool histogramSkipEnabled;
  278. public Bitmap BoxBitmap;
  279. #endregion
  280. #region 直方图操作数据缓存
  281. /// <summary>
  282. /// 亮度
  283. /// </summary>
  284. public double HistogramBeta
  285. {
  286. set
  287. {
  288. this.histogramBeta = value;
  289. }
  290. get
  291. {
  292. return this.histogramBeta;
  293. }
  294. }
  295. /// <summary>
  296. /// 对比度
  297. /// </summary>
  298. public double HistogramAlpha
  299. {
  300. set
  301. {
  302. this.histogramAlpha = value;
  303. }
  304. get
  305. {
  306. return this.histogramAlpha;
  307. }
  308. }
  309. public double HistogramGamma
  310. {
  311. set
  312. {
  313. this.histogramGamma = value;
  314. }
  315. get
  316. {
  317. return this.histogramGamma;
  318. }
  319. }
  320. public int HistogramPercent
  321. {
  322. set
  323. {
  324. this.histogramPercent = value;
  325. }
  326. get
  327. {
  328. return this.histogramPercent;
  329. }
  330. }
  331. public bool HistogramLogEnabled
  332. {
  333. set
  334. {
  335. this.histogramLogEnabled = value;
  336. }
  337. get
  338. {
  339. return this.histogramLogEnabled;
  340. }
  341. }
  342. public bool HistogramSkipEnabled
  343. {
  344. set
  345. {
  346. this.histogramSkipEnabled = value;
  347. }
  348. get
  349. {
  350. return this.histogramSkipEnabled;
  351. }
  352. }
  353. #endregion
  354. public DocumentView()
  355. {
  356. InitializeComponent();
  357. this.document = null;
  358. this.compositionSurface = null;
  359. this.panel.ScaleFactor = this.scaleFactor;
  360. this.panel.Paint += new PaintEventHandler(this.panelPaint);
  361. this.panel.MouseMove += new MouseEventHandler(this.MouseEvent_Move);
  362. this.panel.MouseDown += new MouseEventHandler(this.MouseEvent_Down);
  363. this.panel.MouseUp += new MouseEventHandler(this.MouseEvent_Up);
  364. this.panel.MouseClick += new MouseEventHandler(this.MouseEvent_Click);
  365. this.panel.MouseDoubleClick += new MouseEventHandler(this.MouseEvent_DoubleClick);
  366. this.rendererList = new SurfaceBoxRendererList(this.panel.Size, this.panel.Size);
  367. this.rendererList.Invalidated += new InvalidateEventHandler(Renderers_Invalidated);
  368. this.baseRenderer = new SurfaceBoxBaseRenderer(this.rendererList, null);
  369. this.rendererList.Add(this.baseRenderer, false);
  370. this.initialized = true;
  371. }
  372. public OpenCvSharp.Mat OldMat
  373. {
  374. get
  375. {
  376. //备注:待调试->>200827 add by scc
  377. if (this.CompositionSurface != null)
  378. return OpenCvSharp.Extensions.BitmapConverter.ToMat(this.CompositionSurface.Thumborigin/*CreateAliasedBitmap()*/);
  379. else
  380. return null;
  381. }
  382. }
  383. public OpenCvSharp.Mat BoxMat
  384. {
  385. get
  386. {
  387. //备注:待调试
  388. //if (this.AppWorkspace.ActiveDocumentWorkspace != null)
  389. //{
  390. return OpenCvSharp.Extensions.BitmapConverter.ToMat(this.BoxBitmap);
  391. //}
  392. }
  393. }
  394. public Rectangle DrawRectangle
  395. {
  396. get
  397. {
  398. return this.rectangle;
  399. }
  400. set
  401. {
  402. this.rectangle = value;
  403. }
  404. }
  405. public bool DrawRectangleFlag
  406. {
  407. get
  408. {
  409. return this.drawRectangle;
  410. }
  411. set
  412. {
  413. this.drawRectangle = value;
  414. }
  415. }
  416. public IAppWorkspaceForSurfaceBox AppWorkspaceTop
  417. {
  418. set
  419. {
  420. this.appWorkspace = value;
  421. }
  422. get
  423. {
  424. return this.appWorkspace;
  425. }
  426. }
  427. public GraphicsList GraphicsList
  428. {
  429. get
  430. {
  431. return graphicsList;
  432. }
  433. set
  434. {
  435. graphicsList = value;
  436. if (this.GraphicsList != null)
  437. {
  438. this.AdjustRendering();
  439. //this.graphicsList.AddObserver(this);
  440. }
  441. }
  442. }
  443. void AdjustRendering()
  444. {
  445. Size docSize;
  446. if (this.GraphicsList != null)
  447. {
  448. docSize = this.GraphicsList.GetSize();
  449. //AutoScrollMinSize = docSize;
  450. }
  451. else
  452. {
  453. //AutoScrollMinSize = new Size(0, 0);
  454. }
  455. Invalidate();
  456. }
  457. public Surface CompositionSurface
  458. {
  459. get
  460. {
  461. return this.compositionSurface;
  462. }
  463. set
  464. {
  465. if (value != null)
  466. {
  467. this.compositionSurface = value;
  468. this.Refresh();
  469. }
  470. }
  471. }
  472. public void ResetBoxBitmap()
  473. {
  474. this.BoxBitmap = this.CompositionSurface.CreateAliasedBitmap();
  475. }
  476. public bool GridLineEnabled
  477. {
  478. get
  479. {
  480. return this.gridLineEnabled;
  481. }
  482. set
  483. {
  484. this.gridLineEnabled = value;
  485. }
  486. }
  487. public bool GridLineFullEnabled
  488. {
  489. get
  490. {
  491. return this.gridLineFullEnabled;
  492. }
  493. set
  494. {
  495. this.gridLineFullEnabled = value;
  496. }
  497. }
  498. public bool GridRectangleEnabled
  499. {
  500. get
  501. {
  502. return this.gridRectangleEnabled;
  503. }
  504. set
  505. {
  506. this.gridRectangleEnabled = value;
  507. }
  508. }
  509. public bool GridRoundEnabled
  510. {
  511. get
  512. {
  513. return this.gridRoundEnabled;
  514. }
  515. set
  516. {
  517. this.gridRoundEnabled = value;
  518. }
  519. }
  520. public bool GridCrossCurveEnabled
  521. {
  522. get
  523. {
  524. return this.gridCrossCurveEnabled;
  525. }
  526. set
  527. {
  528. this.gridCrossCurveEnabled = value;
  529. }
  530. }
  531. public double MicronRatio
  532. {
  533. get
  534. {
  535. return this.micronRatio;
  536. }
  537. set
  538. {
  539. this.micronRatio = value;
  540. }
  541. }
  542. public bool AuxiliaryLineEnabled
  543. {
  544. get
  545. {
  546. return auxiliaryLineEnabled;
  547. }
  548. set
  549. {
  550. this.auxiliaryLineEnabled = value;
  551. }
  552. }
  553. public bool ContinuousDrawingLabel
  554. {
  555. get
  556. {
  557. return continuousDrawingLabel;
  558. }
  559. set
  560. {
  561. this.continuousDrawingLabel = value;
  562. }
  563. }
  564. public bool ContinuousDrawingMeasure
  565. {
  566. get
  567. {
  568. return continuousDrawingMeasure;
  569. }
  570. set
  571. {
  572. this.continuousDrawingMeasure = value;
  573. }
  574. }
  575. public bool ContinuousBinaryAction
  576. {
  577. get
  578. {
  579. return continuousBinaryAction;
  580. }
  581. set
  582. {
  583. this.continuousBinaryAction = value;
  584. }
  585. }
  586. public bool ActualSize
  587. {
  588. get
  589. {
  590. return actualsize;
  591. }
  592. set
  593. {
  594. this.actualsize = value;
  595. }
  596. }
  597. public bool SuitableSize
  598. {
  599. get
  600. {
  601. return suitableSize;
  602. }
  603. set
  604. {
  605. this.suitableSize = value;
  606. }
  607. }
  608. public bool SuitableWidth
  609. {
  610. get
  611. {
  612. return suitableWidth;
  613. }
  614. set
  615. {
  616. this.suitableWidth = value;
  617. }
  618. }
  619. public bool SuitableHeight
  620. {
  621. get
  622. {
  623. return suitableHeight;
  624. }
  625. set
  626. {
  627. this.suitableHeight = value;
  628. }
  629. }
  630. public bool LockZoom
  631. {
  632. get
  633. {
  634. return lockZoom;
  635. }
  636. set
  637. {
  638. this.lockZoom = value;
  639. }
  640. }
  641. public bool FixedMultiple
  642. {
  643. get
  644. {
  645. return fixedMultiple;
  646. }
  647. set
  648. {
  649. this.fixedMultiple = value;
  650. }
  651. }
  652. public bool MergeFieldOfView
  653. {
  654. get
  655. {
  656. return mergeFieldOfView;
  657. }
  658. set
  659. {
  660. this.mergeFieldOfView = value;
  661. }
  662. }
  663. public bool DeleteFieldOfView
  664. {
  665. get
  666. {
  667. return deleteFieldOfView;
  668. }
  669. set
  670. {
  671. this.deleteFieldOfView = value;
  672. }
  673. }
  674. public bool RulersEnabled
  675. {
  676. get
  677. {
  678. return rulersEnabled;
  679. }
  680. set
  681. {
  682. //if (this.rulersEnabled != value)
  683. {
  684. this.rulersEnabled = value;
  685. if (this.topRuler != null)
  686. {
  687. this.topRuler.Enabled = value;
  688. this.topRuler.Visible = value;
  689. }
  690. if (this.leftRuler != null)
  691. {
  692. this.leftRuler.Enabled = value;
  693. this.leftRuler.Visible = value;
  694. }
  695. this.OnResize(EventArgs.Empty);
  696. OnRulersEnabledChanged();
  697. }
  698. }
  699. }
  700. public CombineMode CombineMode
  701. {
  702. get
  703. {
  704. return this.combineMode;
  705. }
  706. set
  707. {
  708. this.combineMode = value;
  709. }
  710. }
  711. public SurfaceBoxRendererList RendererList
  712. {
  713. get
  714. {
  715. return this.rendererList;
  716. }
  717. }
  718. /// <summary>
  719. /// 初始化工具
  720. /// </summary>
  721. protected void InitToolsAndManager()
  722. {
  723. // 设置默认工具
  724. //activeTool = DrawToolType.Pointer;
  725. //TODO 全局判断
  726. activeTool = appWorkspace.GetDrawToolType();
  727. // 创建graphic list
  728. // 一是用于存储,二是用于调整层级
  729. GraphicsList = new GraphicsList(this);
  730. // 创建管理器,用于前进后退
  731. undoManager = new UndoManager(GraphicsList);
  732. // init Tools
  733. //tools = new Tool[(int)DrawToolType.NumberOfDrawTools];
  734. tools = new Type[(int)DrawToolType.NumberOfDrawTools];
  735. tools[(int)DrawToolType.Pointer] = typeof(ToolPointer);
  736. //
  737. //标注
  738. //
  739. tools[(int)DrawToolType.DrawRectangle] = typeof(ToolRectangle);
  740. tools[(int)DrawToolType.DrawEllipse] = typeof(ToolEllipse);
  741. tools[(int)DrawToolType.DrawLine] = typeof(ToolLine);
  742. tools[(int)DrawToolType.DrawPolygon] = typeof(ToolPolygon);
  743. tools[(int)DrawToolType.DrawPolygonLine] = typeof(ToolPolygonLine);
  744. tools[(int)DrawToolType.DrawPencil] = typeof(ToolPencil);
  745. tools[(int)DrawToolType.DrawClosedCurve] = typeof(ToolClosedCurve);
  746. tools[(int)DrawToolType.DrawCurve] = typeof(ToolCurve);
  747. tools[(int)DrawToolType.DrawCircle] = typeof(ToolCircle);
  748. tools[(int)DrawToolType.DrawOneArrowLine] = typeof(ToolOneArrowLine);
  749. tools[(int)DrawToolType.DrawTwoArrowLine] = typeof(ToolTwoArrowLine);
  750. tools[(int)DrawToolType.DrawLineSegment] = typeof(ToolLineSegment);
  751. tools[(int)DrawToolType.DrawRoundRectangle] = typeof(ToolRoundRectangle);
  752. tools[(int)DrawToolType.DrawTextString] = typeof(ToolTextString);
  753. tools[(int)DrawToolType.DrawDateMark] = typeof(ToolDateMark);
  754. tools[(int)DrawToolType.DrawTimeMark] = typeof(ToolTimeMark);
  755. tools[(int)DrawToolType.DrawNumberMark] = typeof(ToolNumberMark);
  756. tools[(int)DrawToolType.DrawGainNumber] = typeof(ToolGainNumber);
  757. tools[(int)DrawToolType.DrawWorkType] = typeof(ToolWorkType);
  758. tools[(int)DrawToolType.DrawPointMark] = typeof(ToolPointMark);
  759. tools[(int)DrawToolType.DrawWaterMark] = typeof(ToolWaterMark);
  760. //分析绘图
  761. tools[(int)DrawToolType.DrawCircleA] = typeof(ToolCircleA);
  762. tools[(int)DrawToolType.DrawMulLineA] = typeof(ToolDrawMulLineA);
  763. //
  764. //测量
  765. //
  766. tools[(int)DrawToolType.MeasureLine] = typeof(ToolMeasureLine);
  767. tools[(int)DrawToolType.MeasureDistanceLine] = typeof(ToolMeasureDistanceLine);
  768. tools[(int)DrawToolType.MeasureLength] = typeof(ToolMeasureLength);
  769. tools[(int)DrawToolType.MeasureTraceCurve] = typeof(ToolMeasureTraceCurve);
  770. tools[(int)DrawToolType.MeasureHLine] = typeof(ToolMeasureHLine);
  771. tools[(int)DrawToolType.MeasureVLine] = typeof(ToolMeasureVLine);
  772. tools[(int)DrawToolType.MeasureCircle] = typeof(ToolMeasureCircle);
  773. tools[(int)DrawToolType.MeasureInnerCircle] = typeof(ToolMeasureInnerCircle);
  774. tools[(int)DrawToolType.MeasureOuterCircle] = typeof(ToolMeasureOuterCircle);
  775. tools[(int)DrawToolType.MeasureDiameterCircle] = typeof(ToolMeasureDiameterCircle);
  776. tools[(int)DrawToolType.MeasurePointEdgeSize] = typeof(ToolMeasurePointEdgeSize);
  777. tools[(int)DrawToolType.MeasurePointCenterSize] = typeof(ToolMeasurePointCenterSize);
  778. tools[(int)DrawToolType.MeasureBrokenLine] = typeof(ToolMeasureBrokenLine);
  779. tools[(int)DrawToolType.MeasureCurveLine] = typeof(ToolMeasureCurveLine);
  780. tools[(int)DrawToolType.MeasureThreePointAngle] = typeof(ToolMeasureThreePointAngle);
  781. tools[(int)DrawToolType.MeasureFourPointAngle] = typeof(ToolMeasureFourPointAngle);
  782. tools[(int)DrawToolType.MeasureThreePointArc] = typeof(ToolMeasureThreePointArc);
  783. tools[(int)DrawToolType.MeasurePLine] = typeof(ToolMeasurePLine);
  784. tools[(int)DrawToolType.MeasureMulPLine] = typeof(ToolMeasureMulPLine);
  785. tools[(int)DrawToolType.MeasureHMulPLine] = typeof(ToolMeasureHMulPLine);
  786. tools[(int)DrawToolType.MeasureVMulPLine] = typeof(ToolMeasureVMulPLine);
  787. tools[(int)DrawToolType.MeasureParallelLine] = typeof(ToolMeasureParallelLine);
  788. tools[(int)DrawToolType.MeasureMulParallelLine] = typeof(ToolMeasureMulParallelLine);
  789. tools[(int)DrawToolType.MeasureVMulParallelLine] = typeof(ToolMeasureVMulParallelLine);
  790. tools[(int)DrawToolType.MeasureHMulParallelLine] = typeof(ToolMeasureHMulParallelLine);
  791. tools[(int)DrawToolType.MeasureClosedCurve] = typeof(ToolMeasureClosedCurve);
  792. tools[(int)DrawToolType.MeasurePolygon] = typeof(ToolMeasurePolygon);
  793. tools[(int)DrawToolType.MeasureRectangle] = typeof(ToolMeasureRectangle);
  794. tools[(int)DrawToolType.MeasureRandRectangle] = typeof(ToolMeasureRandRectangle);
  795. tools[(int)DrawToolType.MeasureSquare] = typeof(ToolMeasureSquare);
  796. tools[(int)DrawToolType.MeasureTracePolygon] = typeof(ToolMeasureTracePolygon);
  797. tools[(int)DrawToolType.MeasureMulLine] = typeof(ToolMeasureMulLine);
  798. tools[(int)DrawToolType.MeasureMulSegment] = typeof(ToolMeasureMulSegment);
  799. tools[(int)DrawToolType.MeasureMulHVLine] = typeof(ToolMeasureMulHVLine);
  800. tools[(int)DrawToolType.MeasureMulVLine] = typeof(ToolMeasureMulVLine);
  801. tools[(int)DrawToolType.MeasureRandSquare] = typeof(ToolMeasureRandSquare);
  802. tools[(int)DrawToolType.MeasurePointHLine] = typeof(ToolMeasurePointHLine);
  803. tools[(int)DrawToolType.MeasurePointArcSize] = typeof(ToolMeasurePointArcSize);
  804. tools[(int)DrawToolType.MeasureCenterCenterSize] = typeof(ToolMeasureCenterCenterSize);
  805. tools[(int)DrawToolType.MeasureTwoLineVLDistance] = typeof(ToolMeasureTwoLineVLDistance);
  806. //
  807. //视场
  808. //
  809. tools[(int)DrawToolType.ViewOval] = typeof(ToolViewOval);
  810. tools[(int)DrawToolType.ViewCircle] = typeof(ToolViewCircle);
  811. tools[(int)DrawToolType.ViewRectangle] = typeof(ToolViewRectangle);
  812. tools[(int)DrawToolType.ViewRectangleEx] = typeof(ToolViewRectangleEx);
  813. tools[(int)DrawToolType.ViewTriangle] = typeof(ToolViewTriangle);
  814. tools[(int)DrawToolType.ViewTriangleEx] = typeof(ToolViewTriangleEx);
  815. tools[(int)DrawToolType.ViewSquare] = typeof(ToolViewSquare);
  816. tools[(int)DrawToolType.ViewPolygon] = typeof(ToolViewPolygon);
  817. #region 对象处理
  818. //单个提取
  819. tools[(int)DrawToolType.BinaryExtract] = typeof(ToolBinaryExtract);
  820. //选择
  821. tools[(int)DrawToolType.BinaryChoise] = typeof(ToolBinaryChoise);
  822. tools[(int)DrawToolType.BinaryChoiseRectangle] = typeof(ToolBinaryChoiseRectangle);
  823. tools[(int)DrawToolType.BinaryChoiseOval] = typeof(ToolBinaryChoiseOval);
  824. tools[(int)DrawToolType.BinaryChoisePolygon] = typeof(ToolBinaryChoisePolygon);
  825. //添加
  826. tools[(int)DrawToolType.BinaryAddOval] = typeof(ToolBinaryAddOval);
  827. tools[(int)DrawToolType.BinaryAddRectangle] = typeof(ToolBinaryAddRectangle);
  828. tools[(int)DrawToolType.BinaryAddPolygon] = typeof(ToolBinaryAddPolygon);
  829. tools[(int)DrawToolType.BinaryAddTrack] = typeof(ToolBinaryAddTrack);
  830. //删除
  831. tools[(int)DrawToolType.BinaryDelete] = typeof(ToolBinaryDelete);
  832. tools[(int)DrawToolType.BinaryDeleteRectangle] = typeof(ToolBinaryDeleteRectangle);
  833. tools[(int)DrawToolType.BinaryDeletePolygon] = typeof(ToolBinaryDeletePolygon);
  834. tools[(int)DrawToolType.BinaryDeleteOval] = typeof(ToolBinaryDeleteOval);
  835. //连接
  836. tools[(int)DrawToolType.BinaryConnectionLine] = typeof(ToolBinaryConnectionLine);
  837. tools[(int)DrawToolType.BinaryConnectionPolygonLine] = typeof(ToolBinaryConnectionPolygonLine);
  838. tools[(int)DrawToolType.BinaryConnectionOval] = typeof(ToolBinaryConnectionOval);
  839. //分割
  840. tools[(int)DrawToolType.BinarySplitLine] = typeof(ToolBinarySplitLine);
  841. tools[(int)DrawToolType.BinarySplitPolyline] = typeof(ToolBinarySplitPolyLine);
  842. tools[(int)DrawToolType.BinarySplitOval] = typeof(ToolBinarySplitOval);
  843. #endregion
  844. #region 专用分析
  845. // 夹杂物
  846. tools[(int)DrawToolType.InclusionNoEffect] = typeof(ToolInclusionNoEffect);
  847. tools[(int)DrawToolType.InclusionSelect] = typeof(ToolInclusionSelect);
  848. tools[(int)DrawToolType.InclusionPolygon] = typeof(ToolInclusionPolygon);
  849. tools[(int)DrawToolType.InclusionDrawRecognitionArea] = typeof(ToolInclusionDrawRecognitionArea);
  850. tools[(int)DrawToolType.InclusionSelectRecognitionArea] = typeof(ToolInclusionSelectRecognitionArea);
  851. #endregion
  852. #region 物相提取
  853. //多边形
  854. tools[(int)DrawToolType.PPhasePolygon] = typeof(ToolPPhasePolygon);
  855. //矩形
  856. tools[(int)DrawToolType.PPhaseRectangle] = typeof(ToolPPhaseRectangle);
  857. //椭圆
  858. tools[(int)DrawToolType.PPhaseOval] = typeof(ToolPPhaseOval);
  859. #endregion
  860. //其它
  861. //手型工具
  862. tools[(int)DrawToolType.MoveMode] = typeof(PanTool);
  863. //图片裁剪
  864. tools[(int)DrawToolType.ImageCut] = typeof(ImageCutTool);
  865. //自动标尺
  866. tools[(int)DrawToolType.DrawAutoRuler] = typeof(ToolAutoRuler);
  867. //预存标尺
  868. tools[(int)DrawToolType.DrawPrestoredRuler] = typeof(ToolPrestoredRuler);
  869. //手动标尺
  870. tools[(int)DrawToolType.DrawHandModeRuler] = typeof(ToolHandModeRuler);
  871. //光密度直线绘制
  872. tools[(int)DrawToolType.OpticalDensityLine] = typeof(ToolOpticalDensityLine);
  873. //划痕处理
  874. tools[(int)DrawToolType.DrawScratchTreatmentLine] = typeof(ToolScratchTreatmentLine);
  875. //污迹处理-矩形
  876. tools[(int)DrawToolType.DrawSmudgeRectangle] = typeof(ToolSmudgeRectangle);
  877. //污迹处理-多边形
  878. tools[(int)DrawToolType.DrawSmudgePolygon] = typeof(ToolSmudgePolygon);
  879. //污迹处理-圆形
  880. tools[(int)DrawToolType.DrawSmudgeCircle] = typeof(ToolSmudgeCircle);
  881. //污迹处理-椭圆
  882. tools[(int)DrawToolType.DrawSmudgeEllipse] = typeof(ToolSmudgeEllipse);
  883. //吸管工具
  884. tools[(int)DrawToolType.ColorPicker] = typeof(ColorPickerTool);
  885. //null
  886. tools[(int)DrawToolType.NullTool] = typeof(ToolNull);
  887. // 图像拼接-矩形
  888. tools[(int)DrawToolType.DrawStitchingRectangle] = typeof(ToolStitchingRectangle);
  889. // 图像拼接-圆形
  890. tools[(int)DrawToolType.DrawStitchingCircle] = typeof(ToolStitchingCircle);
  891. // 图像拼接-多边形
  892. tools[(int)DrawToolType.DrawStitchingPolygon] = typeof(ToolStitchingPolygon);
  893. // 工艺图对照点矩形
  894. tools[(int)DrawToolType.DrawArtworkRectangle] = typeof(ToolArtworkRectangle);
  895. // 位置列表十字线
  896. tools[(int)DrawToolType.DrawLocationCross] = typeof(ToolLocationCross);
  897. }
  898. public MeasurementUnit Units
  899. {
  900. get
  901. {
  902. return this.leftRuler.MeasurementUnit;
  903. }
  904. set
  905. {
  906. OnUnitsChanging();
  907. this.leftRuler.MeasurementUnit = value;
  908. this.topRuler.MeasurementUnit = value;
  909. DocumentMetaDataChangedHandler(this, EventArgs.Empty);
  910. OnUnitsChanged();
  911. }
  912. }
  913. protected virtual void OnUnitsChanging()
  914. {
  915. }
  916. protected virtual void OnUnitsChanged()
  917. {
  918. }
  919. /// <summary>
  920. /// 获取系统当前选中单位及每单位像素值
  921. /// </summary>
  922. /// <returns>3位字符串数组,
  923. /// 0:系统选中单位枚举字符串
  924. /// 1:系统选中单位名称字符串
  925. /// 2:系统选中单位符号字符串
  926. /// 3:系统选中单位每单位像素长度
  927. /// 4:系统选中单位每单位物理长度
  928. /// </returns>
  929. protected virtual string[] startUpRules(Dictionary<MeasurementUnit, double> rules)
  930. {
  931. return new string[] { };
  932. }
  933. public bool DrawGrid
  934. {
  935. get
  936. {
  937. return this.gridRenderer.Visible;
  938. }
  939. set
  940. {
  941. /**
  942. if (this.gridRenderer.Visible != value)
  943. {
  944. this.gridRenderer.Visible = value;
  945. OnDrawGridChanged();
  946. }**/
  947. }
  948. }
  949. /// <summary>
  950. /// 获取标尺集合
  951. /// </summary>
  952. /// <returns></returns>
  953. protected virtual List<mic_rulers> Mic_rulersAll()
  954. {
  955. return new List<mic_rulers>();
  956. }
  957. [Browsable(false)]
  958. public override bool Focused
  959. {
  960. get
  961. {
  962. return base.Focused || panel.Focused || leftRuler.Focused || topRuler.Focused;
  963. }
  964. }
  965. public new BorderStyle BorderStyle
  966. {
  967. get
  968. {
  969. return this.panel.BorderStyle;
  970. }
  971. set
  972. {
  973. this.panel.BorderStyle = value;
  974. }
  975. }
  976. private void Renderers_Invalidated(object sender, InvalidateEventArgs e)
  977. {
  978. Rectangle rect = SurfaceToClient(e.InvalidRect);
  979. rect.Inflate(1, 1);
  980. Invalidate(rect);
  981. }
  982. /// <summary>
  983. /// 计算像素跟踪的点
  984. /// </summary>
  985. /// <param name="point"></param>
  986. /// <returns></returns>
  987. public Point CalcPixelPoint(Point point)
  988. {
  989. Rectangle rc = this.panel.ClientRectangle;
  990. int width = (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  991. int height = (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  992. int x = (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  993. int y = (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  994. point.X -= x;
  995. point.Y -= y;
  996. return this.ScaleFactor.UnscalePoint(point);
  997. }
  998. public Surface GetDoubleBuffer(Size size)
  999. {
  1000. Surface localDBSurface = null;
  1001. Size oldSize = new Size(0, 0);
  1002. // If we already have a double buffer surface reference, but if that surface
  1003. // is already disposed then don't worry about it.
  1004. if (this.doubleBufferSurface != null && this.doubleBufferSurface.IsDisposed)
  1005. {
  1006. oldSize = this.doubleBufferSurface.Size;
  1007. this.doubleBufferSurface = null;
  1008. }
  1009. // If we already have a double buffer surface reference, but if that surface
  1010. // is too small, then nuke it.
  1011. if (this.doubleBufferSurface != null &&
  1012. (this.doubleBufferSurface.Width < size.Width || this.doubleBufferSurface.Height < size.Height))
  1013. {
  1014. oldSize = this.doubleBufferSurface.Size;
  1015. this.doubleBufferSurface.Dispose();
  1016. this.doubleBufferSurface = null;
  1017. doubleBufferSurfaceWeakRef = null;
  1018. }
  1019. // If we don't have a double buffer, then we'd better get one.
  1020. if (this.doubleBufferSurface != null)
  1021. {
  1022. // Got one!
  1023. localDBSurface = this.doubleBufferSurface;
  1024. }
  1025. else if (doubleBufferSurfaceWeakRef != null)
  1026. {
  1027. // First, try to get the one that's already shared amongst all SurfaceBox instances.
  1028. localDBSurface = doubleBufferSurfaceWeakRef.Target;
  1029. // If it's disposed, then forget about it.
  1030. if (localDBSurface != null && localDBSurface.IsDisposed)
  1031. {
  1032. oldSize = localDBSurface.Size;
  1033. localDBSurface = null;
  1034. doubleBufferSurfaceWeakRef = null;
  1035. }
  1036. }
  1037. // Make sure the surface is big enough.
  1038. if (localDBSurface != null && (localDBSurface.Width < size.Width || localDBSurface.Height < size.Height))
  1039. {
  1040. oldSize = localDBSurface.Size;
  1041. localDBSurface.Dispose();
  1042. localDBSurface = null;
  1043. doubleBufferSurfaceWeakRef = null;
  1044. }
  1045. // So, do we have a surface? If not then we'd better make one.
  1046. if (localDBSurface == null)
  1047. {
  1048. Size newSize = new Size(Math.Max(size.Width, oldSize.Width), Math.Max(size.Height, oldSize.Height));
  1049. localDBSurface = new Surface(newSize.Width, newSize.Height);
  1050. doubleBufferSurfaceWeakRef = new WeakReference<Surface>(localDBSurface);
  1051. }
  1052. this.doubleBufferSurface = localDBSurface;
  1053. Surface window = localDBSurface.CreateWindow(0, 0, size.Width, size.Height);
  1054. return window;
  1055. }
  1056. private class RenderContext
  1057. {
  1058. public Surface[] windows;
  1059. public Point[] offsets;
  1060. public Rectangle[] rects;
  1061. public DocumentView owner;
  1062. public WaitCallback waitCallback;
  1063. public void RenderThreadMethod(object indexObject)
  1064. {
  1065. int index = (int)indexObject;
  1066. this.owner.rendererList.Render(windows[index], offsets[index]);
  1067. this.windows[index].Dispose();
  1068. this.windows[index] = null;
  1069. }
  1070. }
  1071. public unsafe void DrawArea(RenderArgs ra, Point offset)
  1072. {
  1073. if (compositionSurface == null)
  1074. {
  1075. return;
  1076. }
  1077. if (renderContext == null || (renderContext.windows != null && renderContext.windows.Length != Processor.LogicalCpuCount))
  1078. {
  1079. renderContext = new RenderContext();
  1080. //这里需要计算宽高
  1081. renderContext.owner = this;
  1082. renderContext.waitCallback = new WaitCallback(renderContext.RenderThreadMethod);
  1083. renderContext.windows = new Surface[Processor.LogicalCpuCount];
  1084. renderContext.offsets = new Point[Processor.LogicalCpuCount];
  1085. renderContext.rects = new Rectangle[Processor.LogicalCpuCount];
  1086. }
  1087. Utility.SplitRectangle(ra.Bounds, renderContext.rects);
  1088. for (int i = 0; i < renderContext.rects.Length; ++i)
  1089. {
  1090. if (renderContext.rects[i].Width > 0 && renderContext.rects[i].Height > 0)
  1091. {
  1092. renderContext.offsets[i] = new Point(renderContext.rects[i].X + offset.X, renderContext.rects[i].Y + offset.Y);
  1093. renderContext.windows[i] = ra.Surface.CreateWindow(renderContext.rects[i]);
  1094. //renderContext.windows[i] = ra.Surface.CreateWindow(new Rectangle(renderContext.offsets[i], renderContext.rects[i].Size));
  1095. }
  1096. else
  1097. {
  1098. renderContext.windows[i] = null;
  1099. }
  1100. }
  1101. for (int i = 0; i < renderContext.windows.Length; ++i)
  1102. {
  1103. if (renderContext.windows[i] != null)
  1104. {
  1105. this.threadPool.QueueUserWorkItem(renderContext.waitCallback, BoxedConstants.GetInt32(i));
  1106. }
  1107. }
  1108. try
  1109. {
  1110. this.threadPool.Drain();
  1111. }
  1112. catch { }
  1113. }
  1114. protected override void OnLoad(EventArgs e)
  1115. {
  1116. base.OnLoad(e);
  1117. // Sometimes OnLoad() gets called *twice* for some reason.
  1118. // See bug #1415 for the symptoms.
  1119. /**if (!this.hookedMouseEvents)
  1120. {
  1121. this.hookedMouseEvents = true;
  1122. foreach (Control c in Controls)
  1123. {
  1124. HookMouseEvents(c);
  1125. }
  1126. }**/
  1127. this.panel.Select();
  1128. }
  1129. public void HookMouseEvents()
  1130. {
  1131. if (!this.hookedMouseEvents)
  1132. {
  1133. this.hookedMouseEvents = true;
  1134. foreach (Control c in Controls)
  1135. {
  1136. HookMouseEvents(c);
  1137. }
  1138. }
  1139. }
  1140. public void PerformMouseWheel(Control sender, MouseEventArgs e)
  1141. {
  1142. HandleMouseWheel(sender, e);
  1143. }
  1144. protected override void OnMouseWheel(MouseEventArgs e)
  1145. {
  1146. HandleMouseWheel(this, e);
  1147. base.OnMouseWheel(e);
  1148. }
  1149. /// <summary>
  1150. /// 鼠标滚轮事件
  1151. /// </summary>
  1152. /// <param name="sender"></param>
  1153. /// <param name="e"></param>
  1154. protected virtual void HandleMouseWheel(Control sender, MouseEventArgs e)
  1155. {
  1156. double docDelta = (double)e.Delta / this.ScaleFactor.Ratio;
  1157. double oldX = this.DocumentScrollPositionF.X;
  1158. double oldY = this.DocumentScrollPositionF.Y;
  1159. double newX;
  1160. double newY;
  1161. if (Control.ModifierKeys == Keys.Shift)
  1162. {
  1163. this.panel.Response = false;
  1164. // scroll horizontally
  1165. newX = this.DocumentScrollPositionF.X - docDelta;
  1166. newY = this.DocumentScrollPositionF.Y;
  1167. }
  1168. else if (Control.ModifierKeys == Keys.None)
  1169. {
  1170. this.panel.Response = true;
  1171. // scroll vertically
  1172. newX = this.DocumentScrollPositionF.X;
  1173. newY = this.DocumentScrollPositionF.Y - docDelta;
  1174. }
  1175. else
  1176. {
  1177. this.panel.Response = false;
  1178. // no change
  1179. newX = this.DocumentScrollPositionF.X;
  1180. newY = this.DocumentScrollPositionF.Y;
  1181. }
  1182. if (newX != oldX || newY != oldY)
  1183. {
  1184. this.DocumentScrollPositionF = new PointF((float)newX, (float)newY);
  1185. UpdateRulerOffsets();
  1186. }
  1187. }
  1188. public override bool IsMouseCaptured()
  1189. {
  1190. return panel.Capture || leftRuler.Capture || topRuler.Capture;//this.Capture ||
  1191. }
  1192. /// <summary>
  1193. /// Get or set upper left of scroll location in document coordinates.
  1194. /// </summary>
  1195. [Browsable(false)]
  1196. public PointF DocumentScrollPositionF
  1197. {
  1198. get
  1199. {
  1200. if (this.panel == null)
  1201. {
  1202. return PointF.Empty;
  1203. }
  1204. else
  1205. {
  1206. return this.panel.ScrollPosition;
  1207. //return VisibleDocumentRectangleF.Location;
  1208. }
  1209. }
  1210. set
  1211. {
  1212. if (panel == null)
  1213. {
  1214. return;
  1215. }
  1216. else
  1217. {
  1218. this.panel.ScrollPosition = new Point((int)value.X, (int)value.Y);
  1219. }
  1220. /**
  1221. PointF sbClientF = this.panel.SurfaceToClient(value);
  1222. Point sbClient = Point.Round(sbClientF);
  1223. if (this.panel.AutoScrollPosition != new Point(-sbClient.X, -sbClient.Y))
  1224. {
  1225. this.panel.AutoScrollPosition = sbClient;
  1226. UpdateRulerOffsets();
  1227. this.topRuler.Invalidate();
  1228. this.leftRuler.Invalidate();
  1229. }**/
  1230. }
  1231. }
  1232. public Point PanelScrollPosition
  1233. {
  1234. get
  1235. {
  1236. return this.panel.ScrollPosition;
  1237. }
  1238. set
  1239. {
  1240. this.panel.ScrollPosition = value;
  1241. }
  1242. }
  1243. [Browsable(false)]
  1244. public PointF DocumentCenterPointF
  1245. {
  1246. get
  1247. {
  1248. RectangleF vsb = VisibleDocumentRectangleF;
  1249. PointF centerPt = new PointF((vsb.Left + vsb.Right) / 2, (vsb.Top + vsb.Bottom) / 2);
  1250. return centerPt;
  1251. }
  1252. set
  1253. {
  1254. RectangleF vsb = VisibleDocumentRectangleF;
  1255. PointF newCornerPt = new PointF(value.X - (vsb.Width / 2), value.Y - (vsb.Height / 2));
  1256. this.DocumentScrollPositionF = newCornerPt;
  1257. }
  1258. }
  1259. /// <summary>
  1260. /// Clean up any resources being used.
  1261. /// </summary>
  1262. protected override void Dispose(bool disposing)
  1263. {
  1264. if (disposing)
  1265. {
  1266. if (this.components != null)
  1267. {
  1268. this.components.Dispose();
  1269. this.components = null;
  1270. }
  1271. if (this.compositionSurface != null)
  1272. {
  1273. this.compositionSurface.Dispose();
  1274. this.compositionSurface = null;
  1275. }
  1276. }
  1277. base.Dispose(disposing);
  1278. }
  1279. /// <summary>
  1280. /// 缩放比例改变事件
  1281. /// </summary>
  1282. public event EventHandler ScaleFactorChanged;
  1283. protected virtual void OnScaleFactorChanged()
  1284. {
  1285. if (ScaleFactorChanged != null)
  1286. {
  1287. ScaleFactorChanged(this, EventArgs.Empty);
  1288. }
  1289. }
  1290. /// <summary>
  1291. /// 像素网格是否绘制的bool标记改变事件
  1292. /// </summary>
  1293. public event EventHandler DrawGridChanged;
  1294. protected virtual void OnDrawGridChanged()
  1295. {
  1296. if (DrawGridChanged != null)
  1297. {
  1298. DrawGridChanged(this, EventArgs.Empty);
  1299. }
  1300. }
  1301. /// <summary>
  1302. /// 合适大小
  1303. /// </summary>
  1304. public void ZoomToWindow()
  1305. {
  1306. if (this.document != null)
  1307. {
  1308. Rectangle max = ClientRectangleMax;
  1309. ScaleFactor zoom = ScaleFactor.Min(max.Width - 10,
  1310. document.Width,
  1311. max.Height - 10,
  1312. document.Height,
  1313. ScaleFactor.MinValue);
  1314. ScaleFactor min = ScaleFactor.Min(zoom, ScaleFactor.OneToOne);
  1315. this.ScaleFactor = min;
  1316. }
  1317. }
  1318. /// <summary>
  1319. /// 合适宽度
  1320. /// </summary>
  1321. public void ZoomToWidth()
  1322. {
  1323. if (this.document != null)
  1324. {
  1325. Rectangle max = ClientRectangleMax;
  1326. ScaleFactor zoom = ScaleFactor.UseIfValid(max.Width - 40, document.Width, ScaleFactor.MinValue);
  1327. ScaleFactor min = ScaleFactor.Min(zoom, ScaleFactor.OneToOne);
  1328. this.ScaleFactor = min;
  1329. }
  1330. }
  1331. /// <summary>
  1332. /// 合适高度
  1333. /// </summary>
  1334. public void ZoomToHeight()
  1335. {
  1336. if (this.document != null)
  1337. {
  1338. Rectangle max = ClientRectangleMax;
  1339. ScaleFactor zoom = ScaleFactor.UseIfValid(max.Height - 20, document.Height, ScaleFactor.MinValue);
  1340. ScaleFactor min = ScaleFactor.Min(zoom, ScaleFactor.OneToOne);
  1341. this.ScaleFactor = min;
  1342. }
  1343. }
  1344. private double GetZoomInFactorEpsilon()
  1345. {
  1346. double ratio = this.ScaleFactor.Ratio;
  1347. return (ratio + 0.01) / ratio;
  1348. /**
  1349. // Increase ratio by 1 percentage point
  1350. double currentRatio = this.ScaleFactor.Ratio;
  1351. double factor1 = (currentRatio + 0.01) / currentRatio;
  1352. // Increase ratio so that we increase our view by 1 pixel
  1353. double ratioW = (double)(this.panel.Width + 1) / (double)this.compositionSurface.Width;
  1354. double ratioH = (double)(this.panel.Height + 1) / (double)this.compositionSurface.Height;
  1355. double ratio = Math.Max(ratioW, ratioH);
  1356. double factor2 = ratio / currentRatio;
  1357. double factor = Math.Max(factor1, factor2);
  1358. return factor;
  1359. **/
  1360. }
  1361. private double GetZoomOutFactorEpsilon()
  1362. {
  1363. double ratio = this.ScaleFactor.Ratio;
  1364. return (ratio - 0.01) / ratio;
  1365. }
  1366. public virtual void ZoomIn(double factor)
  1367. {
  1368. Do.TryBool(() => ZoomInImpl(factor));
  1369. }
  1370. private void ZoomInImpl(double factor)
  1371. {
  1372. PointF centerPt = this.DocumentCenterPointF;
  1373. ScaleFactor oldSF = this.ScaleFactor;
  1374. ScaleFactor newSF = this.ScaleFactor;
  1375. int countdown = 3;
  1376. // At a minimum we want to increase the size of visible document by 1 pixel
  1377. // Figure out what the ratio of ourSize : ourSize+1 is, and start out with that
  1378. double zoomInEps = GetZoomInFactorEpsilon();
  1379. double desiredFactor = Math.Max(factor, zoomInEps);
  1380. double newFactor = desiredFactor;
  1381. // Keep setting the ScaleFactor until it actually 'sticks'
  1382. // Important for certain image sizes where not all zoom levels create distinct
  1383. // screen sizes
  1384. do
  1385. {
  1386. newSF = ScaleFactor.FromDouble(newSF.Ratio * newFactor);
  1387. this.ScaleFactor = newSF;
  1388. --countdown;
  1389. newFactor *= 1.10;
  1390. } while (this.ScaleFactor == oldSF && countdown > 0);
  1391. this.DocumentCenterPointF = centerPt;
  1392. }
  1393. public virtual void ZoomIn()
  1394. {
  1395. Do.TryBool(ZoomInImpl);
  1396. }
  1397. private void ZoomInImpl()
  1398. {
  1399. PointF centerPt = this.DocumentCenterPointF;
  1400. ScaleFactor oldSF = this.ScaleFactor;
  1401. ScaleFactor newSF = this.ScaleFactor;
  1402. int countdown = ScaleFactor.PresetValues.Length;
  1403. // Keep setting the ScaleFactor until it actually 'sticks'
  1404. // Important for certain image sizes where not all zoom levels create distinct
  1405. // screen sizes
  1406. do
  1407. {
  1408. newSF = newSF.GetNextLarger();
  1409. this.ScaleFactor = newSF;
  1410. --countdown;
  1411. } while (this.ScaleFactor == oldSF && countdown > 0);
  1412. this.DocumentCenterPointF = centerPt;
  1413. }
  1414. public virtual void ZoomOut(double factor)
  1415. {
  1416. Do.TryBool(() => ZoomOutImpl(factor));
  1417. }
  1418. private void ZoomOutImpl(double factor)
  1419. {
  1420. PointF centerPt = this.DocumentCenterPointF;
  1421. ScaleFactor oldSF = this.ScaleFactor;
  1422. ScaleFactor newSF = this.ScaleFactor;
  1423. int countdown = 3;
  1424. // At a minimum we want to decrease the size of visible document by 1 pixel (without dividing by zero of course)
  1425. // Figure out what the ratio of ourSize : ourSize-1 is, and start out with that
  1426. double zoomOutEps = GetZoomOutFactorEpsilon();
  1427. double factorRecip = 1.0 / factor;
  1428. double desiredFactor = Math.Min(factorRecip, zoomOutEps);
  1429. double newFactor = desiredFactor;
  1430. // Keep setting the ScaleFactor until it actually 'sticks'
  1431. // Important for certain image sizes where not all zoom levels create distinct
  1432. // screen sizes
  1433. do
  1434. {
  1435. newSF = ScaleFactor.FromDouble(newSF.Ratio * newFactor);
  1436. this.ScaleFactor = newSF;
  1437. --countdown;
  1438. newFactor *= 0.9;
  1439. } while (this.ScaleFactor == oldSF && countdown > 0);
  1440. this.DocumentCenterPointF = centerPt;
  1441. }
  1442. public virtual void ZoomOut()
  1443. {
  1444. Do.TryBool(ZoomOutImpl);
  1445. }
  1446. private void ZoomOutImpl()
  1447. {
  1448. PointF centerPt = this.DocumentCenterPointF;
  1449. ScaleFactor oldSF = this.ScaleFactor;
  1450. ScaleFactor newSF = this.ScaleFactor;
  1451. int countdown = ScaleFactor.PresetValues.Length;
  1452. // Keep setting the ScaleFactor until it actually 'sticks'
  1453. // Important for certain image sizes where not all zoom levels create distinct
  1454. // screen sizes
  1455. do
  1456. {
  1457. newSF = newSF.GetNextSmaller();
  1458. this.ScaleFactor = newSF;
  1459. --countdown;
  1460. } while (this.ScaleFactor == oldSF && countdown > 0);
  1461. this.DocumentCenterPointF = centerPt;
  1462. }
  1463. [Browsable(false)]
  1464. public ScaleFactor ScaleFactor
  1465. {
  1466. get
  1467. {
  1468. return this.scaleFactor;
  1469. }
  1470. set
  1471. {
  1472. UI.SuspendControlPainting(this);
  1473. ScaleFactor newValue = ScaleFactor.Min(value, ScaleFactor.MaxValue);
  1474. if (newValue == this.scaleFactor &&
  1475. this.scaleFactor == ScaleFactor.OneToOne)
  1476. {
  1477. // this space intentionally left blank
  1478. }
  1479. else
  1480. {
  1481. RectangleF visibleRect = this.VisibleDocumentRectangleF;
  1482. ScaleFactor oldSF = scaleFactor;
  1483. scaleFactor = newValue;
  1484. // This value is used later below to re-center the document on screen
  1485. PointF centerPt = new PointF(visibleRect.X + visibleRect.Width / 2,
  1486. visibleRect.Y + visibleRect.Height / 2);
  1487. if (compositionSurface != null)
  1488. {
  1489. Rectangle rc = this.panel.ClientRectangle;
  1490. int width = (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  1491. int height = (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  1492. if (rc.Width < width || rc.Height < height)
  1493. {
  1494. this.panel.AutoScrollMinSize = new Size((int)(this.compositionSurface.Width * scaleFactor.Ratio) + offsetW, (int)(this.compositionSurface.Height * scaleFactor.Ratio) + offsetH);
  1495. //////求因缩放产生的位移,进行补偿,实现锚点缩放的效果
  1496. ////VX = (int)((double)x * (ow - pictureBox1.Width) / ow);
  1497. ////VY = (int)((double)y * (oh - pictureBox1.Height) / oh);
  1498. ////pictureBox1.Location = new Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
  1499. //this.panel.AutoScrollPosition = new Point((int)(width - rc.Width) / 2 + 150, (int)(height - rc.Height) / 2 + 75);
  1500. //////this.panel.AutoScrollPosition = sbClient;
  1501. ////UpdateRulerOffsets();
  1502. ////this.topRuler.Invalidate();
  1503. ////this.leftRuler.Invalidate();
  1504. }
  1505. else
  1506. {
  1507. this.panel.AutoScrollMinSize = new Size((int)(this.compositionSurface.Width * scaleFactor.Ratio), (int)(this.compositionSurface.Height * scaleFactor.Ratio));
  1508. //this.panel.AutoScrollPosition = new Point((int)(width - rc.Width) / 2 + 0, (int)(height - rc.Height) / 2 + 0);
  1509. ////UpdateRulerOffsets();
  1510. ////this.topRuler.Invalidate();
  1511. ////this.leftRuler.Invalidate();
  1512. }
  1513. this.panel.ScaleFactor = this.scaleFactor;
  1514. if (leftRuler != null)
  1515. {
  1516. this.leftRuler.ScaleFactor = scaleFactor;
  1517. }
  1518. if (topRuler != null)
  1519. {
  1520. this.topRuler.ScaleFactor = scaleFactor;
  1521. }
  1522. }
  1523. // re center ourself
  1524. RectangleF visibleRect2 = this.VisibleDocumentRectangleF;
  1525. RecenterView(centerPt);
  1526. }
  1527. this.OnResize(EventArgs.Empty);
  1528. this.OnScaleFactorChanged();
  1529. if (this.appWorkspace != null && refueshZoomTrackValue && this.rightDown)
  1530. {
  1531. this.appWorkspace.SetZoonTrackValue(newValue);
  1532. }
  1533. /*
  1534. //设置底部缩放比列的百分比tracker的值
  1535. this.panelBottom.trackBar.Maximum = (int)(ScaleFactor.MaxValue.Ratio * 100);
  1536. if (newValue != null)
  1537. {
  1538. this.PanelBottom.trackBar.ValueChanged -= this.PanelBottom_trackBar_ValueChanged;
  1539. this.PanelBottom.trackBar.Value = (int)(newValue.Ratio * 100);
  1540. this.PanelBottom.trackBar.ValueChanged += new EventHandler(this.PanelBottom_trackBar_ValueChanged);
  1541. }
  1542. //设置底部缩放比例的百分比textbox的值
  1543. this.PanelBottom.textBox.Text = newValue.ToString();
  1544. */
  1545. if (compositionSurface != null)
  1546. this.rendererList.DestinationSize = this.scaleFactor.ScaleSize(compositionSurface.Size);
  1547. UI.ResumeControlPainting(this);
  1548. Invalidate(true);
  1549. }
  1550. }
  1551. /// <summary>
  1552. /// Returns a rectangle for the bounding rectangle of what is currently visible on screen,
  1553. /// in document coordinates.
  1554. /// </summary>
  1555. [Browsable(false)]
  1556. public RectangleF VisibleDocumentRectangleF
  1557. {
  1558. get
  1559. {
  1560. Rectangle panelRect = this.panel.RectangleToScreen(this.panel.ClientRectangle); // screen coords coords
  1561. Rectangle docScreenRect = panelRect; // screen coords
  1562. Rectangle docClientRect = RectangleToClient(docScreenRect);
  1563. RectangleF docDocRectF = ClientToDocument(docClientRect);
  1564. return docDocRectF;
  1565. }
  1566. }
  1567. public Rectangle PanelClientRectangle
  1568. {
  1569. get
  1570. {
  1571. return panel.ClientRectangle;
  1572. }
  1573. }
  1574. public int SurfaceScrollableWidth
  1575. {
  1576. get
  1577. {
  1578. return (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  1579. }
  1580. }
  1581. public int SurfaceScrollableHeight
  1582. {
  1583. get
  1584. {
  1585. return (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  1586. }
  1587. }
  1588. public double ScaleRatio
  1589. {
  1590. get
  1591. {
  1592. return this.scaleFactor.Ratio;
  1593. }
  1594. }
  1595. /// <summary>
  1596. /// Returns a rectangle in <b>screen</b> coordinates that represents the space taken up
  1597. /// by the document that is visible on screen.
  1598. /// </summary>
  1599. [Browsable(false)]
  1600. public Rectangle VisibleDocumentBounds
  1601. {
  1602. get
  1603. {
  1604. // convert coordinates: document -> client -> screen
  1605. return RectangleToScreen(Utility.RoundRectangle(DocumentToClient(VisibleDocumentRectangleF)));
  1606. }
  1607. }
  1608. /// <summary>
  1609. /// Returns a rectangle in client coordinates that denotes the space that the document
  1610. /// may take up. This is essentially the ClientRectangle converted to screen coordinates
  1611. /// and then with the rulers and scrollbars subtracted out.
  1612. /// </summary>
  1613. public Rectangle VisibleViewRectangle
  1614. {
  1615. get
  1616. {
  1617. Rectangle clientRect = this.panel.ClientRectangle;
  1618. Rectangle screenRect = this.panel.RectangleToScreen(clientRect);
  1619. Rectangle ourClientRect = RectangleToClient(screenRect);
  1620. return ourClientRect;
  1621. }
  1622. }
  1623. public Rectangle ClientRectangleMax
  1624. {
  1625. get
  1626. {
  1627. return RectangleToClient(this.panel.RectangleToScreen(this.panel.Bounds));
  1628. }
  1629. }
  1630. public Rectangle ClientRectangleMin
  1631. {
  1632. get
  1633. {
  1634. Rectangle bounds = ClientRectangleMax;
  1635. bounds.Width -= SystemInformation.VerticalScrollBarWidth;
  1636. bounds.Height -= SystemInformation.HorizontalScrollBarHeight;
  1637. return bounds;
  1638. }
  1639. }
  1640. public void SetHighlightRectangle(RectangleF rectF)
  1641. {
  1642. if (rectF.Width == 0 || rectF.Height == 0)
  1643. {
  1644. this.leftRuler.HighlightEnabled = false;
  1645. this.topRuler.HighlightEnabled = false;
  1646. }
  1647. else
  1648. {
  1649. if (this.topRuler != null)
  1650. {
  1651. this.topRuler.HighlightEnabled = true;
  1652. this.topRuler.HighlightStart = rectF.Left;
  1653. this.topRuler.HighlightLength = rectF.Width;
  1654. }
  1655. if (this.leftRuler != null)
  1656. {
  1657. this.leftRuler.HighlightEnabled = true;
  1658. this.leftRuler.HighlightStart = rectF.Top;
  1659. this.leftRuler.HighlightLength = rectF.Height;
  1660. }
  1661. }
  1662. }
  1663. /// <summary>
  1664. /// Gets or sets the Document that is shown through this instance of DocumentView.
  1665. /// </summary>
  1666. /// <remarks>
  1667. /// This property is thread safe and may be called from a non-UI thread. However,
  1668. /// if the setter is called from a non-UI thread, then that thread will block as
  1669. /// the call is marshaled to the UI thread.
  1670. /// </remarks>
  1671. [Browsable(false)]
  1672. public Document Document
  1673. {
  1674. get
  1675. {
  1676. return document;
  1677. }
  1678. set
  1679. {
  1680. if (InvokeRequired)
  1681. {
  1682. this.Invoke(new Procedure<Document, bool>(DocumentSetImpl), new object[2] { value, true });
  1683. }
  1684. else
  1685. {
  1686. DocumentSetImpl(value, true);
  1687. }
  1688. }
  1689. }
  1690. public void setDoc(Document document, bool toeditori)
  1691. {
  1692. if (InvokeRequired)
  1693. {
  1694. this.Invoke(new Procedure<Document, bool>(DocumentSetImpl), new object[2] { document, toeditori });
  1695. }
  1696. else
  1697. {
  1698. DocumentSetImpl(document, toeditori);
  1699. }
  1700. }
  1701. private void DocumentSetImpl(Document value, bool toedit)
  1702. {
  1703. this.DoubleBuffered = true;
  1704. SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
  1705. PointF dspf = DocumentScrollPositionF;
  1706. OnDocumentChanging(value);
  1707. SuspendRefresh();
  1708. try
  1709. {
  1710. if (this.document != null)
  1711. {
  1712. this.document.Invalidated -= Document_Invalidated;
  1713. this.document.Metadata.Changed -= DocumentMetaDataChangedHandler;
  1714. }
  1715. this.document = value;
  1716. if (document != null)
  1717. {
  1718. if (this.compositionSurface != null &&
  1719. this.compositionSurface.Size != document.Size)
  1720. {
  1721. this.compositionSurface.Dispose();
  1722. this.compositionSurface = null;
  1723. }
  1724. //if (this.compositionSurface == null)
  1725. //{
  1726. //this.compositionSurface = new Surface(Document.Size);
  1727. if (!toedit && this.compositionSurface.Thumborigin != null)
  1728. document.surface.Thumborigin = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(OpenCvSharp.Extensions.BitmapConverter.ToMat(this.compositionSurface.Thumborigin));
  1729. this.compositionSurface = document.surface;//((BitmapLayer)(document.Layers[0])).Surface;
  1730. //生成缩略图
  1731. this.compositionSurface.CreateThumbnail();
  1732. //生成原图的备份
  1733. if (toedit)
  1734. this.compositionSurface.CreateThumborigin();
  1735. //}
  1736. this.baseRenderer.Source = document.surface; //((BitmapLayer)(document.Layers[0])).Surface;
  1737. if (this.compositionSurface != null)
  1738. {
  1739. // Maintain the scalefactor
  1740. /*this.Size = this.scaleFactor.ScaleSize(compositionSurface.Size);*/
  1741. this.rendererList.SourceSize = this.compositionSurface.Size;
  1742. this.rendererList.DestinationSize = this.Size;
  1743. }
  1744. this.document.Invalidated += Document_Invalidated;
  1745. this.document.Metadata.Changed += DocumentMetaDataChangedHandler;
  1746. }
  1747. Invalidate(true);
  1748. DocumentMetaDataChangedHandler(this, EventArgs.Empty);
  1749. this.OnResize(EventArgs.Empty);
  1750. OnDocumentChanged();
  1751. }
  1752. finally
  1753. {
  1754. ResumeRefresh();
  1755. }
  1756. DocumentScrollPositionF = dspf;
  1757. }
  1758. public bool PanelAutoScroll
  1759. {
  1760. get
  1761. {
  1762. return panel.AutoScroll;
  1763. }
  1764. set
  1765. {
  1766. if (panel.AutoScroll != value)
  1767. {
  1768. panel.AutoScroll = value;
  1769. }
  1770. }
  1771. }
  1772. public DrawToolType ActiveTool
  1773. {
  1774. get => this.activeTool;
  1775. set
  1776. {
  1777. tools[(int)activeTool].InvokeMember("beginWithNewObject",
  1778. BindingFlags.Public |
  1779. BindingFlags.Static |
  1780. BindingFlags.InvokeMethod,
  1781. null,
  1782. null,
  1783. new object[0] { });
  1784. //tools[(int)this.activeTool].beginWithNewObject();
  1785. if (value != DrawToolType.Pointer && value != DrawToolType.InclusionNoEffect && this.appWorkspace.GetScriptRunning())
  1786. this.appWorkspace.SetScriptStopping(true);
  1787. else if (value == DrawToolType.Pointer)
  1788. {
  1789. this.appWorkspace.SetScriptStopping(false);
  1790. //this.appWorkspace.ResumeScriptRunning();
  1791. }
  1792. if (value == DrawToolType.DrawGainNumber || value == DrawToolType.DrawDateMark
  1793. || value == DrawToolType.DrawTimeMark || value == DrawToolType.DrawWaterMark
  1794. || value == DrawToolType.DrawAutoRuler || value == DrawToolType.DrawPrestoredRuler)
  1795. {
  1796. this.activeTool = value;
  1797. tools[(int)activeTool].InvokeMember("OnMouseDown",
  1798. BindingFlags.Public |
  1799. BindingFlags.Static |
  1800. BindingFlags.InvokeMethod,
  1801. null,
  1802. null,
  1803. new object[2] { this, null });
  1804. this.activeTool = DrawToolType.Pointer;
  1805. //tools[(int)DrawToolType.DrawGainNumber].OnMouseDown(this, null);
  1806. }
  1807. else
  1808. {
  1809. this.activeTool = value;
  1810. }
  1811. appWorkspace.SetDrawToolType(this.activeTool);
  1812. }
  1813. }
  1814. private void HookMouseEvents(Control c)
  1815. {
  1816. /**
  1817. if (this.inkAvailable)
  1818. {
  1819. // This must be in a separate function, otherwise we will throw an exception when JITting
  1820. // because MS.Ink.dll won't be available
  1821. // This is to support systems that don't have ink installed
  1822. try
  1823. {
  1824. Ink.HookInk(this, c);
  1825. }
  1826. catch (InvalidOperationException ioex)
  1827. {
  1828. Tracing.Ping("Exception while initializing ink hooks: " + ioex.ToString());
  1829. this.inkAvailable = false;
  1830. }
  1831. }**/
  1832. //暂时注释掉
  1833. /**
  1834. c.MouseEnter += new EventHandler(this.MouseEnterHandler);
  1835. c.MouseLeave += new EventHandler(this.MouseLeaveHandler);
  1836. c.MouseUp += new MouseEventHandler(this.MouseUpHandler);
  1837. c.MouseMove += new MouseEventHandler(this.MouseMoveHandler);
  1838. c.MouseDown += new MouseEventHandler(this.MouseDownHandler);
  1839. c.Click += new EventHandler(this.ClickHandler);
  1840. foreach (Control c2 in c.Controls)
  1841. {
  1842. HookMouseEvents(c2);
  1843. }**/
  1844. }
  1845. private void UpdateRulerOffsets()
  1846. {
  1847. this.topRuler.Offset = ScaleFactor.UnscaleScalar(UI.ScaleWidth(-16.0f) - this.panel.Location.X);
  1848. this.topRuler.Update();
  1849. this.leftRuler.Offset = ScaleFactor.UnscaleScalar(0.0f - this.panel.Location.Y);
  1850. this.leftRuler.Update();
  1851. }
  1852. private void DoLayout()
  1853. {
  1854. if (panel.ClientRectangle != new Rectangle(0, 0, 0, 0))
  1855. {
  1856. int newX = panel.AutoScrollPosition.X;
  1857. int newY = panel.AutoScrollPosition.Y;
  1858. if (panel.ClientRectangle.Width > this.panel.Width)
  1859. {
  1860. newX = panel.AutoScrollPosition.X + ((panel.ClientRectangle.Width) / 2);
  1861. }
  1862. if (panel.ClientRectangle.Height > this.panel.Height)
  1863. {
  1864. newY = panel.AutoScrollPosition.Y + ((panel.ClientRectangle.Height) / 2);
  1865. }
  1866. }
  1867. //暂时注释掉,因为在预览窗口、位置导航等显示标尺的时候,会闪烁,不知道会不会有啥问题
  1868. this.UpdateRulerOffsets();
  1869. this.UpdateImgLocation();
  1870. }
  1871. private void CheckForFirstInputAfterGotFocus()
  1872. {
  1873. if (this.raiseFirstInputAfterGotFocus)
  1874. {
  1875. this.raiseFirstInputAfterGotFocus = false;
  1876. OnFirstInputAfterGotFocus();
  1877. }
  1878. }
  1879. private void Document_Invalidated(object sender, InvalidateEventArgs e)
  1880. {
  1881. if (this.ScaleFactor == ScaleFactor.OneToOne)
  1882. {
  1883. this.panel.Invalidate(e.InvalidRect);
  1884. }
  1885. else
  1886. {
  1887. Rectangle inflatedInvalidRect = Rectangle.Inflate(e.InvalidRect, 1, 1);
  1888. Rectangle clientRect = this.panel.SurfaceToClient(inflatedInvalidRect);
  1889. Rectangle inflatedClientRect = Rectangle.Inflate(clientRect, 1, 1);
  1890. this.panel.Invalidate(inflatedClientRect);
  1891. }
  1892. }
  1893. protected void UpdateComposition(bool raiseEvent)
  1894. {
  1895. lock (this)
  1896. {
  1897. /*using (RenderArgs ra = new RenderArgs(this.compositionSurface))
  1898. {
  1899. bool result = this.document.Update(ra);
  1900. if (raiseEvent && (result || this.withheldCompositionUpdatedCount > 0))
  1901. {
  1902. OnCompositionUpdated();
  1903. if (!result && this.withheldCompositionUpdatedCount > 0)
  1904. {
  1905. --this.withheldCompositionUpdatedCount;
  1906. }
  1907. }
  1908. else if (!raiseEvent && result)
  1909. {
  1910. // If they want to not raise the event, we must keep track so that
  1911. // the next time UpdateComposition() is called we still raise this
  1912. // event even if Update() returned false (which indicates there
  1913. // was nothing to update)
  1914. ++this.withheldCompositionUpdatedCount;
  1915. }
  1916. }*/
  1917. }
  1918. }
  1919. // Note: You use the Suspend/Resume pattern to suspend and resume refreshing (it hides the controls for a brief moment)
  1920. // This is used by set_Document to avoid twitching/flickering in certain cases.
  1921. // However, you should use Resume followed by Suspend to bypass the set_Document's use of that.
  1922. // Interestingly, SaveConfigDialog does this to avoid 'blinking' when the save parameters are changed.
  1923. public void SuspendRefresh()
  1924. {
  1925. ++this.refreshSuspended;
  1926. }
  1927. public void ResumeRefresh()
  1928. {
  1929. --this.refreshSuspended;
  1930. }
  1931. /// <summary>
  1932. /// 重新设置中心点
  1933. /// </summary>
  1934. /// <param name="newCenter"></param>
  1935. public void RecenterView(PointF newCenter)
  1936. {
  1937. RectangleF visibleRect = VisibleDocumentRectangleF;
  1938. PointF cornerPt = new PointF(
  1939. newCenter.X - (visibleRect.Width / 2),
  1940. newCenter.Y - (visibleRect.Height / 2));
  1941. this.DocumentScrollPositionF = cornerPt;
  1942. }
  1943. /// <summary>
  1944. /// 文档元数据改变事件
  1945. /// </summary>
  1946. /// <param name="sender"></param>
  1947. /// <param name="e"></param>
  1948. private void DocumentMetaDataChangedHandler(object sender, EventArgs e)
  1949. {
  1950. if (this.document != null)
  1951. {
  1952. this.leftRuler.Dpu = 1 / document.PixelToPhysicalY(1, this.leftRuler.MeasurementUnit);
  1953. this.topRuler.Dpu = 1 / document.PixelToPhysicalY(1, this.topRuler.MeasurementUnit);
  1954. }
  1955. }
  1956. /// <summary>
  1957. /// 获取当前文档的视场、标注、测量的xml数据
  1958. /// </summary>
  1959. /// <param name="drawClass"></param>
  1960. /// <returns></returns>
  1961. public List<ViewModel> GetXmlFromViewOrLabelOrMeasure(DrawClass drawClass)
  1962. {
  1963. List<ViewModel> topLVMModels = new List<ViewModel>();
  1964. if (this.GraphicsList != null && this.GraphicsList.Count > 0)
  1965. {
  1966. for (int i = this.GraphicsList.Count - 1; i >= 0; i--)
  1967. {
  1968. ViewModel model = new ViewModel();
  1969. if (this.GraphicsList[i].objectType == drawClass)
  1970. {
  1971. model.Type = this.GraphicsList[i].drawToolType.ToString();
  1972. model.CombineMode = ((ViewBase)(this.GraphicsList[i])).combineMode.ToString();
  1973. model.Rectangle = this.GraphicsList[i].Rectangle;
  1974. model.Points = this.GraphicsList[i].GetPoints();
  1975. model.StartPoint = this.GraphicsList[i].startPoint;
  1976. model.EndPoint = this.GraphicsList[i].endPoint;
  1977. topLVMModels.Add(model);
  1978. }
  1979. }
  1980. }
  1981. return topLVMModels;
  1982. }
  1983. /// <summary>
  1984. /// 从打开视场窗口添加视场到DocumentView
  1985. /// </summary>
  1986. /// <param name="drawObject"></param>
  1987. public void AddGraphicsFromForm(DrawObject drawObject)
  1988. {
  1989. this.GraphicsList.UnselectAll();
  1990. this.GraphicsList.Add(drawObject);
  1991. this.Capture = true;
  1992. this.Refresh();
  1993. this.SetDirty();
  1994. }
  1995. #region 暂未用到的方法, 暂留
  1996. protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  1997. {
  1998. Keys keyCode = keyData & Keys.KeyCode;
  1999. if (Utility.IsArrowKey(keyData) ||
  2000. keyCode == Keys.Delete ||
  2001. keyCode == Keys.Tab)
  2002. {
  2003. KeyEventArgs kea = new KeyEventArgs(keyData);
  2004. // We only intercept WM_KEYDOWN because WM_KEYUP is not sent!
  2005. switch (msg.Msg)
  2006. {
  2007. case 0x100: //NativeMethods.WmConstants.WM_KEYDOWN:
  2008. if (this.ContainsFocus)
  2009. {
  2010. OnDocumentKeyDown(kea);
  2011. //OnDocumentKeyUp(kea);
  2012. if (Utility.IsArrowKey(keyData))
  2013. {
  2014. kea.Handled = true;
  2015. }
  2016. }
  2017. if (kea.Handled)
  2018. {
  2019. return true;
  2020. }
  2021. break;
  2022. /*
  2023. case 0x101: //NativeMethods.WmConstants.WM_KEYUP:
  2024. if (this.ContainsFocus)
  2025. {
  2026. OnDocumentKeyUp(kea);
  2027. }
  2028. return kea.Handled;
  2029. */
  2030. }
  2031. }
  2032. return base.ProcessCmdKey(ref msg, keyData);
  2033. }
  2034. private void MouseEnterHandler(object sender, EventArgs e)
  2035. {
  2036. OnDocumentMouseEnter(EventArgs.Empty);
  2037. }
  2038. private void MouseLeaveHandler(object sender, EventArgs e)
  2039. {
  2040. OnDocumentMouseLeave(EventArgs.Empty);
  2041. }
  2042. private void MouseMoveHandler(object sender, MouseEventArgs e)
  2043. {
  2044. Point docPoint = MouseToDocument((Control)sender, new Point(e.X, e.Y));
  2045. PointF docPointF = MouseToDocumentF((Control)sender, new Point(e.X, e.Y));
  2046. if (RulersEnabled)
  2047. {
  2048. int x;
  2049. if (docPointF.X > 0)
  2050. {
  2051. x = (int)Math.Truncate(docPointF.X);
  2052. }
  2053. else if (docPointF.X < 0)
  2054. {
  2055. x = (int)Math.Truncate(docPointF.X - 1);
  2056. }
  2057. else // if (docPointF.X == 0)
  2058. {
  2059. x = 0;
  2060. }
  2061. int y;
  2062. if (docPointF.Y > 0)
  2063. {
  2064. y = (int)Math.Truncate(docPointF.Y);
  2065. }
  2066. else if (docPointF.Y < 0)
  2067. {
  2068. y = (int)Math.Truncate(docPointF.Y - 1);
  2069. }
  2070. else // if (docPointF.Y == 0)
  2071. {
  2072. y = 0;
  2073. }
  2074. topRuler.Value = x;
  2075. leftRuler.Value = y;
  2076. UpdateRulerOffsets();
  2077. }
  2078. OnDocumentMouseMove(new MouseEventArgs(e.Button, e.Clicks, docPoint.X, docPoint.Y, e.Delta));
  2079. }
  2080. private void MouseUpHandler(object sender, MouseEventArgs e)
  2081. {
  2082. if (sender is Ruler)
  2083. {
  2084. return;
  2085. }
  2086. Point docPoint = MouseToDocument((Control)sender, new Point(e.X, e.Y));
  2087. Point pt = panel.AutoScrollPosition;
  2088. panel.Focus();
  2089. OnDocumentMouseUp(new MouseEventArgs(e.Button, e.Clicks, docPoint.X, docPoint.Y, e.Delta));
  2090. }
  2091. private void MouseDownHandler(object sender, MouseEventArgs e)
  2092. {
  2093. if (sender is Ruler)
  2094. {
  2095. return;
  2096. }
  2097. Point docPoint = MouseToDocument((Control)sender, new Point(e.X, e.Y));
  2098. Point pt = panel.AutoScrollPosition;
  2099. panel.Focus();
  2100. OnDocumentMouseDown(new MouseEventArgs(e.Button, e.Clicks, docPoint.X, docPoint.Y, e.Delta));
  2101. }
  2102. private void ClickHandler(object sender, EventArgs e)
  2103. {
  2104. Point pt = panel.AutoScrollPosition;
  2105. panel.Focus();
  2106. OnDocumentClick();
  2107. }
  2108. #endregion
  2109. #region 对外的各种事件委托
  2110. // these events will report mouse coordinates in document space
  2111. // i.e. if the image is zoomed at 200% then the mouse coordinates will be divided in half
  2112. public event EventHandler CompositionUpdated;
  2113. private void OnCompositionUpdated()
  2114. {
  2115. if (CompositionUpdated != null)
  2116. {
  2117. CompositionUpdated(this, EventArgs.Empty);
  2118. }
  2119. }
  2120. public event EventHandler RulersEnabledChanged;
  2121. protected void OnRulersEnabledChanged()
  2122. {
  2123. if (RulersEnabledChanged != null)
  2124. {
  2125. RulersEnabledChanged(this, EventArgs.Empty);
  2126. }
  2127. }
  2128. public event EventHandler<EventArgs<Document>> DocumentChanging;
  2129. protected virtual void OnDocumentChanging(Document newDocument)
  2130. {
  2131. if (DocumentChanging != null)
  2132. {
  2133. DocumentChanging(this, new EventArgs<Document>(newDocument));
  2134. }
  2135. }
  2136. public event EventHandler DocumentChanged;
  2137. protected virtual void OnDocumentChanged()
  2138. {
  2139. if (DocumentChanged != null)
  2140. {
  2141. DocumentChanged(this, EventArgs.Empty);
  2142. }
  2143. }
  2144. public event EventHandler FirstInputAfterGotFocus;
  2145. protected virtual void OnFirstInputAfterGotFocus()
  2146. {
  2147. if (FirstInputAfterGotFocus != null)
  2148. {
  2149. FirstInputAfterGotFocus(this, EventArgs.Empty);
  2150. }
  2151. }
  2152. /// <summary>
  2153. /// Occurs when the mouse enters an element of the UI that is considered to be part of
  2154. /// the document space.
  2155. /// </summary>
  2156. public event EventHandler DocumentMouseEnter;
  2157. protected virtual void OnDocumentMouseEnter(EventArgs e)
  2158. {
  2159. if (DocumentMouseEnter != null)
  2160. {
  2161. DocumentMouseEnter(this, e);
  2162. }
  2163. }
  2164. /// <summary>
  2165. /// Occurs when the mouse leaves an element of the UI that is considered to be part of
  2166. /// the document space.
  2167. /// </summary>
  2168. /// <remarks>
  2169. /// This event being raised does not necessarily correpond to the mouse leaving
  2170. /// document space, only that it has left the screen space of an element of the UI
  2171. /// that is part of document space. For example, if the mouse leaves the canvas and
  2172. /// then enters the rulers, you will see a DocumentMouseLeave event raised which is
  2173. /// then immediately followed by a DocumentMouseEnter event.
  2174. /// </remarks>
  2175. public event EventHandler DocumentMouseLeave;
  2176. protected virtual void OnDocumentMouseLeave(EventArgs e)
  2177. {
  2178. if (DocumentMouseLeave != null)
  2179. {
  2180. DocumentMouseLeave(this, e);
  2181. }
  2182. }
  2183. /// <summary>
  2184. /// Occurs when the mouse or stylus point is moved over the document.
  2185. /// </summary>
  2186. /// <remarks>
  2187. /// Note: This event will always be raised twice in succession. One will provide a
  2188. /// MouseEventArgs, and the other will provide a StylusEventArgs. It is up to consumers
  2189. /// of this event to decide which one is pertinent and to then filter out the other
  2190. /// type of event.
  2191. /// </remarks>
  2192. public event MouseEventHandler DocumentMouseMove;
  2193. protected virtual void OnDocumentMouseMove(MouseEventArgs e)
  2194. {
  2195. if (DocumentMouseMove != null)
  2196. {
  2197. DocumentMouseMove(this, e);
  2198. }
  2199. }
  2200. /// <summary>
  2201. /// Occurs when the mouse or stylus point is over the document and a mouse button is released
  2202. /// or the stylus is lifted.
  2203. /// </summary>
  2204. /// <remarks>
  2205. /// Note: This event will always be raised twice in succession. One will provide a
  2206. /// MouseEventArgs, and the other will provide a StylusEventArgs. It is up to consumers
  2207. /// of this event to decide which one is pertinent and to then filter out the other
  2208. /// type of event.
  2209. /// </remarks>
  2210. public event MouseEventHandler DocumentMouseUp;
  2211. protected virtual void OnDocumentMouseUp(MouseEventArgs e)
  2212. {
  2213. CheckForFirstInputAfterGotFocus();
  2214. if (DocumentMouseUp != null)
  2215. {
  2216. DocumentMouseUp(this, e);
  2217. }
  2218. }
  2219. /// <summary>
  2220. /// Occurs when the mouse or stylus point is over the document and a mouse button or
  2221. /// stylus is pressed.
  2222. /// </summary>
  2223. /// <remarks>
  2224. /// Note: This event will always be raised twice in succession. One will provide a
  2225. /// MouseEventArgs, and the other will provide a StylusEventArgs. It is up to consumers
  2226. /// of this event to decide which one is pertinent and to then filter out the other
  2227. /// type of event.
  2228. /// </remarks>
  2229. public event MouseEventHandler DocumentMouseDown;
  2230. protected virtual void OnDocumentMouseDown(MouseEventArgs e)
  2231. {
  2232. CheckForFirstInputAfterGotFocus();
  2233. if (DocumentMouseDown != null)
  2234. {
  2235. DocumentMouseDown(this, e);
  2236. }
  2237. }
  2238. public event EventHandler DocumentClick;
  2239. protected void OnDocumentClick()
  2240. {
  2241. CheckForFirstInputAfterGotFocus();
  2242. if (DocumentClick != null)
  2243. {
  2244. DocumentClick(this, EventArgs.Empty);
  2245. }
  2246. }
  2247. public event KeyPressEventHandler DocumentKeyPress;
  2248. protected void OnDocumentKeyPress(KeyPressEventArgs e)
  2249. {
  2250. CheckForFirstInputAfterGotFocus();
  2251. if (DocumentKeyPress != null)
  2252. {
  2253. DocumentKeyPress(this, e);
  2254. }
  2255. }
  2256. public event KeyEventHandler DocumentKeyDown;
  2257. protected void OnDocumentKeyDown(KeyEventArgs e)
  2258. {
  2259. CheckForFirstInputAfterGotFocus();
  2260. if (DocumentKeyDown != null)
  2261. {
  2262. DocumentKeyDown(this, e);
  2263. }
  2264. }
  2265. public event KeyEventHandler DocumentKeyUp;
  2266. protected void OnDocumentKeyUp(KeyEventArgs e)
  2267. {
  2268. CheckForFirstInputAfterGotFocus();
  2269. if (DocumentKeyUp != null)
  2270. {
  2271. DocumentKeyUp(this, e);
  2272. }
  2273. }
  2274. #endregion
  2275. #region 多相相关
  2276. public List<PhaseModel> PhaseModels
  2277. {
  2278. set
  2279. {
  2280. this.phaseModels = value;
  2281. this.appWorkspace.GetPanelBottom().documentStrip.ClearPhase();
  2282. this.appWorkspace.GetPanelBottom().documentStrip.AddPhase(this.phaseModels);
  2283. }
  2284. get
  2285. {
  2286. return this.phaseModels;
  2287. }
  2288. }
  2289. /// <summary>
  2290. /// 将物相和二值的数据带入到图像索引列表
  2291. /// </summary>
  2292. public Bitmap BinarizationThumbnail
  2293. {
  2294. get
  2295. {
  2296. //二值化相关
  2297. if (this.phaseModels.Count == 0)
  2298. return this.CompositionSurface.Thumbnail;
  2299. else
  2300. {
  2301. Bitmap newBit = this.CompositionSurface.Thumbnail;
  2302. Graphics graphics = Graphics.FromImage(newBit);
  2303. for (int i = 0; i < this.phaseModels.Count; i++)
  2304. {
  2305. PhaseModel item = this.phaseModels[i];
  2306. if (item.choise && item.mat != null)
  2307. graphics.DrawImage(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(item.mat), 0, 0, newBit.Width, newBit.Height);
  2308. }
  2309. return newBit;
  2310. }
  2311. }
  2312. }
  2313. /// <summary>
  2314. /// 将物相和二值的数据带入到分析页面
  2315. /// </summary>
  2316. public PhaseModel AnalysisPhaseModel
  2317. {
  2318. get
  2319. {
  2320. //二值化相关
  2321. if (this.phaseModels.Count == 0)
  2322. {
  2323. return null;
  2324. }
  2325. else
  2326. {
  2327. OpenCvSharp.Mat multipleMat = null;
  2328. int index = 0;
  2329. for (index = 0; index < this.phaseModels.Count; index++)
  2330. {
  2331. PhaseModel item = this.phaseModels[index];
  2332. if (item.choise && item.mat != null)
  2333. {
  2334. multipleMat = item.mat;
  2335. break;
  2336. }
  2337. }
  2338. if (multipleMat == null)
  2339. return null;
  2340. Bitmap newBit = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(multipleMat);
  2341. Graphics graphics = Graphics.FromImage(newBit);
  2342. for (int i = index + 1; i < this.phaseModels.Count; i++)
  2343. {
  2344. PhaseModel item = this.phaseModels[i];
  2345. if (item.choise && item.mat != null)
  2346. {
  2347. OpenCvSharp.Mat targetMat = item.mat;
  2348. graphics.DrawImage(OpenCvSharp.Extensions.BitmapConverter.ToBitmap(targetMat), 0, 0, targetMat.Width, targetMat.Height);
  2349. }
  2350. }
  2351. PhaseModel phaseModel = new PhaseModel();
  2352. phaseModel.name = this.PhaseModels[0].name;
  2353. phaseModel.position = this.PhaseModels[0].position;
  2354. phaseModel.color = this.PhaseModels[0].color;
  2355. phaseModel.mat = OpenCvSharp.Extensions.BitmapConverter.ToMat(newBit);
  2356. phaseModel.choise = true;
  2357. phaseModel.rgborhls = this.PhaseModels[0].rgborhls;
  2358. phaseModel.minB = this.PhaseModels[0].minB;
  2359. phaseModel.maxB = this.PhaseModels[0].maxB;
  2360. phaseModel.minG = this.PhaseModels[0].minG;
  2361. phaseModel.maxG = this.PhaseModels[0].maxG;
  2362. phaseModel.minR = this.PhaseModels[0].minR;
  2363. phaseModel.maxR = this.PhaseModels[0].maxR;
  2364. phaseModel.minH = this.PhaseModels[0].minH;
  2365. phaseModel.maxH = this.PhaseModels[0].maxH;
  2366. phaseModel.minL = this.PhaseModels[0].minL;
  2367. phaseModel.maxL = this.PhaseModels[0].maxL;
  2368. phaseModel.minS = this.PhaseModels[0].minS;
  2369. phaseModel.maxS = this.PhaseModels[0].maxS;
  2370. return phaseModel;
  2371. }
  2372. }
  2373. }
  2374. public List<PhaseModel> PhaseModelsForCopy
  2375. {
  2376. get
  2377. {
  2378. List<PhaseModel> list = new List<PhaseModel>();
  2379. if (this.phaseModels != null && this.phaseModels.Count > 0)
  2380. {
  2381. foreach (PhaseModel phase in this.phaseModels)
  2382. {
  2383. if (phase.mat == null)//跳过null的避免报错
  2384. continue;
  2385. PhaseModel phase1 = new PhaseModel();
  2386. phase1.choise = phase.choise;
  2387. phase1.color = phase.color;
  2388. phase1.mat = new OpenCvSharp.Mat();
  2389. phase.mat.CopyTo(phase1.mat);
  2390. phase1.name = phase.name;
  2391. phase1.position = phase.position;
  2392. list.Add(phase1);
  2393. }
  2394. }
  2395. return list;
  2396. }
  2397. }
  2398. #endregion
  2399. #region DocimentView的子控件的事件
  2400. private void Panel_KeyPress(object sender, KeyPressEventArgs e)
  2401. {
  2402. OnDocumentKeyPress(e);
  2403. }
  2404. private void Panel_LostFocus(object sender, EventArgs e)
  2405. {
  2406. this.raiseFirstInputAfterGotFocus = false;
  2407. }
  2408. private void Panel_GotFocus(object sender, EventArgs e)
  2409. {
  2410. this.raiseFirstInputAfterGotFocus = true;
  2411. }
  2412. private void Panel_KeyDown(object sender, KeyEventArgs e)
  2413. {
  2414. if (e.KeyValue == 17)
  2415. {
  2416. this.controlPress = true;
  2417. }
  2418. CheckForFirstInputAfterGotFocus();
  2419. OnDocumentKeyDown(e);
  2420. if (!e.Handled)
  2421. {
  2422. PointF oldPt = this.DocumentScrollPositionF;
  2423. PointF newPt = oldPt;
  2424. RectangleF vdr = VisibleDocumentRectangleF;
  2425. switch (e.KeyData)
  2426. {
  2427. case Keys.Next:
  2428. newPt.Y += vdr.Height;
  2429. break;
  2430. case (Keys.Next | Keys.Shift):
  2431. newPt.X += vdr.Width;
  2432. break;
  2433. case Keys.Prior:
  2434. newPt.Y -= vdr.Height;
  2435. break;
  2436. case (Keys.Prior | Keys.Shift):
  2437. newPt.X -= vdr.Width;
  2438. break;
  2439. case Keys.Home:
  2440. if (oldPt.X == 0)
  2441. {
  2442. newPt.Y = 0;
  2443. }
  2444. else
  2445. {
  2446. newPt.X = 0;
  2447. }
  2448. break;
  2449. case Keys.End:
  2450. if (vdr.Right < this.document.Width - 1)
  2451. {
  2452. newPt.X = this.document.Width;
  2453. }
  2454. else
  2455. {
  2456. newPt.Y = this.document.Height;
  2457. }
  2458. break;
  2459. default:
  2460. break;
  2461. }
  2462. if (newPt != oldPt)
  2463. {
  2464. DocumentScrollPositionF = newPt;
  2465. e.Handled = true;
  2466. }
  2467. }
  2468. }
  2469. private void Panel_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
  2470. {
  2471. OnScroll(e);
  2472. UpdateRulerOffsets();
  2473. }
  2474. private void Panel_KeyUp(object sender, KeyEventArgs e)
  2475. {
  2476. this.controlPress = false;
  2477. OnDocumentKeyUp(e);
  2478. }
  2479. /// <summary>
  2480. /// 鼠标按下事件
  2481. /// </summary>
  2482. /// <param name="sender"></param>
  2483. /// <param name="e"></param>
  2484. private void MouseEvent_Down(object sender, MouseEventArgs e)
  2485. {
  2486. //tools[(int)activeTool].OnMouseDown(this, e);
  2487. tools[(int)activeTool].InvokeMember("OnMouseDown",
  2488. BindingFlags.Public |
  2489. BindingFlags.Static |
  2490. BindingFlags.InvokeMethod,
  2491. null,
  2492. null,
  2493. new object[2] { this, e });
  2494. if (this.appWorkspace != null)
  2495. {
  2496. this.appWorkspace.SetDrawNodes();
  2497. this.appWorkspace.RefreshCameraPriview();
  2498. }
  2499. }
  2500. [DllImport("user32.dll", EntryPoint = "mouse_event", SetLastError = true)]
  2501. private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int a);
  2502. public void MouseEvent_Del(object sender, MouseEventArgs e)
  2503. {
  2504. //TODO 可能有用 不一定
  2505. //if (continuousDrawingMeasure || continuousDrawingLabel)
  2506. //{
  2507. // tools[(int)activeTool].InvokeMember("OnDelKeyDown",
  2508. // BindingFlags.Public |
  2509. // BindingFlags.Static |
  2510. // BindingFlags.InvokeMethod,
  2511. // null,
  2512. // null,
  2513. // new object[2] { this, e });
  2514. //}
  2515. //else {
  2516. //}
  2517. //mouse_event(0x0008 | 0x0010, 410, 0, 0, 0);
  2518. if (tools != null && tools.Length > 0)
  2519. {
  2520. try
  2521. {
  2522. tools[(int)activeTool].InvokeMember("beginWithNewObject",
  2523. BindingFlags.Public |
  2524. BindingFlags.Static |
  2525. BindingFlags.InvokeMethod,
  2526. null,
  2527. null,
  2528. new object[0] { });
  2529. }
  2530. catch (Exception ex)
  2531. {
  2532. }
  2533. tools[0].InvokeMember("OnDelKeyDown",
  2534. BindingFlags.Public |
  2535. BindingFlags.Static |
  2536. BindingFlags.InvokeMethod,
  2537. null,
  2538. null,
  2539. new object[2] { this, e });
  2540. }
  2541. //tools[(int)activeTool].OnDelKeyDown(this, e);
  2542. if (this.appWorkspace != null)
  2543. {
  2544. this.appWorkspace.RefreshLabelListDialog();
  2545. this.appWorkspace.RefreshMeasureListView();
  2546. }
  2547. }
  2548. DateTime _time;
  2549. /// <summary>
  2550. /// 鼠标移动事件
  2551. /// </summary>
  2552. /// <param name="sender"></param>
  2553. /// <param name="e"></param>
  2554. private void MouseEvent_Move(object sender, MouseEventArgs e)
  2555. {
  2556. //
  2557. // 像素跟踪,如果AppWorkspace不等于null,坐标不超范围,则设置像素跟踪
  2558. //
  2559. if (this.AppWorkspaceTop != null && this.compositionSurface != null && this.pixelTrackingEnabled)
  2560. //&& e.Location.X>=0 && e.Location.Y>=0
  2561. //&& e.Location.X<=this.Surface.Width
  2562. //&& e.Location.Y <= this.Surface.Height)
  2563. {
  2564. if ((DateTime.Now - _time).TotalMilliseconds >20)
  2565. {
  2566. //this.AppWorkspaceTop.SetImageAndData(this.ScaleFactor.UnscalePoint(e.Location));
  2567. this.AppWorkspaceTop.SetImageAndData(this.CalcPixelPoint(e.Location));
  2568. _time = DateTime.Now;
  2569. }
  2570. }
  2571. //
  2572. // 是否绘制辅助线
  2573. //
  2574. if (this.auxiliaryLineEnabled)
  2575. {
  2576. path = new GraphicsPath();
  2577. path.AddLine(new Point(0, e.Y), new Point(Width, e.Y));
  2578. path1 = new GraphicsPath();
  2579. path1.AddLine(new Point(e.X, 0), new Point(e.X, Height));
  2580. this.Refresh();
  2581. }
  2582. //
  2583. // 处理标注测量
  2584. //
  2585. if (!initialized)
  2586. return;
  2587. if (e.Button == MouseButtons.Left || e.Button == MouseButtons.None)
  2588. if (tools != null && tools.Length > 0)
  2589. {
  2590. tools[(int)activeTool].InvokeMember("OnMouseMove",
  2591. BindingFlags.Public |
  2592. BindingFlags.Static |
  2593. BindingFlags.InvokeMethod,
  2594. null,
  2595. null,
  2596. new object[2] { this, e });
  2597. }
  2598. //tools[(int)activeTool].OnMouseMove(this, e);
  2599. else
  2600. this.Cursor = Cursors.Default;
  2601. }
  2602. /// <summary>
  2603. /// 鼠标抬起事件
  2604. /// </summary>
  2605. /// <param name="sender"></param>
  2606. /// <param name="e"></param>
  2607. private void MouseEvent_Up(object sender, MouseEventArgs e)
  2608. {
  2609. if (e.Button == MouseButtons.Left)
  2610. tools[(int)activeTool].InvokeMember("OnMouseUp",
  2611. BindingFlags.Public |
  2612. BindingFlags.Static |
  2613. BindingFlags.InvokeMethod,
  2614. null,
  2615. null,
  2616. new object[2] { this, e });
  2617. //tools[(int)activeTool].OnMouseUp(this, e);
  2618. if (this.appWorkspace != null)
  2619. {
  2620. this.appWorkspace.RefreshLabelListDialog();
  2621. this.appWorkspace.RefreshMeasureListView();
  2622. this.appWorkspace.RefreshListView();
  2623. this.appWorkspace.RefreshOpticalDensity();
  2624. }
  2625. }
  2626. /// <summary>
  2627. /// 鼠标单击事件
  2628. /// </summary>
  2629. /// <param name="sender"></param>
  2630. /// <param name="e"></param>
  2631. private void MouseEvent_Click(object sender, MouseEventArgs e)
  2632. {
  2633. tools[(int)activeTool].InvokeMember("OnMouseClick",
  2634. BindingFlags.Public |
  2635. BindingFlags.Static |
  2636. BindingFlags.InvokeMethod,
  2637. null,
  2638. null,
  2639. new object[2] { this, e });
  2640. //tools[(int)activeTool].OnMouseClick(this, e);
  2641. }
  2642. /// <summary>
  2643. /// 鼠标双击事件
  2644. /// </summary>
  2645. /// <param name="sender"></param>
  2646. /// <param name="e"></param>
  2647. private void MouseEvent_DoubleClick(object sender, MouseEventArgs e)
  2648. {
  2649. if (e.Button == MouseButtons.Left && e.Clicks == 2)
  2650. {
  2651. if (this.controlPress)
  2652. {
  2653. List<DrawObject> list = this.graphicsList.GetDrawToolTypeList(DrawToolType.ImageCut);
  2654. if (list != null && list.Count > 0)
  2655. {
  2656. Point ene = GetScalePoint(e.Location);
  2657. RectangleF rect = list[0].Rectangle;
  2658. if (ene.X > rect.X && ene.X < (rect.X + rect.Width) && ene.Y > rect.Y && ene.Y < (rect.Y + rect.Height))
  2659. {
  2660. this.appWorkspace.CopyAndPasteByControlAndDoubleClick();
  2661. }
  2662. }
  2663. this.controlPress = false;
  2664. }
  2665. else
  2666. {
  2667. tools[(int)activeTool].InvokeMember("OnMouseLeftDoubleClick",
  2668. BindingFlags.Public |
  2669. BindingFlags.Static |
  2670. BindingFlags.InvokeMethod,
  2671. null,
  2672. null,
  2673. new object[2] { this, e });
  2674. }
  2675. }
  2676. //tools[(int)activeTool].OnMouseLeftDoubleClick(this, e);
  2677. }
  2678. /// <summary>
  2679. /// 获取panel显示内容 生成图片返回
  2680. /// </summary>
  2681. /// <returns></returns>
  2682. public Bitmap panelBitmap(bool nail = false)
  2683. {
  2684. if (compositionSurface == null)
  2685. {
  2686. return null;
  2687. }
  2688. Bitmap origin = compositionSurface.CreateAliasedBitmap();
  2689. Bitmap newBit = null;
  2690. if (!nail)
  2691. {
  2692. newBit = origin;
  2693. }
  2694. else if (origin.Width > origin.Height)
  2695. {
  2696. newBit = Surface.MakeThumbnail(origin, 64, 64, "W");
  2697. }
  2698. else if (origin.Height > origin.Width)
  2699. {
  2700. newBit = Surface.MakeThumbnail(origin, 64, 64, "H");
  2701. }
  2702. else
  2703. {
  2704. newBit = Surface.MakeThumbnail(origin, 64, 64, "W");
  2705. }
  2706. int width = newBit.Width;// (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  2707. int height = newBit.Height;// (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  2708. int x = 0;// (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  2709. int y = 0;// (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  2710. Graphics graphics = Graphics.FromImage(newBit);
  2711. //
  2712. // 以下是绘制相
  2713. //
  2714. if (phaseModels.Count > 0)
  2715. {
  2716. foreach (PhaseModel model in phaseModels)
  2717. {
  2718. if (model != null && model.choise && model.mat != null)
  2719. {
  2720. Bitmap map = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(model.mat);
  2721. graphics.DrawImage(map, x, y, width, height);
  2722. }
  2723. }
  2724. }
  2725. //
  2726. // 以下是绘制辅助线
  2727. //
  2728. if (this.auxiliaryLineEnabled)
  2729. {
  2730. if (path != null && path1 != null)
  2731. {
  2732. graphics.DrawPath(new Pen(Color.Red), path);
  2733. graphics.DrawPath(new Pen(Color.Red), path1);
  2734. }
  2735. }
  2736. //
  2737. // 以下是绘制网格、标注、测量、视场等开始
  2738. //
  2739. graphics.TranslateTransform(x, y);
  2740. graphics.ScaleTransform((float)1.0/*this.scaleFactor.Ratio*/, (float)1.0/*this.scaleFactor.Ratio*/);
  2741. //
  2742. // 以下是绘制网格
  2743. //
  2744. if (this.gridLineEnabled && this.AppWorkspaceTop != null)
  2745. {
  2746. double unitLength = 1;
  2747. this.appWorkspace.getMeasureInfo().TryGetValue(MeasurementUnit.Micron, out unitLength);
  2748. int unit = AppWorkspaceTop.GetGridModel().grid.Unit; //单位
  2749. int actualLength = AppWorkspaceTop.GetGridModel().grid.ActualLength;//实际长度
  2750. if (unit == (int)MeasurementUnit.Micron)
  2751. {
  2752. AppWorkspaceTop.GetGridModel().grid.SideLength = (int)((double)actualLength / unitLength);
  2753. }
  2754. //else if (unit == (int)MeasurementUnit.Pixel)
  2755. //{
  2756. // AppWorkspaceTop.GetGridModel().grid.SideLength = actualLength;
  2757. //}
  2758. DrawRulerHelper.drawGrid(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  2759. }
  2760. //
  2761. //以下是绘制矩形网格
  2762. //
  2763. if (this.gridRectangleEnabled && this.AppWorkspaceTop != null && micronRatio > 0)
  2764. {
  2765. DrawRulerHelper.drawGridRectangle(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height, micronRatio);
  2766. }
  2767. //
  2768. //以下是绘制圆形网格
  2769. //
  2770. if (this.gridRoundEnabled && this.AppWorkspaceTop != null && micronRatio > 0)
  2771. {
  2772. DrawRulerHelper.drawGridRound(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height, micronRatio);
  2773. }
  2774. //
  2775. //以下是绘制十字线
  2776. //
  2777. if (this.gridCrossCurveEnabled && this.AppWorkspaceTop != null)
  2778. {
  2779. DrawRulerHelper.drawGridCrossCurve(null, graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  2780. }
  2781. //
  2782. // 以下是绘制标注、测量、视场
  2783. //
  2784. if (graphicsList != null)
  2785. {
  2786. graphicsList.Draw(graphics);
  2787. }
  2788. //
  2789. // 以下是绘制网格(覆盖整个图片)
  2790. //
  2791. if (this.gridLineFullEnabled && this.AppWorkspaceTop != null)
  2792. {
  2793. DrawRulerHelper.drawGridFull(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  2794. }
  2795. //
  2796. // 以下是绘制图像(工艺图比照定位点图像绘制)
  2797. //
  2798. if (m_selectedBitmap != null)
  2799. {
  2800. graphics.DrawImage(m_selectedBitmap, new PointF(m_artworkImageRectangle.X, m_artworkImageRectangle.Y));
  2801. }
  2802. return newBit;
  2803. }
  2804. /// <summary>
  2805. /// 获取panel显示内容 生成图片返回
  2806. /// </summary>
  2807. /// <returns></returns>
  2808. public Bitmap NewPanelBitmap(Bitmap newBit)
  2809. {
  2810. int width = newBit.Width;// (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  2811. int height = newBit.Height;// (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  2812. int x = 0;// (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  2813. int y = 0;// (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  2814. Graphics graphics = Graphics.FromImage(newBit);
  2815. //
  2816. // 以下是绘制相
  2817. //
  2818. if (phaseModels.Count > 0)
  2819. {
  2820. foreach (PhaseModel model in phaseModels)
  2821. {
  2822. if (model != null && model.choise && model.mat != null)
  2823. {
  2824. Bitmap map = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(model.mat);
  2825. graphics.DrawImage(map, x, y, width, height);
  2826. }
  2827. }
  2828. }
  2829. //
  2830. // 以下是绘制辅助线
  2831. //
  2832. //if (this.auxiliaryLineEnabled)
  2833. //{
  2834. // if (path != null && path1 != null)
  2835. // {
  2836. // graphics.DrawPath(new Pen(Color.Red), path);
  2837. // graphics.DrawPath(new Pen(Color.Red), path1);
  2838. // }
  2839. //}
  2840. //
  2841. // 以下是绘制网格、标注、测量、视场等开始
  2842. //
  2843. graphics.TranslateTransform(x, y);
  2844. graphics.ScaleTransform((float)1.0/*this.scaleFactor.Ratio*/, (float)1.0/*this.scaleFactor.Ratio*/);
  2845. //
  2846. // 以下是绘制网格
  2847. //
  2848. if (this.gridLineEnabled && this.AppWorkspaceTop != null)
  2849. {
  2850. double unitLength = 1;
  2851. this.appWorkspace.getMeasureInfo().TryGetValue(MeasurementUnit.Micron, out unitLength);
  2852. int unit = AppWorkspaceTop.GetGridModel().grid.Unit; //单位
  2853. int actualLength = AppWorkspaceTop.GetGridModel().grid.ActualLength;//实际长度
  2854. if (unit == (int)MeasurementUnit.Micron)
  2855. {
  2856. AppWorkspaceTop.GetGridModel().grid.SideLength = (int)((double)actualLength / unitLength);
  2857. }
  2858. //else if (unit == (int)MeasurementUnit.Pixel)
  2859. //{
  2860. // AppWorkspaceTop.GetGridModel().grid.SideLength = actualLength;
  2861. //}
  2862. DrawRulerHelper.drawGrid(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  2863. }
  2864. //
  2865. //以下是绘制矩形网格
  2866. //
  2867. if (this.gridRectangleEnabled && this.AppWorkspaceTop != null && micronRatio > 0)
  2868. {
  2869. DrawRulerHelper.drawGridRectangle(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height, micronRatio);
  2870. }
  2871. //
  2872. //以下是绘制圆形网格
  2873. //
  2874. if (this.gridRoundEnabled && this.AppWorkspaceTop != null && micronRatio > 0)
  2875. {
  2876. DrawRulerHelper.drawGridRound(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height, micronRatio);
  2877. }
  2878. //
  2879. //以下是绘制十字线
  2880. //
  2881. if (this.gridCrossCurveEnabled && this.AppWorkspaceTop != null)
  2882. {
  2883. DrawRulerHelper.drawGridCrossCurve(null, graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  2884. }
  2885. //
  2886. // 以下是绘制标注、测量、视场
  2887. //
  2888. if (graphicsList != null)
  2889. {
  2890. graphicsList.Draw(graphics);
  2891. }
  2892. //
  2893. // 以下是绘制网格(覆盖整个图片)
  2894. //
  2895. if (this.gridLineFullEnabled && this.AppWorkspaceTop != null)
  2896. {
  2897. DrawRulerHelper.drawGridFull(AppWorkspaceTop.GetGridModel(), graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  2898. }
  2899. //
  2900. // 以下是绘制图像(工艺图比照定位点图像绘制)
  2901. //
  2902. if (m_selectedBitmap != null)
  2903. {
  2904. graphics.DrawImage(m_selectedBitmap, new PointF(m_artworkImageRectangle.X, m_artworkImageRectangle.Y));
  2905. }
  2906. return newBit;
  2907. }
  2908. /// <summary>
  2909. /// 获取panel标注、测量、视场显示内容 生成图片返回
  2910. /// </summary>
  2911. /// <returns></returns>
  2912. public Bitmap FieldBitmap(Bitmap newBit)
  2913. {
  2914. int width = newBit.Width;// (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  2915. int height = newBit.Height;// (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  2916. int x = 0;// (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  2917. int y = 0;// (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  2918. Graphics graphics = Graphics.FromImage(newBit);
  2919. //
  2920. // 以下是绘制网格、标注、测量、视场等开始
  2921. //
  2922. graphics.TranslateTransform(x, y);
  2923. graphics.ScaleTransform((float)1.0/*this.scaleFactor.Ratio*/, (float)1.0/*this.scaleFactor.Ratio*/);
  2924. //
  2925. // 以下是绘制标注、测量、视场
  2926. //
  2927. if (graphicsList != null)
  2928. {
  2929. graphicsList.Draw(graphics);
  2930. }
  2931. return newBit;
  2932. }
  2933. /// <summary>
  2934. /// 样式信息
  2935. /// </summary>
  2936. //private GuideStyleModel guideStyleModel;
  2937. /// <summary>
  2938. /// 绘制事件
  2939. /// </summary>
  2940. /// <param name="sender"></param>
  2941. /// <param name="e"></param>
  2942. private void panelPaint(object sender, PaintEventArgs e)
  2943. {
  2944. if (compositionSurface != null)
  2945. {
  2946. // 以下是计算绘制图片的位置和大小并绘制图片
  2947. Rectangle rc = this.panel.ClientRectangle;
  2948. int width = (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  2949. int height = (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  2950. int x = (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  2951. int y = (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  2952. //第四个版本,只绘制当前需要显示的大小的图片
  2953. //if (this.compositionSurface.Bitmap != null)
  2954. if (this.compositionSurface.CreateAliasedBitmap() != null)
  2955. {
  2956. using (Surface doubleBuffer = GetDoubleBuffer(new Size((rc.Width < width) ? rc.Width : width, (rc.Height < height) ? rc.Height : height)))
  2957. {
  2958. try
  2959. {
  2960. using (RenderArgs renderArgs = new RenderArgs(doubleBuffer))
  2961. {
  2962. DrawArea(renderArgs, new Point((rc.Width < width) ? -x : 0, (rc.Height < height) ? -y : 0));
  2963. if (m_center)
  2964. {
  2965. e.Graphics.DrawImage(doubleBuffer.CreateAliasedBitmap(),
  2966. (rc.Width < width) ? 0 : (rc.Width - width) / 2,
  2967. (rc.Height < height) ? 0 : (rc.Height - height) / 2,
  2968. (rc.Width < width) ? rc.Width : width, (rc.Height < height) ? rc.Height : height);
  2969. }
  2970. else
  2971. {
  2972. e.Graphics.DrawImage(doubleBuffer.CreateAliasedBitmap(),
  2973. m_pLeft,
  2974. m_pTop,
  2975. (rc.Width < width) ? rc.Width : width, (rc.Height < height) ? rc.Height : height);
  2976. }
  2977. }
  2978. }
  2979. catch { }
  2980. }
  2981. }
  2982. //
  2983. // 绘制选择的矩形
  2984. //
  2985. if (drawRectangle)
  2986. {
  2987. /*Pen pen = new Pen(Color.Black);
  2988. pen.DashStyle = DashStyle.Dot;*/
  2989. e.Graphics.DrawRectangle(new Pen(Color.Black), this.DrawRectangle);
  2990. }
  2991. //
  2992. // 以下是绘制相
  2993. //
  2994. if (phaseModels.Count > 0)
  2995. {
  2996. foreach (PhaseModel model in phaseModels)
  2997. {
  2998. if (model != null && model.choise && model.mat != null && !model.mat.IsDisposed)
  2999. {
  3000. Bitmap map = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(model.mat);
  3001. e.Graphics.DrawImage(map, x, y, width, height);
  3002. }
  3003. }
  3004. }
  3005. //
  3006. // 以下是绘制辅助线
  3007. //
  3008. if (this.auxiliaryLineEnabled)
  3009. {
  3010. if (path != null && path1 != null)
  3011. {
  3012. Pen linePen = new Pen(Color.FromArgb(GetGuideStyleModel().lineColour), (float)GetGuideStyleModel().lineWidth);
  3013. linePen.DashStyle = (DashStyle)GetGuideStyleModel().lineStyle;
  3014. e.Graphics.DrawPath(linePen, path);
  3015. e.Graphics.DrawPath(linePen, path1);
  3016. }
  3017. }
  3018. //
  3019. // 以下是绘制网格、标注、测量、视场等开始
  3020. //
  3021. e.Graphics.TranslateTransform(x, y);
  3022. e.Graphics.ScaleTransform((float)this.scaleFactor.Ratio, (float)this.scaleFactor.Ratio);
  3023. //
  3024. // 以下是绘制网格
  3025. //
  3026. if (this.gridLineEnabled && this.AppWorkspaceTop != null)
  3027. {
  3028. //double unitLength = 1;
  3029. //this.appWorkspace.getMeasureInfo().TryGetValue(MeasurementUnit.Micron, out unitLength);
  3030. double unitLength = this.GetRuler(MeasurementUnit.Micron); //获取标尺
  3031. int unit = AppWorkspaceTop.GetGridModel().grid.Unit; //单位
  3032. int actualLength = AppWorkspaceTop.GetGridModel().grid.ActualLength; //实际宽度
  3033. int actualHeight = AppWorkspaceTop.GetGridModel().grid.ActualHeight; //实际高度
  3034. if (unit == (int)MeasurementUnit.Micron)
  3035. {
  3036. AppWorkspaceTop.GetGridModel().grid.SideLength = (int)((double)actualLength / unitLength);
  3037. AppWorkspaceTop.GetGridModel().grid.SideHeight = (int)((double)actualHeight / unitLength);
  3038. }
  3039. //else if (unit == (int)MeasurementUnit.Pixel)
  3040. //{
  3041. // AppWorkspaceTop.GetGridModel().grid.SideLength = actualLength;
  3042. //}
  3043. DrawRulerHelper.drawGrid(AppWorkspaceTop.GetGridModel(), e.Graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  3044. }
  3045. //
  3046. //以下是绘制矩形网格
  3047. //
  3048. if (this.gridRectangleEnabled && this.AppWorkspaceTop != null && micronRatio > 0)
  3049. {
  3050. double unitLength = this.GetRuler(MeasurementUnit.Micron); //获取标尺
  3051. int unit = AppWorkspaceTop.GetGridModel().grid.Unit; //单位
  3052. int actualLength = AppWorkspaceTop.GetGridModel().rectangle.ActualLength; //实际边长
  3053. if (unit == (int)MeasurementUnit.Micron)
  3054. {
  3055. AppWorkspaceTop.GetGridModel().rectangle.sideLength = (int)((double)actualLength / unitLength);
  3056. }
  3057. DrawRulerHelper.drawGridRectangle(AppWorkspaceTop.GetGridModel(), e.Graphics, this.compositionSurface.Width, this.compositionSurface.Height, micronRatio);
  3058. }
  3059. //
  3060. //以下是绘制圆形网格
  3061. //
  3062. if (this.gridRoundEnabled && this.AppWorkspaceTop != null && micronRatio > 0)
  3063. {
  3064. double unitLength = this.GetRuler(MeasurementUnit.Micron); //获取标尺
  3065. int unit = AppWorkspaceTop.GetGridModel().grid.Unit; //单位
  3066. int actualDiameter = AppWorkspaceTop.GetGridModel().round.ActualDiameter; //实际直径
  3067. if (unit == (int)MeasurementUnit.Micron)
  3068. {
  3069. AppWorkspaceTop.GetGridModel().round.diameter = (int)((double)actualDiameter / unitLength);
  3070. }
  3071. DrawRulerHelper.drawGridRound(AppWorkspaceTop.GetGridModel(), e.Graphics, this.compositionSurface.Width, this.compositionSurface.Height, micronRatio);
  3072. }
  3073. //
  3074. //以下是绘制十字线
  3075. //
  3076. if (this.gridCrossCurveEnabled && this.AppWorkspaceTop != null)
  3077. {
  3078. DrawRulerHelper.drawGridCrossCurve(null, e.Graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  3079. }
  3080. //
  3081. // 以下是绘制标注、测量、视场
  3082. //
  3083. if (graphicsList != null)
  3084. {
  3085. graphicsList.Draw(e.Graphics);
  3086. }
  3087. //
  3088. // 以下是绘制网格(覆盖整个图片)
  3089. //
  3090. if (this.gridLineFullEnabled && this.AppWorkspaceTop != null)
  3091. {
  3092. DrawRulerHelper.drawGridFull(AppWorkspaceTop.GetGridModel(), e.Graphics, this.compositionSurface.Width, this.compositionSurface.Height);
  3093. }
  3094. //
  3095. // 以下是绘制图像(工艺图比照定位点图像绘制)
  3096. //
  3097. if (m_selectedBitmap != null)
  3098. {
  3099. e.Graphics.DrawImage(m_selectedBitmap, new PointF(m_artworkImageRectangle.X, m_artworkImageRectangle.Y));
  3100. }
  3101. e.Graphics.ScaleTransform(1 / (float)this.scaleFactor.Ratio, 1 / (float)this.scaleFactor.Ratio);
  3102. e.Graphics.TranslateTransform(-x, -y);
  3103. }
  3104. }
  3105. #endregion
  3106. /// <summary>
  3107. /// 获取panel可见位置的图片
  3108. /// </summary>
  3109. /// <returns></returns>
  3110. public Bitmap GetClientRangePic()
  3111. {
  3112. Rectangle rc = this.panel.ClientRectangle;
  3113. int width = (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  3114. int height = (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  3115. int x = (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  3116. int y = (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  3117. if (this.compositionSurface.CreateAliasedBitmap() != null)
  3118. {
  3119. using (Surface doubleBuffer = GetDoubleBuffer(new Size((rc.Width < width) ? rc.Width : width, (rc.Height < height) ? rc.Height : height)))
  3120. {
  3121. using (RenderArgs renderArgs = new RenderArgs(doubleBuffer))
  3122. {
  3123. DrawArea(renderArgs, new Point((rc.Width < width) ? -x : 0, (rc.Height < height) ? -y : 0));
  3124. return doubleBuffer.CreateAliasedBitmap();
  3125. }
  3126. }
  3127. }
  3128. return this.compositionSurface.CreateAliasedBitmap();
  3129. /*IntPtr handle = this.panel.Handle;
  3130. IntPtr hdcSrc = SafeNativeMethods.GetWindowDC(handle);
  3131. SafeNativeMethods.RECT windowRect = new SafeNativeMethods.RECT();
  3132. SafeNativeMethods.GetWindowRect(handle, ref windowRect);
  3133. int width = windowRect.right - windowRect.left - ((this.panel.VerticalScroll.Visible) ? 18 : 0);
  3134. int height = windowRect.bottom - windowRect.top - ((this.panel.HorizontalScroll.Visible) ? 18 : 0);
  3135. IntPtr hdcDest = SafeNativeMethods.CreateCompatibleDC(hdcSrc);
  3136. IntPtr hBitmap = SafeNativeMethods.CreateCompatibleBitmap(hdcSrc, width, height);
  3137. IntPtr hOld = SafeNativeMethods.SelectObject(hdcDest, hBitmap);
  3138. SafeNativeMethods.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SafeNativeMethods.SRCCOPY);
  3139. SafeNativeMethods.SelectObject(hdcDest, hOld);
  3140. SafeNativeMethods.DeleteDC(hdcDest);
  3141. SafeNativeMethods.ReleaseDC(handle, hdcSrc);
  3142. Bitmap img = Image.FromHbitmap(hBitmap);
  3143. SafeNativeMethods.DeleteObject(hBitmap);
  3144. return img;*/
  3145. }
  3146. #region DocumentView的事件
  3147. protected override void OnLayout(LayoutEventArgs e)
  3148. {
  3149. DoLayout();
  3150. base.OnLayout(e);
  3151. }
  3152. /// <summary>
  3153. /// 让画布获取焦点
  3154. /// </summary>
  3155. public new void Focus()
  3156. {
  3157. this.panel.Focus();
  3158. }
  3159. protected override void OnResize(EventArgs e)
  3160. {
  3161. // enable or disable timer: no sense drawing selection if we're minimized
  3162. Form parentForm = ParentForm;
  3163. if (parentForm != null)
  3164. {
  3165. if (parentForm.WindowState != this.oldWindowState)
  3166. {
  3167. PerformLayout();
  3168. }
  3169. this.oldWindowState = parentForm.WindowState;
  3170. }
  3171. base.OnResize(e);
  3172. DoLayout();
  3173. }
  3174. #endregion
  3175. #region 坐标转换相关方法
  3176. public PointF MouseToDocumentF(Control sender, Point mouse)
  3177. {
  3178. Point screenPoint = sender.PointToScreen(mouse);
  3179. Point sbClient = this.panel.PointToClient(screenPoint);
  3180. PointF docPoint = this.panel.ClientToSurface(new PointF(sbClient.X, sbClient.Y));
  3181. return docPoint;
  3182. }
  3183. public Point MouseToDocument(Control sender, Point mouse)
  3184. {
  3185. Point screenPoint = sender.PointToScreen(mouse);
  3186. Point sbClient = this.panel.PointToClient(screenPoint);
  3187. Point docPoint = Point.Truncate(this.panel.ClientToSurface(sbClient));
  3188. return docPoint;
  3189. }
  3190. /// <summary>
  3191. /// Converts a point from the Windows Forms "client" coordinate space (wrt the DocumentView)
  3192. /// into the Document coordinate space.
  3193. /// </summary>
  3194. /// <param name="clientPt">A Point that is in our client coordinates.</param>
  3195. /// <returns>A Point that is in Document coordinates.</returns>
  3196. public PointF ClientToDocument(Point clientPt)
  3197. {
  3198. Point screen = PointToScreen(clientPt);
  3199. Point sbClient = this.panel.PointToClient(screen);
  3200. return this.panel.ClientToSurface(sbClient);
  3201. }
  3202. /// <summary>
  3203. /// Converts a point from screen coordinates to document coordinates
  3204. /// </summary>
  3205. /// <param name="screen">The point in screen coordinates to convert to document coordinates</param>
  3206. public PointF ScreenToDocument(PointF screen)
  3207. {
  3208. Point offset = this.panel.PointToClient(new Point(0, 0));
  3209. return this.panel.ClientToSurface(new PointF(screen.X + (float)offset.X, screen.Y + (float)offset.Y));
  3210. }
  3211. /// <summary>
  3212. /// Converts a point from screen coordinates to document coordinates
  3213. /// </summary>
  3214. /// <param name="screen">The point in screen coordinates to convert to document coordinates</param>
  3215. public Point ScreenToDocument(Point screen)
  3216. {
  3217. Point offset = this.panel.PointToClient(new Point(0, 0));
  3218. return this.panel.ClientToSurface(new Point(screen.X + offset.X, screen.Y + offset.Y));
  3219. }
  3220. /// <summary>
  3221. /// Converts a PointF from the RealTimeStylus coordinate space
  3222. /// into the Document coordinate space.
  3223. /// </summary>
  3224. /// <param name="clientPt">A Point that is in RealTimeStylus coordinate space.</param>
  3225. /// <returns>A Point that is in Document coordinates.</returns>
  3226. public PointF ClientToSurface(PointF clientPt)
  3227. {
  3228. return this.panel.ClientToSurface(clientPt);
  3229. }
  3230. /// <summary>
  3231. /// Converts a point from Document coordinate space into the Windows Forms "client"
  3232. /// coordinate space.
  3233. /// </summary>
  3234. /// <param name="clientPt">A Point that is in Document coordinates.</param>
  3235. /// <returns>A Point that is in client coordinates.</returns>
  3236. public PointF DocumentToClient(PointF documentPt)
  3237. {
  3238. PointF sbClient = this.panel.SurfaceToClient(documentPt);
  3239. Point screen = this.panel.PointToScreen(Point.Round(sbClient));
  3240. return PointToClient(screen);
  3241. }
  3242. /// <summary>
  3243. /// Converts a rectangle from the Windows Forms "client" coordinate space into the Document
  3244. /// coordinate space.
  3245. /// </summary>
  3246. /// <param name="clientPt">A Rectangle that is in client coordinates.</param>
  3247. /// <returns>A Rectangle that is in Document coordinates.</returns>
  3248. public RectangleF ClientToDocument(Rectangle clientRect)
  3249. {
  3250. Rectangle screen = RectangleToScreen(clientRect);
  3251. screen.X = screen.X + offsetW;
  3252. screen.Y = screen.Y + offsetH;
  3253. Rectangle sbClient = this.panel.RectangleToClient(screen);
  3254. return this.panel.ClientToSurface((RectangleF)sbClient);
  3255. }
  3256. /// <summary>
  3257. /// Converts a rectangle from Document coordinate space into the Windows Forms "client"
  3258. /// coordinate space.
  3259. /// </summary>
  3260. /// <param name="clientPt">A Rectangle that is in Document coordinates.</param>
  3261. /// <returns>A Rectangle that is in client coordinates.</returns>
  3262. public RectangleF DocumentToClient(RectangleF documentRect)
  3263. {
  3264. RectangleF sbClient = this.panel.SurfaceToClient(documentRect);
  3265. Rectangle screen = this.panel.RectangleToScreen(Utility.RoundRectangle(sbClient));
  3266. return RectangleToClient(screen);
  3267. }
  3268. public Rectangle SurfaceToClient(Rectangle surfaceRect)
  3269. {
  3270. return new Rectangle(SurfaceToClient(surfaceRect.Location), SurfaceToClient(surfaceRect.Size));
  3271. }
  3272. public Point SurfaceToClient(Point surfacePt)
  3273. {
  3274. return ScaleFactor.ScalePoint(surfacePt);
  3275. }
  3276. public Size SurfaceToClient(Size surfaceSize)
  3277. {
  3278. return Size.Round(SurfaceToClient((SizeF)surfaceSize));
  3279. }
  3280. public SizeF SurfaceToClient(SizeF surfaceSize)
  3281. {
  3282. return ScaleFactor.ScaleSize(surfaceSize);
  3283. }
  3284. #endregion
  3285. #region 接口实现
  3286. public void IsDirty(GraphicsList gList)
  3287. {
  3288. this.AdjustRendering();
  3289. }
  3290. public RectangleF GetVisibleDocumentRectangleF()
  3291. {
  3292. return this.AppWorkspaceTop.GetVisibleDocumentRectangleF();
  3293. }
  3294. public SizeF GetDocumentSize()
  3295. {
  3296. return this.AppWorkspaceTop.GetDocumentSize();
  3297. }
  3298. /// <summary>
  3299. /// 添加到标注测量历史
  3300. /// </summary>
  3301. /// <param name="c"></param>
  3302. public void AddCommandToHistory(Command c)
  3303. {
  3304. this.undoManager.AddCommandToHistory(c);
  3305. }
  3306. /// <summary>
  3307. /// 初始化标注测量管理
  3308. /// </summary>
  3309. public void ResetUndoManager()
  3310. {
  3311. this.undoManager = new UndoManager(this.GraphicsList);
  3312. }
  3313. /// <summary>
  3314. /// 标注连续绘制标记
  3315. /// </summary>
  3316. /// <returns></returns>
  3317. bool ISurfaceBox.ContinuousDrawingLabel()
  3318. {
  3319. return ContinuousDrawingLabel;
  3320. }
  3321. /// <summary>
  3322. /// 测量连续绘制标记
  3323. /// </summary>
  3324. /// <returns></returns>
  3325. bool ISurfaceBox.ContinuousDrawingMeasure()
  3326. {
  3327. return ContinuousDrawingMeasure;
  3328. }
  3329. /// <summary>
  3330. /// 实际大小标记
  3331. /// </summary>
  3332. /// <returns></returns>
  3333. bool ISurfaceBox.ActualSize()
  3334. {
  3335. return ActualSize;
  3336. }
  3337. /// <summary>
  3338. /// 合适大小标记
  3339. /// </summary>
  3340. /// <returns></returns>
  3341. bool ISurfaceBox.SuitableSize()
  3342. {
  3343. return SuitableSize;
  3344. }
  3345. /// <summary>
  3346. /// 合适高度标记
  3347. /// </summary>
  3348. /// <returns></returns>
  3349. bool ISurfaceBox.SuitableHeight()
  3350. {
  3351. return SuitableHeight;
  3352. }
  3353. /// <summary>
  3354. /// 合适大小宽度标记
  3355. /// </summary>
  3356. /// <returns></returns>
  3357. bool ISurfaceBox.SuitableWidth()
  3358. {
  3359. return SuitableWidth;
  3360. }
  3361. /// <summary>
  3362. /// 图片扩缩
  3363. /// </summary>
  3364. /// <returns></returns>
  3365. bool ISurfaceBox.LockZoom()
  3366. {
  3367. return LockZoom;
  3368. }
  3369. /// <summary>
  3370. /// 定倍显示
  3371. /// </summary>
  3372. /// <returns></returns>
  3373. bool ISurfaceBox.FixedMultiple()
  3374. {
  3375. return FixedMultiple;
  3376. }
  3377. /// <summary>
  3378. /// 合并视场
  3379. /// </summary>
  3380. /// <returns></returns>
  3381. bool ISurfaceBox.MergeFieldOfView()
  3382. {
  3383. return MergeFieldOfView;
  3384. }
  3385. /// <summary>
  3386. /// 删除视场
  3387. /// </summary>
  3388. /// <returns></returns>
  3389. bool ISurfaceBox.DeleteFieldOfView()
  3390. {
  3391. return DeleteFieldOfView;
  3392. }
  3393. /// <summary>
  3394. /// 二值操作连续绘制标记
  3395. /// </summary>
  3396. /// <returns></returns>
  3397. bool ISurfaceBox.ContinuousBinaryAction()
  3398. {
  3399. return ContinuousBinaryAction;
  3400. }
  3401. public PointF ClientToSurface(Point sbClient)
  3402. {
  3403. return ScaleFactor.UnscalePoint(sbClient);
  3404. //return this.ClientToSurface(sbClient);
  3405. }
  3406. public void SetDirty()
  3407. {
  3408. this.Document.Dirty = true;
  3409. }
  3410. /// <summary>
  3411. /// 获取原始坐标点
  3412. /// </summary>
  3413. /// <returns></returns>
  3414. public Point GetCalcOriginPoint()
  3415. {
  3416. Rectangle rc = this.panel.ClientRectangle;
  3417. int width = (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  3418. int height = (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  3419. int x = (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  3420. int y = (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  3421. return new Point(x, y);
  3422. }
  3423. /// <summary>
  3424. /// 获取未缩放的坐标点
  3425. /// </summary>
  3426. /// <param name="point"></param>
  3427. /// <returns></returns>
  3428. public Point GetScalePoint(PointF point)
  3429. {
  3430. Rectangle rc = this.panel.ClientRectangle;
  3431. int width = this.compositionSurface == null ? 800 : (int)(this.compositionSurface.Width * this.scaleFactor.Ratio);
  3432. int height = this.compositionSurface == null ? 600 : (int)(this.compositionSurface.Height * this.scaleFactor.Ratio);
  3433. int x = (rc.Width < width) ? this.panel.AutoScrollPosition.X + offsetHalfW : (rc.Width - width) / 2;
  3434. int y = (rc.Height < height) ? this.panel.AutoScrollPosition.Y + offsetHalfH : (rc.Height - height) / 2;
  3435. return new Point(
  3436. (int)((point.X - x) / this.scaleFactor.Ratio),
  3437. (int)((point.Y - y) / this.scaleFactor.Ratio)
  3438. );
  3439. //return this.scaleFactor.UnscalePoint(location);
  3440. }
  3441. public int UnscaleScalar(int deltaX)
  3442. {
  3443. return this.scaleFactor.UnscaleScalar(deltaX);
  3444. }
  3445. /// <summary>
  3446. /// 获取标注样式
  3447. /// </summary>
  3448. /// <returns></returns>
  3449. public LabelStyleModel GetLabelStyleModel()
  3450. {
  3451. return this.AppWorkspaceTop.GetLabelStyleModel();
  3452. }
  3453. public PointF GetDocumentScrollPositionF()
  3454. {
  3455. return this.DocumentScrollPositionF;
  3456. //return this.AppWorkspaceTop.GetDocumentScrollPositionF();
  3457. }
  3458. public void SetDocumentScrollPositionF(PointF newScrollPos)
  3459. {
  3460. //PointF sbClientF = this.panel.SurfaceToClient(newScrollPos);
  3461. //Point sbClient = Point.Round(sbClientF);
  3462. Point sbClient = Point.Round(newScrollPos);
  3463. if (this.panel.AutoScrollPosition != new Point(-sbClient.X, -sbClient.Y))
  3464. {
  3465. this.panel.AutoScrollPosition = sbClient;
  3466. UpdateRulerOffsets();
  3467. //this.panelBottom.Invalidate();
  3468. this.topRuler.Invalidate();
  3469. this.leftRuler.Invalidate();
  3470. }
  3471. /*PointF sbClientF = this.panel.SurfaceToClient(value);
  3472. Point sbClient = Point.Round(sbClientF);
  3473. if (this.panel.AutoScrollPosition != new Point(-sbClient.X, -sbClient.Y))
  3474. {
  3475. this.panel.AutoScrollPosition = sbClient;
  3476. UpdateRulerOffsets();
  3477. this.topRuler.Invalidate();
  3478. this.leftRuler.Invalidate();
  3479. }*/
  3480. //this.AppWorkspaceTop.SetDocumentScrollPositionF(newScrollPos);
  3481. }
  3482. public string[] GetPxPerUnit()
  3483. {
  3484. return this.AppWorkspaceTop.GetPxPerUnit();//getRulerList();//.
  3485. }
  3486. public Dictionary<int, string> GetUnitsDictionary()
  3487. {
  3488. return InvariantData.unitsDictionary;
  3489. }
  3490. public Dictionary<int, string> GetUnitSymbolsDictionary()
  3491. {
  3492. return InvariantData.unitSymbolsDictionary;
  3493. }
  3494. public Dictionary<MeasurementUnit, double> getMeasureInfo()
  3495. {
  3496. if (this.rules.Count > 0)
  3497. return this.rules;
  3498. else
  3499. return this.AppWorkspaceTop.getMeasureInfo();
  3500. }
  3501. /// <summary>
  3502. /// 获取测量的样式
  3503. /// </summary>
  3504. /// <returns></returns>
  3505. public MeasureStyleModel GetMeasureStyleModel()
  3506. {
  3507. return this.AppWorkspaceTop.GetMeasureStyleModel();
  3508. }
  3509. /// <summary>
  3510. /// 获取设置-常规设置-辅助线样式信息
  3511. /// </summary>
  3512. /// <returns></returns>
  3513. public GuideStyleModel GetGuideStyleModel()
  3514. {
  3515. return this.AppWorkspaceTop.GetGuideStyleModel();
  3516. }
  3517. /// <summary>
  3518. /// 获取水印样式
  3519. /// </summary>
  3520. /// <returns></returns>
  3521. public WatermarkModel GetWatermarkModel()
  3522. {
  3523. return this.AppWorkspaceTop.GetWatermarkModel();
  3524. }
  3525. /// <summary>
  3526. /// 获取工型样式
  3527. /// </summary>
  3528. /// <returns></returns>
  3529. public WorkTypeStyleModel GetWorkTypeStyleModel()
  3530. {
  3531. return this.AppWorkspaceTop.GetWorkTypeStyleModel();
  3532. }
  3533. /// <summary>
  3534. /// 获取标尺样式
  3535. /// </summary>
  3536. /// <returns></returns>
  3537. public RulerModel GetRulerStyleModel()
  3538. {
  3539. return this.AppWorkspaceTop.GetRulerStyleModel();
  3540. }
  3541. /// <summary>
  3542. /// 获取视场的状态,合并/剪切
  3543. /// </summary>
  3544. /// <returns></returns>
  3545. public CombineMode GetCombineMode()
  3546. {
  3547. return this.AppWorkspaceTop.GetCombineMode();
  3548. }
  3549. /// <summary>
  3550. /// 设置当前鼠标的状态
  3551. /// </summary>
  3552. /// <param name="status"></param>
  3553. public void SetMouseStatus(bool status)
  3554. {
  3555. this.mouseStatus = status;
  3556. }
  3557. /// <summary>
  3558. /// 获取当前鼠标的状态
  3559. /// </summary>
  3560. public bool GetMouseStatus()
  3561. {
  3562. return this.mouseStatus;
  3563. }
  3564. public int GetSegmentationWidth()
  3565. {
  3566. return InvariantData.segmentation;
  3567. }
  3568. public int GetConnectionWidth()
  3569. {
  3570. return InvariantData.connection;
  3571. }
  3572. public decimal GetGainMultiple()
  3573. {
  3574. return this.AppWorkspaceTop.GetGainMultiple();
  3575. }
  3576. /// <summary>
  3577. /// 刷新标注列表
  3578. /// </summary>
  3579. public void RefreshLabelListDialog()
  3580. {
  3581. this.AppWorkspaceTop.RefreshLabelListDialog();
  3582. }
  3583. /// <summary>
  3584. /// 更新命名的延续数字
  3585. /// </summary>
  3586. public void UpdateContinueNum()
  3587. {
  3588. this.AppWorkspaceTop.UpdateContinueNum();
  3589. }
  3590. #endregion
  3591. #region 二值操作,对象处理
  3592. /// <summary>
  3593. /// 预处理 - 交互操作 - 单个提取
  3594. /// </summary>
  3595. /// <param name="point"></param>
  3596. public void BinaryActionExtract(PointF point)
  3597. {
  3598. PointF point1 = GetScalePoint(point);
  3599. if (point1.X >= 0 && point1.Y >= 0
  3600. && point1.X < this.CompositionSurface.Width && point1.Y < this.CompositionSurface.Height)
  3601. {
  3602. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3603. //如果已经存在并选择了相,则在相处理,如果选择多个相,默认最后一个相
  3604. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3605. {
  3606. Color color = Color.FromArgb(choiseList[0].color);
  3607. choiseList[0].mat = PreActionIntent.SingleExtract(OpenCvSharp.Extensions.BitmapConverter.ToMat(this.compositionSurface.CreateAliasedBitmap()), choiseList[0].mat, point1, new OpenCvSharp.Vec4b(color.B, color.G, color.R, 255));
  3608. this.panel.Refresh();
  3609. }
  3610. //如果不存在相或者没有选择相,则在原图上处理
  3611. else
  3612. {
  3613. PhaseModel model = new PhaseModel();
  3614. model.choise = true;
  3615. model.color = Color.Red.ToArgb();
  3616. model.position = 0;
  3617. model.name = "phase0";
  3618. model.mat = PreActionIntent.SingleExtract(OpenCvSharp.Extensions.BitmapConverter.ToMat(this.compositionSurface.CreateAliasedBitmap()), null, point1, new OpenCvSharp.Vec4b(0, 0, 255, 255));
  3619. List<PhaseModel> list = new List<PhaseModel>();
  3620. list.Add(model);
  3621. this.PhaseModels = list;
  3622. this.panel.Refresh();
  3623. }
  3624. }
  3625. }
  3626. /// <summary>
  3627. /// 预处理 - 交互操作 - 选择 - 反选
  3628. /// </summary>
  3629. public void BinaryActionAntiElection(int j)
  3630. {
  3631. OpenCvSharp.Mat[] arr = this.phaseModels[j].mat.Split();
  3632. arr[0] = ~arr[0];
  3633. arr[1] = ~arr[1];
  3634. arr[2] = ~arr[1];
  3635. OpenCvSharp.Cv2.Merge(arr, this.phaseModels[j].mat);
  3636. this.panel.Refresh();
  3637. }
  3638. /// <summary>
  3639. /// 预处理 - 交互操作 - 选择 - 应用
  3640. /// 原有的相颜色变透明,保留反色的
  3641. /// </summary>
  3642. public void BinaryActionAntiConfirm(int j)
  3643. {
  3644. this.phaseModels[j].mat = PreActionIntent.BinaryActionAntiConfirm(this.phaseModels[j].mat, this.phaseModels[j].color);
  3645. this.panel.Refresh();
  3646. }
  3647. /// <summary>
  3648. /// 预处理 - 交互操作 - 选择 - 单个选择
  3649. /// </summary>
  3650. /// <param name="point"></param>
  3651. public void BinaryActionChoise(PointF point)
  3652. {
  3653. Point point1 = GetScalePoint(point);
  3654. if (point1.X >= 0 && point1.Y >= 0
  3655. && point1.X < this.CompositionSurface.Width && point1.Y < this.CompositionSurface.Height)
  3656. {
  3657. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3658. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3659. {
  3660. foreach (PhaseModel phaseModel in choiseList)
  3661. {
  3662. phaseModel.mat = PreActionIntent.SingleChoise(phaseModel.mat, phaseModel.color, point1);
  3663. this.panel.Refresh();
  3664. }
  3665. }
  3666. }
  3667. }
  3668. /// <summary>
  3669. /// 预处理 - 交互操作 - 选择 - 矩形选择
  3670. /// </summary>
  3671. /// <param name="rectangle"></param>
  3672. public void BinaryActionChoiseRectangle(RectangleF rectangle)
  3673. {
  3674. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3675. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3676. {
  3677. foreach (PhaseModel phaseModel in choiseList)
  3678. {
  3679. phaseModel.mat = PreActionIntent.SingleChoiseRectangle(phaseModel.mat, phaseModel.color, rectangle);
  3680. this.panel.Refresh();
  3681. }
  3682. }
  3683. }
  3684. /// <summary>
  3685. /// 预处理 - 交互操作 - 选择 - 椭圆选择
  3686. /// </summary>
  3687. /// <param name="rectangle"></param>
  3688. public void BinaryActionChoiseOval(RectangleF rectangle)
  3689. {
  3690. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3691. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3692. {
  3693. foreach (PhaseModel phaseModel in choiseList)
  3694. {
  3695. phaseModel.mat = PreActionIntent.SingleChoiseOval(phaseModel.mat, phaseModel.color, rectangle);
  3696. this.panel.Refresh();
  3697. }
  3698. }
  3699. }
  3700. /// <summary>
  3701. /// 预处理 - 交互操作 - 选择 - 多边形选择
  3702. /// </summary>
  3703. /// <param name="rectangle"></param>
  3704. public void BinaryActionChoisePolygon(List<PointF> points)
  3705. {
  3706. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3707. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3708. {
  3709. foreach (PhaseModel phaseModel in choiseList)
  3710. {
  3711. phaseModel.mat = PreActionIntent.SingleChoisePolygon(phaseModel.mat, phaseModel.color, points);
  3712. this.panel.Refresh();
  3713. }
  3714. }
  3715. }
  3716. /// <summary>
  3717. /// 预处理 - 交互操作 - 删除 - 单个删除
  3718. /// </summary>
  3719. /// <param name="point"></param>
  3720. public void BinaryActionDelete(PointF point)
  3721. {
  3722. Point point1 = GetScalePoint(point);
  3723. if (point1.X >= 0 && point1.Y >= 0
  3724. && point1.X < this.CompositionSurface.Width
  3725. && point1.Y < this.CompositionSurface.Height)
  3726. {
  3727. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3728. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3729. {
  3730. foreach (PhaseModel p in choiseList)
  3731. {
  3732. Color color = Color.FromArgb(p.color);
  3733. int c = (color.R > color.G) ? ((color.R > color.B) ? color.R : color.B) : ((color.G > color.B) ? color.G : color.B);
  3734. p.mat = PreActionIntent.SingleDelete(p.mat, point1, c);
  3735. this.panel.Refresh();
  3736. }
  3737. }
  3738. }
  3739. }
  3740. /// <summary>
  3741. /// 预处理 - 交互操作 - 删除 - 矩形删除
  3742. /// </summary>
  3743. /// <param name="rect"></param>
  3744. public void BinaryActionDelete(RectangleF rect)
  3745. {
  3746. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3747. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3748. {
  3749. foreach (PhaseModel p in choiseList)
  3750. {
  3751. p.mat = PreActionIntent.RectangleDelete(p.mat, rect);
  3752. this.panel.Refresh();
  3753. }
  3754. }
  3755. }
  3756. /// <summary>
  3757. /// 预处理 - 交互操作 - 删除 - 椭圆删除
  3758. /// </summary>
  3759. /// <param name="rect"></param>
  3760. public void BinaryActionDeleteOval(RectangleF rect)
  3761. {
  3762. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3763. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3764. {
  3765. foreach (PhaseModel p in choiseList)
  3766. {
  3767. p.mat = PreActionIntent.OvalDelete(p.mat, rect);
  3768. this.panel.Refresh();
  3769. }
  3770. }
  3771. }
  3772. /// <summary>
  3773. /// 预处理 - 交互操作 - 删除 - 多边形删除
  3774. /// </summary>
  3775. /// <param name="rect"></param>
  3776. public void BinaryActionDelete(List<PointF> points)
  3777. {
  3778. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3779. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3780. {
  3781. foreach (PhaseModel p in choiseList)
  3782. {
  3783. p.mat = PreActionIntent.PolygonDelete(p.mat, points);
  3784. this.panel.Refresh();
  3785. }
  3786. }
  3787. }
  3788. /// <summary>
  3789. /// 预处理 - 交互操作 - 直线分割
  3790. /// </summary>
  3791. public void BinaryActionSplitLine(PointF start, PointF end)
  3792. {
  3793. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3794. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3795. {
  3796. foreach (PhaseModel p in choiseList)
  3797. {
  3798. p.mat = PreActionIntent.LineSplit(p.mat, start, end, InvariantData.segmentation);
  3799. this.panel.Refresh();
  3800. }
  3801. }
  3802. }
  3803. /// <summary>
  3804. /// 预处理 - 连接 - 直线
  3805. /// </summary>
  3806. /// <param name="start"></param>
  3807. /// <param name="end"></param>
  3808. public void BinaryActionConnectionLine(PointF start, PointF end)
  3809. {
  3810. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3811. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3812. {
  3813. foreach (PhaseModel p in choiseList)
  3814. {
  3815. p.mat = PreActionIntent.LineConnection(p.mat, p.color, start, end, InvariantData.connection);
  3816. this.panel.Refresh();
  3817. }
  3818. }
  3819. }
  3820. /// <summary>
  3821. /// 预处理 - 连接 - 椭圆
  3822. /// </summary>
  3823. /// <param name="start"></param>
  3824. /// <param name="end"></param>
  3825. public void BinaryConnectionOval(RectangleF rect)
  3826. {
  3827. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3828. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3829. {
  3830. foreach (PhaseModel p in choiseList)
  3831. {
  3832. p.mat = PreActionIntent.EllipseConnection(p.mat, p.color, rect, InvariantData.connection);
  3833. this.panel.Refresh();
  3834. }
  3835. }
  3836. }
  3837. /// <summary>
  3838. /// 预处理 - 添加 - 椭圆
  3839. /// </summary>
  3840. /// <param name="start"></param>
  3841. /// <param name="end"></param>
  3842. public void BinaryActionAddOval(RectangleF rect)
  3843. {
  3844. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3845. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3846. {
  3847. foreach (PhaseModel p in choiseList)
  3848. {
  3849. p.mat = PreActionIntent.EllipseAdd(p.mat, p.color, rect);
  3850. this.panel.Refresh();
  3851. }
  3852. }
  3853. }
  3854. /// <summary>
  3855. /// 预处理 - 添加 - 矩形
  3856. /// </summary>
  3857. /// <param name="rect"></param>
  3858. public void BinaryActionAddRectangle(RectangleF rect)
  3859. {
  3860. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3861. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3862. {
  3863. foreach (PhaseModel p in choiseList)
  3864. {
  3865. p.mat = PreActionIntent.RectangleAdd(p.mat, p.color, rect);
  3866. this.panel.Refresh();
  3867. }
  3868. }
  3869. }
  3870. /// <summary>
  3871. /// 预处理 - 交互操作 - 多边形添加
  3872. /// </summary>
  3873. /// <param name="rect"></param>
  3874. public void BinaryActionAddPolygon(List<PointF> points)
  3875. {
  3876. if (points.Count > 2)
  3877. {
  3878. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3879. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3880. {
  3881. foreach (PhaseModel p in choiseList)
  3882. {
  3883. p.mat = PreActionIntent.PolygonAdd(p.mat, p.color, points);
  3884. this.panel.Refresh();
  3885. }
  3886. }
  3887. }
  3888. }
  3889. /// <summary>
  3890. /// 预处理 - 交互操作 - 椭圆分割
  3891. /// </summary>
  3892. /// <param name="rect"></param>
  3893. public void BinaryActionSplitOval(RectangleF rect)
  3894. {
  3895. List<PhaseModel> choiseList = this.phaseModels.FindAll(a => a.choise == true);
  3896. if (this.phaseModels != null && this.phaseModels.Count > 0 && choiseList.Count > 0)
  3897. {
  3898. foreach (PhaseModel p in choiseList)
  3899. {
  3900. p.mat = PreActionIntent.EllipseSplit(p.mat, rect, InvariantData.segmentation);
  3901. this.panel.Refresh();
  3902. }
  3903. }
  3904. }
  3905. #endregion
  3906. #region 物相提取
  3907. public event EventHandler<EventArgs<List<Object>>> PPhaseActionFinish;
  3908. private void OnPPhaseActionFinished(List<Object> mat)
  3909. {
  3910. if (PPhaseActionFinish != null)
  3911. {
  3912. PPhaseActionFinish(this, new EventArgs<List<Object>>(mat));
  3913. }
  3914. }
  3915. /// <summary>
  3916. /// 物相提取 - 交互操作 - 矩形选择
  3917. /// </summary>
  3918. /// <param name="point"></param>
  3919. public void PPhaseActionRectangle(RectangleF rect)
  3920. {
  3921. List<Object> objects = new List<object>();
  3922. objects.Add(ContourParameters.Rectangle);
  3923. objects.Add(rect);
  3924. OnPPhaseActionFinished(objects);
  3925. }
  3926. /// <summary>
  3927. /// 物相提取 - 交互操作 - 多边形选择
  3928. /// </summary>
  3929. /// <param name="points"></param>
  3930. public void PPhaseActionPolygon(List<PointF> points)
  3931. {
  3932. List<Object> objects = new List<object>();
  3933. objects.Add(ContourParameters.Polygon);
  3934. objects.Add(points);
  3935. OnPPhaseActionFinished(objects);
  3936. }
  3937. /// <summary>
  3938. /// 物相提取 - 交互操作 - 椭圆选择
  3939. /// </summary>
  3940. /// <param name="rect"></param>
  3941. public void PPhaseActionOval(RectangleF rect)
  3942. {
  3943. List<Object> objects = new List<object>();
  3944. objects.Add(ContourParameters.Oval);
  3945. objects.Add(rect);
  3946. OnPPhaseActionFinished(objects);
  3947. }
  3948. #endregion
  3949. #region 视场相关
  3950. /// <summary>
  3951. /// 获取界面中的视场区域
  3952. /// </summary>
  3953. /// <returns></returns>
  3954. public Region GetRegion()
  3955. {
  3956. Region a = null;
  3957. if (this.GraphicsList != null && this.GraphicsList.Count > 0)
  3958. {
  3959. for (int i = this.GraphicsList.Count - 1; i >= 0; i--)
  3960. {
  3961. if (this.GraphicsList[i].objectType == DrawClass.View)
  3962. {
  3963. ViewBase view = (ViewBase)this.GraphicsList[i];
  3964. GraphicsPath path = new GraphicsPath();
  3965. switch (view.drawToolType)
  3966. {
  3967. case DrawToolType.ViewOval:
  3968. case DrawToolType.ViewCircle:
  3969. path.AddClosedCurve(view.Points.ToArray());
  3970. break;
  3971. case DrawToolType.ViewRectangle:
  3972. case DrawToolType.ViewSquare:
  3973. case DrawToolType.ViewTriangle:
  3974. case DrawToolType.ViewPolygon:
  3975. case DrawToolType.ViewRectangleEx:
  3976. case DrawToolType.ViewTriangleEx:
  3977. if (view.GetPoints().Count > 2)
  3978. {
  3979. path.AddPolygon(view.GetPoints().ToArray());
  3980. }
  3981. break;
  3982. }
  3983. if (a == null)
  3984. {
  3985. a = new Region(path);
  3986. }
  3987. else
  3988. {
  3989. if (view.combineMode == CombineMode.Union)
  3990. a.Union(path);
  3991. else
  3992. a.Exclude(path);
  3993. }
  3994. }
  3995. }
  3996. }
  3997. return a;
  3998. }
  3999. /// <summary>
  4000. /// 剪切视场/透明剪切(通过颜色区分)
  4001. /// </summary>
  4002. /// <param name="transparent">是否透明</param>
  4003. /// <returns></returns>
  4004. public Bitmap GetCutSizeWithColorWhiteOrTransparentColor(bool transparent)
  4005. {
  4006. Region region = this.GetRegion();
  4007. Bitmap bitmap = this.CompositionSurface.CreateAliasedBitmap();
  4008. if (region != null)
  4009. {
  4010. //创建新图位图
  4011. Bitmap newBitmap = new Bitmap(bitmap.Width, bitmap.Height);
  4012. //将新位图全部设置一个背景色
  4013. Graphics newGraphic = Graphics.FromImage(newBitmap);
  4014. newGraphic.Clear(Color.FromArgb(0, 0, 0, 0));
  4015. //设置新位图绘制区域
  4016. newGraphic.Clip = region;
  4017. //绘制白色背景色
  4018. newGraphic.Clear(Color.FromArgb(255, 255, 255, 255));
  4019. return newBitmap;
  4020. }
  4021. return bitmap;
  4022. }
  4023. /// <summary>
  4024. /// 剪切视场/透明剪切
  4025. /// </summary>
  4026. /// <param name="transparent">是否透明</param>
  4027. /// <returns></returns>
  4028. public Bitmap GetCutSizeWithColorWhiteOrTransparent(bool transparent)
  4029. {
  4030. Region region = this.GetRegion();
  4031. if (region != null)
  4032. {
  4033. //获取当前激活的图片
  4034. Bitmap bitmap = this.CompositionSurface.CreateAliasedBitmap();
  4035. //创建同样大小的新位图
  4036. Bitmap newBitmap = new Bitmap((int)(bitmap.Width), (int)(bitmap.Height));
  4037. //创建作图区域
  4038. Graphics newGraphic = Graphics.FromImage(newBitmap);
  4039. //如果不是透明,则用白色填充
  4040. if (!transparent) newGraphic.Clear(Color.White);
  4041. //设置新位图绘制区域
  4042. newGraphic.Clip = region;
  4043. //截取原图相应区域写入作图区
  4044. newGraphic.DrawImage(bitmap, 0, 0, new RectangleF(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  4045. //创建region的RectangleF大小的位图
  4046. Graphics graphic = Graphics.FromImage(bitmap);
  4047. RectangleF rectangleF = region.GetBounds(graphic);
  4048. Bitmap lastBitmap = new Bitmap((int)(rectangleF.Width), (int)(rectangleF.Height));
  4049. Graphics lastGraphic = Graphics.FromImage(lastBitmap);
  4050. lastGraphic.DrawImage(newBitmap, 0, 0, region.GetBounds(graphic), GraphicsUnit.Pixel);
  4051. return lastBitmap;
  4052. }
  4053. return null;
  4054. }
  4055. /// <summary>
  4056. /// 获取原图大小的Bitmap
  4057. /// 除了视场外的其余均为透明(但保留各通道像素值),
  4058. /// 主要是用于二值提取、物相提取等和视场关联的地方
  4059. /// </summary>
  4060. /// <returns></returns>
  4061. public Bitmap GetFullSizeWithRegion(Bitmap bitmap = null)
  4062. {
  4063. Region region = this.GetRegion();
  4064. if (region != null)
  4065. {
  4066. if (bitmap == null) bitmap = this.CompositionSurface.CreateAliasedBitmap();
  4067. //创建同样的像素值的新位图,不过是透明的
  4068. Bitmap newBitmap = BaseTools.BitmapToTransparent(bitmap);
  4069. //创建作图区域
  4070. Graphics newGraphic = Graphics.FromImage(newBitmap);
  4071. //设置新位图绘制区域
  4072. newGraphic.Clip = region;
  4073. //截取原图相应区域写入作图区
  4074. newGraphic.DrawImage(bitmap, 0, 0, new RectangleF(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);
  4075. return newBitmap;
  4076. }
  4077. return null;
  4078. }
  4079. #endregion
  4080. #region 初始化控件
  4081. private void InitializeComponent()
  4082. {
  4083. this.components = new Container();
  4084. this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
  4085. this.topRuler = new Ruler();
  4086. this.leftRuler = new Ruler();
  4087. this.panel = new PanelEx();
  4088. this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
  4089. this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
  4090. this.toolStripMenuItem3 = new System.Windows.Forms.ToolStripMenuItem();
  4091. this.toolStripMenuItem4 = new System.Windows.Forms.ToolStripMenuItem();
  4092. //this.panelBottom = new PanelBottom();
  4093. //this.panelBottom.SuspendLayout();
  4094. this.panel.SuspendLayout();
  4095. this.contextMenuStrip1.SuspendLayout();
  4096. this.SuspendLayout();
  4097. //
  4098. // topRuler
  4099. //
  4100. this.topRuler.BackColor = Color.White;
  4101. this.topRuler.Dock = DockStyle.Top;
  4102. this.topRuler.Location = new Point(0, 0);
  4103. this.topRuler.Name = "topRuler";
  4104. this.topRuler.Offset = -16;
  4105. this.topRuler.Size = UI.ScaleSize(new Size(384, 16));
  4106. this.topRuler.TabIndex = 3;
  4107. //
  4108. // leftRuler
  4109. //
  4110. this.leftRuler.BackColor = Color.White;
  4111. this.leftRuler.Dock = DockStyle.Left;
  4112. this.leftRuler.Location = new Point(0, 16);
  4113. this.leftRuler.Name = "leftRuler";
  4114. this.leftRuler.Orientation = Orientation.Vertical;
  4115. this.leftRuler.Size = UI.ScaleSize(new Size(16, 304));
  4116. this.leftRuler.TabIndex = 4;
  4117. //
  4118. // panel
  4119. //
  4120. this.panel.ContextMenuStrip = this.contextMenuStrip1;
  4121. this.panel.AutoScroll = true;
  4122. this.panel.Dock = DockStyle.Fill;
  4123. this.panel.Location = new Point(16, 16);
  4124. this.panel.Name = "panel";
  4125. this.panel.ScrollPosition = new Point(0, 0);
  4126. this.panel.Size = new Size(368, 304);
  4127. this.panel.TabIndex = 5;
  4128. this.panel.AllowDrop = false;
  4129. this.panel.Scroll += new ScrollEventHandler(this.Panel_Scroll);
  4130. this.panel.KeyDown += new KeyEventHandler(Panel_KeyDown);
  4131. this.panel.KeyUp += new KeyEventHandler(Panel_KeyUp);
  4132. this.panel.KeyPress += new KeyPressEventHandler(Panel_KeyPress);
  4133. this.panel.GotFocus += new EventHandler(Panel_GotFocus);
  4134. this.panel.LostFocus += new EventHandler(Panel_LostFocus);
  4135. //
  4136. // contextMenuStrip1
  4137. //
  4138. this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
  4139. this.toolStripMenuItem1,
  4140. this.toolStripMenuItem2,
  4141. this.toolStripMenuItem3,
  4142. this.toolStripMenuItem4});
  4143. this.contextMenuStrip1.Name = "contextMenuStrip1";
  4144. this.contextMenuStrip1.Size = new System.Drawing.Size(69, 48);
  4145. //this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.ContextMenuStrip1_Opening);
  4146. this.contextMenuStrip1.Visible = false;
  4147. //
  4148. //
  4149. // toolStripMenuItem1
  4150. //
  4151. this.toolStripMenuItem1.Name = "toolStripMenuItem1";
  4152. this.toolStripMenuItem1.Text = PdnResources.GetString("Menu.Setting.MeasureSetting.Text");
  4153. this.toolStripMenuItem1.Size = new System.Drawing.Size(68, 22);
  4154. this.toolStripMenuItem1.Click += new System.EventHandler(this.ToolStripMenuItem1_Click);
  4155. this.toolStripMenuItem1.Visible = false;
  4156. //
  4157. // toolStripMenuItem2
  4158. //
  4159. this.toolStripMenuItem2.Name = "toolStripMenuItem2";
  4160. this.toolStripMenuItem2.Text = PdnResources.GetString("Menu.Setting.LabelSetting.Text");
  4161. this.toolStripMenuItem2.Size = new System.Drawing.Size(68, 22);
  4162. this.toolStripMenuItem2.Click += new System.EventHandler(this.ToolStripMenuItem2_Click);
  4163. this.toolStripMenuItem2.Visible = false;
  4164. //
  4165. // toolStripMenuItem3
  4166. //
  4167. this.toolStripMenuItem3.Name = "toolStripMenuItem3";
  4168. this.toolStripMenuItem3.Text = PdnResources.GetString("Menu.3celaing.Text");
  4169. this.toolStripMenuItem3.Size = new System.Drawing.Size(68, 22);
  4170. this.toolStripMenuItem3.Click += new System.EventHandler(this.ToolStripMenuItem3_Click);
  4171. this.toolStripMenuItem3.Visible = false;
  4172. //
  4173. // toolStripMenuItem4
  4174. //
  4175. this.toolStripMenuItem4.Name = "toolStripMenuItem4";
  4176. this.toolStripMenuItem4.Text = PdnResources.GetString("Menu.biaozhushuxing.Text");
  4177. this.toolStripMenuItem4.Size = new System.Drawing.Size(68, 22);
  4178. this.toolStripMenuItem4.Click += new System.EventHandler(this.ToolStripMenuItem4_Click);
  4179. this.toolStripMenuItem4.Visible = false;
  4180. // 底部的panelBottom
  4181. //
  4182. /*this.panelBottom.Size = new Size(384, 30);
  4183. this.panelBottom.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
  4184. this.panelBottom.Dock = DockStyle.Bottom;
  4185. this.panelBottom.trackBar.ValueChanged += new EventHandler(this.PanelBottom_trackBar_ValueChanged);
  4186. this.panelBottom.BackColor = Color.FromArgb(240, 240, 240);*/
  4187. //
  4188. // DocumentView
  4189. //
  4190. this.Controls.Add(this.panel);
  4191. this.Controls.Add(this.leftRuler);
  4192. this.Controls.Add(this.topRuler);
  4193. //this.Controls.Add(this.panelBottom);
  4194. this.Name = "DocumentView";
  4195. this.Size = new System.Drawing.Size(384, 320);
  4196. this.panel.ResumeLayout(false);
  4197. //this.panelBottom.ResumeLayout(false);
  4198. this.contextMenuStrip1.ResumeLayout(false);
  4199. this.ResumeLayout(false);
  4200. }
  4201. public void ShowContextMenuStrip1()
  4202. {
  4203. this.contextMenuStrip1.Visible = true;
  4204. if (toolNumber == 0)
  4205. {
  4206. contextMenuStrip1.Visible = true;
  4207. this.toolStripMenuItem1.Visible = true;
  4208. this.toolStripMenuItem2.Visible = true;
  4209. this.toolStripMenuItem3.Visible = false;
  4210. this.toolStripMenuItem4.Visible = false;
  4211. }
  4212. else if (toolNumber == 1)
  4213. {
  4214. contextMenuStrip1.Visible = true;
  4215. this.toolStripMenuItem1.Visible = false;
  4216. this.toolStripMenuItem2.Visible = false;
  4217. this.toolStripMenuItem3.Visible = true;
  4218. this.toolStripMenuItem4.Visible = false;
  4219. }
  4220. else if (toolNumber == 2)
  4221. {
  4222. contextMenuStrip1.Visible = true;
  4223. this.toolStripMenuItem1.Visible = false;
  4224. this.toolStripMenuItem2.Visible = false;
  4225. this.toolStripMenuItem3.Visible = false;
  4226. this.toolStripMenuItem4.Visible = true;
  4227. }
  4228. else
  4229. {
  4230. contextMenuStrip1.Visible = false;
  4231. this.toolStripMenuItem1.Visible = false;
  4232. this.toolStripMenuItem2.Visible = false;
  4233. this.toolStripMenuItem3.Visible = false;
  4234. this.toolStripMenuItem4.Visible = false;
  4235. }
  4236. }
  4237. #endregion
  4238. #region 设定图片位置
  4239. // add by maxb 20200716
  4240. /// <summary>
  4241. /// 图片位置坐标
  4242. /// </summary>
  4243. private bool m_center = true;
  4244. private int m_pTop = 0;
  4245. private int m_pLeft = 0;
  4246. private int m_initTop = 0;
  4247. private int m_initLeft = 0;
  4248. public void SetDocumentPoint(int x, int y)
  4249. {
  4250. m_center = false;
  4251. m_initLeft = x;
  4252. m_initTop = y;
  4253. UpdateImgLocation();
  4254. this.Refresh();
  4255. }
  4256. public void SetCenter(bool center)
  4257. {
  4258. m_center = center;
  4259. }
  4260. private void UpdateImgLocation()
  4261. {
  4262. if (!m_center)
  4263. {
  4264. //Console.WriteLine("m_pLeft: " + m_initLeft + " Ratio:" + this.scaleFactor.Ratio);
  4265. m_pLeft = 16 + (int)(m_initLeft * this.scaleFactor.Ratio);
  4266. m_pTop = 16 + (int)(m_initTop * this.scaleFactor.Ratio);
  4267. //Console.WriteLine("Location: " + m_pLeft + " " + m_pTop);
  4268. }
  4269. }
  4270. #endregion
  4271. #region 工艺图比照
  4272. private RectangleF m_artworkImageRectangle;
  4273. private Bitmap m_selectedBitmap;
  4274. /// <summary>
  4275. /// 工艺比照定位点图像绘制
  4276. /// </summary>
  4277. /// <param name="bitmap"></param>
  4278. /// <param name="rectangle"></param>
  4279. public void SetSelectedBitmap(Bitmap bitmap, RectangleF rectangle)
  4280. {
  4281. m_selectedBitmap = bitmap;
  4282. m_artworkImageRectangle = Rectangle.Ceiling(rectangle);
  4283. this.Refresh();
  4284. }
  4285. /// <summary>
  4286. /// 工艺比照定位点
  4287. /// </summary>
  4288. /// <param name="width"></param>
  4289. /// <param name="height"></param>
  4290. public void CreateArtworkRectangle(int id, Pen pen, Rectangle rect)
  4291. {
  4292. tools[(int)activeTool].InvokeMember("CreateRectangle",
  4293. BindingFlags.Public |
  4294. BindingFlags.Static |
  4295. BindingFlags.InvokeMethod,
  4296. null,
  4297. null,
  4298. new object[4] { this, id, pen, rect });
  4299. }
  4300. #endregion
  4301. #region 图像拼接
  4302. /// <summary>
  4303. /// 生成自定义拼图区域
  4304. /// </summary>
  4305. /// <param name="width"></param>
  4306. /// <param name="height"></param>
  4307. public void CreateStitchingRectangle(int width, int height)
  4308. {
  4309. int x = m_pLeft;
  4310. int y = m_pTop;
  4311. tools[(int)activeTool].InvokeMember("CreateRectangle",
  4312. BindingFlags.Public |
  4313. BindingFlags.Static |
  4314. BindingFlags.InvokeMethod,
  4315. null,
  4316. null,
  4317. new object[5] { this, x, y, width, height });//
  4318. }
  4319. /// <summary>
  4320. /// 获取对应标尺上的坐标点
  4321. /// </summary>
  4322. /// <param name="point"></param>
  4323. /// <returns></returns>
  4324. public PointF GetRulerPointInPanel(PointF point)
  4325. {
  4326. return new PointF(
  4327. (float)((point.X - 16) / this.scaleFactor.Ratio),
  4328. (float)((point.Y - 16) / this.scaleFactor.Ratio)
  4329. );
  4330. }
  4331. public PointF ScalePointToRulerPoint(PointF point)
  4332. {
  4333. Point originPoint = GetCalcOriginPoint();
  4334. return new PointF(
  4335. (float)((point.X * this.scaleFactor.Ratio + originPoint.X - 16) / this.scaleFactor.Ratio),
  4336. (float)((point.Y * this.scaleFactor.Ratio + originPoint.Y - 16) / this.scaleFactor.Ratio)
  4337. );
  4338. }
  4339. /// <summary>
  4340. /// 是否开启鼠标左键双击移动视场
  4341. /// </summary>
  4342. public bool ViewMoveOnMouseLeftDoubleClickEnable
  4343. {
  4344. get; set;
  4345. }
  4346. #endregion
  4347. #region 位置列表
  4348. /// <summary>
  4349. /// 生成自定义拼图区域
  4350. /// </summary>
  4351. /// <param name="width"></param>
  4352. /// <param name="height"></param>
  4353. public void CreateLocationCross(int x, int y, int width, int height, int z)
  4354. {
  4355. //m_pLeft = x;
  4356. //m_pTop = y;
  4357. x = (int)(m_pLeft + x * this.scaleFactor.Ratio);
  4358. y = (int)(m_pTop + y * this.scaleFactor.Ratio);
  4359. ToolLocationCross.CreateLocationCross(this, x, y, width, height, z);//
  4360. }
  4361. #endregion
  4362. #region 右键属性
  4363. /// <summary>
  4364. /// 判断右键的工具
  4365. /// </summary>
  4366. public int ToolNumber
  4367. {
  4368. set
  4369. {
  4370. this.toolNumber = value;
  4371. }
  4372. }
  4373. /// <summary>
  4374. /// 设置右键菜单的选项
  4375. /// </summary>
  4376. /// <param name="sender"></param>
  4377. /// <param name="e"></param>
  4378. private void ContextMenuStrip1_Opening(object sender, CancelEventArgs e)
  4379. {
  4380. if (toolNumber == 0)
  4381. {
  4382. contextMenuStrip1.Visible = true;
  4383. this.toolStripMenuItem1.Visible = true;
  4384. this.toolStripMenuItem2.Visible = true;
  4385. this.toolStripMenuItem3.Visible = false;
  4386. this.toolStripMenuItem4.Visible = false;
  4387. }
  4388. else if (toolNumber == 1)
  4389. {
  4390. contextMenuStrip1.Visible = true;
  4391. this.toolStripMenuItem1.Visible = false;
  4392. this.toolStripMenuItem2.Visible = false;
  4393. this.toolStripMenuItem3.Visible = true;
  4394. this.toolStripMenuItem4.Visible = false;
  4395. }
  4396. else if (toolNumber == 2)
  4397. {
  4398. contextMenuStrip1.Visible = true;
  4399. this.toolStripMenuItem1.Visible = false;
  4400. this.toolStripMenuItem2.Visible = false;
  4401. this.toolStripMenuItem3.Visible = false;
  4402. this.toolStripMenuItem4.Visible = true;
  4403. }
  4404. else
  4405. {
  4406. contextMenuStrip1.Visible = false;
  4407. this.toolStripMenuItem1.Visible = false;
  4408. this.toolStripMenuItem2.Visible = false;
  4409. this.toolStripMenuItem3.Visible = false;
  4410. this.toolStripMenuItem4.Visible = false;
  4411. }
  4412. }
  4413. /// <summary>
  4414. /// 右键菜单(测量设置)
  4415. /// </summary>
  4416. /// <param name="sender"></param>
  4417. /// <param name="e"></param>
  4418. public virtual void ToolStripMenuItem1_Click(object sender, EventArgs e)
  4419. {
  4420. }
  4421. /// <summary>
  4422. /// 右键菜单(标注设置)
  4423. /// </summary>
  4424. /// <param name="sender"></param>
  4425. /// <param name="e"></param>
  4426. public virtual void ToolStripMenuItem2_Click(object sender, EventArgs e)
  4427. {
  4428. }
  4429. /// <summary>
  4430. /// 右键菜单(测量属性)
  4431. /// </summary>
  4432. /// <param name="sender"></param>
  4433. /// <param name="e"></param>
  4434. public virtual void ToolStripMenuItem3_Click(object sender, EventArgs e)
  4435. {
  4436. }
  4437. /// <summary>
  4438. /// 右键菜单(标注属性)
  4439. /// </summary>
  4440. /// <param name="sender"></param>
  4441. /// <param name="e"></param>
  4442. public virtual void ToolStripMenuItem4_Click(object sender, EventArgs e)
  4443. {
  4444. }
  4445. #endregion
  4446. #region 对应图片保存的xml参数
  4447. public bool existenceXML = false;//当前图片是否存在对应的xml
  4448. public mic_rulers xmlSaveModel;//存储标尺信息(xml或者预览拍摄选中标尺)
  4449. /// <summary>
  4450. /// 换算完的标尺信息,包含所有系统内得单位
  4451. /// </summary>
  4452. public Dictionary<MeasurementUnit, double> rules = new Dictionary<MeasurementUnit, double>();
  4453. ///// <summary>
  4454. ///// 系统内选定的单位
  4455. ///// </summary>
  4456. //public MeasurementUnit measurementUnit;
  4457. public mic_rulers RuleAttribute(PicConfigModel PicConfigModel)
  4458. {
  4459. this.xmlSaveModel = new mic_rulers();
  4460. //this.xmlSaveModel.rule = new XmlSaveModel.Rule();
  4461. this.xmlSaveModel.ruler_name = PicConfigModel.rule.ruler_name;
  4462. this.xmlSaveModel.gain_multiple = PicConfigModel.rule.gain_multiple;
  4463. this.xmlSaveModel.pixel_length = PicConfigModel.rule.pixel_length;
  4464. this.xmlSaveModel.physical_length = PicConfigModel.rule.physical_length;
  4465. this.xmlSaveModel.ruler_units = PicConfigModel.rule.ruler_units;
  4466. InitRulerInfo();
  4467. this.existenceXML = true;
  4468. return this.xmlSaveModel;
  4469. }
  4470. //显微镜硬件参数MODEL
  4471. public Hardware hardware;
  4472. public void HardwareAttribute()
  4473. {
  4474. this.hardware = new Hardware();
  4475. }
  4476. public decimal GetMic_Rulers()
  4477. {
  4478. if (this.xmlSaveModel != null)
  4479. {
  4480. return this.xmlSaveModel.gain_multiple;
  4481. }
  4482. else
  4483. {
  4484. return appWorkspace.GetGainMultiple();
  4485. }
  4486. }
  4487. /// <summary>
  4488. /// 初始化标尺信息,图片对应的xml中读取
  4489. /// 然后根据标尺的单位,换算出所有单位的数据
  4490. /// </summary>
  4491. public void InitRulerInfo()
  4492. {
  4493. List<mic_rulers> mic_rulersAll = Mic_rulersAll();
  4494. if (this.xmlSaveModel == null && mic_rulersAll.Count > 0)
  4495. this.xmlSaveModel = mic_rulersAll[0];
  4496. this.rules.Clear();
  4497. if (this.xmlSaveModel != null && this.xmlSaveModel.pixel_length != 0)
  4498. {
  4499. //计算单位长度 比如0.05英寸/像素; 100纳米/像素;
  4500. double unitLength = (double)(this.xmlSaveModel.physical_length / (decimal)this.xmlSaveModel.pixel_length);
  4501. switch (this.xmlSaveModel.ruler_units)
  4502. {
  4503. case (int)MeasurementUnit.Inch://英寸
  4504. this.rules.Add(MeasurementUnit.Inch, unitLength); //英寸
  4505. this.rules.Add(MeasurementUnit.Mil, 1000 * unitLength); //米尔
  4506. this.rules.Add(MeasurementUnit.Centimeter, 2.54 * unitLength); //厘米
  4507. this.rules.Add(MeasurementUnit.Millimeter, 25.4 * unitLength); //毫米
  4508. this.rules.Add(MeasurementUnit.Micron, 25400 * unitLength); //微米
  4509. this.rules.Add(MeasurementUnit.Nano, 25400000 * unitLength); //纳米
  4510. break;
  4511. case (int)MeasurementUnit.Mil://米尔
  4512. this.rules.Add(MeasurementUnit.Inch, 0.001 * unitLength); //英寸
  4513. this.rules.Add(MeasurementUnit.Mil, unitLength); //米尔
  4514. this.rules.Add(MeasurementUnit.Centimeter, 0.00254 * unitLength); //厘米
  4515. this.rules.Add(MeasurementUnit.Millimeter, 0.0254 * unitLength); //毫米
  4516. this.rules.Add(MeasurementUnit.Micron, 25.4 * unitLength); //微米
  4517. this.rules.Add(MeasurementUnit.Nano, 25400 * unitLength); //纳米
  4518. break;
  4519. case (int)MeasurementUnit.Centimeter://厘米
  4520. this.rules.Add(MeasurementUnit.Inch, 0.3937008 * unitLength); //英寸
  4521. this.rules.Add(MeasurementUnit.Mil, 393.7008 * unitLength); //米尔
  4522. this.rules.Add(MeasurementUnit.Centimeter, unitLength); //厘米
  4523. this.rules.Add(MeasurementUnit.Millimeter, 10 * unitLength); //毫米
  4524. this.rules.Add(MeasurementUnit.Micron, 10000 * unitLength); //微米
  4525. this.rules.Add(MeasurementUnit.Nano, 10000000 * unitLength); //纳米
  4526. break;
  4527. case (int)MeasurementUnit.Millimeter://毫米
  4528. this.rules.Add(MeasurementUnit.Inch, 0.0393701 * unitLength); //英寸
  4529. this.rules.Add(MeasurementUnit.Mil, 39.3701 * unitLength); //米尔
  4530. this.rules.Add(MeasurementUnit.Centimeter, 0.1 * unitLength); //厘米
  4531. this.rules.Add(MeasurementUnit.Millimeter, unitLength); //毫米
  4532. this.rules.Add(MeasurementUnit.Micron, 1000 * unitLength); //微米
  4533. this.rules.Add(MeasurementUnit.Nano, 1000000 * unitLength); //纳米
  4534. break;
  4535. case (int)MeasurementUnit.Micron://微米
  4536. this.rules.Add(MeasurementUnit.Inch, 0.00003937007874 * unitLength); //英寸
  4537. this.rules.Add(MeasurementUnit.Mil, 0.03937007874 * unitLength); //米尔
  4538. this.rules.Add(MeasurementUnit.Centimeter, 0.0001 * unitLength); //厘米
  4539. this.rules.Add(MeasurementUnit.Millimeter, 0.001 * unitLength); //毫米
  4540. this.rules.Add(MeasurementUnit.Micron, unitLength); //微米
  4541. this.rules.Add(MeasurementUnit.Nano, 1000 * unitLength); //纳米
  4542. break;
  4543. case (int)MeasurementUnit.Nano://纳米
  4544. this.rules.Add(MeasurementUnit.Inch, 3.9370e-8 * unitLength); //英寸
  4545. this.rules.Add(MeasurementUnit.Mil, 3.9370e-5 * unitLength); //米尔
  4546. this.rules.Add(MeasurementUnit.Centimeter, 1e-7 * unitLength); //厘米
  4547. this.rules.Add(MeasurementUnit.Millimeter, 1e-6 * unitLength); //毫米
  4548. this.rules.Add(MeasurementUnit.Micron, 0.001 * unitLength); //微米
  4549. this.rules.Add(MeasurementUnit.Nano, unitLength); //纳米
  4550. break;
  4551. default:
  4552. this.xmlSaveModel = null;
  4553. break;
  4554. }
  4555. }
  4556. if (this.xmlSaveModel == null || this.xmlSaveModel.physical_length == 0)
  4557. {
  4558. //如果没有标尺的时候,默认1微米/像素
  4559. this.rules.Add(MeasurementUnit.Inch, 0.0000394); //英寸
  4560. this.rules.Add(MeasurementUnit.Mil, 0.0394); //米尔
  4561. this.rules.Add(MeasurementUnit.Centimeter, 0.0001); //厘米
  4562. this.rules.Add(MeasurementUnit.Millimeter, 0.001); //毫米
  4563. this.rules.Add(MeasurementUnit.Micron, 1); //微米
  4564. this.rules.Add(MeasurementUnit.Nano, 1000); //纳米
  4565. this.xmlSaveModel = new mic_rulers();
  4566. this.xmlSaveModel.ruler_name = PdnResources.GetString("Menu.Unselectedruler.Text");
  4567. this.xmlSaveModel.gain_multiple = 1;
  4568. this.xmlSaveModel.physical_length = 100;
  4569. this.xmlSaveModel.pixel_length = 100;
  4570. this.xmlSaveModel.ruler_units = (int)((MeasurementUnit)System.Enum.Parse(typeof(MeasurementUnit), this.getRulerList()[0]));
  4571. }
  4572. //设置文档标尺
  4573. Document.defaultDpi = 1 / rules[MeasurementUnit.Inch];
  4574. }
  4575. /// <summary>
  4576. /// 提供获取系统当前选中单位及每单位像素值公共方法
  4577. /// </summary>
  4578. /// <returns></returns>
  4579. public string[] getRulerList()
  4580. {
  4581. return startUpRules(this.rules.Count > 0 ? this.rules : null);
  4582. }
  4583. /// <summary>
  4584. /// 获取单位标尺
  4585. /// </summary>
  4586. /// <param name="measurementUnit">度量单位的枚举</param>
  4587. /// <returns></returns>
  4588. public double GetRuler(MeasurementUnit measurementUnit)
  4589. {
  4590. double unitLength = 0;
  4591. //判断当前图片是否有对应的标尺
  4592. if (this.rules.Count > 0)
  4593. {
  4594. this.rules.TryGetValue(measurementUnit, out unitLength);
  4595. }
  4596. else
  4597. {
  4598. this.appWorkspace.getMeasureInfo().TryGetValue(MeasurementUnit.Micron, out unitLength);
  4599. }
  4600. return unitLength;
  4601. }
  4602. /// <summary>
  4603. /// 获取放大倍数
  4604. /// </summary>
  4605. /// <returns></returns>
  4606. public double GetMultiple()
  4607. {
  4608. double multiple = 1;
  4609. if (this.xmlSaveModel != null)
  4610. {
  4611. multiple = (double)this.xmlSaveModel.gain_multiple;
  4612. }
  4613. return multiple;
  4614. }
  4615. /// <summary>
  4616. /// 获取单位标尺乘以放大倍数
  4617. /// </summary>
  4618. /// <param name="measurementUnit">度量单位的枚举</param>
  4619. /// <returns></returns>
  4620. public double GetRulerMultiple(MeasurementUnit measurementUnit)
  4621. {
  4622. double unitLength = 0;
  4623. double multiple = 0;
  4624. //判断当前图片是否有对应的标尺
  4625. if (this.rules.Count > 0)
  4626. {
  4627. this.rules.TryGetValue(measurementUnit, out unitLength);
  4628. }
  4629. else
  4630. {
  4631. this.appWorkspace.getMeasureInfo().TryGetValue(MeasurementUnit.Micron, out unitLength);
  4632. }
  4633. if (this.xmlSaveModel != null)
  4634. {
  4635. multiple = (double)this.xmlSaveModel.gain_multiple;
  4636. }
  4637. return unitLength * multiple;
  4638. }
  4639. public void SetContinuousDrawingLabel(bool v)
  4640. {
  4641. ContinuousDrawingLabel = v;
  4642. appWorkspace.SetContinuousDrawingLable(v);
  4643. }
  4644. public void SetContinuousDrawingMeasure(bool v)
  4645. {
  4646. ContinuousDrawingMeasure = v;
  4647. appWorkspace.SetContinuousDrawingMeasure(v);
  4648. }
  4649. #endregion
  4650. public Type[] GetTools()
  4651. {
  4652. return this.tools;
  4653. }
  4654. public MeasurementUnit GetMeasurementUnit()
  4655. {
  4656. return this.Units;
  4657. }
  4658. }
  4659. }