using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Xml; 


namespace SerialPort
{
    ///---------------------------------------------------------------------*
    ///MCU 　  :功能窗体菜单模块　　　                                      *
    ///功能描述:调用其他窗体                                                *
    ///         显示工程说明和注意             　　　　　　                 *　
    ///目    的:用菜单打开其他窗体　　                                      *
    ///说    明:                                                            *
    ///注    意:                                                            *
    ///日    期:2011年9月30日                                               *
    ///编 程 者:胡玉鑫                                                      *
    ///-----------MCU(苏州大学飞思卡尔嵌入式系统研发中心)-------------------*
    public partial class MCU : Form
    {
        int flag;
        string picPath = Application.StartupPath + "\\..\\..\\Pictures\\图片素材\\AW60_1.jpg";
        string picPath1 = Application.StartupPath + "\\..\\..\\Pictures\\图片素材\\AW60_1.jpg";
        string picPath2 = Application.StartupPath + "\\..\\..\\Pictures\\图片素材\\AW60_2.jpg";
        string picPath3 = Application.StartupPath + "\\..\\..\\Pictures\\图片素材\\AW60_3.jpg";
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 函 数 名:MCU:类MCU的构造函数    　　　                          *
        /// 功    能:完成窗体的初始化工作                                   *
        /// 函数调用:InitializeComponent                                    *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        public MCU()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();             
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:串口测试ToolStripMenuItem                              *
        /// 事    件:Click                                                  *   
        /// 功    能:调用串口测试窗体                                       *
      　/// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void SCIToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SCItest f = new SCItest();
            f.Show();
            MCUStatus.Text = "已打开串口测试";
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:小灯测试ToolStripMenuItem                              *
        /// 事    件:Click                                                  *   
        /// 功    能:调用小灯窗体                                           *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void LightToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Light f1 = new Light();
            f1.Show();
            MCUStatus.Text = "已打开小灯Light";
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:lEDToolStripMenuItem                                   *
        /// 事    件:Click                                                  *   
        /// 功    能:调用lED窗体                                            *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void lEDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LED f = new LED();
            f.Show();
            MCUStatus.Text = "已打开数码管LED";
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:LCDToolStripMenuItem                                   *
        /// 事    件:Click                                                  *   
        /// 功    能:调用LCD窗体                                            *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void LCDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LCD f = new LCD();
            f.Show();
            MCUStatus.Text = "已打开液晶LCD";
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:键盘ToolStripMenuItem                                  *
        /// 事    件:Click                                                  *   
        /// 功    能:调用键盘窗体                                           *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void KBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KB f = new KB();
            f.Show();
            MCUStatus.Text = "已打开键盘KB";
        }
        
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:TimerToolStripMenuItem                                 *
        /// 事    件:Click                                                  *   
        /// 功    能:调用键盘窗体                                           *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void timerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Timer f=new Timer();
            f.Show();
            MCUStatus.Text = "已打开时钟Timer";
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:ADToolStripMenuItem                                    *
        /// 事    件:Click                                                  *   
        /// 功    能:调用键盘窗体                                           *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void ADToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AD f = new AD();
            f.Show();
            MCUStatus.Text = "已打开传感器AD";
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:PWMToolStripMenuItem                                   *
        /// 事    件:Click                                                  *   
        /// 功    能:调用键盘窗体                                           *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void PWMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PWM f = new PWM();
            f.Show();
            MCUStatus.Text = "已打开PWM";
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:FlashToolStripMenuItem                                 *
        /// 事    件:Click                                                  *   
        /// 功    能:调用flash窗体                                          *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void FlashToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Flash f = new Flash();
            f.Show();
            MCUStatus.Text = "已打开Flash";
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:看门狗COPToolStripMenuItem                             *
        /// 事    件:Click                                                  *   
        /// 功    能:调用COP窗体                                            *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void COPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            COP f = new COP();
            f.Show();
            MCUStatus.Text = "已打开看门狗";
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:MCU:窗体                                               *
        /// 事    件:Load                                                   *
        /// 功    能:执行加载窗体程序,设置控件提示信息                      * 
        /// 函数调用:无                                                     *
        /// </summary>                                                      *
        /// <param name="sender"></param>                                   *
        /// <param name="e"></param>                                        *
        /// <remarks></remarks>                                             *
        ///-----------------------------------------------------------------*
        private void MCU_Load(object sender, EventArgs e)
        {//设置控件提示信息
            this.toolTip1.ToolTipIcon = ToolTipIcon.Info;
            this.toolTip1.ToolTipTitle = "提示信息";
            this.toolTip1.SetToolTip(this.btnpicchang1, "从默认背景库中，选择一个默认背景图像文件。");
            this.toolTip1.SetToolTip(this.button2, "弹出文件打开对话框，选择一个背景图像文件。");
            SetStyle(ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
            picPath = GetConfigString("picPath");
            try
            {
                BackgroundImage = Image.FromFile(picPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message+"图片不存在将使用默认背景","错误提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                BackgroundImage = Image.FromFile(picPath1);
            }
            
            if (picPath == picPath1) flag = 0;
            else if (picPath == picPath2) flag = 1;
            else if (picPath == picPath3) flag = 2;
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:App.config                                             *
        /// 事    件:读操作                                                 *
        /// 功    能:从App.config中，读取Value值                            *
        /// 函数调用:无                                                     *
        ///-----------------------------------------------------------------*
        public static string GetConfigString(string key)
        {
            // 
            // TODO: 在此处添加构造函数逻辑 
            // 
            return ConfigurationManager.AppSettings[key];
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:App.config                                             *
        /// 事    件:写操作                                                 *
        /// 功    能:将配置写入App.config中，使重启后加载                   *
        /// 函数调用:无                                                     *
        /// </summary>                                                      *
        /// <param name="sAppKey"></param>                                  *
        /// <param name="eAppValue"></param>                                *
        /// <remarks></remarks>                                             *
        ///-----------------------------------------------------------------*
        public static void SetValue(string AppKey, string AppValue)
        {
            XmlDocument xDoc = new XmlDocument();
            //获取可执行文件的路径和名称 
            xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");
            XmlNode xNode;
            XmlElement xElem1;
            XmlElement xElem2;
            xNode = xDoc.SelectSingleNode("//appSettings");
            xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
            if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
            else
            {
                xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("key", AppKey);
                xElem2.SetAttribute("value", AppValue);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
        } 
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:btnpicchange1_Click                                    *
        /// 事    件:Click                                                  *
        /// 功    能:更换主窗体背景图片                                     *
        /// 函数调用:无                                                     *
        /// </summary>                                                      *
        /// <param name="sender"></param>                                   *
        /// <param name="e"></param>                                        *
        /// <remarks></remarks>                                             *
        ///-----------------------------------------------------------------*
        private void btnpicchange1_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                BackgroundImage = Image.FromFile(picPath2);
                SetValue("picPath", picPath2);
                flag++;
            }
            else if (flag == 1)
            {
                BackgroundImage = Image.FromFile(picPath3);
                SetValue("picPath", picPath3);
                flag++;
            }
            else if (flag == 2)
            {
                BackgroundImage = Image.FromFile(picPath1);
                SetValue("picPath", picPath1);
                flag=0;
            }
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:btnpicchange2                                          *
        /// 事    件:Click                                                  *
        /// 功    能:浏览图像文件并更换背景图片                             *
        /// 函数调用:无                                                     *
        /// </summary>                                                      *
        /// <param name="sender"></param>                                   *
        /// <param name="e"></param>                                        *
        /// <remarks></remarks>                                             *
        ///-----------------------------------------------------------------*
        private void btnpicchange2_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = "图像文件(JPeg,Gif,Bmp,etc.)|*.jpg;*.jpeg; *.gif; *.bmp; *.tif; *.tiff; *.png| JPeg 图像文件(*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 图像文件(*.gif)|*.gif |BMP 图像文件(*.bmp)|*.bmp|Tiff 图像文件(*.tif;*.tiff)| *.tif;*.tiff|Png 图像文件(*.png)| *.png |所有文件(*.*)|*.*";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string MyFileName = this.openFileDialog1.FileName;
                this.BackgroundImage = Bitmap.FromFile(MyFileName);
                string MyInfo = "图像文件名：" + this.openFileDialog1.FileName + Environment.NewLine;
                SetValue("picPath", MyFileName);
            }
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:label1:窗体                                            *
        /// 事    件:Paint                                                  *
        /// 功    能:使label1的背景色透明                                   *
        /// 函数调用:无                                                     *
        /// </summary>                                                      *
        /// <param name="sender"></param>                                   *
        /// <param name="e"></param>                                        *
        /// <remarks></remarks>                                             *
        ///-----------------------------------------------------------------*
        private void label1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            this.label1.BackColor = Color.Transparent;
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:label2:窗体                                            *
        /// 事    件:Paint                                                  *
        /// 功    能:使label2的背景色透明                                   *
        /// 函数调用:无                                                     *
        /// </summary>                                                      *
        /// <param name="sender"></param>                                   *
        /// <param name="e"></param>                                        *
        /// <remarks></remarks>                                             *
        ///-----------------------------------------------------------------*
        private void label2_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            this.label2.BackColor = Color.Transparent;
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:label3:窗体                                            *
        /// 事    件:Paint                                                  *
        /// 功    能:使label3的背景色透明                                   *
        /// 函数调用:无                                                     *
        /// </summary>                                                      *
        /// <param name="sender"></param>                                   *
        /// <param name="e"></param>                                        *
        /// <remarks></remarks>                                             *
        ///-----------------------------------------------------------------*
        private void label3_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            this.label3.BackColor = Color.Transparent;
        }

        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:操作帮助ToolStripMenuItem 　　                         *
        /// 事    件:Click                                                  *   
        /// 功    能:调用关于aw60.chm　　　　　　                           *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void 操作帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //显示帮助文件内容
            string MyFileName = Application.StartupPath + "\\..\\..\\帮助\\帮助文件\\AW60功能操作帮助.chm";
            Help.ShowHelp(this, MyFileName, HelpNavigator.TableOfContents);
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:工程说明ToolStripMenuItem 　　                         *
        /// 事    件:Click                                                  *   
        /// 功    能:调用Readme.chm　　　　　　　　                         *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void 工程说明ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //显示帮助文件内容
            string MyFileName = Application.StartupPath + "\\..\\..\\帮助\\帮助文件\\Readme.chm";
            Help.ShowHelp(this, MyFileName, HelpNavigator.TableOfContents);
        }
        ///-----------------------------------------------------------------*
        /// <summary>                                                       *
        /// 对    象:关于本程序ToolStripMenuItem                            *
        /// 事    件:Click                                                  *   
        /// 功    能:调用关于MCU-AW60演示程序窗体                           *
        /// </summary>                                                      *
        ///-----------------------------------------------------------------*
        private void 关于本程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            关于本程序 f = new 关于本程序();
            f.Show();

        }
       
        
    }
}