UE4 Windows游戏窗口操作

提示框

::MessageBox(0, L"aaa.!!", L"Error", MB_OK);

一般需要添加Slate,SlateCore模块

获取屏幕尺寸

const FVector2D ViewportSize = FVector2D(GEngine->GameViewport->Viewport->GetSizeXY());

使窗口可拖拽文件

#include "AllowWindowsPlatformTypes.h"
#include "shellapi.h"

#include "HideWindowsPlatformTypes.h"

HWND hwndd = GetActiveWindow();
DragAcceptFiles(hwndd, true);

移动Windows游戏窗口

if (GEngine && GEngine->GameViewport)
{
	FVector2D WindowPosition = position;
	GEngine->GameViewport->GetWindow()->MoveWindowTo(WindowPosition);

提示Dialog

Response = FMessageDialog::Open(EAppMsgType::YesNoYesAllNoAllCancel, FText::FromString(Message));

const bool bWantOverwrite = Response == EAppReturnType::Yes || Response == EAppReturnType::YesAll;

找多个显示器并移动(添加ApplicationCore)

#include "Misc/CommandLine.h"

void AMyActor::AA()
{
	// Move window to the corresponding monitor
	if (GEngine && GEngine->GameViewport) {


		int MonitorNumber = 1;
		FParse::Value(FCommandLine::Get(), L"monitor=", MonitorNumber);


		FDisplayMetrics Display;
		FDisplayMetrics::GetDisplayMetrics(Display);


		int8 MonitorIndex = MonitorNumber - 1;
		int32 CurrentMonitorWidth = Display.MonitorInfo[MonitorIndex].NativeWidth;


		float WidthPosition = (MonitorIndex)*Display.PrimaryDisplayWidth - CurrentMonitorWidth;


		float HeightPosition = 0.0f;


		FVector2D WindowPosition = FVector2D((-1)*WidthPosition, HeightPosition);
		GEngine->GameViewport->GetWindow()->MoveWindowTo(WindowPosition);
	}
}

使窗口不能被拉伸

void AMyActor::AdjustWindow() {
#if PLATFORM_WINDOWS
	TSharedPtr<FGenericWindow> NativeWindow = GEngine->GameViewport->GetWindow()->GetNativeWindow();
	auto Window = static_cast<FWindowsWindow*>(NativeWindow.Get());
	//https://msdn.microsoft.com/en-us/library/windows/desktop/ms644898%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
	//https://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx
	//DWORD WindowStyle = WS_DLGFRAME;
	DWORD WindowExStyle = WS_EX_APPWINDOW;
	DWORD WindowStyle = WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MAXIMIZEBOX | WS_MINIMIZEBOX
		| WS_VISIBLE;
	//| WS_THICKFRAME | WS_POPUP | WS_DLGFRAME | WS_VISIBLE;

	auto hWnd = Window->GetHWnd();
	SetWindowLongPtr(hWnd, GWL_EXSTYLE, (LONG_PTR)WindowExStyle);
	SetWindowLongPtr(hWnd, GWL_STYLE, (LONG_PTR)WindowStyle);
#endif // #if PLATFORM_WINDOWS
}

打开Dialog

FString AButtonActor::SelectFolder()
{
		FString startpath = FPaths::GameDir();
		FString title = "liulan";
		FString outpath;
		bool bOpened = false;
		IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
		if (DesktopPlatform)
		{
			const void* ParentWindowWindowHandle = FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr);

			bOpened = DesktopPlatform->OpenDirectoryDialog(ParentWindowWindowHandle,title, startpath,outpath);
		}
		if (bOpened)
		{
			return outpath;
		}
		return "failed open";
}

打开文件名

