unreal engine 给游戏创建托盘图标,右键菜单

plugin

https://github.com/ue4plugins/WindowsMessageHandlerExample

关闭editor,解压后新建一个Plugins文件夹,放到 /Plugins 文件夹
在这里插入图片描述

然后打开***.sln,build

右键uproject,generate project fils

在这里插入图片描述

再打开editor,Enable plugin ,可以在Output看到已经在运行了;

在这里插入图片描述
在这里插入图片描述

重新打开vs,可以看到plugin的代码,WindowsMessageHandlerExampleModule.cpp

在这里插入图片描述

修改plugin代码

创建一个托盘

class FExampleHandler

icon在package之后的主目录

#if PLATFORM_WINDOWS
#include "Windows/MinWindows.h"
#include <shellapi.h>
#include <activation.h>
#endif
NOTIFYICONDATA m_nfData;
#define NOTIFY_SHOW WM_USER+2500
#define IDR_MAINFRAME WM_USER +2501




bool CreateIco()
	{
		HWND hWnd = GetActiveWindow();
		m_nfData.hWnd = hWnd;

		// 图标路径是package之后的目录,打包完把图标放到启动程序同一个文件夹
		const FString pathLaunch = FPaths::LaunchDir() + "icon.ico";
		UE_LOG(LogTemp, Warning, TEXT(">>> FPATHS : %s"), *pathLaunch);
		HINSTANCE hInst = NULL;
		HICON hIcon = (HICON)LoadImage(hInst, *(pathLaunch), IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_LOADFROMFILE);

		const uint32 Error = GetLastError();
		GLog->Logf(TEXT("%d"), Error);


		m_nfData.cbSize = sizeof(NOTIFYICONDATA); 
		m_nfData.uID = IDR_MAINFRAME;
		m_nfData.hIcon = hIcon;
		m_nfData.hWnd = hWnd;
		m_nfData.uCallbackMessage = NOTIFY_SHOW;
		m_nfData.uVersion = NOTIFYICON_VERSION;
		m_nfData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_INFO;
		lstrcpy(m_nfData.szTip, L"托盘"); //szAppClassName

		UE_LOG(LogTemp, Warning, TEXT(">>> Shell_NotifyIcon : "));
	
		return Shell_NotifyIcon(NIM_ADD, &m_nfData);
	}


移除托盘

class FExampleHandler

bool DeleteIco()
	{
		return Shell_NotifyIcon(NIM_DELETE, &m_nfData);
	}

给托盘加菜单

	// 添加菜单
	void AddTrayMenu(HWND Hwnd)
	{

		POINT pt;

		GetCursorPos(&pt);
		HMENU menu = CreatePopupMenu();

		// quit
		AppendMenu(menu, MF_STRING, WM_DESTROY, L"quit");

		::SetForegroundWindow(Hwnd);
		int32 MenuID = TrackPopupMenu(menu, TPM_RETURNCMD | TPM_LEFTALIGN | TPM_LEFTBUTTON, pt.x, pt.y, NULL, Hwnd, NULL);

		GLog->Logf(TEXT("%d"), MenuID);

		if (MenuID == WM_DESTROY)
		{
			UE_LOG(LogTemp, Warning, TEXT(">>> WM_DESTROY : "));
			// 退出
			RequestEngineExit("exit");
		}

		DestroyMenu(menu);
	}

处理Message

virtual bool ProcessMessage(HWND Hwnd, uint32 Message, WPARAM WParam, LPARAM LParam, int32& OutResult) override
	{
		// log out some details for the received message
		GLog->Logf(TEXT("WindowsMessageHandlerExampleModule: hwnd = %i, msg = %s, wParam = %i, lParam = %i"), Hwnd, *GetMessageName(Message), WParam, LParam);

		switch (Message)
		{
		case NOTIFY_SHOW:
			{
				switch (LParam)
				{
                    // 右键弹出菜单
				case WM_RBUTTONUP:
					AddTrayMenu(Hwnd);
					break;				

				}
			}


			return false;
		}
	};

package

打包完,把图标放到启动程序目录

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

修改后的plugin完整代码

WindowsMessageHandlerExampleModule.cpp


// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.

#include "Framework/Application/SlateApplication.h"
#include "Misc/OutputDevice.h"
#include "Modules/ModuleInterface.h"
#include "Modules/ModuleManager.h"
#include "Windows/WindowsApplication.h"

#include "WindowsMessageHelpers.h"

#define LOCTEXT_NAMESPACE "FWindowsMessageHandlerExampleModule"
#if PLATFORM_WINDOWS
#include <activation.h>
#include <shellapi.h>
#include "Windows/MinWindows.h"
#endif


