Inno Setup - 如何强制从Inno Setup启动的程序在屏幕上的某个位置打开?

时间:2017-02-11 14:07:24

标签: inno-setup

使用此代码:Install DirectX & VCRedist in freearc default script when progress bar is full & paused after main file extraction我可以使用Inno Setup安装DirectX和VCRedist。但是,是否可以将这些程序的安装窗口强制到屏幕上的某个位置?例如:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

除非应用程序明确支持,否则几乎不可能使应用程序在所需位置启动。

所以一般来说,你可以做的是观察某个窗口出现并在之后移动它。您可以通过标题(FindWindowByWindowName)或类(FindWindowByClassName)来标识窗口。缺点是窗口将短暂出现在其默认位置。

代码使用InnoTools InnoCallback library

[Files]
Source: "DXWebSetup.exe"; DestDir: "{tmp}"; Flags: deleteafterinstall
Source: "InnoCallback.dll"; Flags: dontcopy

[Run]
Filename: "{tmp}\DXWebSetup.exe"; StatusMsg: "Installing DirectX..."; \
  BeforeInstall: StartWaitingForDirectXWindow; AfterInstall: StopWaitingForDirectXWindow


[Code]

type
  TTimerProc = procedure(HandleW, msg, idEvent, TimeSys: LongWord);

function WrapTimerProc(Callback: TTimerProc; ParamCount: Integer): LongWord;
  external 'wrapcallback@files:InnoCallback.dll stdcall delayload';
function SetTimer(hWnd, nIDEvent, uElapse, lpTimerFunc: LongWord): LongWord;
  external 'SetTimer@User32.dll stdcall';
function KillTimer(hWnd, nIDEvent: LongWord): LongWord;
  external 'KillTimer@User32.dll stdcall';
function GetTickCount: DWord; external 'GetTickCount@kernel32 stdcall';
function SetWindowPos(hWnd: HWND; hWndInsertAfter: HWND; X: Integer; Y: Integer;
  cx: Integer; cy: Integer; uFlags: UINT): BOOL;
  external 'SetWindowPos@user32.dll stdcall'; 

const
  SWP_NOSIZE = $01;  
  SWP_NOZORDER = $04;

var
  WindowWaitTimer: LongWord;
  WindowWaitStarted: DWord;
  MoveWindowRunning: Boolean;

procedure MoveDirectXWindowProc(
  H: LongWord; Msg: LongWord; IdEvent: LongWord; Time: LongWord);
var
  Retry: Boolean;
  Handle: HWND;
begin
  Handle := FindWindowByWindowName('Installing Microsoft(R) DirectX(R)');
  if Handle = 0 then
  begin
    if DWord(GetTickCount - WindowWaitStarted) < 5000 then
    begin
      Log('DirectX window not found, will try again shortly');
      Retry := True;
    end
     else
    begin
      Log('Giving up waiting for DirectX window');
      Retry := False;
    end
  end
    else
  begin
    Log('DirectX window found');

    SetWindowPos(
      Handle, 0, WizardForm.Left + ScaleX(150), WizardForm.Top + ScaleX(30),
      0, 0, SWP_NOSIZE or SWP_NOZORDER);
    Retry := False;
  end;

  if not Retry then
  begin
    Log('Stopping timer');
    KillTimer(0, WindowWaitTimer);
    WindowWaitTimer := 0;
  end;
end;

procedure StartWaitingForDirectXWindow;
begin
  Log('Starting waiting for DirectX window');
  WindowWaitTimer := SetTimer(0, 0, 100, WrapTimerProc(@MoveDirectXWindowProc, 4));
  WindowWaitStarted := GetTickCount;
end;

procedure StopWaitingForDirectXWindow;
begin
  if WindowWaitTimer <> 0 then
  begin
    Log('DirectX installer finished, and we are still waiting for its window, stopping');
    KillTimer(0, WindowWaitTimer);
    WindowWaitTimer := 0;
  end
    else
  begin
    Log('DirectX installer finished, and we are no longer waiting for its window');
  end;
end;

Windows positions