FString UFileOperatorFlib::ExtendGetOpenFileName()
{
  IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();

  if (DesktopPlatform)
  {
    TArray<FString> OpenFilenames;
    const bool bOpened = DesktopPlatform->OpenFileDialog(
      nullptr,
      TEXT("OpenFileDialog"),
      FString(TEXT("")),
      TEXT(""),
      TEXT("All Files (*.*)"),
      EFileDialogFlags::None,
      OpenFilenames
    );

    if (OpenFilenames.Num() > 0)
    {
      return FPaths::ConvertRelativePathToFull(OpenFilenames[0]);
    }
  }
  return TEXT("");
}

最小化窗口

GEngine->GameViewport->GetWindow()->Minimize();

隐藏窗口

GEngine->GameViewport->GetWindow()->HideWindow();

5.添加到系统托盘

#include "AllowWindowsPlatformTypes.h"
#include <shellapi.h>
#include <Shlwapi.h>
#include <wtypes.h>
#include <WinUser.h>
#include "HideWindowsPlatformTypes.h"

#include "Engine/Engine.h"
#include "SlateApplication.h"
#include "Runtime/Launch/Resources/Windows/resource.h"

#define WM_ICON_NOTIFY WM_USER + 1009  //自定义消息,用以接收来自托盘的反馈
#define WM_ICON_ID  32514
#define IDR_PAUSE 12
#define IDR_START 13
NOTIFYICONDATA  m_nfData;


bool AWindowsActor::createICO(FString ICOpath)
{
	//FString ICOpath="D:\\png.png";
	//FString szTitle = "icoio";

	HINSTANCE hInst = NULL;

	HWND hWnd = GetActiveWindow();
	m_nfData.hWnd = hWnd;
	//HICON hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDICON_UE4Game));
	//FPaths::ProjectConfigDir() +
	HICON hIcon = (HICON)LoadImage(hInst, *(ICOpath), IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_LOADFROMFILE);
	//HICON hIcon = (HICON)LoadImage(hInst, MAKEINTRESOURCE(IDICON_UE4Game), IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE);
	const uint32 Error = GetLastError();
	GLog->Logf(TEXT("%d"), Error);


	m_nfData.cbSize = sizeof(NOTIFYICONDATA);//GetSize_NotifyIconData(); (DWORD)sizeof(NOTIFYICONDATA);
	m_nfData.uID = IDICON_UE4Game;
	m_nfData.hIcon = hIcon;
	m_nfData.hWnd = hWnd;
	m_nfData.uCallbackMessage = WM_ICON_NOTIFY;
	m_nfData.uVersion = NOTIFYICON_VERSION;
	m_nfData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_INFO;
	lstrcpy(m_nfData.szTip, L"托盘图标");//szAppClassName
	//LPCTSTR szAppClassName = TEXT("托盘图标");

	//气泡
	
	m_nfData.dwInfoFlags = NIIF_INFO;
	m_nfData.uTimeout = 1000;

	
	lstrcpy(m_nfData.szInfo, L"内容");  //气泡内容
	lstrcpy(m_nfData.szInfoTitle, L"实现气泡点击标题"); //气泡标题

	//_tcscpy_s(m_nfData.szTip, *szTitle);

	return Shell_NotifyIcon(NIM_ADD, &m_nfData);

}

6.删减托盘

bool AWindowsActor::deleteICO()
{
	return  Shell_NotifyIcon(NIM_DELETE, &m_nfData);
}

7.添加托盘菜单

