2008年7月9日

Windows XP以降のウィンドウゴースト機能を回避

Windows XP以降ではUIを持ったアプリケーションが5秒以上メッセージループを回さないとウィンドウゴースト機能(window ghosting feature)が働いて、そのアプリケーションのトップレベルウィンドウと同じ位置、サイズ、キャプションを持ったゴーストウィンドウが生成されます。
まぁそれはそれでいいのですが、アプリケーションがこの状態から復帰したときに、(1)Zオーダが狂ってしまい、直後の最前面ウィンドウ表示が最背面に回されてしまう、(2)Windows Vistaのタスクバーにゴーストウィンドウが残ってしまう、という微妙な(状況によっては厄介な)問題があります。
そこでウィンドウゴースト機能を停止してしまえ、ということでDisableProcessWindowsGhostingのサンプルコードです。
uses
  Windows, SysUtils;

procedure DisableProcessWindowsGhosting;
var
  PDisableProcessWindowsGhosting: procedure; stdcall;
begin

  if (Win32Platform = VER_PLATFORM_WIN32_NT) and
     (((Win32MajorVersion = 5) and (Win32MinorVersion >= 1)) or  // Windows XP
      (Win32MajorVersion >= 6)) then                             // Windows Vista or later
  begin
    @PDisableProcessWindowsGhosting := GetProcAddress(GetModuleHandle('user32.dll'),
                                                      'DisableProcessWindowsGhosting');
    if Assigned(PDisableProcessWindowsGhosting) = True then
    begin
      PDisableProcessWindowsGhosting;
    end;
  end;

end;

元ねたはCodeGearのQC3730から。

2011/05/04追記: QC3730のリンクをqc.embarcadero.comのものに差し替え。

0 件のコメント: