UE5 查找外部窗口并置顶(小白学习笔记)

.h

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MywinClass.generated.h"

UCLASS()
class SPLINECPP_API AMywinClass : public AActor
{
	GENERATED_BODY()

public:
	// Sets default values for this actor's properties
	AMywinClass();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UFUNCTION(BlueprintCallable,Category = "myactor")
	static void FindWindowAndTop(FString name,bool SetTop);

    UFUNCTION(BlueprintCallable,Category = "myactor")
	static void OpenCoustomFile(FString FileName);

	UFUNCTION(BlueprintCallable,Category = "myactor")
	static bool CloseCoustomFile(FString FileName);

    UFUNCTION(BlueprintCallable,Category = "myactor")
    static int CMD(FString FileName);
	
	
};

.cpp 

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


#include "MywinClass.h"
#include <Windows.h>


// Sets default values
AMywinClass::AMywinClass()
{
	// 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 AMywinClass::BeginPlay()
{
	Super::BeginPlay();
	
}

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

void AMywinClass::FindWindowAndTop(FString name,bool SetTop)
{
	const TCHAR* WindowName = *name;
	HWND hWnd=NULL;
	hWnd=FindWindowW(NULL,WindowName);
	if(hWnd==NULL)
	{
		return;
	}
	else
	{
		ShowWindow(hWnd,SW_SHOWMAXIMIZED);
		ShowWindow(hWnd, SW_RESTORE);

		if (SetTop)
		{
			SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
		}
		else
		{
			SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

		}
	}
}

void AMywinClass::OpenCoustomFile(FString FileName)
{
	FString abc = FPaths::ProjectDir() + FileName;
	LPCWSTR MyLpcwstr = nullptr;
	MyLpcwstr = TCHAR_TO_WCHAR(*abc);
	
	ShellExecute(NULL,L"open",MyLpcwstr,NULL,NULL,SW_SHOWNORMAL);
		
}

bool AMywinClass::CloseCoustomFile(FString FileName)
{
	const TCHAR* windowname = *FileName;
	HWND CoustomHwnd = FindWindow(NULL,windowname);
	if (IsWindow(CoustomHwnd) != 0)
	{
		//同步调用
		//SendMessage(CoustomHwnd,WM_CLOSE,0,0);
		//异步调用
		PostMessage(CoustomHwnd,WM_CLOSE,0,0);
		return true;
	}
	else
	{
		return false;
	}	
}

int AMywinClass::CMD(FString FileName)
{
    const TCHAR* windowname = *FileName;
    int result = system("cmd /c windowname");
    
    if (result == 0) {
        // 命令执行成功
        // 可以继续处理或执行其他操作
    } else {
        // 命令执行失败
        // 可以根据具体情况处理错误
    }

    return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我救我自己

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值