我想在我的窗口中创建一个PIC盒加载一个位图图像... picBoxDisp使用以下机制创建..在WIN32中显示图像,为什么不显示图像?
picBoxDisp = CreateWindow("STATIC", "image box",
WS_VISIBLE |WS_CHILD | SS_BITMAP |WS_TABSTOP | WS_BORDER,
50, 50, 250, 300, hwnd , (HMENU)10000, NULL, NULL);
现在接下来,我创建了一个HBITMAP对象,并在加载图像以它...
hBitmap = (HBITMAP) LoadImage(NULL,szFileName,IMAGE_BITMAP,0,0,
LR_LOADFROMFILE| LR_DEFAULTSIZE);
SendMessage(picBoxDisp,STM_SETIMAGE,(WPARAM) IMAGE_BITMAP,(LPARAM) NULL);
//now assign the new image
//Create a compatible DC for the original size bitmap, for example originalMemDc.
HDC originalDC = GetDC((HWND)hBitmap);
HDC originalMemDC = CreateCompatibleDC(originalDC);
if(originalMemDC==NULL){
MessageBox(NULL,"Problem while creating DC.","Error",MB_OK);
}
//Select hBitmap into originalMemDc.
SelectObject(originalMemDC,hBitmap);
//Create a compatible DC for the resized bitmap, for example resizedMemDc.
HDC picBoxDC = GetDC(picBoxDisp);
HDC resizedMemDC = CreateCompatibleDC(picBoxDC);
//Create a compatible bitmap of the wanted size for the resized bitmap,
HBITMAP hResizedBitmap = CreateCompatibleBitmap(picBoxDC,250,300);
//Select hResizedBitmap into resizedMemDc.
SelectObject(resizedMemDC,hResizedBitmap);
//Stretch-blit from originalMemDc to resizedMemDc.
//BitBlt(resizedMemDC,0,0,250,300,originalMemDC,0,0,SRCCOPY);
BITMAP bmp_old,bmp_new;
GetObject(hBitmap,sizeof(bmp_old),&bmp_old);
GetObject(hResizedBitmap,sizeof(bmp_new),&bmp_new);
StretchBlt (resizedMemDC,0,0,bmp_new.bmWidth,bmp_new.bmHeight,
originalMemDC,0,0,bmp_old.bmWidth,bmp_new.bmHeight,
SRCCOPY);
////De-select the bitmaps.
if((resizedMemDC==NULL)||(hResizedBitmap == NULL)) {
MessageBox(NULL,"Something is NULL","Error",MB_OK);
}
else
//Set hResizedBitmap as the label image with STM_SETIMAGE
SendMessage(picBoxDisp,STM_SETIMAGE, (WPARAM) IMAGE_BITMAP,(LPARAM) hResizedBitmap);
我只是不明白,为什么上面的代码不工作?
由于提前,
感谢@Johann Gerell ..我尝试了上面的代码,但问题是所有的图像都显示不同的尺寸......有些看起来越来越大,而有些则显示在一个角落(较小的图像)...我只是想将所有图像拉伸至相同的尺寸。 – aProgrammer 2011-04-27 07:25:01
@ArtsitOfProgramming:请参阅我的更新答案与位图大小调整逻辑。 – 2011-04-27 07:50:20
Thanks @Johann Gerell,你可以在第4步和第7步中帮助我。对于resizedMemDC我已经使用下面的代码,请让我知道如果这是错误的HDC picBoxDC = GetDC(picBoxDisp); // picBoxDisp是静态控制的手柄 \t \t \t \t \t \t \t \t \t \t \t \t \t HDC resizedMemDC = CreateCompatibleDC(picBoxDC); – aProgrammer 2011-04-27 09:49:26