How can i add window message?

Hi! guys.

Now, i want to add window message(ex. WM_USER + 200),
and i want to insert command when windows are created.

where can i add this message and insert command?

please help me.

UE4 version : 4.6.1
OS : Windows 7

There is currently no way to hook into the Windows message queue, but it’s something I actually started working on. I may have something by the end of next week. In the meantime you’ll have to modify the Engine code, if that is a possibility for you. The relevant code is in FWindowsApplication::ProcessMessage() in WindowsApplication.cpp.

Check out this GitHub commit for the new API that allows you to intercept Windows messages. I haven’t tested it yet, but it should work. Implement the IWindowsMessageHandler interface and register it with FWindowsApplication::AddMessageHandler(). Don’t forget to remove it when you don’t need it anymore. Also note that this is only for Windows, so you should only use it in code that is wrapped in #if PLATFORM_WINDOWS.

Example:

#if PLATFORM_WINDOWS
    class FMyWindowsMessageHandler
        : public IWindowsMessageHandler
    {
        public:
            virtual bool ProcessMessage(HWND hwnd, uint32 msg, WPARAM wParam, LPARAM lParam, int32& OutResult) override
            {
                // Handle your messages here
            }
    }
#endif


#if PLATFORM_WINDOWS
    TSharedPtr<GenericApplication> GenericApplication = FSlateApplication::Get().GetPlatformApplication();
    FWindowsApplication* WindowsApplication = (FWindowsApplication*)GenericApplication.Get();
    WindowsApplication->AddMessageHandler(MyMessageHandler);
    //...
    WindowsApplication->RemoveMessageHandler(MyMessageHandler);
#endif

I would like to request that you provide a tutorial or snippet for using this in either a game mode or actor class. Right now what makes the most sense is to simply shove a conditional or switch statement into the FWindowsApplication::ProcessMessage() and create a global that signals whatever was looking for the message.

Hi. .

Thanks for your answer.

I modified WindowsApplication.cpp and WindowsApplication.h.

But I will try to call AddMessageHandler funcion of WindowsApplication class.

How can i call AddMessageHandler funtion in my project?

I updated the answer with example code. MyMessageHandler is assumed to be of type FMyWindowsMessageHandler, which is a class you need to implement.

Hi!

I tried to test your example, but it’s error.

Is ‘GenericApplication’ correct in ‘FWindowsApplication* WindowsApplication = (FWindowsApplication*)GenericApplication.Get();’ statements?

Apologies, I forgot to copy/paste one line. Please add:

TSharedPtr<GenericApplication> GenericApplication = FSlateApplication::Get().GetPlatformApplication();

I updated the answer.

Thanks, .
It was very useful for me.

I implemented an example plug-in for this API. You can find it on GitHub

Hi , thanks for the code! I have tried cloning the plug-in to my projects plugin directory but I am presented with errors when I try to build. I have attached the build output. I’m wondering what I’m doing wrong.link text

Looks like there were some changes to Slate headers (which are still severely broken in terms of forward declarations and dependency includes). I updated the plugin to make it compile.

So, It’s not Difficule. But I Wonder, How Can Make the Blueprint Event of Windows Message ? How Can ?

That’s a completely separate topic. Please start a new post for that.

Hello , I was curious if this is designed only for intercepting window messages as it pertains to the unreal window handle or does it act more like a windows hook to intercept messages that happen globally in windows? So far it seems to be the former rather than the latter. If so any insights on the latter? Github plugin tested in 4.26 patch 2 FYI.