读书人

MAKEINTRESOURCE() 与 CString 的转换

发布时间: 2012-01-15 22:57:48 作者: rapoo

MAKEINTRESOURCE() 与 CString 的转换问题,谢谢回答
首先祝各位兄弟51快乐,呵呵

一个在picture上显示位图的程序中,我用了如下代码:

HBITMAP hBitmap;
CStatic *pStatic = (CStatic *)GetDlgItem(IDC_Picture_Unit);
hBitmap = (HBITMAP)LoadImage(
AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BITMAP_DXJSDQ), //数字形ID转为 "字符串 "
IMAGE_BITMAP,
0,
0,
LR_DEFAULTSIZE);
pStatic-> ModifyStyle(0xF, SS_BITMAP|SS_CENTERIMAGE);
pStatic-> SetBitmap(hBitmap);

当我在MAKEINTRESOURCE()中使用固定的位图资源id的时候,显示是正常的
但当我把这一id换成CString变量的时候,编译一样通过,但就是不能显示位图
我想根据程序的运行情况来决定显示哪个位图资源
我用过的方法:
CString mPicture = "IDB_BITMAP_DXJSDQ ";
MAKEINTRESOURCE(((LPCTSTR)mPicture)) 结果是编译通过但无法显示

如果我要换成显示的位图是一个外部文件呢?我用的方法是:
(HBITMAP)LoadImage(NULL,
"c:\\2.bmp ",
IMAGE_BITMAP,
0,
0, LR_DEFAULTSIZE);
pStatic-> ModifyStyle(0xF, SS_BITMAP);
pStatic-> SetBitmap(hBitmap);

结果也是编译通过但无法显示
我该怎么办呢?谢谢回答


[解决办法]
不晓得
帮顶拿分!
[解决办法]
HANDLE LoadImage(
HINSTANCE hinst, // handle to instance
LPCTSTR lpszName, // image to load
UINT uType, // image type
int cxDesired, // desired width
int cyDesired, // desired height
UINT fuLoad // load options
);
第二个参数如果是ID的话用 MAKEINTRESOURCE是 OK的,不然的话应该直接放入图像的名字
If the image resource is to be loaded by name, the lpszName parameter is a pointer to a null-terminated string that contains the name of the image resource. If the image resource is to be loaded by ordinal, use the MAKEINTRESOURCE macro to convert the image ordinal into a form that can be passed to the LoadImage function
MSDN注释
[解决办法]
先转换为ID,也就是整数。或者,你的资源就用“命名”ID,也就是用引号括起来,而不是整数ID。
[解决办法]
呵呵,比较阴险的一招是做个和背景色一样的图标,到时候。。。

读书人网 >VC/MFC

热点推荐