Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to play with the LoadIconWithScaleDown API. I am using Delphi 2007, I wrote a simple sample program where upon a button click I call COMMCTRL.LoadIconWithScaleDown. I tried various combinations, with instance zero, with instance set to hInstance, for the second parameter, I tried to pass the current module name, MAKEINTRESOURCE( IDI_APPLICATION), ... Always no luck, I always get a return value of -2147467263. Any idea what I am doing wrong?

Edited upon David's suggestion to show how I tried to call the API.

procedure TForm31.Button1Click(Sender: TObject);

  var moduleName  : string;
  var moduleNameW : widestring;

  var retVal      : HRESULT;
  var iconHandle  : HICON;

begin
  iconHandle := 0;

  SetLength( moduleName, 1024);

  WINDOWS.GetModuleFileName(
              hInstance, 
              PCHAR(moduleName), 
              LENGTH(moduleName));

  moduleNameW := moduleName;

  retVal := COMMCTRL.LoadIconWithScaleDown(
                         HINSTANCE,
                         PWidechar(moduleNameW),
                         image1.width,
                         image1.height,
                         iconHandle);   
end;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
236 views
Welcome To Ask or Share your Answers For Others

1 Answer

This an excellent demonstration of why you should NOT use WinAPI functions without reading and understanding the documentation.

The documentation for LoadIconWithScaleDown clearly explains what the parameters are and how to use them. There is zero reason to use GetModuleFileName, and the parameter where you're passing it in is wrong anyway, which the documentation clearly states.

Here are examples for using the function both ways, first to load an icon from an external disk file and then to load from an icon resource in your application. It was compiled and tested under Delphi 10 Seattle and works, provided the file or resource exist where you're using it.

uses
  CommCtrl;

var
  hIco: HICON;
  Ico: TIcon;
  NewWidth, NewHeight: Integer;
begin
  NewWidth := 16;
  NewHeight := 16;
  if Succeeded(LoadIconWithScaleDown(0,
                                     'C:ImagesSomeFile.ico',
                                     NewWidth, NewHeight, hIco)) then
  begin
    Ico := TIcon.Create;
    Ico.Handle := hIco;
    // Do whatever with the icon. Clean up is left to you
  end;

  if Succeeded(LoadIconWithScaleDown(hInstance,
                                     'MYRESOURCENAME',
                                     NewWidth, NewHeight, hIco)) then
  begin
    // See code above
  end;
end;

(And no, the issue was not that you needed to call InitCommonControlsEx first. Including CommCtrl does the necessary initialization for you.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...