Skip to content

Instantly share code, notes, and snippets.

@Alia5
Last active March 17, 2022 15:32
Embed
What would you like to do?
Transparent SFML Window
#include <SFML/Graphics.hpp>
#include <Windows.h>
#include <Dwmapi.h>
#pragma comment (lib, "Dwmapi.lib")
int main()
{
sf::RenderWindow window(sf::VideoMode(1280, 720), "Transparent Window");
window.setFramerateLimit(60);
MARGINS margins;
margins.cxLeftWidth = -1;
SetWindowLong(window.getSystemHandle(), GWL_STYLE, WS_POPUP | WS_VISIBLE);
DwmExtendFrameIntoClientArea(window.getSystemHandle(), &margins);
//CircleShape for DemoContent
sf::CircleShape shape(360.f);
shape.setFillColor(sf::Color::Green);
//\
sf::Vector2i grabbedOffset;
bool grabbedWindow = false;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed || event.type == sf::Event::KeyPressed && 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::Transparent); //F*ck yeah it works as you would expect from the wording ;P
window.draw(shape);
window.display();
}
return 0;
}
@Leonetienne
Copy link

Awesome! Thanks :3

@m1rz0
Copy link

m1rz0 commented Aug 23, 2021

you need to put the window style "None" in sfml window creating line for correct window scale like that sf::RenderWindow window(sf::VideoMode(1280, 720), "Transparent Window", sf::Style::None);
otherwise the system mouse will show incorrect window mouse position

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment