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:

  1. 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.
  2. 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.
  3. uType (UINT type): Specifies the type of image resource. It can be one of the following values:
  4. Load bitmap (.bmp) files using IMAGE_BITMAP.
  5. IMAGE_ICON: Used to load icon (.ico) files.
  6. IMAGE_CURSOR: Used to load cursor (.cur) files.
  7. cxDesired (integer type): Specifies the desired width of the image after loading. If set to 0, the original image size will be loaded.
  8. Desired (integer type): Specifies the height of the image after loading. If set to 0, the original image size will be loaded.
  9. fuLoad (of UINT type): Specifies the way in which the image is loaded. It can be one of the following values:
  10. LR_DEFAULTCOLOR: Default color will be used.
  11. Specify the lpszName parameter as a file path instead of a resource name for LR_LOADFROMFILE.
  12. LR_LOADTRANSPARENT: Load transparent image.
  13. LR_SHARED means that the loaded image can be shared with other threads.
  14. LR_CREATEDIBSECTION: Create a DIB (Device-Independent Bitmap) section.
  15. 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).

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds