c#做了个双指针工业仪表盘,怎么让两个指针分别显示
c#做了个双指针工业仪表盘,怎么让两个指针分开
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace Demo_1 { public partial class Form1 : Form { public class PanelEx : Panel { public PanelEx() { SetStyle( ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true ); UpdateStyles(); } } public abstract class DraggableObject { public abstract string Id { get; set; } public abstract int prim_X { get; set; } public abstract int prim_Y { get; set; } public abstract Rectangle Region { get; set; } public abstract Bitmap Setimage { get; set; } public abstract void OnPaint(PaintEventArgs e); } public class Draggable : DraggableObject { private string m_Id; private int m_primX; private int m_primY; private Rectangle m_Region; private Bitmap m_SetImage; public Draggable(string id, Rectangle regin, Bitmap image) { m_Id = id; m_Region = regin; m_SetImage = image; } public override string Id { get => m_Id; set => m_Id = value; } public override int prim_X { get => m_primX; set => m_primX = value; } public override int prim_Y { get => m_primY; set => m_primY = value; } public override Rectangle Region { get => m_Region; set => m_Region = value; } public override Bitmap Setimage { get => m_SetImage; set => m_SetImage = value; } public override void OnPaint(PaintEventArgs e) { e.Graphics.DrawImage(m_SetImage, m_Region); } } public List<DraggableObject> DesignObjects = new List<DraggableObject>(); public PanelEx panelContent; public Label info; private bool Is_Dragging = false; private int index = -1,record = -1; public Form1() { InitializeComponent(); panelContent = new PanelEx() { Dock = DockStyle.Fill, BackgroundImageLayout = ImageLayout.Stretch, BackgroundImage = Image.FromFile("back.jpg") }; panelContent.Paint = PanelContent_Paint; panelContent.MouseMove = PanelContent_MouseMove; panelContent.MouseDown = PanelContent_MouseDown; panelContent.MouseUp = PanelContent_MouseUp; this.Controls.Add(panelContent); PanelEx panelTop = new PanelEx() { Height = 30, Dock = DockStyle.Top }; this.Controls.Add(panelTop); Button[] buttons = new Button[4]; string[] btnText = new string[] { "添加元素","删除元素","元素前移","元素后移"}; for (int i=0;i<4;i ) { buttons[i] = new Button() { Size = new Size(70, 25), Location = new Point(10 i*80,5), Font = new Font("微软雅黑",9,FontStyle.Regular), Text = btnText[i], Tag = i }; buttons[i].Click =Button_Click; panelTop.Controls.Add(buttons[i]); } info = new Label() { AutoSize = true, Location = new Point(350,5), Font = new Font("微软雅黑", 9, FontStyle.Regular), }; panelTop.Controls.Add(info); } private void PanelContent_Paint(object sender, PaintEventArgs e) { foreach (DraggableObject item in DesignObjects) { item.OnPaint(e); } } private void Button_Click(object sender, EventArgs e) { Control pL = (Control)sender; if ((int)pL.Tag == 0) { OpenFileDialog openImgDialog = new OpenFileDialog(); openImgDialog.Filter = "图像文件(*.png;*.jpg;*.gif;*.bmp)|*.png;*.jpg;*.gif;*.bmp"; openImgDialog.RestoreDirectory = true; openImgDialog.FilterIndex = 1; if (openImgDialog.ShowDialog() == DialogResult.OK) { Bitmap bmp = Image.FromFile(openImgDialog.FileName) as Bitmap; Draggable draggableBlock = new Draggable(DateTime.Now.ToString(), new Rectangle(50, 50, bmp.Width, bmp.Height), bmp); DesignObjects.Add(draggableBlock); record = DesignObjects.Count - 1; info.Text = "当前元素:" (record 1) " ID: " DesignObjects[record].Id; } } else if ((int)pL.Tag == 1) { if (record != -1) { DesignObjects.RemoveAt(record); record = DesignObjects.Count == 0 ? -1 : DesignObjects.Count - 1; } } else if ((int)pL.Tag == 2) { if (record != -1) { var swap = DesignObjects[record]; DesignObjects.RemoveAt(record); DesignObjects.Insert(0, swap); record = 0; } } if ((int)pL.Tag == 3) { if (record != -1) { var swap = DesignObjects[record]; DesignObjects.RemoveAt(record); DesignObjects.Add(swap); record = DesignObjects.Count - 1; } } if (record != -1) info.Text = "当前元素:" (record 1) " ID: " DesignObjects[record].Id; panelContent.Invalidate(); } private void PanelContent_MouseUp(object sender, MouseEventArgs e) { Is_Dragging = false; } private void PanelContent_MouseDown(object sender, MouseEventArgs e) { index = DesignObjects.FindIndex(d => d.Region.Contains(e.Location)); if (index != -1) { record = index; Is_Dragging = true; DesignObjects[index].prim_X = e.Location.X - DesignObjects[index].Region.Left; DesignObjects[index].prim_Y = e.Location.Y - DesignObjects[index].Region.Top; info.Text = "当前元素:" (record 1) " ID: " DesignObjects[record].Id; } else { record = -1; Is_Dragging = false; } } private void PanelContent_MouseMove(object sender, MouseEventArgs e) { Control pL = (Control)sender; if (Is_Dragging && index != -1) { int set_x = e.Location.X - DesignObjects[index].prim_X; int set_y = e.Location.Y - DesignObjects[index].prim_Y; set_x = set_x < 0 ? 0 : set_x = set_x > panelContent.Width - DesignObjects[index].Region.Width ? panelContent.Width - DesignObjects[index].Region.Width : set_x; set_y = set_y < 0 ? 0 : set_y = set_y > panelContent.Height - DesignObjects[index].Region.Height ? panelContent.Height - DesignObjects[index].Region.Height : set_y; DesignObjects[index].Region = new Rectangle(set_x, set_y, DesignObjects[index].Region.Width, DesignObjects[index].Region.Height); panelContent.Invalidate(); } else { index = DesignObjects.FindIndex(d => d.Region.Contains(e.Location)); if (index != -1) pL.Cursor = Cursors.SizeAll; else pL.Cursor = Cursors.Default; } } } }
[此贴子已经被作者于2023-3-21 11:29编辑过]
[此贴子已经被作者于2023-3-21 19:34编辑过]