显示一幅图片的问题

usa008 2015-03-01 02:22:25
想只显示一幅图,就像一些软件的启动画面一样,没有边框什么的,现在做法是在一个窗口上显示,然后把窗口标题 宽度这些去掉,但是太麻烦,有没有什么简单的方式实现呢 求教
...全文
112 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
likfeng 2015-03-02
  • 打赏
  • 举报
回复
参考MSDN,在OnPaint中加上显示图像的代码 HOWTO: How To Use LoadImage() to Read a BMP File Q158898 -------------------------------------------------------------------------------- The information in this article applies to: Microsoft Win32 Application Programming Interface (API), used with: Microsoft Windows NT Server version 4.0 Microsoft Windows NT Workstation version 4.0 Microsoft Windows 95 Microsoft Windows 2000 Advanced Server Microsoft Windows 2000 Server Microsoft Windows 2000 Professional -------------------------------------------------------------------------------- SUMMARY The LoadImage API can be used to load a bitmap from a BMP file. However, it does not return palette information. This article provides sample code and describes how to retrieve the palette information for the bitmap with LoadImage. MORE INFORMATION The following code uses the LoadImage API to load the bitmap as a DIBSection, and then creates a palette from the DIBSection's color table. If no color table is present, a halftone palette is used: BOOL LoadBitmapFromBMPFile( LPTSTR szFileName, HBITMAP *phBitmap, HPALETTE *phPalette ) { BITMAP bm; *phBitmap = NULL; *phPalette = NULL; // Use LoadImage() to get the image loaded into a DIBSection *phBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE ); if( *phBitmap == NULL ) return FALSE; // Get the color depth of the DIBSection GetObject(*phBitmap, sizeof(BITMAP), &bm ); // If the DIBSection is 256 color or less, it has a color table if( ( bm.bmBitsPixel * bm.bmPlanes ) <= 8 ) { HDC hMemDC; HBITMAP hOldBitmap; RGBQUAD rgb[256]; LPLOGPALETTE pLogPal; WORD i; // Create a memory DC and select the DIBSection into it hMemDC = CreateCompatibleDC( NULL ); hOldBitmap = (HBITMAP)SelectObject( hMemDC, *phBitmap ); // Get the DIBSection's color table GetDIBColorTable( hMemDC, 0, 256, rgb ); // Create a palette from the color tabl pLogPal = (LOGPALETTE *)malloc( sizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY)) ); pLogPal->palVersion = 0x300; pLogPal->palNumEntries = 256; for(i=0;i<256;i++) { pLogPal->palPalEntry[i].peRed = rgb[i].rgbRed; pLogPal->palPalEntry[i].peGreen = rgb[i].rgbGreen; pLogPal->palPalEntry[i].peBlue = rgb[i].rgbBlue; pLogPal->palPalEntry[i].peFlags = 0; } *phPalette = CreatePalette( pLogPal ); // Clean up free( pLogPal ); SelectObject( hMemDC, hOldBitmap ); DeleteDC( hMemDC ); } else // It has no color table, so use a halftone palette { HDC hRefDC; hRefDC = GetDC( NULL ); *phPalette = CreateHalftonePalette( hRefDC ); ReleaseDC( NULL, hRefDC ); } return TRUE; } The following code demonstrates how to use the LoadBitmapFromBMPFile function: case WM_PAINT: { PAINTSTRUCT ps; HBITMAP hBitmap, hOldBitmap; HPALETTE hPalette, hOldPalette; HDC hDC, hMemDC; BITMAP bm; hDC = BeginPaint( hWnd, &ps ); if( LoadBitmapFromBMPFile( szFileName, &hBitmap, &hPalette ) ) { GetObject( hBitmap, sizeof(BITMAP), &bm ); hMemDC = CreateCompatibleDC( hDC ); hOldBitmap = (HBITMAP)SelectObject( hMemDC, hBitmap ); hOldPalette = SelectPalette( hDC, hPalette, FALSE ); RealizePalette( hDC ); BitBlt( hDC, 0, 0, bm.bmWidth, bm.bmHeight, hMemDC, 0, 0, SRCCOPY ); SelectObject( hMemDC, hOldBitmap ); DeleteObject( hBitmap ); SelectPalette( hDC, hOldPalette, FALSE ); DeleteObject( hPalette ); } EndPaint( hWnd, &ps ); } break; Additional query words: 4.00 kbdsd BITMAP DDB DIB BMP file LoadImage LoadBitmap Keywords : _IK kbOSWinNT400 kbOSWin2000 kbOSWin95 kbSDKWin16 kbGDIFAQ Issue type : kbhowto Technology : kbAudDeveloper kbWin32sSearch kbWin32API
usa008 2015-03-02
  • 打赏
  • 举报
回复
引用 3 楼 likfeng 的回复:

