SFML community forums

Help => Window => Topic started by: degski on December 23, 2019, 05:08:36 pm

Title: Windows: extended window style (some progress added)
Post by: degski on December 23, 2019, 05:08:36 pm
Hi Everyone,

I would like to create (on Windows 10) a window with an extended style (WS_EX_TOOLWINDOW, which does not create a taskbar icon when active, that's the objective). There is a "com-"way, but that seems complicated.

This code (in the constructor of my app-class) does the "non-extended" style creation (my starting-point):

m_context_settings.antialiasingLevel = 8u;
m_render_window.create ( sf::VideoMode ( 1200u, 150u ), L"yawa", sf::Style::None, m_context_settings );

This works (obviously) perfectly fine.

I worked out the extended style should be something like:

m_context_settings.antialiasingLevel = 8u;
sf::WindowHandle handle = CreateWindowEx ( WS_EX_TOOLWINDOW, TEXT ( "YAWA_CLASS" ), TEXT ( "yawa" ), WS_POPUP, 0, 0, 1200u, 150u,
                                           NULL, NULL, GetModuleHandle ( NULL ), NULL );
m_render_window.create ( handle, m_context_settings );

But this does not work, so I'm missing something (or I am mis-understanding things). Could someone please help me understand what to do?

EDIT1: I have now created a function (using Win32) to (properly this time, I hope) create an extended window and return a handle to it:


    //
    //
    // WndProc - Window procedure (dummy)
    //
    //
    LRESULT
    CALLBACK
    WndProc1 ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {

        switch ( uMsg ) {
            case WM_DESTROY: ::PostQuitMessage ( 0 ); break;
            default: return ::DefWindowProc ( hWnd, uMsg, wParam, lParam );
        }

        return 0;
    }

    HWND make_extended_window ( sf::VideoMode const & vm_ ) {

        HINSTANCE hInstance = ( HINSTANCE ) GetModuleHandle ( NULL );

        WNDCLASSEX wcex{ };

        wcex.cbSize        = sizeof ( wcex );                     // WNDCLASSEX size in bytes
        wcex.style         = CS_HREDRAW | CS_VREDRAW;             // Window class styles
        wcex.lpszClassName = TEXT ( "YAWAWINDOWCLASS" );          // Window class name
        wcex.hbrBackground = ( HBRUSH ) ( COLOR_WINDOW + 1 );     // Window background brush color.
        wcex.hCursor       = LoadCursor ( hInstance, IDC_ARROW ); // Window cursor
        wcex.lpfnWndProc   = WndProc1;                            // Window procedure associated to this window class.
        wcex.hInstance     = hInstance;                           // The application instance.

        // Register window and ensure registration success.
        if ( !RegisterClassEx ( &wcex ) )
            return nullptr;

        // Setup window initialization attributes.
        CREATESTRUCT cs{ };

        cs.cx        = vm_.width;             // Window width
        cs.cy        = vm_.height;            // Window height
        cs.hInstance = hInstance;             // Window instance.
        cs.lpszClass = wcex.lpszClassName;    // Window class name
        cs.lpszName  = TEXT ( "yawa" );       // Window title
        cs.style     = WS_VISIBLE | WS_POPUP; // Window style (sf::Style::None)
        cs.dwExStyle = WS_EX_TOOLWINDOW;      // Extended window style, no taskbar icon.

        // Create the window.
        return ::CreateWindowEx ( cs.dwExStyle, cs.lpszClass, cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy, cs.hwndParent, cs.hMenu,
                                  cs.hInstance, cs.lpCreateParams );
    }

and then at the call-site (the constructor):

    m_context_settings.antialiasingLevel = 8u;
    m_render_window.create ( make_extended_window ( sf::VideoMode ( 1200u, 150u ) ), m_context_settings );

This works sort of. To start with, the window icon is not present on the taskbar (mission achieved). I can write and display something in the window, so many things are good! But if I hover over the window, the cursor starts spinning (not with an ordinary window) and when I click the window, the app crashes. So something is still wrong.

When I look at the relevant constructorhttps://github.com/SFML/SFML/blob/80c3bdc23c1874494196bbf8a481a859712ece88/src/SFML/Window/Win32/WindowImplWin32.cpp#L132 (https://github.com/SFML/SFML/blob/80c3bdc23c1874494196bbf8a481a859712ece88/src/SFML/Window/Win32/WindowImplWin32.cpp#L132) , the right things seem to be happening, this-ptr is plugged in (cs.lpCreateParams) and the WndProc ptr is remapped, still something is wrong.

With this additional info, is there someone who sees the missing bit? Thanks in advance!
Title: Re: Windows: extended window style (some progress added)
Post by: Laurent on January 06, 2020, 12:47:33 pm
Wouldn't it be simpler to let SFML create the window, and then change its style with some Win32 function (SetWindowLongPtr with GWL_EXSTYLE, probably)? Remmeber that you can get the native sf::Window handle with window.getHandle().