Partager via


Méthode IProvideWinSATVisuals ::get_Bitmap (winsatcominterfacei.h)

[IProvideWinSATVisuals ::get_Bitmap peut être modifié ou indisponible pour les versions après Windows 8.1.]

Récupère une bitmap pour le score de base WinSAT.

Syntaxe

HRESULT get_Bitmap(
  [in]  WINSAT_BITMAP_SIZE      bitmapSize,
  [in]  WINSAT_ASSESSMENT_STATE state,
  [in]  float                   rating,
  [out] HBITMAP                 *pBitmap
);

Paramètres

[in] bitmapSize

Détermine la taille de l’image bitmap retournée par cette méthode. Pour connaître les valeurs possibles, consultez l’énumération WINSAT_BITMAP_SIZE .

[in] state

État de l’évaluation. Pour obtenir cette valeur, appelez la méthode IProvideWinSATResultsInfo ::get_AssessmentState .

[in] rating

Score de base de l’ordinateur. Pour obtenir cette valeur, appelez la méthode IProvideWinSATResultsInfo ::get_SystemRating .

[out] pBitmap

Handle de la bitmap. Pour libérer le handle lorsque vous avez terminé, appelez la fonction DeleteObject .

Valeur retournée

Cette méthode peut retourner l’une de ces valeurs.

Le tableau suivant répertorie certaines des valeurs HRESULT que cette méthode retourne.

Code/valeur de retour Description
S_OK
La bitmap a été récupérée avec succès.
WINSAT_ERROR_FAILEDTOLOADRESOURCE
0x80040016
La valeur d’évaluation n’est pas valide. Les valeurs valides sont 1.0 à 9.9.

Remarques

La bitmap est retournée uniquement pour les états WINSAT_ASSESSMENT_STATE_VALID et WINSAT_ASSESSMENT_STATE_INCOHERENT_WITH_HARDWARE. La méthode réussit (la valeur de retour est S_OK) si vous passez une autre valeur d’état, mais le paramètre pBitmap est NULL.

Exemples

L’exemple suivant montre comment récupérer une bitmap qui représente le score de base de l’évaluation. L’exemple utilise la classe MOF WMI Win32_WinSAT pour obtenir l’état et le score de base que vous passez à cette méthode.

#include <windows.h>
#include <comutil.h>
#include <commctrl.h>
#include <wbemidl.h>
#include <winsatcominterfacei.h>

#pragma comment(lib, "comsupp.lib") // For _bstr_t
#pragma comment(lib, "comctl32.lib") // For common controls
#pragma comment(lib, "gdi32.lib")

// The WQL query used to retrieve an instance of the Win32_WinSAT class.
#define WINSAT_QUERY_STRING  L"Select * \
From Win32_WinSAT \
Where TimeTaken = 'MostRecentAssessment'"

HRESULT GetBaseScore(float* pBaseScore, WINSAT_ASSESSMENT_STATE* pState);
HRESULT GetScoreBitmap(const float BaseScore, const WINSAT_ASSESSMENT_STATE State, HBITMAP* phbitmap);
LRESULT CALLBACK MonitorWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);

// Global variables
HINSTANCE g_hinst;
HWND g_hwndStatus;
HBITMAP g_hScoreBitmap;

#define IDC_MONITOR        100

int WINAPI WinMain(HINSTANCE hinst,
    HINSTANCE hinstPrev,
    LPSTR pszCmdLine,
    int nCmdShow)
{
    WNDCLASSEX wc;
    ATOM atom;
    HWND hwnd;
    MSG msg;

    g_hinst = hinst;

    ZeroMemory(&wc, sizeof(wc));
    wc.cbSize = sizeof(wc);
    wc.lpfnWndProc = MonitorWndProc;
    wc.hInstance = hinst;
    wc.lpszClassName = TEXT("MainWClass");
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);

    atom = RegisterClassEx(&wc);
    hwnd = CreateWindowEx(0,
        TEXT("MainWClass"),
        TEXT("Experience Index Base Score"),
        WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
        CW_USEDEFAULT, CW_USEDEFAULT, 
        390, 165, 
        NULL,
        NULL,
        hinst,
        NULL);

    if (NULL == hwnd)
    {
        return (int)GetLastError();
    }

    ShowWindow(hwnd, nCmdShow);

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

    return (int)msg.wParam;
}


