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

140 lines
5.4 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace AssistClient.UI
  11. {
  12. public partial class FrmScannerShow : Form
  13. {
  14. public Action reDraw;
  15. private Size size;
  16. private double ratio = 1; // 图片的起始显示比例
  17. private double ratioStep = 0.1;
  18. private Size pic_size;
  19. private int xPos;
  20. private int yPos;
  21. public IntPtr picHwnd
  22. {
  23. get { return this.pictureBox1.Handle; }
  24. }
  25. public FrmScannerShow(Size _size)
  26. {
  27. InitializeComponent();
  28. this.Width = Screen.PrimaryScreen.WorkingArea.Width/3;
  29. this.Location=new Point(0,0);
  30. size = _size;
  31. this.ClientSize = new Size(this.ClientSize.Width,
  32. (int)(this.ClientSize.Width * (size.Height * 1.0f / size.Width)));
  33. this.pictureBox1.Size = this.ClientSize;
  34. this.pictureBox1.Location=new Point(0,0);
  35. }
  36. private void FrmScannerShow_Load(object sender, EventArgs e)
  37. {
  38. pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  39. pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
  40. pictureBox1.MouseMove += pictureBox1_MouseMove;
  41. pictureBox1.MouseDown += pictureBox1_MouseDown;
  42. this.ActiveControl = this.pictureBox1; // 设置焦点
  43. pic_size = this.pictureBox1.Size;
  44. }
  45. public void updateBmp(Bitmap bmp)
  46. {
  47. this.pictureBox1.Image = bmp;
  48. }
  49. private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
  50. {
  51. //Point pictureBoxPoint = this.PointToClient(Cursor.Position);
  52. //this.Text = $"{e.X}:{e.Y}; {pictureBoxPoint.X}:{pictureBoxPoint.Y}; {pictureBox1.Left}:{pictureBox1.Top}; " +
  53. // $"{this.pictureBox1.Width}:{this.pictureBox1.Height}; {this.Width}:{this.Height}; " +
  54. // $"Cursor:{Cursor.Position.X}:{Cursor.Position.Y}";
  55. if (e.Delta > 0)
  56. {
  57. ratio += ratioStep;
  58. if (ratio > 100) // 放大上限
  59. ratio = 100;
  60. else
  61. {
  62. int x = (int)(pictureBox1.Left - e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
  63. int y = (int)(pictureBox1.Top - e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);
  64. this.changePictureBoxSize(new Point(x, y), ratio);
  65. }
  66. }
  67. else if (ratio - ratioStep >= 1)
  68. {
  69. ratio -= ratioStep;
  70. //if (ratio < 0.1) // 放大下限
  71. // ratio = 0.1;
  72. //else
  73. int x = (int)(pictureBox1.Left + e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
  74. int y = (int)(pictureBox1.Top + e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);
  75. this.changePictureBoxSize(new Point(x, y), ratio);
  76. }
  77. }
  78. private void changePictureBoxSize(Point location, double ratio)
  79. {
  80. var picSize = pictureBox1.Size;
  81. picSize.Width = Convert.ToInt32(pic_size.Width * ratio);
  82. picSize.Height = Convert.ToInt32(pic_size.Height * ratio);
  83. pictureBox1.Size = picSize;
  84. if (location.X > 0) location.X = 0;
  85. if (location.Y > 0) location.Y = 0;
  86. if (picSize.Width + location.X < this.ClientSize.Width) location.X = -(picSize.Width - this.ClientSize.Width);
  87. if (picSize.Height + location.Y < this.ClientSize.Height) location.Y = -(picSize.Height - this.ClientSize.Height);
  88. //Point location = new Point();
  89. //location.X = (this.ClientSize.Width - this.pictureBox1.Width) / 2;
  90. //location.Y = (this.ClientSize.Height - this.pictureBox1.Height) / 2;
  91. this.pictureBox1.Location = location;
  92. }
  93. private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
  94. {
  95. try
  96. {
  97. // 鼠标按下拖拽图片
  98. if (e.Button == MouseButtons.Left)
  99. {
  100. // 限制拖拽出框
  101. if (pictureBox1.Width > this.ClientSize.Width || pictureBox1.Height > this.ClientSize.Height)
  102. {
  103. int moveX = e.X - xPos;
  104. int moveY = e.Y - yPos;
  105. if ((pictureBox1.Left + moveX) <= 0
  106. && (pictureBox1.Top + moveY) <= 0
  107. && (pictureBox1.Right + moveX) >= this.ClientSize.Width
  108. && (pictureBox1.Bottom + moveY) >= this.ClientSize.Height)
  109. {
  110. pictureBox1.Left += moveX;//设置x坐标.
  111. pictureBox1.Top += moveY;//设置y坐标.
  112. }
  113. }
  114. }
  115. }
  116. catch (Exception dd)
  117. {
  118. MessageBox.Show(dd.Message);
  119. }
  120. }
  121. private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
  122. {
  123. xPos = e.X;//当前x坐标.
  124. yPos = e.Y;//当前y坐标.
  125. }
  126. }
  127. }