使用SystemParametersInfo SPI_GETICONTITLELOGFONT

时间:2010-09-07 13:34:07

标签: c# delphi windows-7

我目前正在使用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;

2 个答案:

答案 0 :(得分:1)

您可能在个性化菜单中更改了错误的字体?如果我将Icon字体从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.

答案 1 :(得分: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.