void AWindowsActor::addMenu()
{
	POINT pt;
	//LPPOINT lpoint = new tagPOINT;
	//::GetCursorPos(lpoint);//得到鼠标位置
	GetCursorPos(&pt);
	HMENU menu;

	menu = CreatePopupMenu();//声明一个弹出式菜单
	//增加菜单项“关闭”,点击则发送消息WM_DESTROY给主窗口(已
	//隐藏),将程序结束。
	AppendMenu(menu, MF_STRING, WM_DESTROY, L"关闭");

	//HWND hwnd = GetActiveWindow();
	//确定弹出式菜单的位置
	HWND ParentWindowWindowHandle = (HWND)FSlateApplication::Get().FindBestParentWindowHandleForDialogs(nullptr);
	::SetForegroundWindow(ParentWindowWindowHandle);//解决在菜单外单击左键菜单不消失的问题
	//SetForegroundWindow(hwnd);
	int32  MenuID = TrackPopupMenu(menu, TPM_RETURNCMD | TPM_LEFTALIGN | TPM_LEFTBUTTON, pt.x, pt.y, NULL, ParentWindowWindowHandle, NULL);
	GLog->Logf(TEXT("%d"), MenuID);
	if (MenuID == WM_DESTROY)
	{
		GLog->Logf(TEXT("okokoko"));
	}
	//const uint32 Error = GetLastError();
	//GLog->Logf(TEXT("%d"), Error);

	//资源回收
	//HMENU hmenu = menu.Detach(menu);
	DestroyMenu(menu);


}

8.显示气泡

bool AWindowsActor::ShowBalloonTip()
{
	m_nfData.uFlags =  NIF_INFO;
	m_nfData.dwInfoFlags = NIIF_INFO;
	m_nfData.uVersion = NOTIFYICON_VERSION;
	m_nfData.uTimeout = 1000;
	//_tcscpy_s(m_nfData.szInfo, *szMsg);
	
	lstrcpy(m_nfData.szInfo, L"tuopan");
	lstrcpy(m_nfData.szInfoTitle, L"实现气泡点击");
	return Shell_NotifyIcon(NIM_MODIFY, &m_nfData);
}

C++Diolog打开文件路径


bool AMapActor::OpenDiolog(FString& Path)
{
	TCHAR szBuffer[MAX_PATH] = { 0 };
	OPENFILENAME ofn = { 0 };
	ofn.lStructSize = sizeof(ofn);
	//ofn.hwndOwner = m_hWnd;
	ofn.lpstrFilter = _T("umap文件(*.umap)\0*.umap\0所有文件(*.*)\0*.*\0");//要选择的文件后缀   
	ofn.lpstrInitialDir = _T("D:\\Program Files");//默认的文件路径   
	ofn.lpstrFile = szBuffer;//存放文件的缓冲区   
	ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer);
	ofn.nFilterIndex = 0;
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;//标志如果是多选要加上OFN_ALLOWMULTISELECT  
	bool bSel = GetOpenFileName(&ofn);

	Path = szBuffer;
	return bSel;
}
bool AActor::OpenFileDialogCore(const FString& DialogTitle, const FString& DefaultPath, const FString& DefaultFile, const FString& FileTypes, uint32 Flags, TArray< FString >& OutFilenames)
{
	// Calling the main open file dialog function using UE4's FDesktopPlatformModule struct.
	return FDesktopPlatformModule::Get()->OpenFileDialog(nullptr, DialogTitle, DefaultPath, DefaultFile, FileTypes, Flags, OutFilenames);
}


bool AActor::SaveFileDialogCore(const FString& DialogTitle, const FString& DefaultPath, const FString& DefaultFile, const FString& FileTypes, uint32 Flags, TArray< FString >& OutFilenames)
{
	// Calling the main save file dialog function using UE4's FDesktopPlatformModule struct.
	return FDesktopPlatformModule::Get()->SaveFileDialog(nullptr, DialogTitle, DefaultPath, DefaultFile, FileTypes, Flags, OutFilenames);
}

bool AActor::OpenFolderDialogCore(const FString& DialogTitle, const FString& DefaultPath, FString& OutFoldername)
{
	// Calling the main open folder dialog function using UE4's FDesktopPlatformModule struct.
	return FDesktopPlatformModule::Get()->OpenDirectoryDialog(nullptr, DialogTitle, DefaultPath, OutFoldername);
}

修改窗口名称

GEngine->GameViewport->GetWindow()->SetTitle(FText::FromString(TEXT("1111")));

 

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页