Menggunakan Properti Jendela
Bagian ini menjelaskan cara melakukan tugas berikut yang terkait dengan properti jendela.
- Menambahkan Properti Jendela
- Mengambil Properti Jendela
- Properti Jendela Daftar untuk jendela tertentu
- Menghapus Properti Jendela
Menambahkan Properti Jendela
Contoh berikut memuat ikon lalu kursor dan mengalokasikan memori untuk buffer. Contoh kemudian menggunakan fungsiSetPropuntuk menetapkan ikon, kursor, dan handel memori yang dihasilkan sebagai properti jendela untuk jendela yang diidentifikasi oleh variabel hwndSubclass yang ditentukan aplikasi. Properti diidentifikasi oleh string PROP_ICON, PROP_CURSOR, dan PROP_BUFFER.
#define BUFFER 4096
HINSTANCE hinst; // handle of current instance
HWND hwndSubclass; // handle of a subclassed window
HANDLE hIcon, hCursor;
HGLOBAL hMem;
char *lpMem;
TCHAR tchPath[] = "c:\\winnt\\samples\\winprop.c";
HRESULT hResult;
// Load resources.
hIcon = LoadIcon(hinst, MAKEINTRESOURCE(400));
hCursor = LoadCursor(hinst, MAKEINTRESOURCE(220));
// Allocate and fill a memory buffer.
hMem = GlobalAlloc(GPTR, BUFFER);
lpMem = GlobalLock(hMem);
if (lpMem == NULL)
{
// TODO: write error handler
}
hResult = StringCchCopy(lpMem, STRSAFE_MAX_CCH, tchPath);
if (FAILED(hResult))
{
// TO DO: write error handler if function fails.
}
GlobalUnlock(hMem);
// Set the window properties for hwndSubclass.
SetProp(hwndSubclass, "PROP_ICON", hIcon);
SetProp(hwndSubclass, "PROP_CURSOR", hCursor);
SetProp(hwndSubclass, "PROP_BUFFER", hMem);
Memperoleh Properti Jendela
Jendela dapat membuat handel ke data properti jendelanya dan menggunakan data untuk tujuan apa pun. Contoh berikut menggunakan GetProp untuk mendapatkan handle ke properti jendela yang diidentifikasi oleh PROP_ICON, PROP_CURSOR, dan PROP_BUFFER. Contoh kemudian menampilkan konten buffer memori, kursor, dan ikon yang baru diperoleh di area klien jendela.
#define PATHLENGTH 256
HWND hwndSubclass; // handle of a subclassed window
HANDLE hIconProp, hCursProp;
HGLOBAL hMemProp;
char *lpFilename;
TCHAR tchBuffer[PATHLENGTH];
size_t * nSize;
HDC hdc;
HRESULT hResult;
// Get the window properties, then use the data.
hIconProp = (HICON) GetProp(hwndSubclass, "PROP_ICON");
TextOut(hdc, 10, 40, "PROP_ICON", 9);
DrawIcon(hdc, 90, 40, hIconProp);
hCursProp = (HCURSOR) GetProp(hwndSubclass, "PROP_CURSOR");
TextOut(hdc, 10, 85, "PROP_CURSOR", 9);
DrawIcon(hdc, 110, 85, hCursProp);
hMemProp = (HGLOBAL) GetProp(hwndSubclass, "PROP_BUFFER");
lpFilename = GlobalLock(hMemProp);
hResult = StringCchPrintf(tchBuffer, PATHLENGTH,
"Path to file: %s", lpFilename);
if (FAILED(hResult))
{
// TODO: write error handler if function fails.
}
hResult = StringCchLength(tchBuffer, PATHLENGTH, nSize)
if (FAILED(hResult))
{
// TODO: write error handler if function fails.
}
TextOut(hdc, 10, 10, tchBuffer, *nSize);
Mencantumkan Properti Jendela untuk Jendela Tertentu
Dalam contoh berikut, fungsi EnumPropsEx mencantumkan pengidentifikasi string properti jendela untuk jendela yang diidentifikasi oleh variabel hwndSubclass yang ditentukan aplikasi. Fungsi ini bergantung pada fungsi panggilan balik yang ditentukan aplikasi WinPropProc untuk menampilkan string di area klien jendela.
EnumPropsEx(hwndSubclass, WinPropProc, NULL);
// WinPropProc is an application-defined callback function
// that lists a window property.
BOOL CALLBACK WinPropProc(
HWND hwndSubclass, // handle of window with property
LPCSTR lpszString, // property string or atom
HANDLE hData) // data handle
{
static int nProp = 1; // property counter
TCHAR tchBuffer[BUFFER]; // expanded-string buffer
size_t * nSize; // size of string in buffer
HDC hdc; // device-context handle
HRESULT hResult;
hdc = GetDC(hwndSubclass);
// Display window property string in client area.
hResult = StringCchPrintf(tchBuffer, BUFFER, "WinProp %d: %s", nProp++, lpszString);
if (FAILED(hResult))
{
// TO DO: write error handler if function fails.
}
hResult = StringCchLength(tchBuffer, BUFFER, nSize);
if (FAILED(hResult))
{
// TO DO: write error handler if function fails.
}
TextOut(hdc, 10, nProp * 20, tchBuffer, *nSize);
ReleaseDC(hwndSubclass, hdc);
return TRUE;
}
Menghapus Properti Jendela
Ketika jendela dihancurkan, ia harus menghapus properti apa pun yang telah ditetapkannya. Contoh berikut menggunakan fungsiEnumPropsEx dan fungsi panggilan balik yang ditentukan aplikasi DelPropProc untuk menghancurkan properti yang terkait dengan jendela yang diidentifikasi oleh variabel hwndSubclass yang ditentukan aplikasi. Fungsi panggilan balik, yang menggunakan fungsi RemoveProp, juga ditampilkan.
case WM_DESTROY:
EnumPropsEx(hwndSubclass, DelPropProc, NULL);
PostQuitMessage(0);
break;
// DelPropProc is an application-defined callback function
// that deletes a window property.
BOOL CALLBACK DelPropProc(
HWND hwndSubclass, // handle of window with property
LPCSTR lpszString, // property string or atom
HANDLE hData) // data handle
{
hData = RemoveProp(hwndSubclass, lpszString);
//
// if appropriate, free the handle hData
//
return TRUE;
}