LoadIconWithScaleDown总是失败 [英] LoadIconWithScaleDown always fails

查看:131
本文介绍了LoadIconWithScaleDown总是失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LoadIconWithScaleDown API.我使用的是Delphi 2007,我编写了一个简单的示例程序,在单击该按钮时,我调用COMMCTRL.LoadIconWithScaleDown.我尝试了各种组合,其中实例0为零,实例设置为hInstance,对于第二个参数,我尝试传递当前模块名称MAKEINTRESOURCE(IDI_APPLICATION),...永远没有运气,我总是得到-2147467263的返回值.知道我在做什么错吗?

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?

根据David的建议进行了编辑,以显示我如何调用该API.

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;

推荐答案

这很好地说明了为什么不阅读和理解文档就应该使用WinAPI函数的原因.

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

LoadIconWithScaleDown清楚地说明了参数是什么以及如何使用它们.使用 GetModuleFileName 的理由为零,无论如何,传递参数的地方还是错误的,文档中明确指出.

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.

以下是同时使用该函数的示例,首先从外部磁盘文件加载图标,然后从应用程序中的图标资源加载.只要文件或资源存在于您使用的地方,它就会在Delphi 10 Seattle下进行编译和测试,并且可以工作.

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:\Images\SomeFile.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;

(而且不,问题不在于您不需要先调用 InitCommonControlsEx .包括CommCtrl为您执行必要的初始化.)

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

这篇关于LoadIconWithScaleDown总是失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