Skip to content

Update using-icons.md #1906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: docs
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 33 additions & 50 deletions desktop-src/menurc/using-icons.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,15 @@ The following code uses the functions [**CreateIconFromResourceEx**](/windows/de
**Security Warning:** Using [**LoadLibrary**](/windows/desktop/api/libloaderapi/nf-libloaderapi-loadlibrarya) incorrectly can compromise the security of your application by loading the wrong DLL. Refer to the **LoadLibrary** documentation for information on how to correctly load DLLs with different versions of Windows.

```cpp
HICON hIcon1; // icon handle
HINSTANCE hExe; // handle to loaded .EXE file
HRSRC hResource; // handle to FindResource
HRSRC hMem; // handle to LoadResource
BYTE *lpResource; // pointer to resource data
int nID; // ID of resource that best fits current screen

HDC hdc; // handle to display context

// Load the file from which to copy the icon.
HICON hIcon1; // icon handle
HINSTANCE hExe; // handle to loaded .EXE file
HRSRC hResource; // handle to FindResource
HRSRC hMem; // handle to LoadResource
BYTE *lpResource; // pointer to resource data
int nID; // ID of resource that best fits current screen
HDC hdc; // handle to display context

// Load the file from which to copy the icon.
// Note: LoadLibrary should have a fully explicit path.
//
hExe = LoadLibrary("myapp.exe");
Expand All @@ -231,45 +230,29 @@ if (hExe == NULL)
//Error loading module -- fail as securely as possible
return;
}

// Find the icon directory whose identifier is 440.
hResource = FindResource(hExe, MAKEINTRESOURCE(440), RT_GROUP_ICON);

// Load and lock the icon directory.
hMem = LoadResource(hExe, hResource);

lpResource = LockResource(hMem);

// Get the identifier of the icon that is most appropriate for the video display mode.
nID = LookupIconIdFromDirectoryEx((PBYTE) lpResource, TRUE, 0, 0, LR_DEFAULTCOLOR);


// Find the icon directory whose identifier is 440.

hResource = FindResource(hExe,
MAKEINTRESOURCE(440),
RT_GROUP_ICON);

// Load and lock the icon directory.

hMem = LoadResource(hExe, hResource);

lpResource = LockResource(hMem);

// Get the identifier of the icon that is most appropriate
// for the video display.

nID = LookupIconIdFromDirectoryEx((PBYTE) lpResource, TRUE,
CXICON, CYICON, LR_DEFAULTCOLOR);

// Find the bits for the nID icon.

hResource = FindResource(hExe,
MAKEINTRESOURCE(nID),
MAKEINTRESOURCE(RT_ICON));

// Load and lock the icon.

hMem = LoadResource(hExe, hResource);

lpResource = LockResource(hMem);

// Create a handle to the icon.

hIcon1 = CreateIconFromResourceEx((PBYTE) lpResource,
SizeofResource(hExe, hResource), TRUE, 0x00030000,
CXICON, CYICON, LR_DEFAULTCOLOR);

// Draw the icon in the client area.

DrawIcon(hdc, 10, 20, hIcon1);
// Find the bits for the nID icon.
hResource = FindResource(hExe, MAKEINTRESOURCE(nID), MAKEINTRESOURCE(RT_ICON));

// Load and lock the icon.
hMem = LoadResource(hExe, hResource);

lpResource = LockResource(hMem);

// Create a handle to the icon.
hIcon1 = CreateIconFromResourceEx((PBYTE) lpResource, SizeofResource(hExe, hResource), TRUE, 0x00030000, 0, 0, LR_DEFAULTCOLOR);

// Draw the icon in the client area.
DrawIcon(hdc, 10, 20, hIcon1);
```