Reading and Writing Icons from ExtractIcon
ID: Q104570
|
The information in this article applies to:
-
Microsoft Windows Software Development Kit (SDK) 3.1
SUMMARY
This article is specifically about device dependent icons. The
ExtractIcon() function does not save the contents of the icon to a
file. The programmer needs to implement this in his or her application.
Below are code samples that allow you to read and write the data to a file
so that DrawIcon() can be used.
MORE INFORMATION
The following is a summary of the steps needed to save the hIcon to a file:
- Get the desired handle to the icon by using ExtractIcon().
- Find the size of the global memory by using GlobalSize().
- Lock the handle of the icon using GlobalLock().
- Open a new file by using OpenFile().
- Write the contents out to the file by using _lwrite().
- Close the file.
// -----------------------------------------------
// Parameters: hIcon - passed in
// Returns: TRUE on success, FALSE on failure
// -----------------------------------------------
int NEAR _pascal SaveMyIcon( HICON hIcon)
{
int fh, i, iResult;
UINT uiSize;
DWORD dwSize;
OFSTRUCT of;
if (!hIcon)
return FALSE;
dwSize = GlobalSize(hIcon);
lpGMem = GlobalLock(hIcon);
fh = OpenFile ("myicon.bin", &of, OF_WRITE | OF_CREATE);
if (fh == -1) // If NOT opened successfully.
{
MessageBox(NULL, "Unable to create file", NULL, MB_OK );
return FALSE;
}
uiSize = _lwrite(fh, (LPSTR)lpGMem, (UINT)dwSize);
_lclose(fh);
if (uiSize == -1 || uiSize < (UINT)dwSize)
{
MessageBox(NULL, "Unable to read file", NULL, MB_OK );
return FALSE;
}
else // Everything worked, return hGMem.
{
return (HICON)hGMem;
}
}
The following is a summary of the steps needed to read the hIcon from
a file:
- Open the file with OpenFile.
- Get the size of the file with _fstat.
- Make a chunk of memory, hMem, for the file by using GlobalAlloc.
- Lock the memory by using GlobalLock.
- Read the file into memory by using _lread.
- Close the file by using _lclose.
- Unlock the memory by using GlobalUnlock.
- Pass the hMem to DrawIcon.
// --------------------------------------
// Read in the binary icon data from disk
// --------------------------------------
HICON NEAR _pascal LoadMyIcon()
{
int iResult, fh;
UINT nBytesRead;
struct _stat buf;
OFSTRUCT ofFileInfo;
nBytesRead = 0;
if ( OpenFile((LPSTR)"myicon.bin",(LPOFSTRUCT)&ofFileInfo,OF_EXIST) != -1
)
{
// Open the file. Its existence has already been checked.
fh = _lopen("myicon.bin", OF_READ);
if (fh == -1) // if NOT opened successfully
return FALSE;
// File the file structure to get the file size.
iResult = _fstat(fh, &buf);
if ( !iResult)
{
hGMem = GlobalAlloc(GHND, (DWORD)buf.st_size);
if (0==hGMem)
{
_lclose(fh);
return(FALSE);
}
// Lock the memory.
if (!(lpGMem = GlobalLock(hGMem)))
{
GlobalFree(hGMem);
_lclose(fh);
return(FALSE);
}
// Seek to the beginning of the file.
_llseek(fh, 0, 0);
nBytesRead = _lread(fh, (LPSTR)lpGMem, buf.st_size );
}
_lclose(fh);
if (nBytesRead == -1 || nBytesRead < buf.st_size)
{
MessageBox(NULL, "Unable to read file", NULL, MB_OK );
return FALSE;
}
else // Everything worked, return hGMem.
{
return (HICON)hGMem;
}
}
else
{
MessageBox( NULL, "File not found", NULL, MB_OK );
return FALSE;
}
}
WARNING: This will work correctly until the user changes his or her
display-driver type to a different resolution, or from mono to color,
or from color to mono.
Additional query words:
no32bit 3.10
Keywords : kb16bitonly
Version : WINDOWS:3.1
Platform : WINDOWS
Issue type :