using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WCS_Client.From { public partial class Form1 : Form { int zoomStep = 20; //缩放步长 bool ifNativeMouseWheelSupport = false;//鼠标滚轮是否有效 Point mouseDownPoint = new Point(); //记录拖拽过程鼠标位置 bool isMove = false; //判断鼠标在picturebox上移动时,是否处于拖拽过程(鼠标左键是否按下) public Form1() { InitializeComponent(); } private static Form1 childFromInstanc; public static Form1 ChildFromInstanc { get { if (childFromInstanc == null || childFromInstanc.IsDisposed) { childFromInstanc = new Form1(); } return childFromInstanc; } } private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { mouseDownPoint.X = Cursor.Position.X; mouseDownPoint.Y = Cursor.Position.Y; isMove = true; } } private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { isMove = false; } } private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e) { tableLayoutPanel1.Focus(); if (isMove) { int x, y; int moveX, moveY; moveX = Cursor.Position.X - mouseDownPoint.X; moveY = Cursor.Position.Y - mouseDownPoint.Y; x = tableLayoutPanel1.Location.X + moveX; y = tableLayoutPanel1.Location.Y + moveY; tableLayoutPanel1.Location = new Point(x, y); mouseDownPoint.X = Cursor.Position.X; mouseDownPoint.Y = Cursor.Position.Y; } } private void Form1_Load(object sender, EventArgs e) { ifNativeMouseWheelSupport = SystemInformation.NativeMouseWheelSupport && SystemInformation.MouseWheelPresent; if (ifNativeMouseWheelSupport)//如果鼠标滚轮有效,就添加滚轮事件 tableLayoutPanel1.MouseWheel += new MouseEventHandler(this.panel1_MouseWheel); //PanelScrollHelper.InitializePanelScroll(panel1); tableLayoutPanel1.Width = myPanel1.Width; tableLayoutPanel1.Height = myPanel1.Height; } //鼠标滚轮滚动功能 private void panel1_MouseWheel(object sender, MouseEventArgs e) { int x = e.Location.X; int y = e.Location.Y; int ow = tableLayoutPanel1.Width; int oh = tableLayoutPanel1.Height; if (e.Delta > 0) { tableLayoutPanel1.Width += zoomStep; tableLayoutPanel1.Height += zoomStep; } if (e.Delta < 0) { if (tableLayoutPanel1.Width <= myPanel1.Width && tableLayoutPanel1.Height <= myPanel1.Height) return; tableLayoutPanel1.Width -= zoomStep; tableLayoutPanel1.Height -= zoomStep; } //int VX, VY; //VX = (int)((double)x * (ow - panel1.Width) / ow); //VY = (int)((double)y * (oh - panel1.Height) / oh); //panel1.Location = new Point(panel1.Location.X + VX, panel1.Location.Y + VY); } protected override void WndProc(ref Message m) { if (m.Msg == 0x0014) // 禁掉清除背景消息 return; base.WndProc(ref m); } private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e) { //绘制表格右、下边框 Pen tpen = new Pen(Color.Black); tpen.Width = 1F; int x = tableLayoutPanel1.Bounds.X; int y = tableLayoutPanel1.Bounds.Y; int width = tableLayoutPanel1.Width; int height = tableLayoutPanel1.Height; //绘制矩形 //Rectangle rectangle = new Rectangle(x, y, x + width, y + height); //e.Graphics.DrawRectangle(tpen, rectangle); //绘制底边 e.Graphics.DrawLine(tpen, x, y + height, x + width, y + height); //绘制右边 e.Graphics.DrawLine(tpen, x + width, y, x + width, y + height); } } }