LRESULT CALLBACK MonitorWndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    HRESULT  hr = S_OK;

    switch(msg)
    {
        case WM_CREATE:
        {
            INITCOMMONCONTROLSEX initctrls;
            RECT rc;
            float BaseScore = 0.0;
            WINSAT_ASSESSMENT_STATE AssessmentState;

            SetWindowText(hwnd, TEXT("Experience Index Base Score"));

            initctrls.dwSize = sizeof(initctrls);
            initctrls.dwICC = ICC_BAR_CLASSES;
            InitCommonControlsEx(&initctrls);

            GetClientRect(hwnd, &rc);

            g_hwndStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
                WS_CHILD | WS_BORDER | WS_VISIBLE,
                rc.left, rc.bottom - 20, rc.right, 20,
                hwnd, (HMENU) IDC_MONITOR, g_hinst, NULL);
                
            if (NULL == g_hwndStatus)
                return (LRESULT) NULL;

            hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

            // Get the base score and the state of the assessment.
            if (FAILED(hr = GetBaseScore(&BaseScore, &AssessmentState)))
            {
                SetWindowText(g_hwndStatus, TEXT("Failed to get base score and state"));
            }

            // Get the bitmap for the specified score and state values.
            if (FAILED(hr = GetScoreBitmap(BaseScore, AssessmentState, &g_hScoreBitmap)) || NULL == g_hScoreBitmap)
            {
                SetWindowText(g_hwndStatus, TEXT("Failed to get base score bitmap"));
            }

            break;
        }

        case WM_PAINT:
            PAINTSTRUCT ps;
            HDC hdcWin;
            HDC hdcCompatible;
            HGDIOBJ bmpOld;
            BITMAP bitmap;

            // Write the bitmap to the window.
            if (g_hScoreBitmap)
            {
                hdcWin = BeginPaint(hwnd, &ps);
                hdcCompatible = CreateCompatibleDC(hdcWin);
                bmpOld = SelectObject(hdcCompatible, g_hScoreBitmap);
                GetObject(g_hScoreBitmap, sizeof(BITMAP), &bitmap);
                BitBlt(hdcWin, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcCompatible, 0, 0, SRCCOPY);
                SelectObject(hdcCompatible, bmpOld);
                DeleteDC(hdcCompatible);
                EndPaint(hwnd, &ps);
            }

            break;

        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;

        case WM_DESTROY:
            CoUninitialize();
            DeleteObject(g_hScoreBitmap);
            PostQuitMessage(0);
            break;

        default:
            return DefWindowProc(hwnd, msg, wparam, lparam);
    }

    return 0;
}

// Use the Win32_WinSAT WMI class to retrieve the base score and
// state of the most recent assessment.
HRESULT GetBaseScore(float* pBaseScore, WINSAT_ASSESSMENT_STATE* pState)
{
    HRESULT hr = S_OK;
    IWbemServices* pServices = NULL;
    IWbemLocator* pLocator = NULL;
    IEnumWbemClassObject* pEnum = NULL;
    IWbemClassObject* pWinSAT = NULL;
    _variant_t vBaseScore;
    _variant_t vState;
    ULONG returned = 0;

    // WMI code to retrieve the Win32_WinSAT instance.
    hr = CoCreateInstance(__uuidof(WbemLocator),
        NULL,
        CLSCTX_INPROC_SERVER,
        __uuidof(IWbemLocator),
        (void**)&pLocator);

    if (FAILED(hr))
    {
        // Handle error
        goto cleanup;
    }

    hr = pLocator->ConnectServer(_bstr_t(L"root\\cimv2"), 
        NULL, NULL, NULL, 0L, NULL, NULL,
        &pServices);

    if (FAILED(hr))
    {
        // Handle error
        goto cleanup;
    }
    
    hr = pServices->ExecQuery(_bstr_t(L"WQL"),
        _bstr_t(WINSAT_QUERY_STRING),
        WBEM_FLAG_USE_AMENDED_QUALIFIERS | WBEM_FLAG_FORWARD_ONLY,
        NULL,
        &pEnum);

    if (FAILED(hr))
    {
        // Handle error
        goto cleanup;
    }
    
    // The query will return only one instance. 
    hr = pEnum->Next(WBEM_INFINITE, 1, &pWinSAT, &returned);
    if (FAILED(hr))
    {
        // Handle error
        goto cleanup;
    }
    
    hr = pWinSAT->Get(L"WinSPRLevel", 0, &vBaseScore, NULL, NULL);
    if (FAILED(hr))
    {
        // Handle error
        goto cleanup;
    }

    *pBaseScore = vBaseScore.fltVal;

    hr = pWinSAT->Get(L"WinSATAssessmentState", 0, &vState, NULL, NULL);
    if (FAILED(hr))
    {
        // Handle error
        goto cleanup;
    }

    *pState = (WINSAT_ASSESSMENT_STATE)vState.lVal;

cleanup:

    if (pLocator)
        pLocator->Release();

    if (pServices)
        pServices->Release();

    if (pEnum)
        pEnum->Release();

    if (pWinSAT)
        pWinSAT->Release();

    return hr;
}


HRESULT GetScoreBitmap(const float BaseScore, const WINSAT_ASSESSMENT_STATE State, HBITMAP* phbitmap)
{
    HRESULT hr = S_OK;
    IProvideWinSATVisuals* pVisuals = NULL;

    // Code to retrieve the bitmap from WinSAT. Get the bitmap
    // only if the state is one of the following states.
    if (WINSAT_ASSESSMENT_STATE_VALID == State ||
        WINSAT_ASSESSMENT_STATE_INCOHERENT_WITH_HARDWARE == State)
    {
        hr = CoCreateInstance(__uuidof(CProvideWinSATVisuals),
            NULL,
            CLSCTX_INPROC_SERVER,
            __uuidof(IProvideWinSATVisuals),
            (void**)&pVisuals);

        if (FAILED(hr))
        {
            // Handle error
            goto cleanup;
        }

        hr = pVisuals->get_Bitmap(WINSAT_BITMAP_SIZE_NORMAL, 
            State, 
            BaseScore, 
            phbitmap);

        if (FAILED(hr))
        {
            // Handle error
            goto cleanup;
        }
    }

cleanup:

    if (pVisuals)
        pVisuals->Release();

    return hr;
}

Configuration requise

Condition requise Valeur
Client minimal pris en charge Windows Vista [applications de bureau uniquement]
Serveur minimal pris en charge Aucun pris en charge
Plateforme cible Windows
En-tête winsatcominterfacei.h
DLL Winsatapi.dll

Voir aussi

IProvideWinSATVisuals