Cara Menggambar Gambar

Topik ini menunjukkan cara menggunakan fungsi ImageList_Draw untuk menggambar gambar.

Apa yang perlu Anda ketahui

Teknologi

Prasyarat

  • C/C++
  • Pemrograman Antarmuka Pengguna Windows

Peraturan

Untuk menggambar gambar, Anda menggunakan fungsi ImageList_Draw atau ImageList_DrawEx. Anda menentukan handle ke daftar citra, indeks gambar yang akan digambar, handle ke konteks perangkat yang dituju, posisi dalam konteks perangkat, dan satu atau beberapa gaya gambar.

Fungsi yang ditentukan pengguna dalam contoh kode C++ berikut menggunakan fungsi ImageList_Draw untuk menggambar gambar dan menyimpan koordinat klien dari persegi panjang pembatas gambar. Fungsi selanjutnya menggunakan kotak pembatas untuk menentukan apakah pengguna telah mengklik pada gambar.

// 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; 
} 

Referensi Daftar Gambar

Tentang Daftar Gambar

Menggunakan Daftar Gambar