본문 바로가기
Unity3D/Unity & C#

Unity & C# ~ 한/영 전환 하는 클래스

by 캬캬백곰 2023. 6. 29.
728x90
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class ImeChange
{
    #region imm32.dll :: Get_IME_Mode IME가져오기
    [DllImport("imm32.dll")]
    public static extern IntPtr ImmGetContext(IntPtr hWnd);

    [DllImport("imm32.dll")]
    public static extern bool ImmSetConversionStatus(IntPtr hIMC, int fdwConversion, int fdwSentence);

    [DllImport("imm32.dll")]
    private static extern IntPtr ImmGetDefaultIMEWnd(IntPtr hWnd);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr IParam);

    public const int IME_CMODE_ALPHANUMERIC = 0x0;   // 영문
    public const int IME_CMODE_NATIVE = 0x1;         // 한글
    #endregion


    #region User_Fn
    /// <summary>
    /// [한/영]전환 true=한글, false=영어
    /// </summary>
    /// <param name="b_toggle"></param>
    public static void ChangeIME(bool b_toggle)
    {
        // 현재 Unity 게임 프로세스 가져오기
        Process currentProcess = Process.GetCurrentProcess();

        // Unity 게임 창의 MainWindowHandle 가져오기
        IntPtr mainWindowHandle = currentProcess.MainWindowHandle;


        IntPtr hwnd = ImmGetContext(mainWindowHandle);  // C# WindowForm만 적용됨.
                                                        // [한/영]전환 b_toggle : true=한글, false=영어
        int dwConversion = b_toggle ? IME_CMODE_NATIVE : IME_CMODE_ALPHANUMERIC;
        ImmSetConversionStatus(hwnd, dwConversion, 0);
    }

    public static void ChangeImeCap(bool b_ime)
    {
        // [한/영]전환 true=한글, false=영어
        ChangeIME(b_ime);
    }
    #endregion
}
728x90
반응형