void CScreenWnd::CreatLjxWnd()
{
   //建立大小与位图大小相同的窗口
   CreateEx(0,
   AfxRegisterWndClass(0,AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
						"animatesplash",
						WS_POPUP,
						0,0,640,124,	//不能采样Photoshop生成的jpg图片
						NULL,
						NULL,
						NULL );
}

请问 我这样为什么显示不出来呢,HBITMAP hbit 是读取的一幅图, 然后转换成BITMAP,但是显示不出,如果直接读取本地的BMP文件 保存在BITMAP 就可以显示,请问什么原因呢
void CWzdSplash::Create(HBITMAP hbit)
{
	BITMAP bitmap;
	GetObject(hbit, sizeof(bitmap), (LPSTR)&bitmap);
	//CreateEx(0,AfxRegisterWndClass(0),"",WS_POPUP|WS_VISIBLE|WS_BORDER,0,0,bitmap.bmWidth,bitmap.bmHeight,NULL,0);
	CreateEx(0,
		AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
		NULL, WS_POPUP | WS_VISIBLE, 0, 0, bitmap.bmWidth, bitmap.bmHeight, NULL , NULL);
}
usa008 2015-03-02
  • 打赏
  • 举报
回复
引用 2 楼 likfeng 的回复:
从CWnd直接自定义一个类CScrennWnd

void CScreenWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	//*
	//重画客户区颜色
	CPen Pen;
	CPen *pOldPen;
	Pen.CreatePen(PS_SOLID, 1, RGB(0x85, 0x85, 0x85));
	pOldPen = dc.SelectObject(&Pen);

	CBrush brush;
	brush.CreateSolidBrush(RGB(0x85, 0x85, 0x85));
	CBrush* pOldBrush = dc.SelectObject(&brush);
	CRect rc;
	GetClientRect(&rc);
	dc.Rectangle(&rc);

	dc.SelectObject(pOldPen);
	dc.SelectObject(pOldBrush);
//*/

	//绘制公司标志
	CRect rect;
	GetClientRect(&rect);
	int nX = 640;
	int nY = 124;

	CString strName;					//文件名
// 	char cDir[255];
// 	GetCurrentDirectory(255, cDir);		//取得程序目录
	CString strAppPath;
//	strAppPath.Format(_T("%s"), cDir);	//程序目录
	CString path0; 
	GetModuleFileName(NULL,path0.GetBufferSetLength(MAX_PATH+1),MAX_PATH); 
	path0.ReleaseBuffer(); 
	int pos = path0.ReverseFind('\\'); 
	path0 = path0.Left(pos);
	strAppPath = path0;
	if (strAppPath.Right(1) == _T("\\"))
	{
		strName = strAppPath + _T("splash.jpg");
	}
	else
	{
		strName = strAppPath + _T("\\splash.jpg");
	}
	
	BOOL bFileExist;					//检查标志图片是否存在
	CFile file;
	bFileExist = file.Open(strName, CFile::modeRead);
	if (bFileExist) 
	{
		file.Close();
	}
	else
	{
		return;					//不存在返回
	}

	HANDLE hFile;						//打开文件
	if((hFile = ::CreateFile(strName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN, NULL)) != INVALID_HANDLE_VALUE)
	{
		//创建文件流 
		DWORD dwSize = DWORD(::GetFileSize(hFile, NULL)); 
		ASSERT(dwSize != -1); 
		HGLOBAL hMemory = ::GlobalAlloc(GMEM_MOVEABLE, dwSize); 
		ASSERT(hMemory != NULL);
		LPVOID pData = ::GlobalLock(hMemory); 
		ASSERT(pData != NULL);
		DWORD dwCount;
		::ReadFile(hFile, pData, dwSize, &dwCount, NULL);
		::GlobalUnlock(hMemory);
		IStream * pStream = NULL;
		::CreateStreamOnHGlobal(hMemory, TRUE, &pStream);
		ASSERT(pStream != NULL);
		//从流中装入图片 
		IPicture * pPicture = NULL;
		if(SUCCEEDED(::OleLoadPicture( pStream, dwSize, TRUE, IID_IPicture,(LPVOID*)&pPicture)))
		{ 
			// 向DC中绘图(右下角)
			OLE_XSIZE_HIMETRIC hmWidth;
			OLE_YSIZE_HIMETRIC hmHeight;
			pPicture->get_Width(&hmWidth);
			pPicture->get_Height(&hmHeight);
			pPicture->Render(dc, 0, 0, nX, nY, 0, hmHeight, hmWidth, -hmHeight, NULL);
			pPicture->Release();
		}
		//释放流和文件
		pStream->Release();
		::CloseHandle(hFile);
	}
}

