FrmCameraPlay.cs 4.7 KB
using AccAOI.camera;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
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 FrmCameraPlay : Form
    {
        public bool ISShow = false ;
        public int ScreenIndex = ConfigAppSettings.GetIntValue(Setting_Init.VideoFrom_Index,2);
        public bool NeedShow = ConfigAppSettings.GetBoolValue(Setting_Init.VideoForm_Show );
        public static FrmCameraPlay instance = new FrmCameraPlay();
        public FrmCameraPlay()
        {
            NeedShow = false ;
            InitializeComponent();
        }

        private void FrmCameraPlay_Load(object sender, EventArgs e)
        {
        }
        public bool ShowForm()
        {
            ISShow = true;
            if (!NeedShow)
            {
                return false;
            }
            Screen[] sc = Screen.AllScreens;
            Screen screen = null;
            if (sc.Count() > ScreenIndex)
            {
                screen = sc[ScreenIndex];

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

                this.StartPosition = FormStartPosition.Manual;
                this.WindowState = FormWindowState.Maximized;
                this.Location = new Point(0, 0);
                this.ShowDialog();
                LogUtil.info("实时视频界面显示:屏幕索引=" + ScreenIndex + " !");
                return true;
            }
            else if (sc.Count() > 0)
            {
                screen = sc[0]; 
                this.StartPosition = FormStartPosition.Manual;
                this.Size = new Size(400, 300);
                this.Location = new Point(screen.Bounds.Left+ screen.Bounds.Width - this.Width, screen.Bounds.Top+ 5); 
                this.ShowDialog();
                LogUtil.info("实时视频界面显示:屏幕索引=" + ScreenIndex + " !");
                return true;
            }
            else
            {
                LogUtil.info("实时视频界面显示:屏幕索引=" + ScreenIndex + " 未找到");
                return false;
            }

        }

        private void FrmCameraPlay_Shown(object sender, EventArgs e)
        {
            timer1.Start();
            this.TopMost = true;
        }
         
        private void timer1_Tick(object sender, EventArgs e)
        {
            if(this.Visible  )
            {
                try
                {
                    string camera = ConfigAppSettings.GetValue(Setting_Init.CameraName);
                    if (camera == "")
                    {
                        camera = AccAOI.camera.CameraManager.hikNameList.ToArray().FirstOrDefault();
                    }
                    Image currImage = CameraManager.GetCamerImage(camera);

                    if (currImage == null)
                    {
                        string file = @"G:\test.bmp";
                        if (File.Exists(file))
                        {
                            //从本地加载图片 
                            Bitmap aa = new Bitmap(file);
                            currImage = ImageManager.DeepClone(aa);
                        }
                    }

                    if (currImage != null)
                    {
                        if (pictureBox1.Image != null)
                        {
                            pictureBox1.Image.Dispose();
                        }
                        pictureBox1.Image = currImage;
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.error("实时视频播放出错:" + ex.ToString());
                }
            }
        }

        private void FrmCameraPlay_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }

        public const int HWND_TOP = 0;
        public const int HWND_BOTTOM = 1;
        public const int HWND_TOPMOST = -1;
        public const int HWND_NOTOPMOST = -2;


        [DllImport("user32.dll")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint wFlags);


        [DllImport("user32.dll")]
        public static extern bool GetWindowRect(IntPtr hWnd, out WindowRect lpRect);
    }
    public struct WindowRect
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }
}