| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 6605 人关注过本帖
标题:c#做了个双指针工业仪表盘,怎么让两个指针分别显示
只看楼主 加入收藏
lochao
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-8-14
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
c#做了个双指针工业仪表盘,怎么让两个指针分别显示
c#做了个双指针工业仪表盘,怎么让两个指针分开
图片附件: 游客没有浏览图片的权限,请 登录注册
显示两个不同的参数,现在是两个指针一起指示
搜索更多相关主题的帖子: 表盘 c# 双指针 显示 指针 
2023-03-20 15:19
qq2889577966
Rank: 4
等 级:业余侠客
威 望:5
帖 子:68
专家分:287
注 册:2021-4-14
收藏
得分:20 
1、Task多线程,跨线程,wpf参考http://bbs.,winform线程里使用Paint重绘
2、在一个while更新值,然后重绘界面(winform),wpf移动位置
2023-03-20 16:44
lochao
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-8-14
收藏
得分:0 
谢谢楼上,主要是重绘的时候,两个一起重绘
2023-03-21 08:41
qq2889577966
Rank: 4
等 级:业余侠客
威 望:5
帖 子:68
专家分:287
注 册:2021-4-14
收藏
得分:0 
回复 3楼 lochao
链接:https://pan.baidu.com/s/1vjIjNlna653afu4Vyl2rvw
提取码:3il8
多图像的例子,自个改改,把鼠标移动改成固定位置就可以了,什么双缓冲、无闪烁什么的都有了
只是举个例子,参考就行,看下
public override void OnPaint(PaintEventArgs e)
            {
                e.Graphics.DrawImage(m_SetImage, m_Region);
            }
在哪里写,重绘的是哪部分内容

程序代码:
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-03-21 14:52 送鲜花  1朵   附言:感谢回复!
  • lochao2023-03-21 16:16 送鲜花  1朵  
2023-03-21 11:27
lochao
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-8-14
收藏
得分:0 
感谢回复 我学习学习
2023-03-21 16:16
lochao
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-8-14
收藏
得分:0 
工业仪表盘.rar (3 MB)
麻烦帮我看看
2023-03-21 16:52
qq2889577966
Rank: 4
等 级:业余侠客
威 望:5
帖 子:68
专家分:287
注 册:2021-4-14
收藏
得分:0 
回复 6楼 lochao
你那个动的。。。。还不如写入生成个bitmap旋转。前面的demo应该已经把多个元素动说清了吧,可以简化,写成这样子

 public class BandObject
    {
        public Bitmap Image; // 图像
        public data  // 角度、范围什么的等等
......
......
        public void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawImage(......);
        }
    }

public List<BandObject> Band_Objects = new List<BandObject>(); // 通过Band_Objects.Add()添加2个指针图像

main()
{
    while(true)
    {
        Band_Objects[0].data = ...    // 改变数据,应该是变化的,省事这么写,
        Band_Objects[1].data = ...    // 改变数据
        Thread.Sleep(1000); // 停1秒
        容器.Invalidate();
    }
}


private void 容器_Paint(object sender, PaintEventArgs e)
        {
            foreach (var item in Band_Objects)
            {
                item.OnPaint(e);
            }
        }

最后觉得吧,那个作图是美工的事,程序干的是调用的活,把图放到位置上。

[此贴子已经被作者于2023-3-21 19:34编辑过]

2023-03-21 19:19
lochao
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2013-8-14
收藏
得分:0 
这个思路不错
2023-03-24 08:48
快速回复:c#做了个双指针工业仪表盘,怎么让两个指针分别显示
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029610 second(s), 12 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved