Windows 10 Close, Minimize and Maximize buttons

to paint themed button I use this code:

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;

This code works fine but painted button looks like from Windows 7 theme, even on Windows 8 or 10. This is possible to paint the Close button using Windows 10 or 8 theme?


One of ways to resolve this question: manual parsing active *.msstyles file. Usual this is aero.msstyles. Bitmap for different window controls stored in STREAM section. For Windows 7 ResId = 971, Windows 8: Id = 1060, Windows 10: Id = 1194. But this is manual work and this bitmaps is different.

Update:

I found, that even for one version of the Windows (tested for 8) we can have different values of the resource id for this Bitmap (png image) and now I can provide the code to obtain resource id on any Windows (tested for 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 + 'ResourcesThemesAeroaero.msstyles'), 
                        0, LOAD_LIBRARY_AS_DATAFILE);
  ResId := GetStyleResourceId(hLib);
  RS := TResourceStream.CreateFromID(hLib, ResId, 'STREAM');
  Png := TPngImage.Create;
  Png.LoadFromStream(RS);  
  ...
end;

Update 2:

Found not hacked method using official 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;

I can get Rect for minimized button, but don't understand how to use GetThemeStream? There should be used PBuf or PPBuf?


Workable solution to get bitmaps from theme:

var
  h: HTHEME;
  Rect: TRect;
  BufSize: Cardinal;    

h := OpenThemeData(Handle, 'DWMWINDOW');
if h <> 0 then
try
  GetThemeRect(h, WP_MINCAPTION, MNCS_ACTIVE, TMT_ATLASRECT, Rect);
  ...
  GetThemeStream(...);
finally
  CloseThemeData(h);
end;

And how to use GetThemeStream described here: GetThemeStream usage, many thanks to Andreas Verhoeven, author of the program Vista Style Builder

链接地址: http://www.djcxy.com/p/30784.html

上一篇: 如何对齐UIButton右边的UIButton图像

下一篇: Windows 10关闭,最小化和最大化按钮