ImagePointControl.cs 5.6 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;
using System.Drawing.Drawing2D;

namespace UserFromControl
{
    public partial class ImagePointControl : UserControl
    {
        public ImagePointControl()
        {
            InitializeComponent();
        }


        public int selectIndex = 0;
        public bool ImageNormal = true;
        public bool ShowName = true; 
        private float imageXiShu = 1;

        public double CurrBeiLu = 1;
        private Image PicImage = null;
        private List<SMTPointInfo> PointList = new List<SMTPointInfo>();
        private int Width = 0;
        private int Height = 0;
        public void SetImage(Image image)
        {
            picBoard.Image = image;
            this.PicImage = image;
        }

        public void LoadPoint(int width, int height, List<SMTPointInfo> pointList = null )
        {
            this.PointList = pointList;
            Width = width;
            Height = height;
            CurrBeiLu = 1;
            //picBoard.SizeMode = PictureBoxSizeMode.AutoSize;
            if (pointList == null)
            {
                return;
            }
         
            if (!ImageNormal)
            {
                picBoard.Width = picBoard.Image.Width;
                picBoard.Height = picBoard.Image.Height; 
            }
            else
            {
                int xishu = 100;

                picBoard.Width = width * xishu;
                picBoard.Height = height * xishu;
                if (picBoard.Width > panBoard.Width)
                {
                    picBoard.Height = height * xishu * panBoard.Width / (width * xishu);
                    picBoard.Width = panBoard.Width;
                }
                if (picBoard.Height > panBoard.Height)
                {
                    picBoard.Width = width * xishu * panBoard.Height / (height * xishu);
                    picBoard.Height = panBoard.Height;
                }
            }
          
            //显示点 
            picBoard.Refresh();
            panBoard.Refresh();

            imageXiShu = (float)picBoard.Width / (float)width;

            Graphics grfx = picBoard.CreateGraphics();
            //float preX = 0;
            //float preY = 0;
            int index = 0;
            foreach (SMTPointInfo weld in pointList)
            {
                float x = (float)Math.Abs(weld.PositionX) * imageXiShu;
                float y = (float)Math.Abs(weld.PositionY) * imageXiShu;

                int lineLength = 8;
                Color color = Color.HotPink;
                Brush brushes = Brushes.HotPink;
                if (weld.CheckOK)
                {
                    color = Color.Red;
                    brushes = Brushes.Red;
                }
                if (index.Equals(selectIndex))
                {
                    color = Color.Orange;
                    brushes = Brushes.Orange;
                }
                grfx.DrawLine(new Pen(color, 2), x, y - lineLength, x, y + lineLength);
                grfx.DrawLine(new Pen(color, 2), x - lineLength, y, x + lineLength, y);
                //写字 
                if (index.Equals(selectIndex) || ShowName)
                {
                    grfx.DrawString(weld.GetDisplayStr(), new Font("Arial ", 9, FontStyle.Bold), brushes, x - lineLength / 2 + 2, y + lineLength / 2 + 2);
                }
                index++;
            }

            grfx.SmoothingMode = SmoothingMode.HighQuality; 
            grfx.Dispose(); 
        }

        private void ImagePointControl_Load(object sender, EventArgs e)
        {
            picBoard.Tag = false;
            picBoard.SizeMode = PictureBoxSizeMode.StretchImage;
            picBoard.MouseWheel += picBoard_MouseWheel;
          
        }
        private void PicBoardBlowUp()
        {
            if (this.picBoard.Width > 5000 || this.picBoard.Height > 5000)
            {
                //不能继续放大
                return;
            }
            float w = this.picBoard.Width * 1.2f; //每次放大 20%
            float h = this.picBoard.Height * 1.2f;
            CurrBeiLu = CurrBeiLu * 1.2f;
            this.picBoard.Size = Size.Ceiling(new SizeF(w, h));
            picBoard.Invalidate();
        }
        private void PicBoardShrink()
        {
            if (this.picBoard.Width < panBoard.Width && this.picBoard.Height < panBoard.Height)
            {
                //不能继续缩小
                return;
            }
            float w = this.picBoard.Width * 0.8f; //每次縮小 20%  
            float h = this.picBoard.Height * 0.8f;
            this.picBoard.Size = Size.Ceiling(new SizeF(w, h));
            CurrBeiLu = CurrBeiLu * 0.8f;
        }
          
        void picBoard_MouseWheel(object sender, MouseEventArgs e)
        {
            this.picBoard.SizeMode = PictureBoxSizeMode.Zoom;
            //向前
            if (e.Delta > 0)
            {
                //PicBoardBlowUp();
            }
            //向后
            else if (e.Delta < 0)
            {
                //PicBoardShrink();
            }
        }

        //private void chbShowName_CheckedChanged(object sender, EventArgs e)
        //{
        //    ShowName = chbShowName.Checked;
        //    LoadPoint(Width, Height, PointList);
        //}

        //private void chbNormal_CheckedChanged(object sender, EventArgs e)
        //{
        //    ImageNormal = chbNormal.Checked;
        //    LoadPoint(Width, Height, PointList);
        //}
    }
}