Jak nakreslit obrázek

Toto téma ukazuje, jak pomocí funkce ImageList_Draw nakreslit obrázek.

Co potřebujete vědět

Technologie

Požadavky

  • C/C++
  • Programování uživatelského rozhraní systému Windows

Instrukce

Pokud chcete nakreslit obrázek, použijte funkci ImageList_Draw nebo ImageList_DrawEx. Určíte popisovač seznamu obrázků, index obrázku k nakreslení, popisovač kontextu cílového zařízení, umístění v rámci kontextu zařízení a jeden nebo více stylů kreslení.

Uživatelem definovaná funkce v následujícím příkladu kódu C++ používá funkci ImageList_Draw k vykreslení obrázku a uložení souřadnic klienta ohraničujícího obdélníku obrázku. Následující funkce používá ohraničující obdélník k určení, zda uživatel kliknul na obrázek.

// DrawTheImage - draws an image transparently and saves 
// the bounding rectangle of the drawn image.
// Returns TRUE if successful, or FALSE otherwise. 
// hwnd - handle to the window in which to draw the image. 
// himl - handle to the image list that contains the image. 
// cx and cy - client coordinates for the upper-left corner of the image. 
// 
// Global variables and constants 
//     g_nImage - index of the image to draw. 
//     g_rcImage - bounding rectangle of the image. 
//     CX_IMAGE and CY_IMAGE - width and height of the image. 
extern int g_nImage; 
extern RECT g_rcImage; 
 
#define CX_IMAGE 32 
#define CY_IMAGE 32 
 
BOOL DrawTheImage(HWND hwnd, HIMAGELIST himl, int cx, int cy) 
{ 
    HDC hdc; 
 
    if ((hdc = GetDC(hwnd)) == NULL) 
        return FALSE; 
    if (!ImageList_Draw(himl, g_nImage, hdc, cx, cy, ILD_TRANSPARENT)) 
        return FALSE; 
    ReleaseDC(hwnd, hdc); 
 
    SetRect(&g_rcImage, cx, cy, CX_IMAGE + cx, CY_IMAGE + cy); 
 
    return TRUE; 
} 

Reference seznamů obrázků

O Seznamech Obrázků

Použití seznamů obrázků