【问题标题】:Control path to pinned application to taskbar with Qt使用 Qt 将应用程序固定到任务栏的控制路径
【发布时间】:2021-09-27 16:10:56
【问题描述】:

如何使用 Qt 控制将应用程序固定到任务栏的路径

【问题讨论】:

  • 请不要在您的问题中发布答案。如果您想分享您的解决方案,请将其作为专用答案提交。
  • 首先,问一个问题,然后自己回答,并检查它的绿色复选标记。

标签: c++ qt winapi


【解决方案1】:

由于我自己一直在努力寻找一种方法来自定义固定到任务栏的应用程序的程序路径,因此我想在这里发布我与 Qt 相关的解决方案。

问题是,我在固定主程序时将启动程序链接到一组程序(启动程序/更新程序、主程序)中。

EventFilterPinnable.h

#pragma once

#include <QAbstractNativeEventFilter>

#include <windows.h>
#include <windowsx.h>
#include <shellapi.h>
#include <propsys.h>
#include <propkey.h>
#include <propvarutil.h>

HRESULT PropertyStoreSetStringValue(IPropertyStore* propertyStore, REFPROPERTYKEY pkey, PCWSTR value)
{
    PROPVARIANT propVariant;
    HRESULT hr = InitPropVariantFromString(value, &propVariant);
    if(SUCCEEDED(hr))
    {
        hr = propertyStore->SetValue(pkey, propVariant);
        PropVariantClear(&propVariant);
    }
    return hr;
}

HRESULT MakeWindowPinnable(HWND hwnd, PCWSTR userModelId, PCWSTR relaunchCommand, PCWSTR relaunchDisplayName, PCWSTR relaunchIcon = NULL)
{
    IPropertyStore* propertyStore;
    HRESULT hr = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&propertyStore));
    if(SUCCEEDED(hr))
    {
        PropertyStoreSetStringValue(propertyStore, PKEY_AppUserModel_ID, userModelId);
        PropertyStoreSetStringValue(propertyStore, PKEY_AppUserModel_RelaunchCommand, relaunchCommand);
        PropertyStoreSetStringValue(propertyStore, PKEY_AppUserModel_RelaunchDisplayNameResource, relaunchDisplayName);

        if(relaunchIcon != NULL)
        {
            PropertyStoreSetStringValue(propertyStore, PKEY_AppUserModel_RelaunchIconResource, relaunchIcon);
        }

        propertyStore->Release();
    }
    return hr;
}

void OnCreate(HWND hwnd)
{
    std::wstring sAppId = L"unique.app.id";
    std::wstring sAppRelaunchCommand = L"\"C:\\Program Files\\Path with spaces\\Starter.exe\" -a argument1";
    std::wstring sAppRelaunchDisplayName = L"Name of progam";
    std::wstring sAppRelaunchIcon = L"\"C:\\Program Files\\Path with spaces\\Icon.ico\"";

    MakeWindowPinnable(hwnd, sAppId.c_str(), sAppRelaunchCommand.c_str(), sAppRelaunchDisplayName.c_str(), sAppRelaunchIcon.c_str());
}


class EventFilterPinnable : public QAbstractNativeEventFilter
{
public:
    EventFilterPinnable() {}

    bool nativeEventFilter(const QByteArray& eventType, void* message, long* /*res*/) override
    {
        if(eventType == "windows_generic_MSG") {
            MSG* msg = static_cast<MSG*>(message);
            HWND hwnd = msg->hwnd;

            if (msg->message==WM_CREATE)
            {
                OnCreate(hwnd);
            }
        }
        return false;
    }
};

在 main.cpp 中简单地添加

QApplication app(argc, argv);    
app.installNativeEventFilter(new EventFilterPinnable());
....
app.exec();

【讨论】:

  • 但是switch(msg-&gt;message) { HANDLE_MSG(hwnd, WM_CREATE, OnCreate); } 是什么(tf)?
  • 如果你需要,你也可以放更多的消息开关......但我只需要处理 WM_CREATE #define HANDLE_MSG(hwnd, message, fn) \ case (message): return HANDLE_##message( (hwnd), (wParam), (lParam), (fn))
  • 但您还有其他消息
  • 处理程序中return TRUE; 的用途是什么?看 。这是非常糟糕的方法
  • 我还是看不到HANDLE_WM_CREATE是什么
猜你喜欢
  • 2010-12-22
  • 2012-03-09
  • 1970-01-01
  • 1970-01-01
  • 2010-11-30
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
  • 2015-10-21
相关资源
最近更新 更多