//调用
CScreenWnd *s=new CScreenWnd;	//建立一个新窗口对象
s->CreatLjxWnd();			//创建窗口
s->CenterWindow();			//在屏幕中央
s->ShowWindow(SW_SHOW);	//显示窗口
s->UpdateWindow();			//更新窗口,激活OnPait函数
Sleep(2000);					//等待函数指定秒钟
if (s!=NULL) 
{
	s->SendMessage(WM_CLOSE);	//关闭窗口
}
请问 你的代码是CreateFile 打开的本地文件,如果我获取的图片是在HBITMAP中 怎么转换显示出呢
likfeng 2015-03-01
  • 打赏
  • 举报
回复

void CScreenWnd::CreatLjxWnd()
{
   //建立大小与位图大小相同的窗口
   CreateEx(0,
   AfxRegisterWndClass(0,AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
						"animatesplash",
						WS_POPUP,
						0,0,640,124,	//不能采样Photoshop生成的jpg图片
						NULL,
						NULL,
						NULL );
}

likfeng 2015-03-01
  • 打赏
  • 举报
回复
从CWnd直接自定义一个类CScrennWnd

void CScreenWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	//*
	//重画客户区颜色
	CPen Pen;
	CPen *pOldPen;
	Pen.CreatePen(PS_SOLID, 1, RGB(0x85, 0x85, 0x85));
	pOldPen = dc.SelectObject(&Pen);

	CBrush brush;
	brush.CreateSolidBrush(RGB(0x85, 0x85, 0x85));
	CBrush* pOldBrush = dc.SelectObject(&brush);
	CRect rc;
	GetClientRect(&rc);
	dc.Rectangle(&rc);

	dc.SelectObject(pOldPen);
	dc.SelectObject(pOldBrush);
//*/

	//绘制公司标志
	CRect rect;
	GetClientRect(&rect);
	int nX = 640;
	int nY = 124;

	CString strName;					//文件名
// 	char cDir[255];
// 	GetCurrentDirectory(255, cDir);		//取得程序目录
	CString strAppPath;
//	strAppPath.Format(_T("%s"), cDir);	//程序目录
	CString path0; 
	GetModuleFileName(NULL,path0.GetBufferSetLength(MAX_PATH+1),MAX_PATH); 
	path0.ReleaseBuffer(); 
	int pos = path0.ReverseFind('\\'); 
	path0 = path0.Left(pos);
	strAppPath = path0;
	if (strAppPath.Right(1) == _T("\\"))
	{
		strName = strAppPath + _T("splash.jpg");
	}
	else
	{
		strName = strAppPath + _T("\\splash.jpg");
	}
	
	BOOL bFileExist;					//检查标志图片是否存在
	CFile file;
	bFileExist = file.Open(strName, CFile::modeRead);
	if (bFileExist) 
	{
		file.Close();
	}
	else
	{
		return;					//不存在返回
	}

	HANDLE hFile;						//打开文件
	if((hFile = ::CreateFile(strName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN, NULL)) != INVALID_HANDLE_VALUE)
	{
		//创建文件流 
		DWORD dwSize = DWORD(::GetFileSize(hFile, NULL)); 
		ASSERT(dwSize != -1); 
		HGLOBAL hMemory = ::GlobalAlloc(GMEM_MOVEABLE, dwSize); 
		ASSERT(hMemory != NULL);
		LPVOID pData = ::GlobalLock(hMemory); 
		ASSERT(pData != NULL);
		DWORD dwCount;
		::ReadFile(hFile, pData, dwSize, &dwCount, NULL);
		::GlobalUnlock(hMemory);
		IStream * pStream = NULL;
		::CreateStreamOnHGlobal(hMemory, TRUE, &pStream);
		ASSERT(pStream != NULL);
		//从流中装入图片 
		IPicture * pPicture = NULL;
		if(SUCCEEDED(::OleLoadPicture( pStream, dwSize, TRUE, IID_IPicture,(LPVOID*)&pPicture)))
		{ 
			// 向DC中绘图(右下角)
			OLE_XSIZE_HIMETRIC hmWidth;
			OLE_YSIZE_HIMETRIC hmHeight;
			pPicture->get_Width(&hmWidth);
			pPicture->get_Height(&hmHeight);
			pPicture->Render(dc, 0, 0, nX, nY, 0, hmHeight, hmWidth, -hmHeight, NULL);
			pPicture->Release();
		}
		//释放流和文件
		pStream->Release();
		::CloseHandle(hFile);
	}
}

//调用
CScreenWnd *s=new CScreenWnd;	//建立一个新窗口对象
s->CreatLjxWnd();			//创建窗口
s->CenterWindow();			//在屏幕中央
s->ShowWindow(SW_SHOW);	//显示窗口
s->UpdateWindow();			//更新窗口,激活OnPait函数
Sleep(2000);					//等待函数指定秒钟
if (s!=NULL) 
{
	s->SendMessage(WM_CLOSE);	//关闭窗口
}
zgl7903 2015-03-01
  • 打赏
  • 举报
回复

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