Skip to content

Instantly share code, notes, and snippets.

@tateisu
Last active June 2, 2023 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tateisu/301bf8da57f0320d30b57ffda8f982ab to your computer and use it in GitHub Desktop.
Save tateisu/301bf8da57f0320d30b57ffda8f982ab to your computer and use it in GitHub Desktop.
(Windows) アクティブウィンドウのIMEをOFFにする
#!/usr/bin/perl --
use 5.32.0;
use strict;
use warnings;
use Win32::API;
my $WM_IME_CONTROL = 0x0283;
my $IMC_SETOPENSTATUS = 0x06;
my $TRUE = 1;
my $FALSE = 0;
sub dllImport($$$$){
Win32::API::More->new(@_) or die(
"dllImport: $_[1] in $_[0], ", Win32::FormatMessage(Win32::GetLastError())
);
}
my $GetGUIThreadInfo = dllImport 'user32.dll', 'GetGUIThreadInfo', 'IT','i';
my $ImmGetDefaultIMEWnd = dllImport 'imm32.dll', 'ImmGetDefaultIMEWnd', 'N','N';
my $SendMessage = dllImport 'user32.dll', 'SendMessage', 'NIii','N';
my @guiThreadInfoParams = qw(
DWORD cbSize;
DWORD flags;
HWND hwndActive;
HWND hwndFocus;
HWND hwndCapture;
HWND hwndMenuOwner;
HWND hwndMoveSize;
HWND hwndCaret;
LONG left;
LONG top;
LONG right;
LONG bottom;
);
Win32::API::Struct->typedef( 'GUITHREADINFO', @guiThreadInfoParams);
sub getFocusWindow(){
my $struct = Win32::API::Struct->new('GUITHREADINFO');
# ゼロ初期化
for my $k (@guiThreadInfoParams){
$k =~ s/;//g;
$struct->{$k}=0;
}
$struct->{cbSize} = $struct->sizeof;
$GetGUIThreadInfo->Call(0, $struct) or die "GetGUIThreadInfo failed.";
my $hwndFocus = $struct->{hwndFocus};
return $hwndFocus;
}
my $wFocus = getFocusWindow();
my $wIme = $ImmGetDefaultIMEWnd->Call($wFocus);
# IME-ONしてIME-OFFする
$SendMessage->Call($wIme,$WM_IME_CONTROL, $IMC_SETOPENSTATUS,$TRUE);
$SendMessage->Call($wIme,$WM_IME_CONTROL, $IMC_SETOPENSTATUS,$FALSE);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment