版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

381 lines
13 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace pbox_zoom_control
  11. {
  12. public partial class pbox_zoom: UserControl
  13. {
  14. public event EventHandler Click2;
  15. public event MouseEventHandler Mouse_Move_pic;
  16. public event EventHandler Mouse_Hover_pic;
  17. public event EventHandler Mouse_Enter_pic;
  18. public event EventHandler Mouse_Leave_pic;
  19. private string path = "";
  20. private void HandleClick2(object sender, EventArgs e)
  21. {
  22. // we'll explain this in a minute
  23. this.OnClick2(EventArgs.Empty);
  24. }
  25. protected virtual void OnClick2(EventArgs e)
  26. {
  27. this.Click2?.Invoke(this, e);
  28. }
  29. private void HandleMouse_Move_pic(object sender, MouseEventArgs e)
  30. {
  31. // we'll explain this in a minute
  32. this.OnMouse_Move_pic(e);
  33. }
  34. private void OnMouse_Move_pic(MouseEventArgs e)
  35. {
  36. this.Mouse_Move_pic?.Invoke(this, e);
  37. }
  38. private void HandleMouse_Hover_pic(object sender, EventArgs e)
  39. {
  40. // we'll explain this in a minute
  41. this.OnMouse_Hover_pic(EventArgs.Empty);
  42. }
  43. protected virtual void OnMouse_Hover_pic(EventArgs e)
  44. {
  45. this.Mouse_Hover_pic?.Invoke(this, e);
  46. }
  47. private void HandleMouse_Enter_pic(object sender, EventArgs e)
  48. {
  49. // we'll explain this in a minute
  50. this.OnMouse_Enter_pic(EventArgs.Empty);
  51. }
  52. protected virtual void OnMouse_Enter_pic(EventArgs e)
  53. {
  54. this.Mouse_Enter_pic?.Invoke(this, e);
  55. }
  56. private void HandleMouse_Leave_pic(object sender, EventArgs e)
  57. {
  58. // we'll explain this in a minute
  59. this.OnMouse_Leave_pic(EventArgs.Empty);
  60. }
  61. protected virtual void OnMouse_Leave_pic(EventArgs e)
  62. {
  63. this.Mouse_Leave_pic?.Invoke(this, e);
  64. }
  65. private double ZOOMFACTOR = 1.25; // = 25% smaller or larger
  66. private int MINMAX = 10; // 5 times bigger or smaller than the ctrl
  67. private Bitmap bild;
  68. private int mouse_x = 0;
  69. private int mouse_y = 0;
  70. public pbox_zoom()
  71. {
  72. InitializeComponent();
  73. InitCtrl();
  74. }
  75. private void InitCtrl()
  76. {
  77. panel1.Cursor = System.Windows.Forms.Cursors.NoMove2D;
  78. panel1.MouseEnter += new EventHandler(pbox_1_MouseEnter);
  79. pbox_1.MouseEnter += new EventHandler(pbox_1_MouseEnter);
  80. panel1.MouseWheel += new MouseEventHandler(pbox_1_MouseWheel);
  81. pbox_1.Click += HandleClick2;
  82. pbox_1.MouseMove += HandleMouse_Move_pic;
  83. pbox_1.MouseHover += HandleMouse_Hover_pic;
  84. pbox_1.MouseEnter += HandleMouse_Enter_pic;
  85. pbox_1.MouseLeave += HandleMouse_Leave_pic;
  86. }
  87. public Bitmap Image
  88. {
  89. get
  90. {
  91. return bild;
  92. }
  93. set
  94. {
  95. open_picture(value);
  96. }
  97. }
  98. public double zoomfactor
  99. {
  100. get
  101. {
  102. return ZOOMFACTOR;
  103. }
  104. set
  105. {
  106. ZOOMFACTOR = value;
  107. }
  108. }
  109. public int minmax
  110. {
  111. get
  112. {
  113. return MINMAX;
  114. }
  115. set
  116. {
  117. MINMAX = value;
  118. }
  119. }
  120. public string ImageLocation
  121. {
  122. get
  123. {
  124. return path;
  125. }
  126. set
  127. {
  128. if(value != "" && value != null)
  129. open_picture(value);
  130. }
  131. }
  132. public Point Mouse_Position
  133. {
  134. get
  135. {
  136. return new Point(mouse_x,mouse_y);
  137. }
  138. }
  139. public int Mouse_X
  140. {
  141. get
  142. {
  143. return mouse_x;
  144. }
  145. }
  146. public int Mouse_Y
  147. {
  148. get
  149. {
  150. return mouse_y;
  151. }
  152. }
  153. public void open_picture(string pic_path)
  154. {
  155. bool save_position = false;
  156. Point position = new Point();
  157. Size size = new Size();
  158. Size old_size = new Size();
  159. if (pbox_1.Image != null)
  160. old_size = pbox_1.Image.Size;
  161. int v = 0;
  162. int h = 0;
  163. if (bild != null) bild.Dispose();
  164. bild = new Bitmap(pic_path);
  165. path = pic_path;
  166. if(bild != null && pbox_1.Image != null)
  167. if (bild.Size == old_size) save_position = true;
  168. if (save_position)
  169. {
  170. position = pbox_1.Location;
  171. size = pbox_1.Size;
  172. v = VerticalScroll.Value;
  173. h = HorizontalScroll.Value;
  174. }
  175. pbox_1.Image = bild;
  176. if (((double)bild.Width / (double)bild.Height) > ((double)panel1.Width / (double)panel1.Height))
  177. {
  178. pbox_1.Width = panel1.Width;
  179. pbox_1.Height = pbox_1.Width * bild.Height / bild.Width;
  180. }
  181. else
  182. {
  183. pbox_1.Height = panel1.Height;
  184. pbox_1.Width = pbox_1.Height * bild.Width / bild.Height;
  185. }
  186. if (save_position)
  187. {
  188. pbox_1.Location = position;
  189. pbox_1.Size = size;
  190. VerticalScroll.Value = v;
  191. HorizontalScroll.Value = h;
  192. }
  193. else
  194. {
  195. update_scrollbar();
  196. update_picture_position();
  197. }
  198. }
  199. public void open_picture(Bitmap Image)
  200. {
  201. bool save_position = false;
  202. Point position = new Point();
  203. Size size = new Size();
  204. Size old_size = new Size();
  205. if (pbox_1.Image != null)
  206. old_size = pbox_1.Image.Size;
  207. int v = 0;
  208. int h = 0;
  209. path = "";
  210. if (bild != null) bild.Dispose();
  211. bild = new Bitmap(Image);
  212. if (bild != null && pbox_1.Image != null)
  213. if (bild.Size == old_size) save_position = true;
  214. if (save_position)
  215. {
  216. position = pbox_1.Location;
  217. size = pbox_1.Size;
  218. v = VerticalScroll.Value;
  219. h = HorizontalScroll.Value;
  220. }
  221. pbox_1.Image = bild;
  222. if (((double)bild.Width / (double)bild.Height) > ((double)panel1.Width / (double)panel1.Height))
  223. {
  224. pbox_1.Width = panel1.Width;
  225. pbox_1.Height = pbox_1.Width * bild.Height / bild.Width;
  226. }
  227. else
  228. {
  229. pbox_1.Height = panel1.Height;
  230. pbox_1.Width = pbox_1.Height * bild.Width / bild.Height;
  231. }
  232. if (save_position)
  233. {
  234. pbox_1.Location = position;
  235. pbox_1.Size = size;
  236. VerticalScroll.Value = v;
  237. HorizontalScroll.Value = h;
  238. }
  239. else
  240. {
  241. update_scrollbar();
  242. update_picture_position();
  243. }
  244. }
  245. private void ZoomIn()
  246. {
  247. if ((pbox_1.Width < (MINMAX * panel1.Width)) &&
  248. (pbox_1.Height < (MINMAX * panel1.Height)))
  249. {
  250. pbox_1.Width = Convert.ToInt32(pbox_1.Width * ZOOMFACTOR);
  251. pbox_1.Height = Convert.ToInt32(pbox_1.Height * ZOOMFACTOR);
  252. pbox_1.SizeMode = PictureBoxSizeMode.StretchImage;
  253. }
  254. }
  255. private void ZoomOut()
  256. {
  257. if ((pbox_1.Width > (panel1.Width / MINMAX)) &&
  258. (pbox_1.Height > (panel1.Height / MINMAX)))
  259. {
  260. pbox_1.SizeMode = PictureBoxSizeMode.StretchImage;
  261. pbox_1.Width = Convert.ToInt32(pbox_1.Width / ZOOMFACTOR);
  262. pbox_1.Height = Convert.ToInt32(pbox_1.Height / ZOOMFACTOR);
  263. }
  264. }
  265. private void pbox_1_MouseEnter(object sender, EventArgs e)
  266. {
  267. if (pbox_1.Focused == false)
  268. {
  269. pbox_1.Focus();
  270. update_picture_position();
  271. }
  272. }
  273. private void pbox_1_MouseWheel(object sender, MouseEventArgs e)
  274. {
  275. Point old_position = pbox_1.Location;
  276. Size old_size_picture = pbox_1.Size;
  277. Size old_size_frame = panel1.Size;
  278. if (e.Delta > 0)
  279. {
  280. ZoomIn();
  281. }
  282. else
  283. {
  284. ZoomOut();
  285. }
  286. Size new_size_picture = pbox_1.Size;
  287. Size new_size_frame = panel1.Size;
  288. //pbox_1.Location = new_picture_position(old_position, old_size_picture, old_size_frame, new_size_picture, new_size_frame);
  289. Point mouse = e.Location;
  290. pbox_1.Location = new_picture_position(old_position, old_size_picture, mouse, new_size_picture, mouse);
  291. update_scrollbar();
  292. update_picture_position();
  293. }
  294. Point new_picture_position(Point old_position, Size old_size_picture, Size old_size_frame, Size new_size_picture, Size new_size_frame)
  295. {
  296. Point old_position_center = new Point(old_size_frame.Width / 2 - old_position.X, old_size_frame.Height / 2 - old_position.Y);
  297. Point new_position_center = new Point(old_position_center.X * new_size_picture.Width / old_size_picture.Width, old_position_center.Y * new_size_picture.Height / old_size_picture.Height);
  298. Point new_pos = new Point(new_size_frame.Width / 2 - new_position_center.X, new_size_frame.Height / 2 - new_position_center.Y);
  299. return new_pos;
  300. }
  301. Point new_picture_position(Point old_position, Size old_size_picture, Point old_position_curser, Size new_size_picture, Point new_position_curser)
  302. {
  303. Point old_position_center = new Point(old_position_curser.X - old_position.X, old_position_curser.Y - old_position.Y);
  304. Point new_position_center = new Point(old_position_center.X * new_size_picture.Width / old_size_picture.Width, old_position_center.Y * new_size_picture.Height / old_size_picture.Height);
  305. Point new_pos = new Point(new_position_curser.X - new_position_center.X, new_position_curser.Y - new_position_center.Y);
  306. return new_pos;
  307. }
  308. private void pbox_1_SizeChanged(object sender, EventArgs e)
  309. {
  310. update_scrollbar();
  311. update_picture_position();
  312. }
  313. private void Form1_SizeChanged(object sender, EventArgs e)
  314. {
  315. update_scrollbar();
  316. update_picture_position();
  317. }
  318. void update_scrollbar()
  319. {
  320. int max_x = pbox_1.Width - panel1.Width;
  321. int max_y = pbox_1.Height - panel1.Height;
  322. if (0 < max_x)
  323. {
  324. hScrollBar1.Enabled = true;
  325. hScrollBar1.Maximum = max_x;
  326. hScrollBar1.Value = Math.Max(0, Math.Min(max_x, -pbox_1.Location.X));
  327. }
  328. else
  329. {
  330. hScrollBar1.Enabled = false;
  331. }
  332. if (0 < max_y)
  333. {
  334. vScrollBar1.Enabled = true;
  335. vScrollBar1.Maximum = max_y;
  336. vScrollBar1.Value = Math.Max(0, Math.Min(max_y, -pbox_1.Location.Y));
  337. }
  338. else
  339. {
  340. vScrollBar1.Enabled = false;
  341. }
  342. }
  343. private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
  344. {
  345. update_picture_position();
  346. }
  347. private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
  348. {
  349. update_picture_position();
  350. }
  351. void update_picture_position()
  352. {
  353. int x = -hScrollBar1.Value;
  354. int y = -vScrollBar1.Value;
  355. if (pbox_1.Size.Width < panel1.Width)
  356. x = (panel1.Width - pbox_1.Width) / 2;
  357. if (pbox_1.Size.Height < panel1.Height)
  358. y = (panel1.Height - pbox_1.Height) / 2;
  359. pbox_1.Location = new Point(x, y);
  360. }
  361. private void pbox_1_MouseMove(object sender, MouseEventArgs e)
  362. {
  363. mouse_x = bild.Width * e.Location.X / pbox_1.Width;
  364. mouse_y = bild.Height * e.Location.Y / pbox_1.Height;
  365. }
  366. private void pbox_zoom_Load(object sender, EventArgs e)
  367. {
  368. bild = new Bitmap(10, 10);
  369. }
  370. }
  371. }