NOTIFYICONDATA nid;

#define NOTIFY_SHOW WM_USER+2500
#define IDR_MAINFRAME WM_USER +2501

/**
 * Example Windows message handler.
 */
class FExampleHandler
	: public IWindowsMessageHandler
{
public:
	bool CreateIco()
	{
		HWND hWnd = GetActiveWindow();
		nid.hWnd = hWnd;

		// 图标路径是package之后,主目录
		const FString pathLaunch = FPaths::LaunchDir() + "icon.ico";
		UE_LOG(LogTemp, Warning, TEXT(">>> FPATHS : %s"), *pathLaunch);
		HINSTANCE hInst = NULL;
		HICON hIcon = (HICON)LoadImage(hInst, *(pathLaunch), IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_LOADFROMFILE);

		const uint32 Error = GetLastError();
		GLog->Logf(TEXT("%d"), Error);


		nid.cbSize = sizeof(NOTIFYICONDATA); 
		nid.uID = IDR_MAINFRAME;
		nid.hIcon = hIcon;
		nid.hWnd = hWnd;
		nid.uCallbackMessage = NOTIFY_SHOW;
		nid.uVersion = NOTIFYICON_VERSION;
		nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_INFO;
		lstrcpy(nid.szTip, L"LOOP TIMER"); //szAppClassName

		UE_LOG(LogTemp, Warning, TEXT(">>> Shell_NotifyIcon : "));
	
		return Shell_NotifyIcon(NIM_ADD, &nid);
	}
	
	bool DeleteIco()
	{
		return Shell_NotifyIcon(NIM_DELETE, &nid);
	}
	
	// 添加菜单
	void AddTrayMenu(HWND Hwnd)
	{

		POINT pt;

		GetCursorPos(&pt);
		HMENU menu = CreatePopupMenu();

		// quit
		AppendMenu(menu, MF_STRING, WM_DESTROY, L"quit");

		::SetForegroundWindow(Hwnd);
		int32 MenuID = TrackPopupMenu(menu, TPM_RETURNCMD | TPM_LEFTALIGN | TPM_LEFTBUTTON, pt.x, pt.y, NULL, Hwnd, NULL);

		GLog->Logf(TEXT("%d"), MenuID);

		if (MenuID == WM_DESTROY)
		{
			UE_LOG(LogTemp, Warning, TEXT(">>> WM_DESTROY : "));
			// 退出
			RequestEngineExit("exit");
		}

		DestroyMenu(menu);
	}


	//~ IWindowsMessageHandler interface

	virtual bool ProcessMessage(HWND Hwnd, uint32 Message, WPARAM WParam, LPARAM LParam, int32& OutResult) override
	{
		// log out some details for the received message
		GLog->Logf(TEXT("WindowsMessageHandlerExampleModule: hwnd = %i, msg = %s, wParam = %i, lParam = %i"), Hwnd, *GetMessageName(Message), WParam, LParam);

		switch (Message)
		{
		case NOTIFY_SHOW:
			{
				switch (LParam)
				{
				case WM_RBUTTONUP:
					AddTrayMenu(Hwnd);
					break;
								
				}
			}

			//...
		}
		
		// we did not handle this message, so make sure it gets passed on to other handlers
		return false;
	}
};


/**
 * Implements the WindowsMessageHandlerExample module.
 */
class FWindowsMessageHandlerExampleModule
	: public IModuleInterface
{
public:

	//~ IModuleInterface interface

	virtual void StartupModule() override
	{
		
		// register our handler
		FWindowsApplication* Application = GetApplication();
        // -----------------------托盘-------------------------
		Handler.CreateIco();
		if (Application != nullptr)
		{
			Application->AddMessageHandler(Handler);
		}
	}


	virtual void ShutdownModule() override
	{
		
		// unregister our handler
		FWindowsApplication* Application = GetApplication();
        // -----------------------移除托盘-------------------------
		Handler.DeleteIco();
		if (Application != nullptr)
		{
			Application->RemoveMessageHandler(Handler);
		}
	}

protected:

	FWindowsApplication* GetApplication() const
	{
		if (!FSlateApplication::IsInitialized())
		{
			return nullptr;
		}

		return (FWindowsApplication*)FSlateApplication::Get().GetPlatformApplication().Get();
	}

private:

	FExampleHandler Handler;
};


#undef LOCTEXT_NAMESPACE
	
IMPLEMENT_MODULE(FWindowsMessageHandlerExampleModule, WindowsMessageHandlerExample)



参考

https://blog.csdn.net/u014532636/article/details/80451370
https://unrealengine.com/id/authorize?client_id=c4c02c7c99e94ed9870a9dbeafab2c3f&response_type=code

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值