UNREAL/Unreal Study

외부 프로그램 화면 가장 앞or 뒤로 옮기기

GREEN나무 2024. 3. 17. 21:53
728x90

 

사용

 

실행된 파일 중에 일치하는 윈도우 타일명을 찾아서 창을 화면 앞이나 뒤로 옮기기 

// MyBlueprintFunctionLibrary.h

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

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"

UCLASS()
class VISIONT_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

public:
    UFUNCTION(BlueprintCallable, Category = "Window Management")
        static FString SetFullWindow(FString WindowTitleKeyword);

public:
    UFUNCTION(BlueprintCallable, Category = "Window Management")
        static FString SetWindowOnTop(FString WindowName);

public:
    UFUNCTION(BlueprintCallable, Category = "Window Management")
        static FString SetWindowOnTopN_Down(FString WindowName, int32 MoveX, int32 MoveY);

    /*
    UCLASS()
    class VISIONT_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
    {
        GENERATED_BODY()

    public:
        UFUNCTION(BlueprintCallable, Category = "Window Management")
            static FString SetWindowOnTop(FString WindowName);
        */

public:
    UFUNCTION(BlueprintCallable, Category = "Window Management")
        static FString SetWindowOnBottom(FString WindowTitleKeyword);
};

 

 

// MyBlueprintFunctionLibrary.cpp


#include "MyBlueprintFunctionLibrary.h"
#include "Windows/MinWindows.h"

#include <stdlib.h> // system 함수를 사용하기 위해 필요



FString UMyBlueprintFunctionLibrary::SetFullWindow(FString WindowTitleKeyword)
{
    if (WindowTitleKeyword.IsEmpty())
    {
        return TEXT("Is Empty");
    }

    // 윈도우 타이틀 키워드를 포함하는 윈도우 찾기
    HWND hWnd = FindWindow(NULL, NULL);
    while (hWnd)
    {
        TCHAR buffer[256];
        GetWindowText(hWnd, buffer, 256);
        FString WindowTitle(buffer);
        if (WindowTitle.Contains(WindowTitleKeyword))
        {
            // 창 스타일 변경하기
            LONG lStyle = GetWindowLong(hWnd, GWL_STYLE);
            lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU);
            SetWindowLong(hWnd, GWL_STYLE, lStyle);
            LONG lExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
            lExStyle &= ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
            SetWindowLong(hWnd, GWL_EXSTYLE, lExStyle);

            // 시스템 해상도 가져오기
            int screenWidth = GetSystemMetrics(SM_CXSCREEN);
            int screenHeight = GetSystemMetrics(SM_CYSCREEN);

            // 창을 전체 화면으로 설정
            BOOL setPosResult = SetWindowPos(hWnd, HWND_TOP, 0, 0, screenWidth, screenHeight, SWP_FRAMECHANGED);


        }
    }

    return TEXT("Can't Find");
}


FString UMyBlueprintFunctionLibrary::SetWindowOnTop(FString WindowName)
{
    if (WindowName.IsEmpty())
    {
        return TEXT("Is Empty");
    }

    // FString을 TCHAR로 변환하여 윈도우 타이틀로 사용
    const TCHAR* WindowTitle = *WindowName;
    HWND hWnd = FindWindow(NULL, WindowTitle);

    if (!hWnd)
    {
        return TEXT("Can't Find");
    }

    // 해당 윈도우를 최상위로 설정
    BOOL setTopResult = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

    if (setTopResult == 0) // SetWindowPos가 실패하면 0을 반환
    {
        return TEXT("Find. But HWND_TOPMOST Failed");
    }

    return TEXT("Success");
}

FString UMyBlueprintFunctionLibrary::SetWindowOnTopN_Down(FString WindowName, int32 MoveX, int32 MoveY)
{
    if (WindowName.IsEmpty())
    {
        return TEXT("Is Empty");
    }

    // 이하 기존 코드 유지
    const TCHAR* WindowTitle = *WindowName;
    HWND hWnd = FindWindow(NULL, WindowTitle);
    if (!hWnd)
    {
        return TEXT("Can't Find");
    }

    // 창 스타일 변경하기
    LONG lStyle = GetWindowLong(hWnd, GWL_STYLE);
    lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU);
    SetWindowLong(hWnd, GWL_STYLE, lStyle);
    LONG lExStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
    lExStyle &= ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
    SetWindowLong(hWnd, GWL_EXSTYLE, lExStyle);

    // 시스템 해상도 가져오기
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    BOOL setTopResult = SetWindowPos(hWnd, HWND_TOPMOST, MoveX, MoveY, 1920, 1080, 0);

    if (setTopResult == 0)
    {
        return TEXT("Find. But HWND_TOPMOST Failed");
    }

    return TEXT("Success");
}


FString UMyBlueprintFunctionLibrary::SetWindowOnBottom(FString WindowTitleKeyword)
{
    if (WindowTitleKeyword.IsEmpty())
    {
        return TEXT("Is Empty");
    }

    // 윈도우 타이틀 키워드를 포함하는 윈도우 찾기
    HWND hWnd = FindWindow(NULL, NULL);
    while (hWnd)
    {
        TCHAR buffer[256];
        GetWindowText(hWnd, buffer, 256);
        FString WindowTitle(buffer);
        if (WindowTitle.Contains(WindowTitleKeyword))
        {
            // 창을 화면의 맨 뒤로 보냄
            BOOL setBottomResult = SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
            if (setBottomResult != 0) // SetWindowPos가 성공하면
            {
                return TEXT("Success");
            }
            else // SetWindowPos가 실패하면
            {
                return TEXT("Find. But HWND_BOTTOM Failed");
            }
        }
        hWnd = GetNextWindow(hWnd, GW_HWNDNEXT);
    }

    return TEXT("Can't Find");
}

 

#include "Windows/MinWindows.h"
...

BOOL setTopResult = SetWindowPos(hWnd, HWND_TOPMOST, MoveX, MoveY, 1920, 1080, 0);

hWnd는 "핸들(Window Handle)"의 줄임말로, 윈도우를 식별하는 데 사용되는 윈도우 핸들입니다. 
HWND_TOPMOST : 화면 맨 앞   <-> HWND_BOTTOM 화면 맨 뒤
MoveX, MoveY : 입력 변수 MoveX, Y 가져옴, 화면의 사작 위치(좌측 상단)
FString UMyBlueprintFunctionLibrary::SetWindowOnTopN_Down(FString WindowName, int32 MoveX, int32 MoveY)
1920, 1080 : 화면 창 크기
0 : 아래처럼 화면 위치와 사이즈 지정하지 않을 경우 " SWP_NOMOVE | SWP_NOSIZE". 변화가 필요하면 0을 적으세요
BOOL setBottomResult = SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

 

나중에 써볼 방법

화면 위에 고정.txt
0.00MB