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

namespace TSA_V
{
    public partial class FrmProjectorScreen : Form
    {
        public static  FrmProjectorScreen instance = new FrmProjectorScreen();
        public FrmProjectorScreen()
        {
            InitializeComponent();
        }
        public int ScreenIndex = 1;
        public int ScreenWidth = 1920;
        public int ScreenHeight = 1280;
        public int LastX = 0;
        public int LastY = 0;
        public int LastType = 1;
        public int LastSizeX = 2;
        public int LastSizeY = 2;
        public bool ISShow = false;
   //     private bool hasShow = false;
        private void FrmScreenTest_Load(object sender, EventArgs e)
        {
            this.Text = "显示测试_" + ScreenIndex;
            this.StartPosition = FormStartPosition.Manual; 
        }
        public bool  CloseShowName=false;
        public bool ShowForm()
        {
            Screen[] sc = Screen.AllScreens;
            this.StartPosition = FormStartPosition.Manual;
            this.WindowState = FormWindowState.Maximized;
            if (sc.Count() > ScreenIndex)
            {
                Screen screen = sc[ScreenIndex];
                foreach (Screen s in sc)
                {
                    if (!s.Primary)
                    {
                        screen = s;
                        break;
                    }
                }

                string text = "名称:" + screen.DeviceName + "\r\n";
                text += "Width:" + screen.Bounds.Width + "\r\n";
                text += "Heigh:" + screen.Bounds.Height + "\r\n";
                text += "是否主显示器:" + screen.Primary.ToString() + "\r\n";
                //lblInfo.Text = text;

                ScreenWidth = screen.Bounds.Width;
                ScreenHeight = screen.Bounds.Height;

                TSAVBean.X_Max = ScreenWidth;
                TSAVBean.Y_Max = ScreenHeight;
                TSAVBean.X_Min = 1;
                TSAVBean.Y_Min = 1;

                ISShow = true;

                this.Location = new Point(screen.Bounds.Left, screen.Bounds.Top);
                this.Show();
                LogUtil.info("投影显示成功,屏幕索引=" + ScreenIndex + " !");
                return true;
            }
            else
            {
                LogUtil.info("投影显示失败:屏幕索引=" + ScreenIndex + " 未找到");
                return false;
            }

        }

        private void FrmScreenTest_Shown(object sender, EventArgs e)
        {
            ShowPoint(new ProjectorPInfo( ScreenWidth / 2, ScreenHeight / 2));
            DrawArea();
        }
        public void DrawArea()
        {
            if (!ISShow)
            {
                return;
            }
            int startX = 0;
            int startY = 0;
            int Point_X_Length = 3;
            int Point_Y_Length = 3;
            int lineWidth = (ScreenWidth  ) / (Point_X_Length - 1);
            int lineHeight = (ScreenHeight  ) / (Point_Y_Length - 1); 
            Graphics grfx = panel1.CreateGraphics();
            Color color = Color.White;
            grfx.SmoothingMode = SmoothingMode.HighQuality;

            //循环画线
            for (int i = 0; i <= Point_X_Length; i++)
            {
                float line = 0.2F;
                float picLocationX = startX + lineWidth * i;
                if (picLocationX >= ScreenWidth)
                {
                    picLocationX = ScreenWidth - 2;
                }
                grfx.DrawLine(new Pen(color, line), picLocationX, startY, picLocationX, ScreenHeight); 

            }
            for (int i = 0; i <= Point_Y_Length; i++)
            {
                float line = 0.2F;
                float picLocationY = startY + lineHeight * i;
                if (picLocationY >= ScreenHeight)
                {
                    picLocationY = ScreenHeight - 1;
                }
                grfx.DrawLine(new Pen(color, line), startX, picLocationY, ScreenWidth, picLocationY);
            }
            grfx.Dispose();
        }
        public  void DrawLine()
        {
            if (!ISShow)
            {
                return;
            }
            int startX = 0;
            int startY = 0;
            int Point_X_Length = 21;
            int Point_Y_Length = 21;

            int lineWidth = (ScreenWidth - startX * 2) / (Point_X_Length-1);
            int lineHeight = (ScreenHeight - startY * 2) / (Point_Y_Length-1);



            Graphics grfx = panel1.CreateGraphics();
            Color color = Color.White;
            grfx.SmoothingMode = SmoothingMode.HighQuality;
            
            //循环画线
            for (int i = 0; i <= Point_X_Length; i++)
            {
                float line = 0.2F;
                float picLocationX = startX + lineWidth * i;
                if (i.Equals(Point_X_Length / 2))
                {
                    line = 1;
                }
                else if (((i % 5).Equals(0)) && (i > 0) && (i < Point_X_Length))
                {
                    line = 0.5F;
                }

                grfx.DrawLine(new Pen(color, line), picLocationX, startY, picLocationX, ScreenHeight);

            }
            for (int i = 0; i <= Point_Y_Length; i++)
            {
                float line = 0.2F;
                float picLocationY = startY + lineHeight * i;
                if (i.Equals(Point_Y_Length / 2))
                {
                    line = 1;
                }
                else if (((i % 5).Equals(0)) && (i > 0) && (i < Point_Y_Length))
                {
                    line = 0.5F;
                }
                grfx.DrawLine(new Pen(color, line), startX, picLocationY, ScreenWidth, picLocationY);
            }
            grfx.Dispose();
        }
      
