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();

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyjwwy.html