2016-08-08 105 views
-2

我正在尝试为修复其一些错误的游戏制作启动程序。 现在我只是在界面上工作,我想制作自定义按钮,而不仅仅是那些通用的方块,但我无法弄清楚如何。如何在Visual Studio中创建自定义按钮?

下面是一些示例图像。

Regular button, not moused over.

Moused over/Highlighted button.

我只是把这些按钮快速在一起,但是这就是我想要的。 我希望按钮突出显示,当我将鼠标悬停在上面时,它不在默认的方形按钮内。

+4

你使用哪种技术为你的发射器? WPF,WinForms,还有别的? – ChrisF

+0

WinForms,对不起,我没有提到。 – ShadyOrb09

回答

0

我使用的图片框,然后在我的按钮图片具有透明背景添加。然后添加一个点击事件,鼠标进入和鼠标离开事件。

1

这可以通过自定义绘制按钮来完成。来自MSDN的This demo显示了如何覆盖OnPaint并通过响应OnMouseDownOnMouseUp来交换位图。为了让图像在悬停时改变,只需在OnEnterOnLeave期间交换位图。

这里有一个删节例如,从链接页面:

public class PictureButton : Control 
{ 
    Image staticImage, hoverImage; 
    bool pressed = false; 

    // staticImage is the primary default button image 
    public Image staticImage 
    { 
     get { 
      return this.staticImage; 
     } 
     set { 
      this.staticImage = value; 
     } 
    } 

    // hoverImage is what appears when the mouse enters 
    public Image hoverImage 
    { 
     get { 
      return this.hoverImage; 
     } 
     set { 
      this.hoverImage = value; 
     } 
    } 

    protected override void OnEnter(EventArgs e) 
    { 
     this.pressed = true; 
     this.Invalidate(); 
     base.OnEnter(e); 
    } 

    protected override void OnLeave(EventArgs e) 
    { 
     this.pressed = false; 
     this.Invalidate(); 
     base.OnLeave(e); 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     if (this.pressed && this.hoverImage != null) 
      e.Graphics.DrawImage(this.hoverImage, 0, 0); 
     else 
      e.Graphics.DrawImage(this.staticImage, 0, 0); 

     base.OnPaint(e); 
    } 
}