HWindow_Final.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. // 版权所有(C) ChoiceTech Corporation。保留所有权利。
  2. // 此代码的发布遵从
  3. // ChoiceTech 公共许可(HY-PL,http://choicetech.cn/hy-pl.html)的条款。
  4. //
  5. //版权所有(C) ChoiceTech Corporation。保留所有权利。
  6. using System;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.Drawing;
  10. using System.Data;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Windows.Forms;
  14. using HalconDotNet;
  15. using System.Threading;
  16. namespace ChoiceTech.Halcon.Control
  17. {
  18. public delegate void DoubleClick();
  19. /// <summary>
  20. /// halcon鼠标缩放控件
  21. ///
  22. /// 描述:
  23. /// 1, 必须首先通过this.HobjectToHimage(HObject hobject)传入图片,此图片称为"背景图"
  24. /// 2, 有了背景图,就可以通过本控件自定义的 this.DispObj(HObject hObj)显示HObject,类似原方法
  25. /// 3,默认显示红色,DispObj(HObject hObj,string color)可显示其他颜色
  26. /// </summary>
  27. public partial class HWindow_Final : UserControl
  28. {
  29. #region 私有变量定义.
  30. private HWindow /**/ hv_window; //halcon窗体控件的句柄 this.mCtrl_HWindow.HalconWindow;
  31. // 窗体控件右键菜单内容
  32. ToolStripMenuItem saveImg_strip;
  33. ToolStripMenuItem saveWindow_strip;
  34. ToolStripMenuItem barVisible_strip;
  35. ToolStripMenuItem histogram_strip;
  36. private HImage /**/ hv_image; //缩放时操作的图片 此处千万不要使用hv_image = new HImage(),不然在生成控件dll的时候,会导致无法序列化,去你妈隔壁,还好老子有版本控制,不然都找不到这种恶心问题
  37. private int /**/ hv_imageWidth, hv_imageHeight; //图片宽,高
  38. private string /**/ str_imgSize; //图片尺寸大小 5120X3840
  39. private bool /**/ drawModel = false; //绘制模式下,不允许缩放和鼠标右键菜单
  40. public ViewWindow.ViewWindow viewWindow; /**/ //ViewWindow
  41. public HWindowControl hWindowControl; /**/ // 当前halcon窗口
  42. #endregion
  43. /// <summary>
  44. /// 初始化控件
  45. /// </summary>
  46. public HWindow_Final()
  47. {
  48. InitializeComponent();
  49. //
  50. viewWindow = new ViewWindow.ViewWindow(mCtrl_HWindow);
  51. hWindowControl = this.mCtrl_HWindow;
  52. hv_window = this.mCtrl_HWindow.HalconWindow;
  53. // 设定鼠标按下时图标的形状
  54. // 'arrow' 'default' 'crosshair' 'text I-beam' 'Slashed circle' 'Size All'
  55. // 'Size NESW' 'Size S' 'Size NWSE' 'Size WE' 'Vertical Arrow' 'Hourglass'
  56. //
  57. // hv_window.SetMshape("Hourglass");
  58. barVisible_strip = new ToolStripMenuItem("显示StatusBar");
  59. barVisible_strip.CheckOnClick = true;
  60. barVisible_strip.CheckedChanged += new EventHandler(barVisible_strip_CheckedChanged);
  61. m_CtrlHStatusLabelCtrl.Visible = false;
  62. mCtrl_HWindow.Height = this.Height;
  63. saveImg_strip = new ToolStripMenuItem("保存原始图像");
  64. saveImg_strip.Click += new EventHandler((s, e) => SaveImage());
  65. saveWindow_strip = new ToolStripMenuItem("保存窗口缩略图");
  66. saveWindow_strip.Click += new EventHandler((s, e) => SaveWindowDump());
  67. histogram_strip = new ToolStripMenuItem("显示直方图(H)");
  68. histogram_strip.CheckOnClick = true;
  69. histogram_strip.Checked = false;
  70. barVisible_strip.Enabled = true;
  71. histogram_strip.Enabled = false;
  72. saveImg_strip.Enabled = false;
  73. saveWindow_strip.Enabled = false;
  74. mCtrl_HWindow.SizeChanged += new EventHandler((s, e) => DispImageFit());
  75. }
  76. /// <summary>
  77. /// 绘制模式下,不允许缩放和鼠标右键菜单
  78. /// </summary>
  79. public bool DrawModel
  80. {
  81. get { return drawModel; }
  82. set
  83. {
  84. //缩放控制
  85. viewWindow.setDrawModel(value);
  86. drawModel = value;
  87. }
  88. }
  89. private bool _EditModel = true;//绘制的图形是否可以编辑
  90. public bool EditModel
  91. {
  92. get
  93. {
  94. return _EditModel;
  95. }
  96. set
  97. {
  98. viewWindow.setEditModel(value);
  99. _EditModel = value;
  100. }
  101. }
  102. /// <summary>
  103. /// 设置image,初始化控件参数
  104. /// </summary>
  105. public HImage Image
  106. {
  107. get
  108. {
  109. return this.hv_image;
  110. }
  111. set
  112. {
  113. if (value != null)
  114. {
  115. if (this.hv_image != null)
  116. {
  117. this.hv_image.Dispose();
  118. }
  119. this.hv_image = value;
  120. hv_image.GetImageSize(out hv_imageWidth, out hv_imageHeight);
  121. str_imgSize = String.Format("{0}X{1}", hv_imageWidth, hv_imageHeight);
  122. //DispImageFit(mCtrl_HWindow);
  123. try
  124. {
  125. barVisible_strip.Enabled = true;
  126. histogram_strip.Enabled = true;
  127. saveImg_strip.Enabled = true;
  128. saveWindow_strip.Enabled = true;
  129. }
  130. catch (Exception)
  131. {
  132. }
  133. int W, H, w, h;
  134. W = this.Width;
  135. H = this.Height;
  136. w = hv_imageWidth;
  137. h = hv_imageHeight;
  138. //if (w * 1.0 / W < h * 1.0 / H)
  139. //{
  140. // HOperatorSet.SetPart(this.HWindowHalconID, 0, -(h * W / H - w) / 2, h, w + (h * W / H - w) / 2);
  141. //}
  142. //else if (w * 1.0 / W > h * 1.0 / H)
  143. //{
  144. // HOperatorSet.SetPart(this.HWindowHalconID, (h - w * H / H) / 2, 0, h - (h - w * H / H) / 2, w);
  145. //}
  146. //else
  147. //{
  148. // HOperatorSet.SetPart(this.HWindowHalconID, 0, 0, H, W);
  149. //}
  150. // HOperatorSet.SetPart(this.HWindowHalconID, 0, 0, 3844, 4000);
  151. viewWindow.displayImage(hv_image);
  152. }
  153. }
  154. }
  155. /// <summary>
  156. /// 获得halcon窗体控件的句柄
  157. /// </summary>
  158. public IntPtr HWindowHalconID
  159. {
  160. get { return this.mCtrl_HWindow.HalconID; }
  161. }
  162. public HWindowControl getHWindowControl()
  163. {
  164. return this.mCtrl_HWindow;
  165. }
  166. /// <summary>
  167. /// 状态条 显示/隐藏 CheckedChanged事件
  168. /// </summary>
  169. /// <param name="sender"></param>
  170. /// <param name="e"></param>
  171. public void barVisible_strip_CheckedChanged(object sender, EventArgs e)
  172. {
  173. ToolStripMenuItem strip = sender as ToolStripMenuItem;
  174. this.SuspendLayout();
  175. if (strip.Checked)
  176. {
  177. m_CtrlHStatusLabelCtrl.Visible = true;
  178. //mCtrl_HWindow.Height = this.Height - m_CtrlHStatusLabelCtrl.Height - m_CtrlHStatusLabelCtrl.Margin.Top - m_CtrlHStatusLabelCtrl.Margin.Bottom;
  179. mCtrl_HWindow.HMouseMove += HWindowControl_HMouseMove;
  180. }
  181. else
  182. {
  183. m_CtrlHStatusLabelCtrl.Visible = false;
  184. // mCtrl_HWindow.Height = this.Height;
  185. mCtrl_HWindow.HMouseMove -= HWindowControl_HMouseMove;
  186. }
  187. //DispImageFit(mCtrl_HWindow);
  188. this.ResumeLayout(false);
  189. this.PerformLayout();
  190. }
  191. public void showStatusBar()
  192. {
  193. barVisible_strip.Checked = true;
  194. }
  195. /// <summary>
  196. /// 保存窗体截图到本地
  197. /// </summary>
  198. private void SaveWindowDump()
  199. {
  200. SaveFileDialog sfd = new SaveFileDialog();
  201. sfd.Filter = "PNG图像|*.png|所有文件|*.*";
  202. if (sfd.ShowDialog() == DialogResult.OK)
  203. {
  204. if (String.IsNullOrEmpty(sfd.FileName))
  205. return;
  206. //截取窗口图
  207. HOperatorSet.DumpWindow(HWindowHalconID, "png best", sfd.FileName);
  208. }
  209. }
  210. /// <summary>
  211. /// 保存原始图片到本地
  212. /// </summary>
  213. private void SaveImage()
  214. {
  215. SaveFileDialog sfd = new SaveFileDialog();
  216. sfd.Filter = "BMP图像|*.bmp|所有文件|*.*";
  217. if (sfd.ShowDialog() == DialogResult.OK)
  218. {
  219. if (String.IsNullOrEmpty(sfd.FileName))
  220. {
  221. return;
  222. }
  223. HOperatorSet.WriteImage(hv_image, "bmp", 0, sfd.FileName);
  224. }
  225. }
  226. /// <summary>
  227. /// 图片适应大小显示在窗体
  228. /// </summary>
  229. /// <param name="hw_Ctrl">halcon窗体控件</param>
  230. public void DispImageFit()
  231. {
  232. try
  233. {
  234. this.viewWindow.resetWindowImage();
  235. }
  236. catch (Exception)
  237. {
  238. }
  239. }
  240. /// <summary>
  241. /// 鼠标在空间窗体里滑动,显示鼠标所在位置的灰度值
  242. /// </summary>
  243. /// <param name="sender"></param>
  244. /// <param name="e"></param>
  245. private void HWindowControl_HMouseMove(object sender, HMouseEventArgs e)
  246. {
  247. if (hv_image != null)
  248. {
  249. try
  250. {
  251. int button_state;
  252. double positionX, positionY;
  253. string str_value;
  254. string str_position;
  255. bool _isXOut = true, _isYOut = true;
  256. HTuple channel_count;
  257. HOperatorSet.CountChannels(hv_image, out channel_count);
  258. hv_window.GetMpositionSubPix(out positionY, out positionX, out button_state);
  259. str_position = String.Format("RC: {0:0000},{1:0000}", positionY, positionX);
  260. _isXOut = (positionX < 0 || positionX >= hv_imageWidth);
  261. _isYOut = (positionY < 0 || positionY >= hv_imageHeight);
  262. if (!_isXOut && !_isYOut)
  263. {
  264. if ((int)channel_count == 1)
  265. {
  266. double grayVal;
  267. grayVal = hv_image.GetGrayval((int)positionY, (int)positionX);
  268. str_value = String.Format("Val: {0:000}", grayVal);
  269. }
  270. else if ((int)channel_count == 3)
  271. {
  272. double grayValRed, grayValGreen, grayValBlue;
  273. HImage _RedChannel, _GreenChannel, _BlueChannel;
  274. _RedChannel = hv_image.AccessChannel(1);
  275. _GreenChannel = hv_image.AccessChannel(2);
  276. _BlueChannel = hv_image.AccessChannel(3);
  277. grayValRed = _RedChannel.GetGrayval((int)positionY, (int)positionX);
  278. grayValGreen = _GreenChannel.GetGrayval((int)positionY, (int)positionX);
  279. grayValBlue = _BlueChannel.GetGrayval((int)positionY, (int)positionX);
  280. _RedChannel.Dispose();
  281. _GreenChannel.Dispose();
  282. _BlueChannel.Dispose();
  283. str_value = String.Format("Gray: ({0:000}, {1:000}, {2:000})", grayValRed, grayValGreen, grayValBlue);
  284. }
  285. else
  286. {
  287. str_value = "";
  288. }
  289. m_CtrlHStatusLabelCtrl.Text = string.Format("Ch:{0} ", channel_count.ToDArr()) + str_imgSize + " " + str_position + " " + str_value;
  290. }
  291. }
  292. catch (Exception ex)
  293. {
  294. //不处理
  295. }
  296. }
  297. }
  298. public void ClearWindow()
  299. {
  300. try
  301. {
  302. this.Invoke(new Action(
  303. () =>
  304. {
  305. //this.hv_image = null;
  306. m_CtrlHStatusLabelCtrl.Visible = false;
  307. barVisible_strip.Enabled = false;
  308. histogram_strip.Enabled = false;
  309. saveImg_strip.Enabled = false;
  310. saveWindow_strip.Enabled = false;
  311. mCtrl_HWindow.HalconWindow.ClearWindow();
  312. viewWindow.ClearWindow();
  313. }
  314. ));
  315. }
  316. catch (Exception ex)
  317. {
  318. Console.WriteLine(ex.ToString());
  319. }
  320. }
  321. /// <summary>
  322. /// Hobject转换为的临时Himage,显示背景图
  323. /// </summary>
  324. /// <param name="hobject">传递Hobject,必须为图像</param>
  325. public void HobjectToHimage(HObject hobject)
  326. {
  327. if (hobject == null || !hobject.IsInitialized())
  328. {
  329. ClearWindow();
  330. return;
  331. }
  332. this.Image = new HImage(hobject);
  333. }
  334. #region 缩放后,再次显示传入的HObject
  335. /// <summary>
  336. /// 默认红颜色显示
  337. /// </summary>
  338. /// <param name="hObj">传入的region.xld,image</param>
  339. public void DispObj(HObject hObj)
  340. {
  341. lock (this)
  342. {
  343. viewWindow.displayHobject(hObj, null);
  344. }
  345. }
  346. /// <summary>
  347. /// 重新开辟内存保存 防止被传入的HObject在其他地方dispose后,不能重现
  348. /// </summary>
  349. /// <param name="hObj">传入的region.xld,image</param>
  350. /// <param name="color">颜色</param>
  351. public void DispObj(HObject hObj, string color)
  352. {
  353. lock (this)
  354. {
  355. viewWindow.displayHobject(hObj, color);
  356. }
  357. }
  358. #endregion
  359. /// <summary>
  360. /// 鼠标离开事件
  361. /// </summary>
  362. /// <param name="sender"></param>
  363. /// <param name="e"></param>
  364. private void mCtrl_HWindow_MouseLeave(object sender, EventArgs e)
  365. {
  366. //避免鼠标离开窗口,再返回的时候,图表随着鼠标移动
  367. viewWindow.mouseleave();
  368. }
  369. public event DoubleClick doubleClick;
  370. private void mCtrl_HWindow_Click(object sender, EventArgs e)
  371. {
  372. }
  373. int i = 0;
  374. private void mCtrl_HWindow_HMouseDown(object sender, HMouseEventArgs e)
  375. {
  376. i += 1;
  377. System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
  378. timer.Interval = 300;
  379. timer.Tick += (s, e1) => { timer.Enabled = false; i = 0; };
  380. timer.Enabled = true;
  381. if (i % 2 == 0)
  382. {
  383. timer.Enabled = false;
  384. i = 0;
  385. if (doubleClick != null)
  386. doubleClick();
  387. Application.DoEvents();
  388. Thread.Sleep(5);
  389. DispImageFit();
  390. //this.WindowState = (this.WindowState == FormWindowState.Maximized ? FormWindowState.Normal : FormWindowState.Maximized);
  391. }
  392. }
  393. }
  394. }