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;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.WinForms;
using Leadtools.Drawing;
using Leadtools.Annotations;



namespace AnnotationTest
{
    public partial class Form1 : Form
    {
        RasterImageViewer viewer = new RasterImageViewer();
        RasterImage img;
        AnnAutomationManager annger;
        AnnAutomation automation;

        public Form1()
        {
            InitializeComponent();
            initControl();
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog odlg = new OpenFileDialog();
            odlg.Filter = "(*.jpg,*.png,*.jpeg,*.bmp)|*.jgp;*.png;*.jpeg;*.bmp|All files(*.*)|*.*";
            if (odlg.ShowDialog() == DialogResult.OK)
                loadImage(odlg.FileName);
        }

        private void loadImage(string filename)
        {
            if(automation!=null)
                automation.Container.Objects.Clear();
            img = new RasterCodecs().Load(filename);
            viewer.Image = img;
        }
        private void initControl() {
            Support.SetLicense();
            viewer.Dock = DockStyle.Fill;
            panel1.Controls.Add(viewer);
            viewer.HorizontalAlignMode = RasterPaintAlignMode.Center;
            viewer.VerticalAlignMode = RasterPaintAlignMode.Center;
            loadImage("qwe.jpg");
            if (viewer.Image != null)
            {
                // create and setup the automation manager 
                annger = new AnnAutomationManager();

                // Instruct the manager to create the default (all) automation objects. 
                annger.CreateDefaultObjects();


                // setup the automation (will create the container as well) 
                automation = new AnnAutomation(annger, viewer);

                // add an event handler for changes to the current designer 

                // setup this automation as the active one 
                automation.Active = true;
            }

        }




        private void addAnnText(int sign,string text) {
            AnnTextObject textObj = new AnnTextObject();
            textObj.Bounds = new AnnRectangle(getLocation(sign),new AnnSize(100,30));
            textObj.Font = new AnnFont("Arial", new AnnLength(30), FontStyle.Bold);
            textObj.TextColor = Color.Blue;
            textObj.Text = text;
            // Add the object to the automation container 
            //automation.Container.Children.Add(textObj);
            automation.Container.Objects.Add(textObj);
            viewer.Refresh();

        }
        //获取定位
        private AnnPoint getLocation(int sign) {
            int annwidth = 60;
            int annheight = 30;
            AnnPoint ap=new AnnPoint();
            switch (sign)
            {
                //1 2 3
                //4   5
                //6 7 8
                case 1:
                    ap = new AnnPoint(0, 0);
                    break;
                case 2:
                    ap = new AnnPoint((img.Width - annwidth) / 2, 0);
                    break;
                case 3:
                    ap = new AnnPoint(img.Width - annwidth, 0);
                    break;
                case 4:
                    ap = new AnnPoint(0, (img.Height - annheight) / 2);
                    break;
                case 5:
                    ap = new AnnPoint(img.Width - annwidth, (img.Height - annheight) / 2);
                    break;
                case 6:
                    ap = new AnnPoint(0, img.Height - annheight);
                    break;
                case 7:
                    ap = new AnnPoint((img.Width - annwidth) / 2, img.Height - annheight);
                    break;
                case 8:
                    ap = new AnnPoint(img.Width - annwidth, img.Height - annheight);
                    break;
            }
            return ap;
        }

        private void 左上ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(1,(sender as ToolStripMenuItem).Text);
        }

        private void 上方ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(2, (sender as ToolStripMenuItem).Text);
        }

        private void 右上ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(3, (sender as ToolStripMenuItem).Text);
        }

        private void 左边ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(4, (sender as ToolStripMenuItem).Text);
        }

        private void 右边ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(5, (sender as ToolStripMenuItem).Text);
        }

        private void 坐下ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(6, (sender as ToolStripMenuItem).Text);
        }

        private void 下方ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(7, (sender as ToolStripMenuItem).Text);
        }

        private void 右下ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addAnnText(8, (sender as ToolStripMenuItem).Text);
        }
    }
}
