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

Author Topic: How to process events from a menu  (Read 6366 times)

0 Members and 1 Guest are viewing this topic.

Gustavo72s

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
How to process events from a menu
« on: October 23, 2021, 12:32:31 pm »
Hello.

I'm trying to add a Windows API menu to an SFML window.
And I achieved it with this code:

#include <SFML/Graphics.hpp>
#include "windows.h"

int main() {
        sf::RenderWindow win(...);
        HWND hWnd = win.getSystemHandle();
       
        HMENU hMenu = CreateMenu();
        AppendMenu(...);
        AppendMenu(...);
        ...
        AppendMenu(...);
       
        SetMenu(hWnd, hMenu);
        while(win.isOpen()) {
                sf::Event event
                while(win.pollEvent(event)) {
                        ...
                }
        }
        return 0;
}
 

But I can't detect the menu events.

I understand that pollEvent only returns events from SFML and filters other events like those generated by the menu.

How can I go about implementing something like WindowProcedure before getting to pollEvent?

Thanks!
« Last Edit: October 23, 2021, 04:03:13 pm by Gustavo72s »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10828
    • View Profile
    • development blog
    • Email
Re: How to process events from a menu
« Reply #1 on: October 29, 2021, 02:16:52 pm »
You can hook into the Windows event queue yourself if really necessary.
Of course that will make your code platform specific.

Here's some (untested) example code:

#include <SFML/Graphics.hpp>
#include <Windows.h>

LONG_PTR SfmlEventCallback = 0x0;

LRESULT CALLBACK CustomEventCallback(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
    if (message == WM_MENU...)
    {
        // Your event handling
    }

    return CallWindowProcW(reinterpret_cast<WNDPROC>(SfmlEventCallback), handle, message, wParam, lParam);
}

int main()
{
    auto app = sf::RenderWindow{ { 640u, 480u }, "Custom Event Handling" };
    HWND handle = app.getSystemHandle();

    SfmlEventCallback = SetWindowLongPtrW(handle, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(CustomEventCallback));

    while (app.isOpen())
    {
        for (auto event = sf::Event{}; app.pollEvent(event);)
        {
            if (event.type == sf::Event::Closed)
            {
                app.close();
            }
        }

        app.clear();
        app.display();
    }
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Gustavo72s

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: How to process events from a menu
« Reply #2 on: October 30, 2021, 06:57:04 pm »
eXpl0it3r:
Thank you very much, this just works!

 

anything