版博士V2.0程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

191 lines
7.5 KiB

  1. using ProductionControl.Device;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace ProductionControl.UI
  12. {
  13. public partial class FrmScannerShow : Form
  14. {
  15. public Action reDraw;
  16. private Size size;
  17. private double ratio = 1; // 图片的起始显示比例
  18. private double ratioStep = 0.1;
  19. private Size pic_size;
  20. private int xPos;
  21. private int yPos;
  22. ScannerDev scannerDev;
  23. public IntPtr picHwnd
  24. {
  25. get { return this.pictureBox1.Handle; }
  26. }
  27. public FrmScannerShow(Size _size, ScannerDev dev=null)
  28. {
  29. InitializeComponent();
  30. //this.WindowState = FormWindowState.Maximized;
  31. this.Width = Screen.PrimaryScreen.WorkingArea.Width / 3;
  32. this.Location = new Point(0, 0);
  33. size = _size;
  34. scannerDev = dev;
  35. if (scannerDev == null)
  36. {
  37. toolstrip1.Visible = numExposureTime.Visible= this.label1.Visible= false;
  38. this.ClientSize = new Size(this.ClientSize.Width,
  39. (int)(this.ClientSize.Width * (size.Height * 1.0f / size.Width)));
  40. this.pictureBox1.Size = this.ClientSize;
  41. this.pictureBox1.Location = new Point(0, 0);
  42. }
  43. else
  44. {
  45. this.ClientSize = new Size(this.ClientSize.Width,
  46. (int)(this.ClientSize.Width * (size.Height * 1.0f / size.Width))+ toolstrip1.Height);
  47. this.pictureBox1.Size = new Size(this.ClientSize.Width,this.ClientSize.Height- toolstrip1.Height);
  48. this.pictureBox1.Location = new Point(0, toolstrip1.Height);
  49. dev.getParam();
  50. this.numExposureTime.Value = (decimal)dev.ExposureTime;
  51. }
  52. this.Text = $"{(toolstrip1.Visible ? toolstrip1.Height:0)},{this.Height},{this.ClientSize.Height},{this.pictureBox1.Height},{this.pictureBox1.Top}";
  53. }
  54. private void FrmScannerShow_Load(object sender, EventArgs e)
  55. {
  56. pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  57. pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
  58. pictureBox1.MouseMove += pictureBox1_MouseMove;
  59. pictureBox1.MouseDown += pictureBox1_MouseDown;
  60. this.ActiveControl = this.pictureBox1; // 设置焦点
  61. pic_size = this.pictureBox1.Size;
  62. }
  63. private void FrmScannerShow_FormClosing(object sender, FormClosingEventArgs e)
  64. {
  65. if (scannerDev != null)
  66. {
  67. scannerDev.stop();
  68. scannerDev.close();
  69. scannerDev = null;
  70. }
  71. }
  72. public void updateBmp(Bitmap bmp)
  73. {
  74. this.pictureBox1.Image = bmp;
  75. }
  76. private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
  77. {
  78. //Point pictureBoxPoint = this.PointToClient(Cursor.Position);
  79. //this.Text = $"{e.X}:{e.Y}; {pictureBoxPoint.X}:{pictureBoxPoint.Y}; {pictureBox1.Left}:{pictureBox1.Top}; " +
  80. // $"{this.pictureBox1.Width}:{this.pictureBox1.Height}; {this.Width}:{this.Height}; " +
  81. // $"Cursor:{Cursor.Position.X}:{Cursor.Position.Y}";
  82. if (e.Delta > 0)
  83. {
  84. ratio += ratioStep;
  85. if (ratio > 100) // 放大上限
  86. ratio = 100;
  87. else
  88. {
  89. int x = (int)(pictureBox1.Left - e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
  90. int y = (int)(pictureBox1.Top - e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);
  91. this.changePictureBoxSize(new Point(x, y), ratio);
  92. }
  93. }
  94. else if (ratio - ratioStep >= 1)
  95. {
  96. ratio -= ratioStep;
  97. //if (ratio < 0.1) // 放大下限
  98. // ratio = 0.1;
  99. //else
  100. int x = (int)(pictureBox1.Left + e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
  101. int y = (int)(pictureBox1.Top + e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);
  102. this.changePictureBoxSize(new Point(x, y), ratio);
  103. }
  104. }
  105. private void changePictureBoxSize(Point location, double ratio)
  106. {
  107. var picSize = pictureBox1.Size;
  108. picSize.Width = Convert.ToInt32(pic_size.Width * ratio);
  109. picSize.Height = Convert.ToInt32(pic_size.Height * ratio);
  110. pictureBox1.Size = picSize;
  111. if (location.X > 0) location.X = 0;
  112. if (location.Y > 0) location.Y = 0;
  113. if (picSize.Width + location.X < this.ClientSize.Width) location.X = -(picSize.Width - this.ClientSize.Width);
  114. if (picSize.Height + location.Y < this.ClientSize.Height) location.Y = -(picSize.Height - this.ClientSize.Height);
  115. //Point location = new Point();
  116. //location.X = (this.ClientSize.Width - this.pictureBox1.Width) / 2;
  117. //location.Y = (this.ClientSize.Height - this.pictureBox1.Height) / 2;
  118. this.pictureBox1.Location = location;
  119. }
  120. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  121. {
  122. try
  123. {
  124. // 鼠标按下拖拽图片
  125. if (e.Button == MouseButtons.Left)
  126. {
  127. // 限制拖拽出框
  128. if (pictureBox1.Width > this.ClientSize.Width || pictureBox1.Height > this.ClientSize.Height)
  129. {
  130. int moveX = e.X - xPos;
  131. int moveY = e.Y - yPos;
  132. if ((pictureBox1.Left + moveX) <= 0
  133. && (pictureBox1.Top + moveY) <= 0
  134. && (pictureBox1.Right + moveX) >= this.ClientSize.Width
  135. && (pictureBox1.Bottom + moveY) >= this.ClientSize.Height)
  136. {
  137. pictureBox1.Left += moveX;//设置x坐标.
  138. pictureBox1.Top += moveY;//设置y坐标.
  139. }
  140. }
  141. }
  142. }
  143. catch (Exception dd)
  144. {
  145. MessageBox.Show(dd.Message);
  146. }
  147. }
  148. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  149. {
  150. xPos = e.X;//当前x坐标.
  151. yPos = e.Y;//当前y坐标.
  152. }
  153. private void numExposureTime_ValueChanged(object sender, EventArgs e)
  154. {
  155. if(scannerDev != null && scannerDev.IsInit)
  156. {
  157. scannerDev.setParam((float)numExposureTime.Value);
  158. }
  159. }
  160. private void tsbtnOpenDir_Click(object sender, EventArgs e)
  161. {
  162. if (scannerDev != null && scannerDev.IsInit && string.IsNullOrWhiteSpace(scannerDev.bmpSavePath))
  163. System.Diagnostics.Process.Start("explorer.exe", scannerDev.bmpSavePath+"\\");
  164. }
  165. private void tsbtnScnner_Click(object sender, EventArgs e)
  166. {
  167. if (scannerDev != null && scannerDev.IsInit)
  168. scannerDev.scan(1);
  169. System.Diagnostics.Process.Start("explorer.exe",Application.StartupPath+"\\temp\\");
  170. }
  171. }
  172. }