【问题标题】:Is it possible to 'Pin to start menu' using Inno Setup?是否可以使用 Inno Setup 来“固定到开始菜单”?
【发布时间】:2009-10-27 08:22:15
【问题描述】:

我正在使用出色的 Inno Setup 安装程序,我注意到一些应用程序(通常来自 Microsoft)在安装时其启动图标在开始菜单(在 Windows 7 中)中已经非常明显(“固定?”)。我是否完全依赖最近使用的算法让我的图标在开始菜单中变“大”,或者有没有办法从安装程序中推广我的应用程序?

【问题讨论】:

    标签: scripting windows-7 installation inno-setup startmenu


    【解决方案1】:

    可以固定程序,但不是正式的。根据this thread 中发布的代码(使用与@Mark Redman 链接的文章中描述的相同方式),我编写了以下代码:

    [Code]
    #ifdef UNICODE
      #define AW "W"
    #else
      #define AW "A"
    #endif
    
    const
      // these constants are not defined in Windows
      SHELL32_STRING_ID_PIN_TO_TASKBAR = 5386;
      SHELL32_STRING_ID_PIN_TO_STARTMENU = 5381;
      SHELL32_STRING_ID_UNPIN_FROM_TASKBAR = 5387;
      SHELL32_STRING_ID_UNPIN_FROM_STARTMENU = 5382;
    
    type
      HINSTANCE = THandle;
      HMODULE = HINSTANCE;
    
      TPinDest = (
        pdTaskbar,
        pdStartMenu
      );
    
    function LoadLibrary(lpFileName: string): HMODULE;
      external 'LoadLibrary{#AW}@kernel32.dll stdcall';
    function FreeLibrary(hModule: HMODULE): BOOL;
      external 'FreeLibrary@kernel32.dll stdcall';
    function LoadString(hInstance: HINSTANCE; uID: UINT;
      lpBuffer: string; nBufferMax: Integer): Integer;
      external 'LoadString{#AW}@user32.dll stdcall';
    
    function TryGetVerbName(ID: UINT; out VerbName: string): Boolean;
    var
      Buffer: string;
      BufLen: Integer;
      Handle: HMODULE;
    begin
      Result := False;
    
      Handle := LoadLibrary(ExpandConstant('{sys}\Shell32.dll'));
      if Handle <> 0 then
      try
        SetLength(Buffer, 255);
        BufLen := LoadString(Handle, ID, Buffer, Length(Buffer));
    
        if BufLen <> 0 then
        begin
          Result := True;
          VerbName := Copy(Buffer, 1, BufLen);
        end;
      finally
        FreeLibrary(Handle);
      end;
    end;
    
    function ExecVerb(const FileName, VerbName: string): Boolean;
    var
      I: Integer;
      Shell: Variant;
      Folder: Variant;
      FolderItem: Variant;
    begin
      Result := False;
    
      Shell := CreateOleObject('Shell.Application');
      Folder := Shell.NameSpace(ExtractFilePath(FileName));
      FolderItem := Folder.ParseName(ExtractFileName(FileName));
    
      for I := 1 to FolderItem.Verbs.Count do
      begin
        if FolderItem.Verbs.Item(I).Name = VerbName then
        begin
          FolderItem.Verbs.Item(I).DoIt;
          Result := True;
          Exit;
        end;
      end;  
    end;
    
    function PinAppTo(const FileName: string; PinDest: TPinDest): Boolean;
    var
      ResStrID: UINT;
      VerbName: string;
    begin
      case PinDest of
        pdTaskbar: ResStrID := SHELL32_STRING_ID_PIN_TO_TASKBAR;
        pdStartMenu: ResStrID := SHELL32_STRING_ID_PIN_TO_STARTMENU;
      end;
      Result := TryGetVerbName(ResStrID, VerbName) and ExecVerb(FileName, VerbName);
    end;
    
    function UnpinAppFrom(const FileName: string; PinDest: TPinDest): Boolean;
    var
      ResStrID: UINT;
      VerbName: string;
    begin
      case PinDest of
        pdTaskbar: ResStrID := SHELL32_STRING_ID_UNPIN_FROM_TASKBAR;
        pdStartMenu: ResStrID := SHELL32_STRING_ID_UNPIN_FROM_STARTMENU;
      end;
      Result := TryGetVerbName(ResStrID, VerbName) and ExecVerb(FileName, VerbName);
    end;
    

    上面的代码首先从Shell32.dll库的字符串表中读取用于固定或取消固定应用程序的菜单项的标题。然后连接到 Windows Shell,并连接到目标应用程序。 path 创建Folder 对象,然后获得FolderItem 对象,并在此对象上迭代所有可用的动词并检查它们的名称是否与从Shell32.dll 库字符串表中读取的名称匹配。如果是这样,它会通过调用DoIt 方法来调用动词项操作并退出迭代。

    这是上述代码的一种可能用法,用于固定:

    if PinAppTo(ExpandConstant('{sys}\calc.exe'), pdTaskbar) then
      MsgBox('Calc has been pinned to the taskbar.', mbInformation, MB_OK);
    if PinAppTo(ExpandConstant('{sys}\calc.exe'), pdStartMenu) then
      MsgBox('Calc has been pinned to the start menu.', mbInformation, MB_OK);
    

    对于取消固定:

    if UnpinAppFrom(ExpandConstant('{sys}\calc.exe'), pdTaskbar) then
      MsgBox('Calc is not pinned to the taskbar anymore.', mbInformation, MB_OK);
    if UnpinAppFrom(ExpandConstant('{sys}\calc.exe'), pdStartMenu) then
      MsgBox('Calc is not pinned to the start menu anymore.', mbInformation, MB_OK);
    

    请注意,尽管此代码在 Windows 7 上工作(并且任务栏固定也可以在我测试过的 Windows 8.1 上),但它确实是一种 hacky 方式,因为没有官方方法可以以编程方式将程序固定到任务栏,也开始菜单。那是用户应该根据自己的选择去做的事情。

    【讨论】:

    • 天啊,你花了多少小时才弄清楚这一点?非常感谢!
    • @tmighty,我很高兴它帮助了某人!我花了不到一个小时;它只是来自链接线程的重构代码:-)
    • 谢谢! :-) 你能告诉我你执行代码的首选方式吗?我的意思是“如果 PinAppTo(ExpandConstant('{sys}\calc.exe'), pdTaskbar) 那么......”。你在哪里做?您是将其定义为任务还是自动执行此代码?我之前从未运行过代码,除了安装我使用 [Run] "Filename: "{tmp}\vcredist_x86.exe"; Parameters: "/q"; Check: VCRedistNeedsInstall 运行的 vcredist_x86 运行时。这只是我的一个 exe必须运行,而不是像你这样的功能。
    • @tmighty,我从来没有被要求部署一个将自己固定在某个地方的应用程序。这就是用户首先应该自己做的事情。但是如果有这样的要求,我认为任务就足够公平了(因为任务通常用于确认创建图标)。用于执行I would use post install step.
    • @tmighty,你可以使用this helper function。如果您运行的是 Windows Vista 或更高版本,则返回 True,否则返回 False。
    【解决方案2】:

    no programmatic way 将内容固定到任务栏/开始菜单是有原因的。以我的经验,我看到了开始菜单highlight newly-created shortcuts,它就是专门为处理这种情况而设计的。当您在开始菜单上看到新安装的程序时,可能是因为该算法,而不是因为安装程序将它放在那里。

    也就是说,如果一个新的快捷方式没有突出显示,这可能是因为安装程序提取了一个预先存在的快捷方式并在其上保留了旧的时间戳,而不是使用 API 函数来创建开始菜单中的快捷方式。

    【讨论】:

      【解决方案3】:

      【讨论】:

      • 如那篇文章所述,没有用于直接固定的 API 是有原因的。请不要向桌面/任务栏/开始菜单/等发送垃圾邮件。
      猜你喜欢
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      • 2014-07-26
      • 1970-01-01
      • 1970-01-01
      • 2012-04-11
      • 1970-01-01
      相关资源
      最近更新 更多