Posted on 2008-06-27 13:32
若我 閱讀(1448)
評論(0) 編輯 收藏 引用
顯示位圖,你應該使用GDI+里面的Bitmap類或者Image類,這兩個類都提供了方法從硬盤上的一個文件打開文件,創建相應的內存中的位圖對象的工作。然后你可以使用Graphics類的DrawImage方法來繪制該位圖。
下面的代碼初始化GDI+,顯示一個打開文件對話框并且創建Bitmap對象,顯示位圖:
GdiplusStartupInput input;
ULONG_PTR gdiPlusToken;
if(GdiplusStartup(&gdiPlusToken,&input,NULL)!=Ok)
{
return -1;
}
char fileName[200];
OPENFILENAME openStruct;
memset(&openStruct,0,sizeof(OPENFILENAME));
openStruct.lStructSize=sizeof(OPENFILENAME);
openStruct.Flags=OFN_EXPLORER;
openStruct.lpstrFilter="Jpeg files(*.jpg)\0*.jpg\0\0";
openStruct.lpstrFile=fileName;
openStruct.nMaxFile=200;
if(GetOpenFileName(&openStruct))
{
imageFileName=fileName;
}
HDC hdc=GetDC(hPicWnd);
if(hdc==NULL)
{
MessageBox(NULL,"","",MB_OK);
}
Graphics *g=new Graphics(hdc);
WCHAR tmpFileName[100];
size_t numOfChars;
if(/*mbstowcs_s(&numOfChars,tmpFileName,100,fileName,199)*/MultiByteToWideChar(CP_ACP,MB_COMPOSITE,imageFileName.c_str(),200,tmpFileName,100)/*==-1*/)
{
MessageBox(NULL,"Error occured in string convertion!","Error!",MB_OK);
return wParam;
}
HWND hPicWnd;
RECT rc;
GetWindowRect(hPicWnd,&rc);
RectF drawRect(rc.left,rc.top,rc.right-rc.left,rc.bottom-rc.top);
Bitmap *bmp=new Bitmap(tmpFileName);
bmpMain=bmp;
g->DrawImage(bmp,drawRect);
delete g;
ReleaseDC(hwnd,hdc);
GdiplusShutdown(gdiPlusToken);