VC画图

BOOL DrawPic(HDC hdc, TCHAR*   lpstrFile,   HWND   hWnd,   int   nScrWidth,   int   nScrHeight)
{
	IPicture*	pPic		= NULL;
	IStream*	pStm		= NULL;
	BOOL		bResult		= FALSE;
	HANDLE		hFile		= NULL;
	DWORD		dwFileSize	= 0;
	DWORD		dwByteRead	= 0;

	//打开硬盘中的图形文件
	hFile = CreateFile(lpstrFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (INVALID_HANDLE_VALUE != hFile)
	{
		//获取文件字节数
		dwFileSize=GetFileSize(hFile, NULL);
		if (0xFFFFFFFF == dwFileSize)
		{
			return E_FAIL;   
		}   
		else
		{
			return E_FAIL;   
		}

		//分配全局存储空间   
		HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize);   
		LPVOID pvData = NULL;   

		if (NULL == hGlobal)
		{
			return E_FAIL;
		}

		//锁定分配内存块 
		pvData = GlobalLock(hGlobal);
		if (NULL == pvData)
		{
			return E_FAIL;
		}

		//把文件读入内存缓冲区
		ReadFile(hFile, pvData, dwFileSize, &dwByteRead, NULL);
		GlobalUnlock(hGlobal);
		CreateStreamOnHGlobal(hGlobal, TRUE, &pStm);

		//装入图形文件   
		bResult = OleLoadPicture(pStm, dwFileSize, TRUE, IID_IPicture, (LPVOID*)&pPic);   

		if(FAILED(bResult))
		{
			return E_FAIL;
		}

		OLE_XSIZE_HIMETRIC   hmWidth;//图片的真实宽度   
		OLE_YSIZE_HIMETRIC   hmHeight;//图片的真实高度   
		pPic-> get_Width(&hmWidth);   
		pPic-> get_Height(&hmHeight);   


		//将图形输出到屏幕上(有点像BitBlt)
		bResult = pPic-> Render(hdc,   0,   0,   nScrHeight,   nScrWidth,   0,   hmHeight,   hmWidth,   -hmHeight,   NULL); 
		pPic-> Release();

		//关闭打开的文件
		CloseHandle(hFile);

		if (SUCCEEDED(bResult))   
		{   
			return S_OK;   
		}   
		else 
		{   
			return E_FAIL;   
		}   
	} 
}
 

你可能感兴趣的:(vc++)