Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Moving Borderless Windows  (Read 9309 times)

0 Members and 1 Guest are viewing this topic.

malkierian

  • Newbie
  • *
  • Posts: 39
    • View Profile
Moving Borderless Windows
« on: February 13, 2014, 12:51:53 am »
Alright, so I know that you can make a window that is completely borderless, meaning no title bar, no Exit/Max/Min buttons, no resizing frame, but there are plenty of examples out there of borderless windows that you can still move around.  How would I go about doing this in SFML?

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Moving Borderless Windows
« Reply #1 on: February 13, 2014, 01:31:16 am »
You could see if a user has their mouse in the window plus holding the left mouse button using events. If they do, simply use sf::RenderWindow::setPosition with the new coordinates.
« Last Edit: February 13, 2014, 01:34:57 am by The Terminator »
Current Projects:
Technoport

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Moving Borderless Windows
« Reply #2 on: February 13, 2014, 01:39:37 am »
Correct method is something like this...

bool windowdrag = false;
sf::Vector2i mouseoffset;
while (window.isOpen())
{
   sf::Event event;
   while (window.pollEvent(event))
   {
      if (event.type == sf::Event::MouseDown && dragableArea.contains(event.mousePos))
      {
         windowdrag = true;
         mouseoffset = event.mousePos;
      }
      else if (event.type == sf::Event::MouseMove && windowdrag)
      {
         window.setPosition(event.mousePos - mouseoffset);
      }
      else if (event.type == sf::Event::MouseUp)
      {
         windowdrag = false;
      }
   }
}
 
« Last Edit: February 13, 2014, 02:33:27 am by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Moving Borderless Windows
« Reply #3 on: February 13, 2014, 01:58:30 am »
I'm glad you posed this problem. It was an interesting thing to solve. This can be run as-is:
#include <SFML/Graphics.hpp>
int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 600), "", sf::Style::None);
        sf::Vector2i grabbedOffset;
        bool grabbedWindow = false;
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                        else if (event.type == sf::Event::KeyPressed)
                        {
                                if (event.key.code == sf::Keyboard::Escape)
                                        window.close();
                        }
                        else if (event.type == sf::Event::MouseButtonPressed)
                        {
                                if (event.mouseButton.button == sf::Mouse::Left)
                                {
                                        grabbedOffset = window.getPosition() - sf::Mouse::getPosition();
                                        grabbedWindow = true;
                                }
                        }
                        else if (event.type == sf::Event::MouseButtonReleased)
                        {
                                if (event.mouseButton.button == sf::Mouse::Left)
                                        grabbedWindow = false;
                        }
                        else if (event.type == sf::Event::MouseMoved)
                        {
                                if (grabbedWindow)
                                        window.setPosition(sf::Mouse::getPosition() + grabbedOffset);
                        }
                }
                window.clear(sf::Color::Red);
                window.display();
        }
}
It works on all window types, not just borderless.
You can press Escape to close or use another closing method (taskbar etc.)

EDIT: removed some code I was using for testing.
EDIT 2: just noticed that it also works in fullscreen mode  :o
« Last Edit: February 13, 2014, 02:13:26 am by Golden Eagle »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Moving Borderless Windows
« Reply #4 on: February 13, 2014, 02:26:19 am »
I would move some of your code outside the event loop to prevent some unwanted behaviour like dragging on an edge and moving too fast -> window doesn't move until the mouse is inside the window again and you have to click again to free the mouse^^

#include <SFML/Graphics.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "", sf::Style::None);
    sf::Vector2i grabbedOffset;
    bool grabbedWindow = false;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
            else if (event.type == sf::Event::KeyPressed)
            {
                if (event.key.code == sf::Keyboard::Escape)
                    window.close();
            }
            else if (event.type == sf::Event::MouseButtonPressed)
            {
                if (event.mouseButton.button == sf::Mouse::Left)
                    grabbedOffset = window.getPosition() - sf::Mouse::getPosition();
            }
        }
        window.clear(sf::Color::Red);

                grabbedWindow = sf::Mouse::isButtonPressed(sf::Mouse::Left);
                if (grabbedWindow)
                        window.setPosition(sf::Mouse::getPosition() + grabbedOffset);

        window.display();
    }
}
Failing to succeed does not mean failing to progress!

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Moving Borderless Windows
« Reply #5 on: February 13, 2014, 02:47:47 am »
If someone insists on moving that fast, they deserve to have to click to reset it  :P
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*