Parameters and usage of LoadImage in Windows programming
The LoadImage function is a function in the Windows API used for loading image resources.
The parameters and usage of the LoadImage function are as follows:
- hInst (HINSTANCE type): specifies the handle of the module containing image resources. Typically obtained by using GetModuleHandle(NULL) to get the handle of the current application.
- lpszName (of type LPCTSTR): specifies the name of the image resource. It can be an integer resource ID (such as MAKEINTRESOURCE(IDB_BITMAP1)), or a pointer to a string containing the resource name.
- uType (UINT type): Specifies the type of image resource. It can be one of the following values:
- Load bitmap (.bmp) files using IMAGE_BITMAP.
- IMAGE_ICON: Used to load icon (.ico) files.
- IMAGE_CURSOR: Used to load cursor (.cur) files.
- cxDesired (integer type): Specifies the desired width of the image after loading. If set to 0, the original image size will be loaded.
- Desired (integer type): Specifies the height of the image after loading. If set to 0, the original image size will be loaded.
- fuLoad (of UINT type): Specifies the way in which the image is loaded. It can be one of the following values:
- LR_DEFAULTCOLOR: Default color will be used.
- Specify the lpszName parameter as a file path instead of a resource name for LR_LOADFROMFILE.
- LR_LOADTRANSPARENT: Load transparent image.
- LR_SHARED means that the loaded image can be shared with other threads.
- LR_CREATEDIBSECTION: Create a DIB (Device-Independent Bitmap) section.
- Return value (of type HANDLE): If the loading is successful, an valid handle is returned; if loading fails, NULL is returned.
After calling the LoadImage function, you can use the returned handle for subsequent operations, such as drawing on a device or setting as an image for a control.
The example code is shown below:
HINSTANCE hInst = GetModuleHandle(NULL);
HBITMAP hBitmap = (HBITMAP)LoadImage(hInst, MAKEINTRESOURCE(IDB_BITMAP1), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR);
if (hBitmap != NULL)
{
// 在设备上绘制图像
HDC hdc = GetDC(hwnd);
HDC hdcMem = CreateCompatibleDC(hdc);
SelectObject(hdcMem, hBitmap);
BitBlt(hdc, 0, 0, cx, cy, hdcMem, 0, 0, SRCCOPY);
// 释放资源
DeleteDC(hdcMem);
ReleaseDC(hwnd, hdc);
DeleteObject(hBitmap);
}
In the example code above, after loading the bitmap resource IDB_BITMAP1, it is drawn on the window device corresponding to the hwnd handle. After drawing is completed, it is necessary to release the relevant resources, including the device context (hdcMem and hdc) and the bitmap object (hBitmap).