使用 SystemParametersInfo SPI_GETICONTITLELOGFONT

发布于 2024-09-18 13:25:25 字数 973 浏览 5 评论 0原文

我当前正在使用函数 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;

I am currently using the function SystemParametersInfo to retrieve SPI_GETICONTITLELOGFONT. According to the MSDN documentation this is the
http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspx

"Retrieves the logical font information for the current icon-title font"

But this always retrieves 'Segoe UI' even when I change my font to 'VivlaidD'. I am on a Windows 7 machine. Is it that this function only retrieves the system default? Or is there something wrong with 'SystemParametersInfo'?

Here is my code for retrieving the font:

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

闻呓 2024-09-25 13:25:25

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

May be you change the wrong font in Personalization menu? If I change the Icon font from Segoe UI to Verdana, the following code works properly:

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.
说不完的你爱 2024-09-25 13:25:25

问题不在于您的代码,正如您在以下 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.

The problem is not with your code as you can see with the following D2010 console app, changing and retrieving the font, and working well on 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文