Form1.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 WCS_Client.From
  11. {
  12. public partial class Form1 : Form
  13. {
  14. int zoomStep = 20; //缩放步长
  15. bool ifNativeMouseWheelSupport = false;//鼠标滚轮是否有效
  16. Point mouseDownPoint = new Point(); //记录拖拽过程鼠标位置
  17. bool isMove = false; //判断鼠标在picturebox上移动时,是否处于拖拽过程(鼠标左键是否按下)
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. }
  22. private static Form1 childFromInstanc;
  23. public static Form1 ChildFromInstanc
  24. {
  25. get
  26. {
  27. if (childFromInstanc == null || childFromInstanc.IsDisposed)
  28. {
  29. childFromInstanc = new Form1();
  30. }
  31. return childFromInstanc;
  32. }
  33. }
  34. private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
  35. {
  36. if (e.Button == MouseButtons.Left)
  37. {
  38. mouseDownPoint.X = Cursor.Position.X;
  39. mouseDownPoint.Y = Cursor.Position.Y;
  40. isMove = true;
  41. }
  42. }
  43. private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
  44. {
  45. if (e.Button == MouseButtons.Left)
  46. {
  47. isMove = false;
  48. }
  49. }
  50. private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
  51. {
  52. tableLayoutPanel1.Focus();
  53. if (isMove)
  54. {
  55. int x, y;
  56. int moveX, moveY;
  57. moveX = Cursor.Position.X - mouseDownPoint.X;
  58. moveY = Cursor.Position.Y - mouseDownPoint.Y;
  59. x = tableLayoutPanel1.Location.X + moveX;
  60. y = tableLayoutPanel1.Location.Y + moveY;
  61. tableLayoutPanel1.Location = new Point(x, y);
  62. mouseDownPoint.X = Cursor.Position.X;
  63. mouseDownPoint.Y = Cursor.Position.Y;
  64. }
  65. }
  66. private void Form1_Load(object sender, EventArgs e)
  67. {
  68. ifNativeMouseWheelSupport = SystemInformation.NativeMouseWheelSupport && SystemInformation.MouseWheelPresent;
  69. if (ifNativeMouseWheelSupport)//如果鼠标滚轮有效,就添加滚轮事件
  70. tableLayoutPanel1.MouseWheel += new MouseEventHandler(this.panel1_MouseWheel);
  71. //PanelScrollHelper.InitializePanelScroll(panel1);
  72. tableLayoutPanel1.Width = myPanel1.Width;
  73. tableLayoutPanel1.Height = myPanel1.Height;
  74. }
  75. //鼠标滚轮滚动功能
  76. private void panel1_MouseWheel(object sender, MouseEventArgs e)
  77. {
  78. int x = e.Location.X;
  79. int y = e.Location.Y;
  80. int ow = tableLayoutPanel1.Width;
  81. int oh = tableLayoutPanel1.Height;
  82. if (e.Delta > 0)
  83. {
  84. tableLayoutPanel1.Width += zoomStep;
  85. tableLayoutPanel1.Height += zoomStep;
  86. }
  87. if (e.Delta < 0)
  88. {
  89. if (tableLayoutPanel1.Width <= myPanel1.Width && tableLayoutPanel1.Height <= myPanel1.Height) return;
  90. tableLayoutPanel1.Width -= zoomStep;
  91. tableLayoutPanel1.Height -= zoomStep;
  92. }
  93. //int VX, VY;
  94. //VX = (int)((double)x * (ow - panel1.Width) / ow);
  95. //VY = (int)((double)y * (oh - panel1.Height) / oh);
  96. //panel1.Location = new Point(panel1.Location.X + VX, panel1.Location.Y + VY);
  97. }
  98. protected override void WndProc(ref Message m)
  99. {
  100. if (m.Msg == 0x0014) // 禁掉清除背景消息
  101. return;
  102. base.WndProc(ref m);
  103. }
  104. private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
  105. {
  106. //绘制表格右、下边框
  107. Pen tpen = new Pen(Color.Black);
  108. tpen.Width = 1F;
  109. int x = tableLayoutPanel1.Bounds.X;
  110. int y = tableLayoutPanel1.Bounds.Y;
  111. int width = tableLayoutPanel1.Width;
  112. int height = tableLayoutPanel1.Height;
  113. //绘制矩形
  114. //Rectangle rectangle = new Rectangle(x, y, x + width, y + height);
  115. //e.Graphics.DrawRectangle(tpen, rectangle);
  116. //绘制底边
  117. e.Graphics.DrawLine(tpen, x, y + height, x + width, y + height);
  118. //绘制右边
  119. e.Graphics.DrawLine(tpen, x + width, y, x + width, y + height);
  120. }
  121. }
  122. }