zoukankan      html  css  js  c++  java
  • C# 在winform画面上点击某个区域造成按钮按下效果的方法

    在Winform的窗体中,存在背景图的情况下,不想在画面上加入按钮,只希望利用窗体自身的几个事件来实现按钮按下的效果,怎么样实现呢?

    其实这也不难,只需要按照下列步骤即可实现,首先确定要按下的几个区域的坐标点和大小,准备好按下和弹出的图片,将这些坐标点和大小

    组成一个个区域(使用GraphicsPath来完成)。然后使用几个Flage标志,标识是否区域的按下。具体实现的代码如下:

     /// <summary>
        /// 实时监控,酒位展示,酒柜介绍,网络订购4个按钮的位置参数
        /// </summary>
        int[,] BtnLocation = new int[4, 2]{{637,161},{683,269},{714,374},{637,478}};
        /// <summary>
        /// 4个按钮的Size参数
        /// </summary>
        Size BtnSize = new Size(285,92);    
        /// <summary>
        /// 实时监控按钮是否按下的标志
        /// </summary>
        bool FlageMouseDownRealTime = false;
        /// <summary>
        ///  酒位展示按钮是否按下的标志
        /// </summary>
        bool FlageMouseDownWinePos = false;
        /// <summary>
        /// 酒柜介绍按钮是否按下的标志
        /// </summary>
        bool FlageMouseDownCupIntrol = false;
        /// <summary>
        /// 网络订购按钮是否按下的标志
        /// </summary>
        bool FlageMouseDownNetPurchase = false;
        /// <summary>
        /// 实时监控按钮的位置范围
        /// </summary>
        GraphicsPath gpRealTime = new GraphicsPath();
        /// <summary>
        /// 酒位展示按钮的位置范围
        /// </summary>
        GraphicsPath gpWinePos = new GraphicsPath();
        /// <summary>
        /// 酒柜介绍按钮的位置范围
        /// </summary>
        GraphicsPath gpCupIntrol = new GraphicsPath();
        /// <summary>
        /// 网络订购按钮的位置范围
        /// </summary>
        GraphicsPath gpNetPurchase = new GraphicsPath();

        Graphics dc = null;


    /// <summary>
        /// 获取4个按钮的区域范围
        /// </summary>
        private void GetButtonGraphicPath()
        {
            FlageMouseDownRealTime = false;
            FlageMouseDownWinePos = false;
            FlageMouseDownCupIntrol = false;
            FlageMouseDownNetPurchase = false;

            Point[] pt = new Point[]{
                    new Point(BtnLocation[0,0],BtnLocation[0,1]),
                    new Point(BtnLocation[0,0]+BtnSize.Width,BtnLocation[0,1]),
                    new Point(BtnLocation[0,0]+BtnSize.Width,BtnLocation[0,1]+BtnSize.Height),
                    new Point(BtnLocation[0,0],BtnLocation[0,1]+BtnSize.Height)
                };
            gpRealTime.AddLines(pt);
            pt = new Point[]{
                    new Point(BtnLocation[1,0],BtnLocation[1,1]),
                    new Point(BtnLocation[1,0]+BtnSize.Width,BtnLocation[1,1]),
                    new Point(BtnLocation[1,0]+BtnSize.Width,BtnLocation[1,1]+BtnSize.Height),
                    new Point(BtnLocation[1,0],BtnLocation[1,1]+BtnSize.Height)
                };
            gpWinePos.AddLines(pt);
            pt = new Point[]{
                    new Point(BtnLocation[2,0],BtnLocation[2,1]),
                    new Point(BtnLocation[2,0]+BtnSize.Width,BtnLocation[2,1]),
                    new Point(BtnLocation[2,0]+BtnSize.Width,BtnLocation[2,1]+BtnSize.Height),
                    new Point(BtnLocation[2,0],BtnLocation[2,1]+BtnSize.Height)
                };
            gpCupIntrol.AddLines(pt);
            pt = new Point[]{
                    new Point(BtnLocation[3,0],BtnLocation[3,1]),
                    new Point(BtnLocation[3,0]+BtnSize.Width,BtnLocation[3,1]),
                    new Point(BtnLocation[3,0]+BtnSize.Width,BtnLocation[3,1]+BtnSize.Height),
                    new Point(BtnLocation[3,0],BtnLocation[3,1]+BtnSize.Height)
                };
            gpNetPurchase.AddLines(pt);

        }


     private void InitialButtonsStyle()
        {
            GetButtonGraphicPath();

            pxControl.Location = new Point(0,0);
            pxControl.Size = new Size(1024,768);
            SetBackGroudImg();
            this.Controls.Add(pxControl);
            dc = pxControl.CreateGraphics();
            pxControl.MouseDoubleClick +=new MouseEventHandler(this.Main_MouseDoubleClick);
            pxControl.MouseDown+=new MouseEventHandler(this.pxControl_MouseDown);
            pxControl.MouseUp += new MouseEventHandler(this.pxControl_MouseUp);
            pxControl.MouseMove += new MouseEventHandler(this.pxControl_MouseMove);
            pxControl.MouseClick += new MouseEventHandler(this.pxControl_MouseClick);

      }

     /// <summary>
        ///按下4个按钮时的状态变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pxControl_MouseDown(object sender,MouseEventArgs e)
        {
            if (gpRealTime.IsVisible(e.Location))
            {
               using(Image imgTemp = Image.FromFile(strWineRealStatusDownPath))
               {
                    dc.DrawImage(imgTemp, BtnLocation[0, 0], BtnLocation[0, 1], BtnSize.Width,BtnSize.Height);
                    FlageMouseDownRealTime = true;
               }
            }
            if (gpWinePos.IsVisible(e.Location))
            {
                using (Image imgTemp = Image.FromFile(strWinePosQueryDownPath))
                {
                    dc.DrawImage(imgTemp, BtnLocation[1, 0], BtnLocation[1, 1], BtnSize.Width, BtnSize.Height);
                    FlageMouseDownWinePos = true;
                }
            }
            if (gpCupIntrol.IsVisible(e.Location))
            {
                using (Image imgTemp = Image.FromFile(strWineCupIntroDownPath))
                {
                    dc.DrawImage(imgTemp, BtnLocation[2, 0], BtnLocation[2, 1], BtnSize.Width, BtnSize.Height);
                    FlageMouseDownCupIntrol = true;
                }
            }
            if (gpNetPurchase.IsVisible(e.Location))
            {
                using (Image imgTemp = Image.FromFile(strWinePurchaseDownPath))
                {
                    dc.DrawImage(imgTemp, BtnLocation[3, 0], BtnLocation[3, 1], BtnSize.Width, BtnSize.Height);
                    FlageMouseDownNetPurchase = true;
                }
            }
        }

    /// <summary>
        ///离开4个按钮时的状态变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pxControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (gpRealTime.IsVisible(e.Location))
            {
                if (FlageMouseDownRealTime)
                {
                    using (Image imgTemp = Image.FromFile(strWineRealStatusDownPath))
                    {
                        dc.DrawImage(imgTemp, BtnLocation[0, 0], BtnLocation[0, 1], BtnSize.Width, BtnSize.Height);
                    }
                }
            }
            else
            {
                if (FlageMouseDownRealTime)
                {
                    using (Image imgTemp = Image.FromFile(strWineRealStatusUpPath))
                    {
                        dc.DrawImage(imgTemp, BtnLocation[0, 0], BtnLocation[0, 1], BtnSize.Width, BtnSize.Height);
                    }
                    FlageMouseDownRealTime = false;
                }
            }
            if (gpWinePos.IsVisible(e.Location))
            {
                if (FlageMouseDownWinePos)
                {
                    using (Image imgTemp = Image.FromFile(strWinePosQueryDownPath))
                    {
                        dc.DrawImage(imgTemp, BtnLocation[1, 0], BtnLocation[1, 1], BtnSize.Width, BtnSize.Height);
                    }
                }
            }
            else
            {
                if (FlageMouseDownWinePos)
                {
                    using (Image imgTemp = Image.FromFile(strWinePosQueryUpPath))
                    {
                        dc.DrawImage(imgTemp, BtnLocation[1, 0], BtnLocation[1, 1], BtnSize.Width, BtnSize.Height);
                    }
                    FlageMouseDownWinePos = false;
                }
            }
            if (gpCupIntrol.IsVisible(e.Location))
            {
                if (FlageMouseDownCupIntrol)
                {
                    using (Image imgTemp = Image.FromFile(strWineCupIntroDownPath))
                    {
                        dc.DrawImage(imgTemp, BtnLocation[2, 0], BtnLocation[2, 1], BtnSize.Width, BtnSize.Height);
                    }
                }
            }
            else
            {
                if (FlageMouseDownCupIntrol)
                {
                    using (Image imgTemp = Image.FromFile(strWineCupIntroUpPath))
                    {
                        dc.DrawImage(imgTemp, BtnLocation[2, 0], BtnLocation[2, 1], BtnSize.Width, BtnSize.Height);
                    }
                    FlageMouseDownCupIntrol = false;
                }
            }
            if (gpNetPurchase.IsVisible(e.Location))
            {
                if (FlageMouseDownNetPurchase)
                {
                    using (Image imgTemp = Image.FromFile(strWinePurchaseDownPath))
                    {
                        dc.DrawImage(imgTemp, BtnLocation[3, 0], BtnLocation[3, 1], BtnSize.Width, BtnSize.Height);
                    }
                }
            }
            else
            {
                if (FlageMouseDownNetPurchase)
                {
                    using (Image imgTemp = Image.FromFile(strWinePurchaseUpPath))
                    {
                        dc.DrawImage(imgTemp, BtnLocation[3, 0], BtnLocation[3, 1], BtnSize.Width, BtnSize.Height);
                    }
                    FlageMouseDownNetPurchase = false;
                }
            }
        }

     /// <summary>
        ///松开4个按钮时的状态变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pxControl_MouseUp(object sender, MouseEventArgs e)
        {
            if (gpRealTime.IsVisible(e.Location))
            {
                using (Image imgTemp = Image.FromFile(strWineRealStatusUpPath))
                {
                    dc.DrawImage(imgTemp, BtnLocation[0, 0], BtnLocation[0, 1], BtnSize.Width, BtnSize.Height);
                    FlageMouseDownRealTime = false;
                }
            }
            if (gpWinePos.IsVisible(e.Location))
            {
                using (Image imgTemp = Image.FromFile(strWinePosQueryUpPath))
                {
                    dc.DrawImage(imgTemp, BtnLocation[1, 0], BtnLocation[1, 1], BtnSize.Width, BtnSize.Height);
                    FlageMouseDownWinePos = false;
                }
            }
            if (gpCupIntrol.IsVisible(e.Location))
            {
                using (Image imgTemp = Image.FromFile(strWineCupIntroUpPath))
                {
                    dc.DrawImage(imgTemp, BtnLocation[2, 0], BtnLocation[2, 1], BtnSize.Width, BtnSize.Height);
                    FlageMouseDownCupIntrol = false;
                }
            }
            if (gpNetPurchase.IsVisible(e.Location))
            {
                using (Image imgTemp = Image.FromFile(strWinePurchaseUpPath))
                {
                    dc.DrawImage(imgTemp, BtnLocation[3, 0], BtnLocation[3, 1], BtnSize.Width, BtnSize.Height);
                    FlageMouseDownNetPurchase = false;
                }
            }
        }


      /// <summary>
        /// 按下4个按钮时的事件的变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pxControl_MouseClick(object sender, MouseEventArgs e)
        {
            if (gpRealTime.IsVisible(e.Location))
            {
                btnRealStatus_Click(null, null);
            }
            else if (gpWinePos.IsVisible(e.Location))
            {
                btnWinePosQuery_Click(null, null);
            }
            else if (gpCupIntrol.IsVisible(e.Location))
            {
                btnWineCupIntro_Click(null, null);
            }
            else if (gpNetPurchase.IsVisible(e.Location))
            {
                btnPurchase_Click(null, null);
            }
        }




  • 相关阅读:
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Arctic Network POJ
    Truck History POJ
    QS Network ZOJ
  • 原文地址:https://www.cnblogs.com/kevinGao/p/2671024.html
Copyright © 2011-2022 走看看