【问题标题】:Using SystemParametersInfo SPI_GETICONTITLELOGFONT使用 SystemParametersInfo SPI_GETICONTITLELOGFONT
【发布时间】:2011-04-09 05:08:41
【问题描述】:

我目前正在使用函数 SystemParametersInfo 来检索 SPI_GETICONTITLELOGFONT。根据 MSDN 文档,这是 http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspx

"获取当前图标-标题字体的逻辑字体信息"

但即使我将字体更改为“VivlaidD”,它也总是会检索“Segoe UI”。我在 Windows 7 机器上。是不是这个函数只检索系统默认值?或者“SystemParametersInfo”有什么问题吗?

这是我检索字体的代码:

procedure GetUserFontPreference(out FaceName: string; out PixelHeight: Integer);
var
  lf: LOGFONT;
begin
  ZeroMemory(@lf, SizeOf(lf));

  if SystemParametersInfo(SPI_GETICONTITLELOGFONT, SizeOf(lf), @lf, 0) then
  begin
    FaceName := PChar(Addr(lf.lfFaceName[0]));
    PixelHeight := lf.lfHeight;
  end
  else
  begin
    {
            If we can't get it, then assume the same non-user preferences that
            everyone else does.
    }
    FaceName := 'MS Shell Dlg 2';
    PixelHeight := 8;
  end;
end;

【问题讨论】:

  • 字体名称拼写正确吗?应该是VivaldiD吗?
  • 注意可能会被小磅值替代的暗示不佳的字体。
  • 双重检查,绝对是 VivladiD
  • 是时候包含您的偏好截图,显示项目:图标,字体:VivladiD

标签: c# delphi windows-7


【解决方案1】:

问题不是您的代码,正如您在以下 D2010 控制台应用程序中看到的那样,更改和检索字体,并且在 Win7 x64 上运行良好:

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils, Windows;


procedure GetUserFontPreference(out FaceName: string; out PixelHeight: Integer);
var
  lf: LOGFONT;
begin
  ZeroMemory(@lf, SizeOf(lf));

  if SystemParametersInfo(SPI_GETICONTITLELOGFONT, SizeOf(lf), @lf, 0) then
  begin
    FaceName := lf.lfFaceName; // simpler than PChar(Addr(lf.lfFaceName[0]));
    PixelHeight := lf.lfHeight;
  end
  else
  begin
    {
            If we can t get it, then assume the same non-user preferences that
            everyone else does.
    }
    FaceName := 'MS Shell Dlg 2';
    PixelHeight := 8;
  end;
end;

procedure SetUserFontPreference(const AFaceName: string; const APixelHeight: Integer);
var
  lf: LOGFONT;
begin
  ZeroMemory(@lf, SizeOf(lf));
  Move(AFaceName[1], lf.lfFaceName, Length(AFaceName)*SizeOf(Char));
  lf.lfHeight := APixelHeight;
  SystemParametersInfo(SPI_SETICONTITLELOGFONT, SizeOf(lf), @lf, 0);
end;

procedure Test;
var
  FontName, NewFontName, OldFontName: string;
  FontHeight: Integer;
begin
  GetUserFontPreference(OldFontName, FontHeight);
  Writeln('Current (Old) Font is ', OldFontName);
  Readln;

  NewFontName := 'Rage Italic';  //'Segoe UI';//'Rage Italic';
  SetUserFontPreference(NewFontName, FontHeight);
  GetUserFontPreference(FontName, FontHeight);
  Assert(FontName=NewFontName);
  Writeln('Current (New) Font is ', FontName);
  Readln;

  SetUserFontPreference(OldFontName, FontHeight);
  GetUserFontPreference(FontName, FontHeight);
  Assert(FontName=OldFontName);
  Writeln('Current Font is back to (Old) ', FontName);
  Readln;
end;

begin
  try
    Test;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

【讨论】:

    【解决方案2】:

    可能是您在个性化菜单中更改了错误的字体?如果我将图标字体从 Segoe UI 更改为 Verdana,则以下代码可以正常工作:

    program Project1;
    
    {$APPTYPE CONSOLE}
    
    uses
      SysUtils, Windows;
    
    var
      LogFont: TLogFont;
    
    begin
      try
        if SystemParametersInfo(SPI_GETICONTITLELOGFONT, SizeOf(LogFont),
          @LogFont, 0) then
          Writeln('Current Font is ', LogFont.lfFaceName)
        else
          Writeln('Error #', GetLastError);
    
        Readln;
      except
        on E: Exception do
          Writeln(E.ClassName, ': ', E.Message);
      end;
    end.
    

    【讨论】:

    • 我还认为我可能更改了错误的字体,所以我将所有字体更改为 VivaldiD,但我仍然只能获得“Segoe UI”。
    • 我编辑了我的原始帖子,你可以看到它几乎相同,但从技术上讲,不,我没有尝试你的代码。
    猜你喜欢
    • 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
    相关资源
    最近更新 更多