Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

delphi - LoadIconWithScaleDown always fails

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

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

1 Reply

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...