// ALL RIGHTS RESERVED:
// ====================
// The contents of this file, and associated files in this directory, are 
// Copyright (C) SPT , all rights reserved, 2009.
// All software Source Code, Images, Database-Design and code, Graphics Design 
// and source files, and related content (collectively referred to as SOURCE) are 
// Copyright (c) SPT, 2009, All Rights Reserved.
// http://SerialPortTool.com

// PTZ Controller is a Registered Trademark of SerialPortTool.com.
using System;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace PTZController
{
    public class SkinForm : Form
    {
        public SkinForm()
        {
            this.MouseDown+=new MouseEventHandler(SkinForm_MouseDown);
            this.MouseMove += new MouseEventHandler(SkinForm_MouseMove);
            this.MouseUp += new MouseEventHandler(SkinForm_MouseUp);
        }

        protected void ApplyStyle()
        {
            try
            {
                Bitmap bmp = new Bitmap(BackgroundImage);

                HelpFunction.CreateControlRegion(this, bmp);
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }

        #region moving form
        private void SkinForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
//                 m_mouseDown = true;
// 
//                 m_windowPoint.X = Left;
//                 m_windowPoint.Y = Top;
// 
//                 m_mousePoint = Cursor.Position;

                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); 
            }
        }

        void SkinForm_MouseMove(object sender, MouseEventArgs e)
        {
            /*
                        if (!m_mouseDown)
                        {
                            return;
                        }

                        int deltaX =Cursor.Position.X - m_mousePoint.X;
                        int deltaY =Cursor.Position.Y - m_mousePoint.Y;

                        if (m_docked)
                        {
                            deltaX = 0;
                        }

                        if (deltaX != 0 || deltaY != 0)
                        {
                            Left = m_windowPoint.X + deltaX;
                            Top = m_windowPoint.Y + deltaY;
                        }

                        if (OwnerForm == null)
                        {
                             return;
                        }

                        if (Math.Abs(Right - OwnerForm.Left) <= LinkMargin)
                        {
                            this.Left = OwnerForm.Left - Width;
                            m_docked = true;
                        }

                        if (Math.Abs(Left - OwnerForm.Right) <= LinkMargin)
                        {
                            this.Left = OwnerForm.Right;
                            m_docked = true;
                        }
            */
        }

        void SkinForm_MouseUp(object sender, MouseEventArgs e)
        {
//             m_mouseDown = false;
        }

        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HTCAPTION = 0x2;
        [DllImport("User32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("User32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); 

        #endregion

        private const int LinkMargin = 5;

/*
        private bool m_mouseDown = false;
        private Point m_windowPoint;
        private Point m_mousePoint;
        private bool m_docked = false;
*/

        private SkinForm m_ownerForm;

        public SkinForm OwnerForm
        {
            get { return  m_ownerForm; }
            set {  m_ownerForm = value; }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // SkinForm
            // 
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Name = "SkinForm";
            this.ResumeLayout(false);

        }

    }
}
