1

我创建了一个包含纹理、标题和矩形的 GuiWindow 类,以便我可以绘制纹理和标题。我一直在尝试使它可拖动,尽管我遇到了一些麻烦。最初我将 GuiWindow 的边界矩形锁定到鼠标位置:

if(bounds.contains(MouseHandle.Update()) && MouseHandle.Pressed()) //checks if the bounds rectangle contains the mouse rectangle and the mouse left button is pressed
{
    bounds.X = MouseHandle.Update().X;
    bounds.Y = MouseHandle.Update().Y;
}

这将允许我拖动,尽管只能在一个方向上。然后我尝试了

if(bounds.contains(MouseHandle.Update()) && MouseHandle.Pressed())
{
    int offx = bounds.X - MouseHandle.Update().X;
    int offy = bounds.Y - MouseHandle.Update().Y;
    bounds.X = MouseHandle.Update().X + offx;
    bounds.Y = MouseHandle.Update().Y + offy;
}

这一次,当我尝试拖动时,窗口只是静止不动。我很确定我有下拉的基本概念。难道我做错了什么?

4

2 回答 2

2

好吧,这是我在一些 XNA 应用程序中使用鼠标移动对象的一些代码。希望这可以帮助您解决问题。

//Fields
Texture2D object;
Vector2 object_position;
Rectangle collisionRectangle;
MouseState preMouse;
bool moving = false;
Vector2 mouseOffset;


//initialize fields in LoadContent method
protected override void LoadContent()
{
    object = Content.Load<Texture2D>("nameOfYourImage");
    object_position = new Vector2((graphics.PreferredBackBufferWidth - object.Width)/2, graphics.PreferredBackBufferHeight - object.Height - 60);
    collisionRectangle = new Rectangle((int)object_position.X, (int)object_position.Y, (int)object.Width, (int)object.Height);
}


//add code to Update method



public void MouseInput(MouseState mouse)
{
    if (collsionRectangle.Contains(mouse.X, mouse.Y) && //mouse is over the object
        //the user is clicking the left mousebutton
        mouse.LeftButton == ButtonState.Pressed && 
        //in the previous Update() call the left mousebutton was released, 
        //meaning the user has just clicked the object
        preMouse.LeftButton == ButtonState.Released)
    {
        moving = true;

        //stores what the objects position should be offset by so it doesn't
        //snap to the mouse's position every time you click on it
        mouseOffset = new Vector2(Position.X - mouse.X, Position.Y - mouse.Y);
    }

    if (moving)
    {
        //if the player stops holding down the mousebutton i.e stops moving the object
        if (mouse.LeftButton == ButtonState.Released)  
            moving = false;

        //modifies the position
        Position.X = mouse.X + mouseOffset.X;
        Position.Y = mouse.Y + mouseOffset.Y;

        //prevents object from going off the screen and getting lost
        if (Convert.ToInt32(object_position.X) < 0)
            object_position.X = 0;
        if (object_position.X + object.Width > graphics.PreferredBackBufferWidth)
            object_position.X = graphics.PreferredBackBufferWidth - object.Width;
        if (Convert.ToInt32(object_position.Y) < 0)
            object_position.Y = 0;
        if (object_position.Y + object.Height > graphics.PreferredBackBufferHeight)
            object_position.Y = graphics.PreferredBackBufferHeight - object.Height;

        //updates the collision rectangle
        collisionRectangle = new Rectangle(Postion.X, Position.Y, WIDTH, HEIGHT);
    }

    preMouse = mouse; //stores the current mouseState for use in the next Update() call
}

这将使您能够用鼠标拖动对象。现在当然这不会直接复制到您的应用程序,因为我不知道您的 GuiWindow 的代码如何,但它应该很容易转换为您的需求。希望这可以帮助 :)

于 2012-11-16T23:00:37.097 回答
0

这是我的窗口基类的一部分,它在 XNA 中处理移动和调整大小。我对它在 VB.NET 中的应用感到满意,但您应该能够轻松地对其进行转换。您可能无法直接使用其中的任何一个,但希望它能为您指明正确的方向。

    Public Overrides Sub OnMouseDown(e As Ui.MouseEventArgs)
        MyBase.OnMouseDown(e)

        Dim localLoc As Point = ScreenToWindow(e.Location)
        'If Movable AndAlso localLoc.Y >= 0 AndAlso localLoc.Y <= mBackground.TopMargin Then
        If Closable AndAlso Me.CloseButtonBounds.Contains(e.Location) Then
            Me.OnCloseButton()
        ElseIf Movable AndAlso Me.DragArea.Contains(e.Location) Then
            mMouseAnchor = localLoc
            mDragType = DragType.Move
            ' ElseIf Resizable AndAlso localLoc.X > Me.Bounds.Width - mBackground.RightMargin AndAlso localLoc.Y > Me.Bounds.Height - mBackground.BottomMargin Then
        ElseIf Resizable AndAlso Me.ResizeArea.Contains(e.Location) Then
            mMouseAnchor = New Point(Me.Bounds.Right - e.Location.X, Me.Bounds.Bottom - e.Location.Y)
            mDragType = DragType.Resize
        End If
    End Sub

    Public Overrides Sub OnMouseUp(e As Ui.MouseEventArgs)
        MyBase.OnMouseUp(e)
        mDragType = DragType.None
    End Sub


    Public Overrides Sub OnMouseMove(e As Ui.MouseEventArgs)
        MyBase.OnMouseMove(e)

        If mDragType = DragType.Move Then
            Dim change As New Vector2((e.Location.X - mMouseAnchor.X) - Me.X, (e.Location.Y - mMouseAnchor.Y) - Me.Y)
            Me.Bounds = New Rectangle(e.Location.X - mMouseAnchor.X,
                                      e.Location.Y - mMouseAnchor.Y,
                                      Me.Bounds.Width,
                                      Me.Bounds.Height)
            UpdateControlLocations(change)
        ElseIf mResizable AndAlso mDragType = DragType.Resize Then
            Me.Bounds = New Rectangle(Me.Bounds.X,
                                      Me.Bounds.Y,
                                      Me.Bounds.Width - (Me.Bounds.Right - e.Location.X) + mMouseAnchor.X,
                                      Me.Bounds.Height - (Me.Bounds.Bottom - e.Location.Y) + mMouseAnchor.Y)
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.SizeNWSE
        ElseIf mDragType = DragType.None Then
            If mResizable AndAlso Me.ResizeArea.Contains(e.Location) Then
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.SizeNWSE
            Else
                System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            End If
        End If
    End Sub
于 2012-11-16T21:23:06.167 回答