我从未使用过您之前提到的库。但是,您可以尝试使用这种方法:
/// <summary>
/// Describes the state of the button.
/// </summary>
public enum ButtonStatus
{
//The reason the enum is called ButtonStatus is to avoid confusion
//with the XNA enum called ButtonState.
Normal,
MouseOver,
Pressed,
}
/// <summary>
/// Stores the appearance and functionality of a button.
/// </summary>
class Button : Sprite3
{
// Store the MouseState of the last frame.
private MouseState currentMouseState;
private MouseState previousMouseState;
private float cursor_x, cursor_y;
// The the different state textures.
private Texture2D texHover;
private Texture2D texPressed;
// Store the current state of the button.
private ButtonStatus buttonState = ButtonStatus.Normal;
// Gets fired when the button is pressed.
public event EventHandler Clicked;
// Gets fired when the button is held down.
public event EventHandler OnPress;
//Constructor
/// <summary>
/// Constructs a new button.
/// </summary>
/// <param name="texture">The normal texture for the button.</param>
/// <param name="hoverTexture">The texture drawn when the mouse is over the button.</param>
/// <param name="pressedTexture">The texture drawn when the button has been pressed.</param>
/// <param name="position">The position where the button will be drawn.</param>
public Button(bool visibleZ, Texture2D texZ, Texture2D texHover,
Texture2D texPressed, float x, float y)
: base(visibleZ, texZ, x, y)
{
this.texHover = texHover;
this.texPressed = texPressed;
}//constructor
/// <summary>
/// Updates the buttons state.
/// </summary>
/// <param name="gameTime">The current game time.</param>
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
previousMouseState = currentMouseState;
currentMouseState = Mouse.GetState();
cursor_x = currentMouseState.X;
cursor_y = currentMouseState.Y;
bool isMouseOver = this.getBoundingBoxAA().Contains(cursor_x, cursor_y);
if (isMouseOver && buttonState != ButtonStatus.Pressed)
{
buttonState = ButtonStatus.MouseOver;
}
else if (isMouseOver == false && buttonState != ButtonStatus.Pressed)
{
buttonState = ButtonStatus.Normal;
}
// Check if the player holds down the button.
if (currentMouseState.LeftButton == ButtonState.Pressed &&
previousMouseState.LeftButton == ButtonState.Released)
{
if (isMouseOver == true)
{
// Update the button state.
buttonState = ButtonStatus.Pressed;
if (OnPress != null)
{
// Fire the OnPress event.
OnPress(this, EventArgs.Empty);
}
}
}
// Check if the player releases the button.
if (currentMouseState.LeftButton == ButtonState.Released &&
previousMouseState.LeftButton == ButtonState.Pressed)
{
if (isMouseOver == true)
{
// update the button state.
buttonState = ButtonStatus.MouseOver;
if (Clicked != null)
{
// Fire the clicked event.
Clicked(this, EventArgs.Empty);
}
}
else if (buttonState == ButtonStatus.Pressed)
{
buttonState = ButtonStatus.Normal;
}
}
}//end Update
/// <summary>
/// Draws the button.
/// </summary>
/// <param name="spriteBatch">A SpriteBatch that has been started</param>
public override void Draw(SpriteBatch spriteBatch)
{
switch (buttonState)
{
case ButtonStatus.Normal:
spriteBatch.Draw(texBase, this.getBoundingBoxAA(), Color.White);
//base.Draw(spriteBatch);
break;
case ButtonStatus.MouseOver:
spriteBatch.Draw(texHover, this.getBoundingBoxAA(), Color.White);
break;
case ButtonStatus.Pressed:
spriteBatch.Draw(texPressed, this.getBoundingBoxAA(), Color.White);
break;
default:
spriteBatch.Draw(texBase, this.getBoundingBoxAA(), Color.White);
break;
}
}//end Draw
}
如果您有任何不明白的地方,请提出问题。我会尽力为你澄清:)