【问题标题】:INNO Setup: How to get the primary monitor's resolution?INNO 设置:如何获得主显示器的分辨率?
【发布时间】:2011-03-28 15:54:33
【问题描述】:

我正在尝试使用 INNO 编写安装程序脚本,但我被困在需要获取运行安装程序的机器的屏幕分辨率并使用该值在桌面上创建快捷方式的地方决议作为论据之一。我知道如何创建快捷方式,但是我不知道如何提取屏幕分辨率以及如何传递该信息(可能存储在自定义变量中)以在桌面快捷方式中使用它。

感谢您的宝贵时间 :)

编辑:我无法更改应用程序,因为我无权这样做。所以请不要建议这样做。

【问题讨论】:

  • 我删除了我的答案,因为您已经明确表示您无法更改应用程序。你有我的同情。似乎在应用程序上工作的人拥有所有权力,他们正在滥用这种权力。

标签: windows inno-setup


【解决方案1】:

我对此的解决方案是使用GetSystemMetrics(),它可以在 user32.dll 中找到。这段代码正是我想要的,并且已经在具有双显示器设置的 Windows7 Professional(64 位)上进行了测试。

[Code]
function GetSystemMetrics (nIndex: Integer): Integer;
  external 'GetSystemMetrics@User32.dll stdcall setuponly';

Const
    SM_CXSCREEN = 0; // The enum-value for getting the width of the cient area for a full-screen window on the primary display monitor, in pixels.
    SM_CYSCREEN = 1; // The enum-value for getting the height of the client area for a full-screen window on the primary display monitor, in pixels.

function InitializeSetup(): Boolean;
  var 
      hDC: Integer;
      xres: Integer;
      yres: Integer;
begin
    xres := GetSystemMetrics(SM_CXSCREEN);
    yres := GetSystemMetrics(SM_CYSCREEN); //vertical resolution

    MsgBox( 'Current resolution is ' + IntToStr(xres) +
        'x' + IntToStr(yres)
, mbInformation, MB_OK );

    Result := true;
end;

编辑:似乎索引应该是 SM_CXSCREEN 和 SM_CYSCREEN。更改了代码以反映这一点。

【讨论】:

  • 很好的解决方案,非常感谢!在 Win Srv Std 2012 x64 上仍然有效 :-)
【解决方案2】:

您需要一些代码来获取当前分辨率。然后您可以将这些值添加到 [Icon] 条目以创建快捷方式。以下是一些帮助您入门的代码:

[Setup]
AppName=DisplayResoltution
AppVerName=DisplayResoltution
DefaultDirName=DisplayResoltution
DisableStartupPrompt=true
Uninstallable=false

[Files]
Source: "C:\util\innosetup\Examples\MyProg.exe"; DestDir: "{app}"; Flags: ignoreversion
; NOTE: Don't use "Flags: ignoreversion" on any shared system files

[Icons]
Name: "{group}\My Program"; Filename: "{app}\MyProg.exe"; Parameters: {code:GetParams}


[Code]
// Functions to get BPP & resolution

function DeleteDC (hDC: Integer): Integer;
 external 'DeleteDC@GDI32 stdcall';

function CreateDC (lpDriverName, lpDeviceName, lpOutput: String; lpInitData: Integer): Integer;
 external 'CreateDCA@GDI32 stdcall';

function GetDeviceCaps (hDC, nIndex: Integer): Integer;
 external 'GetDeviceCaps@GDI32 stdcall';

Const
    HORZRES = 8;    //horizontal resolution
    VERTRES = 10;   //vertical resolution
    BITSPIXEL = 12; //bits per pixel
    PLANES = 14;    //number of planes (color depth=bits_per_pixel*number_of_planes)
var
xres, yres, bpp, pl, tmp: Integer;

function InitializeSetup(): Boolean;
  var 
      hDC: Integer;
begin

    //get resolution & BPP
    hDC := CreateDC('DISPLAY', '', '', 0);
    pl := GetDeviceCaps(hDC, PLANES);
    bpp := GetDeviceCaps(hDC, BITSPIXEL);
    xres := GetDeviceCaps(hDC, HORZRES); //horizontal resolution
    yres := GetDeviceCaps(hDC, VERTRES); //vertical resolution
    tmp := DeleteDC(hDC);
    bpp := pl * bpp;   //color depth

    MsgBox( 'Current resolution is ' + IntToStr(xres) +
        'x' + IntToStr(yres) +
        ' and color depth is ' + IntToStr( bpp )
        , mbInformation, MB_OK );

    Result := true;
end;

function GetParams(def: string): string;
var
sTemp : string;
begin
  sTemp := 'xres=' + IntToStr(xres) + ' yres=' +IntToStr(yres);
  result := sTemp;
end;

代码改编自http://www.vincenzo.net/isxkb/index.php?title=Detect_current_display_resolution_and_color_depth

【讨论】:

  • 嗨 mirtheil,感谢您的回答。我今天应该可以测试它。请不要介意我无法尽快验证。
  • 我试过你的代码,它显示Current resolution is 0x0 and color depth is 0
  • 什么操作系统?我在 Win7 x64 上尝试过,它对我有用。你在其他机器上试过了吗?
  • 我也在 Win7 x64 上尝试。但是我通过使用来自 User32.dll 的GetSystemMetrics() 使用索引 SM_CXFULLSCREEN 和 SM_CYFULLSCREEN 得到了结果。我与您的系统有一个不同(可能);我的是双显示器设置。如果那是导致问题的原因,我不知道。而且我的解决方案的代码要少得多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多