DocumentPreview.cs 164 KB

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