1. 程式人生 > >UE接收windows訊息

UE接收windows訊息

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once
#include "Runtime/ApplicationCore/Public/Windows/WindowsApplication.h"
#include "Runtime/Slate/Public/Framework/Application/SlateApplication.h"


#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "ProceduralMeshComponent.h"
#include "MyActor1.generated.h"


class FExampleHandler : public IWindowsMessageHandler
{
public:

	//~ IWindowsMessageHandler interface

	virtual bool ProcessMessage(HWND Hwnd, uint32 Message, WPARAM WParam, LPARAM LParam, int32& OutResult) override
	{
		// log out some details for the received message
		GEngine->AddOnScreenDebugMessage((uint64)-1, 2.0f, FColor::Emerald, FString::Printf(TEXT("wParam = %d lParam = %d msg = %d"), WParam, LParam, Message));

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

UCLASS()
class VCTEST_API AMyActor1 : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AMyActor1();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	FExampleHandler Handler;
	FWindowsApplication* GetApplication();
};
// Fill out your copyright notice in the Description page of Project Settings.

#include "MyActor1.h"

// Sets default values
AMyActor1::AMyActor1()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
}

// Called when the game starts or when spawned
void AMyActor1::BeginPlay()
{
	Super::BeginPlay();

	FWindowsApplication* Application = GetApplication();

	if (Application != nullptr)
	{
		Application->AddMessageHandler(Handler);
	}
}

void AMyActor1::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
	Super::EndPlay(EndPlayReason);

	FWindowsApplication* Application = GetApplication();

	if (Application != nullptr)
	{
		Application->RemoveMessageHandler(Handler);
	}
}

// Called every frame
void AMyActor1::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

FWindowsApplication* AMyActor1::GetApplication()
{
	if (!FSlateApplication::IsInitialized())
	{
		return nullptr;
	}
	return (FWindowsApplication*)FSlateApplication::Get().GetPlatformApplication().Get();
}