Windows 10 关闭、最小化和最大化按钮

2024-05-04

要绘制主题按钮,我使用以下代码:

var
  h: HTHEME;
begin
  if UseThemes then begin
    SetWindowTheme(Handle, 'explorer', nil);
    h := OpenThemeData(Handle, 'WINDOW');
    if h <> 0 then
    try
      DrawThemeBackground(h, Canvas.Handle, WP_CLOSEBUTTON, GetAeroState, ClientRect, nil);
    finally
      CloseThemeData(h);
    end;
  end
  else
    DrawFrameControl(Canvas.Handle, ClientRect, DFC_CAPTION, DFCS_CAPTIONCLOSE or GetClassicState)
end;

此代码工作正常,但绘制的按钮看起来像 Windows 7 主题,即使在 Windows 8 或 10 上也是如此。这可以使用 Windows 10 或 8 主题绘制“关闭”按钮吗?


解决这个问题的方法之一:手动解析活动*.msstyles文件。通常这是 aero.msstyles。不同窗口控件的位图存储在 STREAM 部分。对于 Windows 7 ResId = 971,Windows 8:Id = 1060,Windows 10:Id = 1194。但这是手动工作,并且此位图不同。

Update:

我发现,即使对于 Windows 的一个版本(针对 8 进行测试),我们也可以为该位图(png 图像)拥有不同的资源 id 值,现在我可以提供代码来获取任何 Windows 上的资源 id(针对 7 进行测试) ,8,10):

function EnumStreamProc(hModule: HMODULE; AType, AName: PChar; Params: LPARAM): BOOL; stdcall;
var
  Id: NativeInt;
begin
  PNativeInt(Params)^ := Integer(AName);
  Result := False;
end;

function GetStyleResourceId(AModule: HMODULE): Integer;
begin
  Result := 0;
  EnumResourceNames(AMODULE, 'STREAM', @EnumStreamProc, LPARAM(@Result));
end;

var
  hLib: HMODULE;
  ResId: Integer;
  RS: TResourceStream;
  Png: TPngImage;

begin
  hLib := LoadLibraryEx(PChar(GetWindowsPath + 'Resources\Themes\Aero\aero.msstyles'), 
                        0, LOAD_LIBRARY_AS_DATAFILE);
  ResId := GetStyleResourceId(hLib);
  RS := TResourceStream.CreateFromID(hLib, ResId, 'STREAM');
  Png := TPngImage.Create;
  Png.LoadFromStream(RS);  
  ...
end;

更新2:

使用官方api发现未破解的方法:

var
  h: HTHEME;
  Rect: TRect;
  PBuf, PPBuf: Pointer;
  BufSize: Cardinal;
  Buf: array[0..1024*1024] of Byte;


h := OpenThemeData(Handle, 'DWMWINDOW');
if h <> 0 then
try
  GetThemeRect(h, WP_MINCAPTION, MNCS_ACTIVE, TMT_ATLASRECT, Rect);
  PBuf := @Buf[0];
  PPBuf := @PBuf;
  GetThemeStream(h, WP_MINCAPTION, MNCS_ACTIVE, TMT_ATLASRECT, PBuf, BufSize, hInstance);
finally
  CloseThemeData(h);
end;

我可以获取最小化按钮的 Rect,但不明白如何使用 GetThemeStream?还有应该用PBuf还是PPBuf?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Windows 10 关闭、最小化和最大化按钮 的相关文章

随机推荐