using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProductionControl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //int ret = API.GWQ_CallShowPDF(0, 'K', 9600, this.textBox1.Text.Trim(), this.textBox2.Text.Trim(), "", "");
                //if (ret != 0)
                //    MessageBox.Show("失败，ret="+ret, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private double ratio = 1;        // 图片的起始显示比例
        private double ratioStep = 0.1;
        private Size pic_size;
        private int xPos;
        private int yPos;

        private void Form1_Load(object sender, EventArgs e)
        {
            var bmp = Bitmap.FromFile("f:\\1.jpg");
            Size size = bmp.Size;
            this.panel1.ClientSize = new Size(this.panel1.ClientSize.Width,
                (int)(this.panel1.ClientSize.Width * (size.Height * 1.0f / size.Width)));  
            this.pictureBox1.Size = this.panel1.ClientSize;
            this.pictureBox1.Location = new Point(0, 0);
            this.pictureBox1.Image = bmp;

            pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel);
            pictureBox1.MouseMove += pictureBox1_MouseMove;
            pictureBox1.MouseDown += pictureBox1_MouseDown; 
            this.ActiveControl = this.pictureBox1; // 设置焦点   

            pic_size = this.panel1.ClientSize;

            //
            //MessageBox.Show("菜单栏厚度" + SystemInformation.MenuHeight.ToString());  //获取标准菜单栏的厚度        
            //MessageBox.Show("标题栏厚度" + SystemInformation.CaptionHeight.ToString()); //获取标准标题栏的厚度
            this.Height = panel1.Bottom+ SystemInformation.CaptionHeight + SystemInformation.MenuHeight+ statusStrip1.Height;
        }
        private void panel1_SizeChanged(object sender, EventArgs e)
        {
            pic_size = this.panel1.ClientSize;
        }
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            Point pictureBoxPoint = this.PointToClient(Cursor.Position);
            //this.Text = $"{e.X}:{e.Y}; {pictureBoxPoint.X}:{pictureBoxPoint.Y}; {pictureBox1.Left}:{pictureBox1.Top}; " +
            //    $"{this.pictureBox1.Width}:{this.pictureBox1.Height}; {this.Width}:{this.Height}; " +
            //    $"Cursor:{Cursor.Position.X}:{Cursor.Position.Y}";
            if (e.Delta > 0)
            {
                ratio += ratioStep;
                if (ratio > 100) // 放大上限
                    ratio = 100;
                else
                {
                    int x = (int)(pictureBox1.Left-e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
                    int y = (int)(pictureBox1.Top- e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) + 0.5);

                    //this.Text = $"{pictureBox1.Width}-{pic_size.Width};{ratio}，{pictureBox1.Left}-{e.X}* 比例:{e.X*1.0d/pictureBox1.Width}={e.X}/{pictureBox1.Width} * {(ratioStep)}={x} | " +
                    //    $"{pictureBox1.Top}-{e.Y}* {e.Y * 1.0f / pictureBox1.Height * (ratio - 1.0d)}={y}"; 
                    this.changePictureBoxSize(new Point(x, y), ratio);
                }

            }
            else if (ratio - ratioStep >= 1)
            {
                ratio -= ratioStep;
                //if (ratio < 0.1)  // 放大下限
                //    ratio = 0.1;
                //else 
                int x = (int)(pictureBox1.Left + e.X * (e.X * 1.0d / pictureBox1.Width * ratioStep) + 0.5);
                int y = (int)(pictureBox1.Top + e.Y * (e.Y * 1.0d / pictureBox1.Height * ratioStep) +0.5);
                this.changePictureBoxSize(new Point(x, y), ratio);
            }
        }
        private void changePictureBoxSize(Point location, double ratio)
        {
            var picSize = pictureBox1.Size;
            picSize.Width = Convert.ToInt32(pic_size.Width * ratio);
            picSize.Height = Convert.ToInt32(pic_size.Height * ratio);
            pictureBox1.Size = picSize;

            if (location.X > 0) location.X = 0;
            if (location.Y > 0) location.Y = 0;
            if (picSize.Width+location.X <this.panel1.ClientSize.Width) location.X = -(picSize.Width- this.panel1.ClientSize.Width);
            if (picSize.Height + location.Y < this.panel1.ClientSize.Height) location.Y = -(picSize.Height - this.panel1.ClientSize.Height);

            //Point location = new Point();
            //location.X = (this.ClientSize.Width - this.pictureBox1.Width) / 2;
            //location.Y = (this.ClientSize.Height - this.pictureBox1.Height) / 2;
            this.pictureBox1.Location = location;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                // 鼠标按下拖拽图片
                if (e.Button == MouseButtons.Left)
                {
                    this.Text = $"{e.X}:{e.Y};{xPos}:{yPos}";
                    // 限制拖拽出框
                    if (pictureBox1.Width > this.panel1.ClientSize.Width 
                        || pictureBox1.Height > this.panel1.ClientSize.Height)
                    {
                        int moveX = e.X - xPos;
                        int moveY = e.Y - yPos;
                        if ((pictureBox1.Left + moveX) <= 0 
                            && (pictureBox1.Top + moveY) <= 0 
                            && (pictureBox1.Right + moveX) >= this.panel1.ClientSize.Width
                            && (pictureBox1.Bottom + moveY) >= this.panel1.ClientSize.Height)
                        {
                            pictureBox1.Left += moveX;//设置x坐标.
                            pictureBox1.Top += moveY;//设置y坐标. 
                        }
                    }
                }
            }
            catch (Exception dd)
            {
                MessageBox.Show(dd.Message);
            }
        } 
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            xPos = e.X;//当前x坐标.
            yPos = e.Y;//当前y坐标.
        }

        //--------------
        void pictureBox1_MouseWheel1(object sender, MouseEventArgs e)//鼠标滚轮事件
        {
            double step = 1.2;//缩放倍率
            if (e.Delta > 0)
            {
                if (pictureBox1.Height >= Screen.PrimaryScreen.Bounds.Height * 10)
                    return;
                pictureBox1.Height = (int)(pictureBox1.Height * step);
                pictureBox1.Width = (int)(pictureBox1.Width * step);
                int px = Cursor.Position.X - pictureBox1.Location.X;
                int py = Cursor.Position.Y - pictureBox1.Location.Y;
                int px_add = (int)(px * (step - 1.0));
                int py_add = (int)(py * (step - 1.0));
                pictureBox1.Location = new Point(pictureBox1.Location.X - px_add, pictureBox1.Location.Y - py_add);
                Application.DoEvents();
            }
            else
            {
                if (pictureBox1.Height <= Screen.PrimaryScreen.Bounds.Height) return;
                pictureBox1.Height = (int)(pictureBox1.Height / step);
                pictureBox1.Width = (int)(pictureBox1.Width / step);
                int px = Cursor.Position.X - pictureBox1.Location.X;
                int py = Cursor.Position.Y - pictureBox1.Location.Y;
                int px_add = (int)(px * (1.0 - 1.0 / step));
                int py_add = (int)(py * (1.0 - 1.0 / step));
                pictureBox1.Location = new Point(pictureBox1.Location.X + px_add, pictureBox1.Location.Y + py_add);
                Application.DoEvents();
            }
        }

        private void tsbtnClose_Click(object sender, EventArgs e)
        {
            this.checkBox1.Checked = !this.checkBox1.Checked;
            //this.panel1.Width *= 2;
            //this.panel1.Height *= 2;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.Text=this.checkBox1.Checked.ToString();
        }

        private void tsbtnMakeTag_Click(object sender, EventArgs e)
        {

        }
    }
}
