Advertisement
Guest User

comctl32 v6 activation context for kobik

a guest
Apr 1st, 2012
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 5.57 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.CommCtrl, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls, Vcl.ImgList, Winapi.ShellAPI;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     ImageList1: TImageList;
  12.     PopupMenu1: TPopupMenu;
  13.     MenuItem1: TMenuItem;
  14.     Button1: TButton;
  15.     Button2: TButton;
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure Button2Click(Sender: TObject);
  19.   private
  20.     FileName: string;
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.dfm}
  29.  
  30. procedure TForm1.FormCreate(Sender: TObject);
  31. begin
  32.   PopupMenu1.Images := ImageList1;
  33.   FileName := 'C:\Program Files (x86)\Embarcadero\RAD Studio\9.0\bin\bds.exe';
  34. end;
  35.  
  36. type
  37.   (* TActivationContext is a loose wrapper around the Windows Activation Context API and can be used
  38.      to ensure that comctl32 v6 and visual styles are available for UI elements created from a DLL .*)
  39.   TActivationContext = class
  40.   private
  41.     FCookie: LongWord;
  42.     FSucceeded: Boolean;
  43.   public
  44.     constructor Create;
  45.     destructor Destroy; override;
  46.   end;
  47.  
  48. var
  49.   ActCtxHandle: THandle=INVALID_HANDLE_VALUE;
  50.   CreateActCtx: function(var pActCtx: TActCtx): THandle; stdcall;
  51.   ActivateActCtx: function(hActCtx: THandle; var lpCookie: LongWord): BOOL; stdcall;
  52.   DeactivateActCtx: function(dwFlags: DWORD; ulCookie: LongWord): BOOL; stdcall;
  53.   ReleaseActCtx: procedure(hActCtx: THandle); stdcall;
  54.  
  55. constructor TActivationContext.Create;
  56. begin
  57.   inherited;
  58.   FSucceeded := (ActCtxHandle<>INVALID_HANDLE_VALUE) and ActivateActCtx(ActCtxHandle, FCookie);
  59. end;
  60.  
  61. destructor TActivationContext.Destroy;
  62. begin
  63.   if FSucceeded then begin
  64.     DeactivateActCtx(0, FCookie);
  65.   end;
  66.   inherited;
  67. end;
  68.  
  69. procedure InitialiseActivationContext;
  70. var
  71.   ActCtx: TActCtx;
  72.   hKernel32: HMODULE;
  73. begin
  74.   hKernel32 := GetModuleHandle(kernel32);
  75.   CreateActCtx := GetProcAddress(hKernel32, 'CreateActCtxW');
  76.   if Assigned(CreateActCtx) then begin
  77.     ReleaseActCtx := GetProcAddress(hKernel32, 'ReleaseActCtx');
  78.     ActivateActCtx := GetProcAddress(hKernel32, 'ActivateActCtx');
  79.     DeactivateActCtx := GetProcAddress(hKernel32, 'DeactivateActCtx');
  80.     ZeroMemory(@ActCtx, SizeOf(ActCtx));
  81.     ActCtx.cbSize := SizeOf(ActCtx);
  82.     ActCtx.lpSource := 'C:\desktop\manifest.txt';
  83.     ActCtxHandle := CreateActCtx(ActCtx);
  84.   end;
  85. end;
  86.  
  87. procedure FinaliseActivationContext;
  88. begin
  89.   if ActCtxHandle<>INVALID_HANDLE_VALUE then begin
  90.     ReleaseActCtx(ActCtxHandle);
  91.   end;
  92. end;
  93.  
  94. var
  95.   comctllib: HMODULE;
  96.  
  97. function ImageList_Create_V6(CX, CY: Integer; Flags: UINT; Initial, Grow: Integer): HIMAGELIST;
  98. var
  99.   _ImageList_Create: function(CX, CY: Integer; Flags: UINT;
  100.     Initial, Grow: Integer): HIMAGELIST; stdcall;
  101. begin
  102.   with TActivationContext.Create do
  103.     try
  104.       if comctllib=0 then
  105.         comctllib := LoadLibrary('comctl32.dll');
  106.       if comctllib=0 then
  107.         RaiseLastOSError;
  108.       _ImageList_Create := GetProcAddress(comctllib, 'ImageList_Create');
  109.       if not Assigned(_ImageList_Create) then
  110.         RaiseLastOSError;
  111.       Result := _ImageList_Create(CX, CY, Flags, Initial, Grow);
  112.     finally
  113.       Free;
  114.     end;
  115. end;
  116.  
  117. procedure TForm1.Button1Click(Sender: TObject);
  118. var
  119.   IconPath: string;
  120.   IconIndex: Integer;
  121.   hIconLarge, hIconSmall: HICON;
  122. begin
  123.   IconPath := FileName;
  124.   IconIndex := 0; // index can be other than 0
  125.  
  126.   ExtractIconEx(PChar(IconPath), IconIndex, hIconLarge, hIconSmall, 1);
  127.  
  128.   Self.Refresh; // erase form
  129.   DrawIconEx(Canvas.Handle, 10, 10, hIconSmall, 0, 16, 16, 0,
  130.     DI_IMAGE or DI_MASK); // this will draw ok on the form
  131.  
  132.   // ImageList1.DrawingStyle := dsTransparent;
  133.   ImageList1.Handle := {ImageList_Create}ImageList_Create_V6(ImageList1.Width, ImageList1.Height,
  134.     {ILC_COLORDDB} ILC_COLOR32 or ILC_MASK, 0, ImageList1.AllocBy);
  135.   ImageList_AddIcon(ImageList1.Handle, hIconSmall);
  136.  
  137.   MenuItem1.ImageIndex := 0;
  138.  
  139.   DestroyIcon(hIconSmall);
  140.   DestroyIcon(hIconLarge);
  141.  
  142.   PopupMenu1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
  143. end;
  144.  
  145. procedure TForm1.Button2Click(Sender: TObject);
  146. // using sys image-list will work with or without Manifest
  147. type
  148.   DWORD_PTR = DWORD;
  149. var
  150.   ShFileINfo :TShFileInfo;
  151.   SysImageList: DWORD_PTR;
  152.   FileName: string;
  153. begin
  154.   SysImageList := ShGetFileInfo('c:\desktop\SHGetFileInfo function.htm', 0, ShFileInfo, SizeOf(ShFileInfo),
  155.     SHGFI_SYSICONINDEX OR SHGFI_SMALLICON);
  156.  
  157.   if SysImageList = 0 then Exit;
  158.   ImageList1.Handle := SysImageList;
  159.   ImageList1.ShareImages := True;
  160.  
  161.   if ShGetFileInfo(PChar(FileName), 0, ShFileInfo, SizeOf(ShFileInfo),
  162.     SHGFI_SYSICONINDEX OR SHGFI_ICON OR SHGFI_SMALLICON) <> 0 then
  163.   begin
  164.     MenuItem1.ImageIndex := ShFileInfo.IIcon;
  165.     Self.Refresh; // erase form
  166.     DrawIconEx(Canvas.Handle, 10, 10, ShFileInfo.hIcon, 0, 16, 16, 0,
  167.       DI_IMAGE or DI_MASK);
  168.     DestroyIcon(ShFileInfo.hIcon); // todo: do I need to destroy here?
  169.  
  170.     PopupMenu1.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
  171.   end;
  172. end;
  173.  
  174. initialization
  175.   InitialiseActivationContext;
  176.  
  177. finalization
  178.   FinaliseActivationContext;
  179.  
  180. end.
  181.  
  182.  
  183. **Manifest file**
  184.  
  185. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  186. <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  187.   <dependency>
  188.     <dependentAssembly>
  189.       <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0"
  190.         processorArchitecture="*" publicKeyToken="6595b64144ccf1df"/>
  191.     </dependentAssembly>
  192.   </dependency>
  193. </assembly>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement