Delphi程序运行时修改布局缩放报错:SystemParametersInfo入口点未找到
解决Delphi中SystemParametersInfo入口点找不到的问题
问题原因
user32.dll并未导出名为SystemParametersInfo的通用函数,仅提供了区分字符集的两个版本:SystemParametersInfoA(ANSI编码)和SystemParametersInfoW(Unicode编码)。你的代码中使用无后缀的函数名,导致运行时无法找到对应入口点。
修复方案
根据你的Delphi版本,选择对应字符集的函数声明:
方案1:适配Unicode版本Delphi(Delphi 2009及以后)
直接使用SystemParametersInfoW函数:
var Form1: TForm1; function SystemParametersInfoW(uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT): BOOL; stdcall; external 'user32.dll'; implementation {$R *.dfm} function SetScaling: Boolean; begin Result := SystemParametersInfoW($000F, 125, nil, 1); end; procedure TForm1.FormCreate(Sender: TObject); begin SetScaling; end;
方案2:兼容ANSI和Unicode版本
通过条件编译适配不同Delphi版本:
var Form1: TForm1; {$IFDEF UNICODE} function SystemParametersInfoW(uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT): BOOL; stdcall; external 'user32.dll'; {$ELSE} function SystemParametersInfoA(uiAction, uiParam: UINT; pvParam: Pointer; fWinIni: UINT): BOOL; stdcall; external 'user32.dll'; {$ENDIF} implementation {$R *.dfm} function SetScaling: Boolean; begin {$IFDEF UNICODE} Result := SystemParametersInfoW($000F, 125, nil, 1); {$ELSE} Result := SystemParametersInfoA($000F, 125, nil, 1); {$ENDIF} end; procedure TForm1.FormCreate(Sender: TObject); begin SetScaling; end;
额外提示
你当前使用的$000F对应SPI_SETICONSIZE(用于设置图标大小),如果你的目标是修改系统DPI布局缩放,应该使用SPI_SETLOGICALDPIOVERRIDE(值为$009E),且参数格式需要调整:
function SetScaling: Boolean; var NewDPI: Integer; begin NewDPI := 125; // SPIF_UPDATEINIFILE 保存设置到注册表,SPIF_SENDCHANGE 通知系统更新 Result := SystemParametersInfoW($009E, 0, @NewDPI, SPIF_UPDATEINIFILE or SPIF_SENDCHANGE); end;
内容的提问来源于stack exchange,提问作者David Kennedy
相关产品推荐
相关产品推荐

