using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

using AxSftButtonLib25;
using SftButtonLib25;
using Softelvdm.OLEConvert;

namespace Popup
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
        private AxSftButtonLib25.AxSftButton axSftButton1;
        private AxSftButtonLib25.AxSftButton axSftButton2;
        private AxSftButtonLib25.AxSftButton axSftButton3;
        private AxSftButtonLib25.AxSftButton axSftButton4;
        private AxSftButtonLib25.AxSftButton axSftButton5;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.ImageList imageList1;
        private System.Windows.Forms.Timer timer1;
        private System.Windows.Forms.ContextMenu contextMenu1;
        private System.Windows.Forms.MenuItem menuItem1;
        private System.Windows.Forms.MenuItem menuItem2;
        private System.Windows.Forms.MenuItem menuItem3;
        private System.Windows.Forms.MenuItem menuItem4;
        private Softelvdm.OCXHelper.SftHelperComponent sftHelperComponent1;
        private System.ComponentModel.IContainer components;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.axSftButton1 = new AxSftButtonLib25.AxSftButton();
            this.axSftButton2 = new AxSftButtonLib25.AxSftButton();
            this.axSftButton3 = new AxSftButtonLib25.AxSftButton();
            this.axSftButton4 = new AxSftButtonLib25.AxSftButton();
            this.axSftButton5 = new AxSftButtonLib25.AxSftButton();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.contextMenu1 = new System.Windows.Forms.ContextMenu();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.menuItem4 = new System.Windows.Forms.MenuItem();
            this.sftHelperComponent1 = new Softelvdm.OCXHelper.SftHelperComponent(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.axSftButton1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.axSftButton2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.axSftButton3)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.axSftButton4)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.axSftButton5)).BeginInit();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.button1.Location = new System.Drawing.Point(304, 8);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(64, 24);
            this.button1.TabIndex = 0;
            this.button1.Text = "Close";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.label1.Location = new System.Drawing.Point(8, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(272, 32);
            this.label1.TabIndex = 1;
            this.label1.Text = "SftButton/OCX And Popup Menus";
            // 
            // axSftButton1
            // 
            this.axSftButton1.Location = new System.Drawing.Point(32, 40);
            this.axSftButton1.Name = "axSftButton1";
            this.axSftButton1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftButton1.OcxState")));
            this.axSftButton1.Size = new System.Drawing.Size(112, 32);
            this.axSftButton1.TabIndex = 2;
            this.axSftButton1.TabStop = false;
            this.axSftButton1.ClickEvent += new System.EventHandler(this.axSftButton1_ClickEvent);
            this.axSftButton1.DropDownClick += new System.EventHandler(this.axSftButton1_DropDownClick);
            this.axSftButton1.DropDownDblClick += new System.EventHandler(this.axSftButton1_DropDownClick);
            // 
            // axSftButton2
            // 
            this.axSftButton2.Location = new System.Drawing.Point(32, 88);
            this.axSftButton2.Name = "axSftButton2";
            this.axSftButton2.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftButton2.OcxState")));
            this.axSftButton2.Size = new System.Drawing.Size(112, 32);
            this.axSftButton2.TabIndex = 3;
            this.axSftButton2.TabStop= false;
            this.axSftButton2.ClickEvent += new System.EventHandler(this.axSftButton2_ClickEvent);
            this.axSftButton2.DropDownClick += new System.EventHandler(this.axSftButton2_DropDownClick);
            this.axSftButton2.DropDownDblClick += new System.EventHandler(this.axSftButton2_DropDownClick);
            // 
            // axSftButton3
            // 
            this.axSftButton3.Location = new System.Drawing.Point(32, 136);
            this.axSftButton3.Name = "axSftButton3";
            this.axSftButton3.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftButton3.OcxState")));
            this.axSftButton3.Size = new System.Drawing.Size(88, 32);
            this.axSftButton3.TabIndex = 4;
            this.axSftButton3.TabStop = false;
            this.axSftButton3.ClickEvent += new System.EventHandler(this.axSftButton3_ClickEvent);
            this.axSftButton3.DropDownClick += new System.EventHandler(this.axSftButton3_DropDownClick);
            this.axSftButton3.DropDownDblClick += new System.EventHandler(this.axSftButton3_DropDownClick);
            // 
            // axSftButton4
            // 
            this.axSftButton4.Location = new System.Drawing.Point(32, 184);
            this.axSftButton4.Name = "axSftButton4";
            this.axSftButton4.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftButton4.OcxState")));
            this.axSftButton4.Size = new System.Drawing.Size(128, 56);
            this.axSftButton4.TabIndex = 5;
            this.axSftButton4.TabStop = false;
            this.axSftButton4.ClickEvent += new System.EventHandler(this.axSftButton4_ClickEvent);
            this.axSftButton4.DropDownClick += new System.EventHandler(this.axSftButton4_DropDownClick);
            this.axSftButton4.DropDownDblClick += new System.EventHandler(this.axSftButton4_DropDownClick);
            // 
            // axSftButton5
            // 
            this.axSftButton5.Location = new System.Drawing.Point(136, 136);
            this.axSftButton5.Name = "axSftButton5";
            this.axSftButton5.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axSftButton5.OcxState")));
            this.axSftButton5.Size = new System.Drawing.Size(40, 24);
            this.axSftButton5.TabIndex = 6;
            this.axSftButton5.TabStop = false;
            this.axSftButton5.ClickEvent += new System.EventHandler(this.axSftButton5_ClickEvent);
            this.axSftButton5.DropDownClick += new System.EventHandler(this.axSftButton5_DropDownClick);
            this.axSftButton5.DropDownDblClick += new System.EventHandler(this.axSftButton5_DropDownClick);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(184, 48);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(184, 56);
            this.label2.TabIndex = 7;
            this.label2.Text = "A few samples demonstrating the use of the drop down button to display a popup me" +
                "nu.";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(184, 112);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(184, 56);
            this.label3.TabIndex = 8;
            this.label3.Text = "The drop down button is enabled using the ButtonStyle property.";
            // 
            // label4
            // 
            this.label4.Location = new System.Drawing.Point(184, 176);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(184, 56);
            this.label4.TabIndex = 9;
            this.label4.Text = "Also notice the flicker-free display as the bitmap is updated in the SftButton co" +
                "ntrol on the left.";
            // 
            // imageList1
            // 
            this.imageList1.ImageSize = new System.Drawing.Size(32, 32);
            this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
            this.imageList1.TransparentColor = System.Drawing.Color.White;
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.Interval = 200;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // contextMenu1
            // 
            this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
                                                                                         this.menuItem1,
                                                                                         this.menuItem2,
                                                                                         this.menuItem3,
                                                                                         this.menuItem4});
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 0;
            this.menuItem1.Text = "Default Action";
            this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 1;
            this.menuItem2.Text = "Action 1";
            this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 2;
            this.menuItem3.Text = "Action 2";
            this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
            // 
            // menuItem4
            // 
            this.menuItem4.Index = 3;
            this.menuItem4.Text = "Action 3";
            this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.button1;
            this.ClientSize = new System.Drawing.Size(376, 262);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.axSftButton5);
            this.Controls.Add(this.axSftButton4);
            this.Controls.Add(this.axSftButton3);
            this.Controls.Add(this.axSftButton2);
            this.Controls.Add(this.axSftButton1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "Form1";
            this.Text = "Softel vdm, Inc. - Popup Sample";
            ((System.ComponentModel.ISupportInitialize)(this.axSftButton1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.axSftButton2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.axSftButton3)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.axSftButton4)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.axSftButton5)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }
        //--locate-marker--

        private void button1_Click(object sender, System.EventArgs e)
        {
            Application.Exit();
        }

        private int m_WorldIndex = 0;

        private void SetImage()
        {
            // Update the button with the next image in the image list
            // to get the spinning globe effect
            axSftButton4.Image1.NETImageObject = imageList1.Images[m_WorldIndex];
            ++m_WorldIndex;
            if (m_WorldIndex >= imageList1.Images.Count) m_WorldIndex = 0;
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            // Update the button bitmap every so often to get the
            // spinning globe effect
            SetImage();
        }

        // In order to support .NET 1.0 through the latest, we use TrackPopupMenu
        // to nicely right-align the popup menu (Windows API), because the 
        // ContextMenu class doesn't support defining the alignment
        [DllImport("user32.dll", EntryPoint="TrackPopupMenu")]
        public static extern IntPtr TrackPopupMenu(IntPtr Handle, uint uFlags, int x, int y, int nReserved, IntPtr hwnd, IntPtr prcRect);
        const int TPM_RIGHTALIGN = 0x0008;
        const int TPM_TOPALIGN = 0x0000;
        const int TPM_LEFTBUTTON = 0x0000;

        private void menuItem1_Click(object sender, System.EventArgs e)
        {
            MessageBox.Show("Default Action selected");
        }

        private void menuItem2_Click(object sender, System.EventArgs e)
        {
            MessageBox.Show("Action 1 selected");
        }

        private void menuItem3_Click(object sender, System.EventArgs e)
        {
            MessageBox.Show("Action 2 selected");
        }

        private void menuItem4_Click(object sender, System.EventArgs e)
        {
            MessageBox.Show("Action 3 selected");
        }

        private void axSftButton1_ClickEvent(object sender, System.EventArgs e)
        {
            // Invoke the default action, because the button was clicked
            menuItem1_Click(axSftButton1, new System.EventArgs());
        }

        private void axSftButton1_DropDownClick(object sender, System.EventArgs e)
        {
            // The drop down button was clicked. Display a popup menu
            // right-aligned with the right edge of the button
            Point pt = new Point(axSftButton1.Location.X + axSftButton1.Size.Width,
                                    axSftButton1.Location.Y + axSftButton1.Size.Height);
            pt = PointToScreen(pt);
            axSftButton1.CancelMode();
            TrackPopupMenu(contextMenu1.Handle, TPM_RIGHTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.X, pt.Y, 0, this.Handle, IntPtr.Zero);
        }

        private void axSftButton2_ClickEvent(object sender, System.EventArgs e)
        {
            // Invoke the default action, because the button was clicked
            menuItem1_Click(axSftButton2, new System.EventArgs());
        }

        private void axSftButton2_DropDownClick(object sender, System.EventArgs e)
        {
            // Thedrop down button was clicked. Display a popup menu
            // right-aligned with the right edge of the button
            Point pt = new Point(axSftButton2.Location.X + axSftButton2.Size.Width,
                axSftButton2.Location.Y + axSftButton2.Size.Height);
            pt = PointToScreen(pt);
            axSftButton2.CancelMode();
            TrackPopupMenu(contextMenu1.Handle, TPM_RIGHTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.X, pt.Y, 0, this.Handle, IntPtr.Zero);
        }

        private void axSftButton3_ClickEvent(object sender, System.EventArgs e)
        {
            // Invoke the default action, because the button was clicked
            menuItem1_Click(axSftButton3, new System.EventArgs());
        }

        private void axSftButton3_DropDownClick(object sender, System.EventArgs e)
        {
            // The drop down button was clicked. Display a popup menu
            // right-aligned with the right edge of the button
            Point pt = new Point(axSftButton3.Location.X + axSftButton3.Size.Width,
                axSftButton3.Location.Y + axSftButton3.Size.Height);
            pt = PointToScreen(pt);
            axSftButton3.CancelMode();
            TrackPopupMenu(contextMenu1.Handle, TPM_RIGHTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.X, pt.Y, 0, this.Handle, IntPtr.Zero);
        }

        private void axSftButton4_ClickEvent(object sender, System.EventArgs e)
        {
            // Invoke the default action, because the button was clicked
            menuItem1_Click(axSftButton4, new System.EventArgs());
        }

        private void axSftButton4_DropDownClick(object sender, System.EventArgs e)
        {
            // The drop down button was clicked. Display a popup menu
            // right-aligned with the right edge of the button
            Point pt = new Point(axSftButton4.Location.X + axSftButton4.Size.Width,
                axSftButton4.Location.Y + axSftButton4.Size.Height);
            pt = PointToScreen(pt);
            axSftButton4.CancelMode();
            TrackPopupMenu(contextMenu1.Handle, TPM_RIGHTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.X, pt.Y, 0, this.Handle, IntPtr.Zero);
        }

        private void axSftButton5_ClickEvent(object sender, System.EventArgs e)
        {
            // Invoke the default action, because the button was clicked
            menuItem1_Click(axSftButton5, new System.EventArgs());
        }

        private void axSftButton5_DropDownClick(object sender, System.EventArgs e)
        {
            // The drop down button was clicked. Display a popup menu
            // right-aligned with the right edge of the button
            Point pt = new Point(axSftButton5.Location.X + axSftButton5.Size.Width,
                axSftButton5.Location.Y + axSftButton5.Size.Height);
            pt = PointToScreen(pt);
            axSftButton5.CancelMode();
            TrackPopupMenu(contextMenu1.Handle, TPM_RIGHTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.X, pt.Y, 0, this.Handle, IntPtr.Zero);
        }
    }
}