Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8845627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:47:04+00:00 2026-06-14T11:47:04+00:00

I created a GuiWindow class that held a texture, title, and rectangle so I

  • 0

I created a GuiWindow class that held a texture, title, and rectangle so I can draw the texture and title. I’ve been trying to make it draggable, though I am having a bit of trouble. Originally I had the GuiWindow’s bounds rectangle just lock onto the mouse positions:

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

which would allow me to drag, though only in a single direction. I then tried

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

this time the window just stayed still when I tried dragging. I’m pretty sure I have the basic concept of dragging down. Am I doing something wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:47:05+00:00Added an answer on June 14, 2026 at 11:47 am

    Alright this is some code that I’ve been using to move objects with the mouse in some of my XNA applications. Hopefully this will help you with your problem.

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

    This will make you able to drag the object with the mouse. Now of course this won’t be directly copyable to your application, as i don’t know how the code of your GuiWindow, but it should be easy to convert to your needs. Hope this helps 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got an app, that uses custom title feature. I'm trying to migrate it
I'm working on a GUI in SDL. I've created a slave/master class that contains
I'm trying to override a class derived from Activity (called NativeActivity) so I can
I am trying to run a class that will check for information in a
Created an OpenGraph action and object. Trying to submit my action. When I click
I created a following class within a namespace Global namespace Global { public static
I created a keyspace with the cassandra-cli tool, and i can use it. However
I created a web view and im trying to just launch google.com for now.
I have a couple UIViewControllers that I am trying to access an array inside
I need to access gui components outside the class that defines them. My gui

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.