Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#XNA可拖动用户界面_C#_Xna - Fatal编程技术网

C#XNA可拖动用户界面

C#XNA可拖动用户界面,c#,xna,C#,Xna,我创建了一个包含纹理、标题和矩形的GUI窗口类,这样我就可以绘制纹理和标题。虽然我有点麻烦,但我一直在努力使它可以拖动。最初,我将GUI窗口的边界矩形锁定在鼠标位置上: if(bounds.contains(MouseHandle.Update()) && MouseHandle.Pressed()) //checks if the bounds rectangle contains the mouse rectangle and the mouse left button is

我创建了一个包含纹理、标题和矩形的GUI窗口类,这样我就可以绘制纹理和标题。虽然我有点麻烦,但我一直在努力使它可以拖动。最初,我将GUI窗口的边界矩形锁定在鼠标位置上:

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;
}

这一次,当我试图拖动时,窗户只是静止不动。我很确定我有拖拽的基本概念。我做错什么了吗?

下面是我的窗口基类的一部分,它在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
Public覆盖mousedown上的子对象(例如Ui.MouseEventArgs)
MyBase.OnMouseDown(e)
Dim localLoc As Point=屏幕指向窗口(e.位置)

'如果Movable AndAlso localLoc.Y>=0 AndAlso localLoc.Y好的,这是我在一些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
}
//字段
纹理2D对象;
矢量2物体位置;
矩形碰撞矩形;
穆斯庄园公寓;
布尔移动=假;
矢量2鼠标偏移;
//初始化LoadContent方法中的字段
受保护的覆盖void LoadContent()
{
object=Content.Load(“nameOfYourImage”);
object_position=new Vector2((graphics.PreferredBackBufferWidth-object.Width)/2,graphics.PreferredBackBufferHeight-object.Height-60);
碰撞矩形=新矩形((int)object\u position.X,(int)object\u position.Y,(int)object.Width,(int)object.Height);
}
//向更新方法添加代码
公共无效鼠标输入(鼠标输入)
{
如果(collsionRectangle.Contains(mouse.X,mouse.Y)&&&//鼠标位于对象上方
//用户正在单击鼠标左键
mouse.LeftButton==ButtonState.Pressed&&
//在上一次Update()调用中,释放了左鼠标按钮,
//这意味着用户刚刚单击了对象
preMouse.LeftButton==ButtonState.Released)
{
移动=真;
//存储对象位置的偏移量,使其不会偏移
//每次单击鼠标时捕捉到鼠标的位置
mouseOffset=newvector2(Position.X-mouse.X,Position.Y-mouse.Y);
}
如果(移动)
{
//如果玩家停止按住鼠标按钮,即停止移动对象
if(mouse.LeftButton==ButtonState.Released)
移动=假;
//修改位置
Position.X=mouse.X+mouseOffset.X;
Position.Y=mouse.Y+mouseOffset.Y;
//防止对象离开屏幕并丢失
if(转换为32(对象位置X)<0)
对象位置X=0;
if(对象位置X+对象宽度>图形首选BackBufferWidth)
object_position.X=graphics.PreferredBackBufferWidth-object.Width;
if(转换为32(对象位置Y)<0)
对象位置Y=0;
if(对象位置Y+对象高度>图形优先BackBufferHeight)
object_position.Y=graphics.PreferredBackBufferHeight-object.Height;
//更新碰撞矩形
collisionRectangle=新矩形(Position.X,Position.Y,WIDTH,HEIGHT);
}
preMouse=mouse;//存储当前的mouseState,以便在下一次Update()调用中使用
}

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

我想我们需要更多地了解处理拖动的代码,以帮助您为什么要多次调用MouseHandle.Update()?似乎应该只调用一次并存储在变量中。我认为我们需要看到您接受至少50%的答案,以鼓励我们从日常工作中抽出时间来回答。@user1541269,请停止尝试对其他用户的帖子进行无意义的编辑。您已经尝试过几次添加自己的代码,最可笑的是,在这个答案的代码中更改变量的名称。这是不合适的,并且每次都被委员会拒绝。