        private Color CurrColor = Color.White;
     
        public void ClearPoint()
        {
            if (!ISShow)
            {
                return;
            }
            Graphics g = this.panel1.CreateGraphics();
            g.Clear(Color.Black);
            g.Dispose();//释放资源
        }
        private void DrawPoint(Graphics g, int x, int y, int type = 0, int sizeX = 2, int sizeY = 2, int lineLength = 2, string name = "")
        {
            if (sizeX < 2)
            {
                sizeX = 2;
            }
            if (sizeY < 2)
            {
                sizeY = 2;
            }
            LastX = x;
            LastY = y;
            LastSizeX = sizeX;
            LastSizeY = sizeY;
            LastType = type;
            Pen pen = new Pen(CurrColor, lineLength);

            System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(CurrColor);
            //点类型,1 = 点,2 = +,3 =|,4 = -,5 = 方形,6 = 圆圈
            if (type.Equals(PointType_Define.PlusSign_2))
            {
                g.DrawLine(pen, new Point(x - sizeX, y), new Point(x + sizeX, y));
                g.DrawLine(pen, new Point(x, y - sizeY), new Point(x, y + sizeY));
            }
            else if (type.Equals(PointType_Define.VerticalLine_3))
            {
                g.DrawLine(pen, new Point(x, y - sizeY), new Point(x, y + sizeY));
            }
            else if (type.Equals(PointType_Define.Line_4))
            {
                g.DrawLine(pen, new Point(x - sizeX, y), new Point(x + sizeX, y));
            }
            else if (type.Equals(PointType_Define.Rectangle_5))
            {
                g.DrawRectangle(pen, x - sizeX / 2, y - sizeY / 2, sizeX, sizeY);
            }
            else if (type.Equals(PointType_Define.Cycle_6))
            {
                g.DrawEllipse(pen, x - sizeX / 2, y - sizeY / 2, sizeX, sizeY);
            }
            else if (type.Equals(PointType_Define.Plus_Up_7))
            {
                int plusSize = sizeX / 2;
                int pX = x;
                int pY = y - sizeY / 2 - lineLength - plusSize;
                g.DrawLine(pen, new Point(pX - plusSize, pY), new Point(pX + plusSize, pY));
                g.DrawLine(pen, new Point(pX, pY - plusSize), new Point(pX, pY + plusSize));

                g.DrawRectangle(pen, x - sizeX / 2, y - sizeY / 2, sizeX, sizeY);
            }
            else if (type.Equals(PointType_Define.Plus_Down_8))
            {
                g.DrawRectangle(pen, x - sizeX / 2, y - sizeY / 2, sizeX, sizeY);

                int plusSize = sizeX / 2;
                int pX = x;
                int pY = y + sizeY / 2 + lineLength + plusSize;
                g.DrawLine(pen, new Point(pX - plusSize, pY), new Point(pX + plusSize, pY));
                g.DrawLine(pen, new Point(pX, pY - plusSize), new Point(pX, pY + plusSize));
            }
            else if (type.Equals(PointType_Define.Plus_Left_9))
            {
                g.DrawRectangle(pen, x - sizeX / 2, y - sizeY / 2, sizeX, sizeY);

                int plusSize = sizeY / 2;
                int pX = x - sizeX / 2 - lineLength - plusSize;
                int pY = y;
                g.DrawLine(pen, new Point(pX - plusSize, pY), new Point(pX + plusSize, pY));
                g.DrawLine(pen, new Point(pX, pY - plusSize), new Point(pX, pY + plusSize));
            }
            else if (type.Equals(PointType_Define.Plus_Right_10))
            {
                g.DrawRectangle(pen, x - sizeX / 2, y - sizeY / 2, sizeX, sizeY);

                int plusSize = sizeY / 2;
                int pX = x + sizeX / 2 + lineLength + plusSize;
                int pY = y;
                g.DrawLine(pen, new Point(pX - plusSize, pY), new Point(pX + plusSize, pY));
                g.DrawLine(pen, new Point(pX, pY - plusSize), new Point(pX, pY + plusSize));
            }
            else
            {
                g.FillEllipse(myBrush, new Rectangle(x - sizeX / 2, y - sizeY / 2, sizeX, sizeY));//画实心椭圆          
            }
            if (!name.Equals(""))
            {
                int size = (sizeX + sizeY) / 2;
                size = 30;
                if (!CloseShowName)
                {
                    g.DrawString(name, new Font("Arial ", size, FontStyle.Regular), Brushes.White, x - size + 5, y + size + 2);
                }
            }
        }
        public void ShowPoint(DeviceLibrary.ProjectorPInfo p )
        {
            if (!ISShow)
            {
                return;
            }
            Graphics g = panel1.CreateGraphics();
            DrawPoint(g,  p.PX,p.PY,p.PType,p.SizeX,p.SizeY,p.PenWidth, p.ShowText);
            g.Dispose();
        }
        public void ShowPoint(bool showName  , params SMTPointInfo[] points  )
        {
            if (!ISShow)
            {
                return;
            }
            ClearPoint();
            Graphics g = panel1.CreateGraphics();
            foreach (SMTPointInfo point in points)
            {
                int type = point.PointType;
                int x = (int)point.NodePositionX;
                int y = (int)point.NodePositionY;
                int sizex = point.PointSizeX;
                int sizey = point.PointSizeY;
                DrawPoint(g, x, y, type, sizex,sizey,point.PenWidth,"");
               
                if (showName)
                {
                    int size = (sizex + sizey) / 2;
                    if (!CloseShowName)
                    {
                        if (point.CheckOK && (points.Length > 1))
                        {
                            g.DrawString(point.ShowText, new Font("Arial ", size, FontStyle.Regular), Brushes.Red, x - size / 2 + 5, y + size / 2 + 2);
                        }
                        else
                        {
                            g.DrawString(point.ShowText, new Font("Arial ", size, FontStyle.Regular), Brushes.White, x - size / 2 + 5, y + size / 2 + 2);
                        }
                    }
                }
            }
            g.Dispose();
        }
    }

    public class PointType_Define
    {

        public static int None = 0;
        /// <summary>
        ///  "1=●",
        /// </summary>
        public static int Point_1 = 1;
        /// <summary>
        ///   "2=✚",
        /// </summary>
        public static int PlusSign_2 = 2;
        /// <summary>
        ///  "3=┃",
        /// </summary>
        public static int VerticalLine_3 = 3;
        /// <summary>
        ///   "4=━━",
        /// </summary>
        public static int Line_4 = 4;
        /// <summary>
        ///   "5=□",
        /// </summary>
        public static int Rectangle_5 = 5;
        /// <summary>
        ///   "6=○"
        /// </summary>
        public static int Cycle_6 = 6;
        
           
        /// <summary>
        ///    "7=正极限_上方",
        /// </summary>
        public static int Plus_Up_7 = 7;

        /// <summary>
        ///    "8=正极限_下方",
        /// </summary>
        public static int Plus_Down_8 = 8;

        /// <summary>
        /// "9=正极限_左方 ✚□",
        /// </summary>
        public static int Plus_Left_9 = 9;

        /// <summary>
        ///"10=正极限_右方 □✚"
        /// </summary>
        public static int Plus_Right_10 = 10;
    }
}