簡體   English   中英

無法將縮略圖按鈕添加到窗口?

[英]Unable to add thumbnail buttons to a window?

我正在嘗試將縮略圖按鈕添加到窗口,但是沒有錯誤,也沒有顯示縮略圖按鈕。 我閱讀了以下頁面以供參考:

  1. 您的第一個Windows程序 ;

  2. ITaskbarList3 :: ThumbBarAddButtons方法

  3. github上的一些代碼

環境:Win10 64bit,vs2015

// function WindowProc and wWinMain are copied from msdn directly.

#include "stdafx.h"
#include <windows.h>
#include "shobjidl.h" 
#include <exception>


LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

HRESULT addThumbnailButtons(HWND hwnd) {
    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    if (SUCCEEDED(hr)) {
        ITaskbarList4* ptbl = nullptr;
        HRESULT hr = CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER,
            IID_PPV_ARGS(&ptbl));

        if (SUCCEEDED(hr)) {
            // create 2 buttons
            THUMBBUTTON thmb[2] = {};

            thmb[0].dwMask = THB_TOOLTIP;
            thmb[0].iId = 0;
            wcscpy_s(thmb[0].szTip, L"Button 1");

            thmb[1].dwMask = THB_TOOLTIP;
            thmb[1].iId = 1;
            wcscpy_s(thmb[1].szTip, L"Button 2");

            //ptbl->HrInit();
            hr = ptbl->ThumbBarAddButtons(hwnd, ARRAYSIZE(thmb), thmb);

            ptbl->Release();

            return hr;
        }
        else {
            throw std::exception("createInstance failed");
        }
    }else{
        throw std::exception("coinitialize failed");
    }
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[] = L"Sample Window Class";

    WNDCLASS wc = {};

    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

                                        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
    );

    if (hwnd == NULL)
    {
        return 0;
    }

    HRESULT hr = addThumbnailButtons(hwnd);
    if (FAILED(hr)) {
        throw std::exception("addbuttons failed");
    }

    ShowWindow(hwnd, nCmdShow);

    // Run the message loop.

    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);

        FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));

        EndPaint(hwnd, &ps);
    }
    return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

根據ITaskBarList3文檔:

當應用程序顯示窗口時,其任務欄按鈕由系統創建。 當按鈕就位時,任務欄將TaskbarButtonCreated消息發送到窗口。 您的應用程序應調用RegisterWindowMessage(L"TaskbarButtonCreated")並在其RegisterWindowMessage(L"TaskbarButtonCreated")處理該消息。 在調用任何ITaskbarList3方法之前,該消息必須由您的應用程序接收

在調用addThumbnailButtons()之前,您必須等待該消息,例如:

UINT uMsgTaskbarCreated;

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    uMsgTaskbarCreated = RegisterWindowMessage(L"TaskbarButtonCreated");

    // Register the window class.
    const wchar_t CLASS_NAME[] = L"Sample Window Class";

    WNDCLASS wc = {};

    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindowEx(
        0,                              // Optional window styles.
        CLASS_NAME,                     // Window class
        L"Learn to Program Windows",    // Window text
        WS_OVERLAPPEDWINDOW,            // Window style

                                        // Size and position
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // Parent window    
        NULL,       // Menu
        hInstance,  // Instance handle
        NULL        // Additional application data
    );

    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    // Run the message loop.

    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;

        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);

            FillRect(hdc, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1));

            EndPaint(hwnd, &ps);
            return 0;
        }

        default:
            if ((uMsg == uMsgTaskbarCreated) && (uMsgTaskbarCreated != 0))
            {
                HRESULT hr = addThumbnailButtons(hwnd);
                if (FAILED(hr)) {
                    ...;
                }
            }
            break;
        }

    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM