When initializing a CBitmap object, Cbitmap: LoadBitmap is commonly used, that is, the following two types:
BOOL LoadBitmap (LPCTSTR lpszRecourceName );
BOOL LoadBitmap (UINT nIDResource );
The description in MSDN is:
"If the return value is successful, a non-zero value is returned. Otherwise, the value is 0.
LpszResourceName points to a string containing the bitmap Resource Name (this string ends with null ). NIDResource specifies the ID of the resource in the bitmap resource. This function loads bitmap resources with names specified by lpszResourceName or ID signs specified by nIDResource from the executable files of the application. The loaded bitmap is attached to the Cbitmap object. If the object named by lpszResourceName does not exist or there is not enough memory to load the bitmap, the function returns 0. You can call the CgdiObject: DeleteObject function to delete the bitmap loaded by LoadBitmap. Otherwise, the Cbitmap destructor will delete the bitmap object. Warning before deleting a bitmap object, make sure it is not selected to the device context. In Windows3.1 and later versions, the following bitmap is added: OBM_UPARROWIORM_DNARROWIOBM_RGARROWIOBM_LFARROWI ......"
At the beginning, I directly gave the image path name to lpszRecourceName, but it was always unsuccessful. I checked the program carefully and did not find any errors. Why ?... I found the problem on lpszRecourceName after I checked the CSDN file. lpszRecourceName literally seems to be a "name string pointing to a resource". What is the actual situation? It is not a general understanding of the name of the resource file on the disk, but the name of the resource that has been imported in the VC project, so the name of the external file cannot be assigned to it. How can this problem be solved? --- Use the API function HBITMAP LoadImage ("file name"). However, this function returns a handle pointing to the Load image. Therefore, you need to use the Attach method of CBitmap:
//// Load the image directly from an external file
HBITMAP bitmap;
Bitmap = (HBITMAP) LoadImage (AfxGetInstanceHandle (), strFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
M_backBitmap.DeleteObject ();
If (! M_backBitmap.Attach (bitmap ))
{
MessageBox ("failed to import background image! "," Prompt ", MB_ OK );
Return;
}
****************************************
Void CitemView: getBitMap (CDC * pDC)
{
CDC MemDC;
HBITMAP hBmp;
BITMAP bm;
CBitmap Bitmap;
CPoint point (10, 10 );
CString cStr;
// HBmp = (HBITMAP): LoadImage (NULL, "BG.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
HBmp = (HBITMAP): LoadImage (AfxGetInstanceHandle (), "BG.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
Bitmap. DeleteObject ();
Bitmap. Attach (hBmp );
Bitmap. GetObject (sizeof (BITMAP), & bm );
MemDC. CreateCompatibleDC (pDC );
MemDC. SelectObject (& Bitmap );
PDC-> BitBlt (point. x, point. y, bm. bmWidth, bm. bmHeight, & MemDC, 0, 0, SRCCOPY );
MemDC. DeleteDC ();
}
**************************************** ***********
Which name is lpszResourceName in LoadBitmap (LPCTSTR lpszResourceName )?
BOOL LoadBitmap (LPCTSTR lpszResourceName );
BOOL LoadBitmap (UINT nIDResource );
NIDResource refers to the resource ID.
But what does lpszResourceName mean?
For example, I created a BITMAP resource IDB_BITMAP1. What does lpszResourceName mean?
Bitmap1.bmp? If yes, why is the following code incorrect.
CBitmap bmp;
Bmp. LoadBitmap ("d: \ .. \ res \ bitmpa1.bmp ");
CDC memdc;
BITMAP bmstru;
Bmp. GetBitmap (& bmstru );
Memdc. CreateCompatibleDC (pDC );
Memdc. SelectObject (& bmp );
PDC-> BitBlt (, bmstru. bmWidth, bmstru. bmHeight, & memdc, SRCCOPY );
Open the *. rc file with Notepad and find a line similar to the following:
IDB_BITMAP BITMAP "res \ background.bmp"
Changed:
Bitmap1 BITMAP "res \ background.bmp"
Alternatively, check the properties of Bitmap resources in VC and change the ID column to "Bitmap" (note that quotation marks must be added ).
Then call:
Bmp. LoadBitmap ("Bitmap1 ");
Success.
Resources can be identified by an integer or a string. However, in any case, these IDs do not refer to bitmap file names. Do not confuse them.
The LoadBitmap parameter is for resources regardless of the type. CBitmap does not provide the function of Directly Reading bitmap from files!
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/include1224/archive/2008/11/22/3352323.aspx