在实际项目中,为了提供给用户更加好的用户体验,在点击按钮时,要能显示按钮被按下时的状态,给用户一种动态的感觉。
其实这在程序中实现起来也比较容易,只需多一张按钮背景图片即可,在按钮没有按下或者弹起时显示一种状态图,在按钮被压下时
显示另外一种状态图。
具体实现就是除了写按钮的Click方法,还要写按钮的MouseUp和MouseDown事件。
示例代码如下:
/// <summary>
/// 网上订购画面刷新按钮(弹起状态时)
/// </summary>
private string strPurchaseRefreshBtnImg = Application.StartupPath + "\\Resources\\" + "btnRefresh.png";
/// <summary>
/// 网上订购画面刷新按钮(压下状态时)
/// </summary>
private string strPurchaseRefreshBtnDownImg = Application.StartupPath + "\\Resources\\" + "btnRefresh_Down.png";/// <summary>
/// 按下Refresh时发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRefresh_MouseDown(object sender, MouseEventArgs e)
{
btnRefresh.BackgroundImage = Image.FromFile(strPurchaseRefreshBtnDownImg);
}
/// <summary>
/// 松开Refresh按钮时发生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRefresh_MouseUp(object sender, MouseEventArgs e)
{
btnRefresh.BackgroundImage = Image.FromFile(strPurchaseRefreshBtnImg);
}
/// <summary>
/// 刷新页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnRefresh_Click(object sender, EventArgs e)
{
//do something...}