我想在我的窗口中創建一個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