win32编程,非MFC,用图片地址来使图片控件显示图片 20
用win32编程,非MFC,就是用一个图片控件,要实现通过代码,用图片地址来使图片显示在控件上(为的是登录界面,用户可以选择一个头像)。。要怎么做,求大神回复...
用win32编程,非MFC,就是用一个图片控件,要实现通过代码,用图片地址来使图片显示在控件上(为的是登录界面,用户可以选择一个头像)。。要怎么做,求大神回复
展开
2个回答
展开全部
#define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料
#include <windows.h>
#include <tchar.h>
/****************************************************************************
CDrawBitmap封装了一些画图片的函数。
调用如下:
CDrawBitmap g_bmp1("D:\\My Documents\\My Pictures\\风景图片\\4244123431.bmp");
g_bmp1.Draw(m_hWnd);
CDrawBitmap g_bmp2("D:\\My Documents\\My Pictures\\风景图片\\plane.bmp");
g_bmp2.DrawTransparent(m_hWnd);
CDrawBitmap g_bmp3("D:\\My Documents\\My Pictures\\风景图片\\jzg2.bmp");
g_bmp3.DrawTransparent(m_hWnd);
****************************************************************************/
//使用TransparentBlt需要添加 #pragma comment(lib,"msimg32.lib")
#pragma comment(lib,"msimg32.lib")
class CDrawBitmap
{
private:
HBITMAP m_bitmap;
int m_bitmap_cx;
int m_bitmap_cy;
public:
CDrawBitmap() {}
CDrawBitmap(TCHAR * PicturePath) { LoadPicture(PicturePath); }
CDrawBitmap(WORD ResourceID) { LoadResource(ResourceID); }
~CDrawBitmap() { ::DeleteObject(m_bitmap);}
bool LoadPicture(TCHAR * PicturePath)
{
m_bitmap=(HBITMAP)LoadImage(NULL, //应用程序实例
PicturePath, //路径
IMAGE_BITMAP,
0, //如果不置0,则此参数指定m_bitmap的宽(位图被拉伸)
0, //如果不置0,则此参数指定m_bitmap的高(位图被拉伸)
LR_LOADFROMFILE|LR_CREATEDIBSECTION|LR_DEFAULTSIZE);
if (!m_bitmap)
return false;
BITMAP bmp;
GetObject(m_bitmap,sizeof(BITMAP),(LPSTR)&bmp);
m_bitmap_cx=bmp.bmWidth;
m_bitmap_cy=bmp.bmHeight;
return true;
}
bool LoadResource(WORD ResourceID)
{
m_bitmap=LoadBitmap(GetModuleHandle(NULL),MAKEINTRESOURCE (ResourceID) );
if (!m_bitmap)
return false;
BITMAP bmp;
GetObject(m_bitmap,sizeof(BITMAP),(LPSTR)&bmp);
m_bitmap_cx=bmp.bmWidth;
m_bitmap_cy=bmp.bmHeight;
return true;
}
void Draw(HWND hWnd=NULL,int x=0,int y=0,int width=100,int height=100)
{
HDC hScrDC=GetDC(hWnd); //创建屏幕DC
Draw(hScrDC,x,y,width,height);
::ReleaseDC(hWnd,hScrDC) ; //释放屏幕DC
}
void Draw(HDC hScrDC,int x=0,int y=0,int width=100,int height=100)
{
HDC hMemDC=CreateCompatibleDC(hScrDC); //创建内存DC
::SelectObject(hMemDC,m_bitmap); //把位图选进内存DC
BitBlt(hScrDC,x,y,width,height,hMemDC,0,0,SRCCOPY); //内存DC映射到屏幕DC
::DeleteDC(hMemDC) ; //删除内存DC
}
void DrawTransparent(HWND hWnd=NULL,int x=0,int y=0)
{
HDC hScrDC=GetDC(hWnd); //创建屏幕DC
DrawTransparent(hScrDC,x,y);
::ReleaseDC(hWnd,hScrDC) ; //释放屏幕DC
}
//不进行拉伸处理,如需要,请自行修改代码TransparentBlt中的参数
//使用TransparentBlt需要添加 #pragma comment(lib,"msimg32.lib")
void DrawTransparent(HDC hScrDC,int x=0,int y=0,UINT crTransparent=RGB(255,255,255))
{
HDC hMemDC=CreateCompatibleDC(hScrDC); //创建内存DC
::SelectObject(hMemDC,m_bitmap); //把位图选进内存DC
::TransparentBlt(hScrDC,x,y,m_bitmap_cx,m_bitmap_cy,hMemDC,0,0,m_bitmap_cx,m_bitmap_cy,crTransparent);
::DeleteDC(hMemDC) ; //删除内存DC
}
void DrawStretch(HWND hWnd=NULL,int x=0,int y=0,int width=100,int height=100)
{
HDC hScrDC=GetDC(hWnd); //创建屏幕DC
DrawStretch(hScrDC,x,y,width,height);
::ReleaseDC(hWnd,hScrDC) ; //释放屏幕DC
}
void DrawStretch(HDC hScrDC=NULL,int x=0,int y=0,int width=100,int height=100)
{
HDC hMemDC=CreateCompatibleDC(hScrDC); //创建内存DC
::SelectObject(hMemDC,m_bitmap); //把位图选进内存DC
SetStretchBltMode(hScrDC,COLORONCOLOR); //一定要先调用这个
StretchBlt(hScrDC,x,y,width,height,hMemDC,0,0,m_bitmap_cx,m_bitmap_cy,SRCCOPY); //内存DC映射到屏幕DC
::DeleteDC(hMemDC) ; //删除内存DC
}
};
void DrawBitmap(HBITMAP m_bitmap,HDC hScrDC,int x=0,int y=0,int width=100,int height=100)
{
HDC hMemDC=CreateCompatibleDC(hScrDC); //创建内存DC
::SelectObject(hMemDC,m_bitmap); //把位图选进内存DC
BitBlt(hScrDC,x,y,width,height,hMemDC,0,0,SRCCOPY); //内存DC映射到屏幕DC
::DeleteDC(hMemDC) ; //删除内存DC
}
void DrawBitmap(HBITMAP m_bitmap,HWND hWnd=NULL,int x=0,int y=0,int width=100,int height=100)
{
HDC hScrDC=GetDC(hWnd); //创建屏幕DC
DrawBitmap(m_bitmap,hScrDC,x,y,width,height);
::ReleaseDC(hWnd,hScrDC) ; //释放屏幕DC
}
CDrawBitmap g_bmp1("E:\\pictures\\风景图片\\Sunset.bmp");
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc;
hdc = ::BeginPaint(hWnd, &ps);
g_bmp1.Draw(hdc,0,0,500,500);
::EndPaint(hWnd, &ps);
}
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}
HWND CreateMainWnd(WNDPROC lpfnWndProc);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
//create window
HWND MainWnd=CreateMainWnd(WndProc);
if ( !MainWnd )
return false;
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam ;
}
static
HWND CreateMainWnd(WNDPROC lpfnWndProc)
{
WNDCLASS wndcls;
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.lpszMenuName = NULL;
wndcls.hInstance = NULL;
wndcls.style = CS_HREDRAW|CS_VREDRAW;
wndcls.hCursor = LoadCursor(NULL,IDC_ARROW);
wndcls.lpszClassName = _T("Demo_MainWnd");
wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hIcon = LoadIcon(::GetModuleHandle(NULL),NULL);
wndcls.lpfnWndProc = lpfnWndProc;
if (!RegisterClass(&wndcls))
return NULL;
HWND hWnd = CreateWindowEx( NULL ,_T("Demo_MainWnd"),_T("Demo"),WS_OVERLAPPEDWINDOW,225,160,400,240,NULL,NULL,NULL,NULL) ;
if (!hWnd)
{
::MessageBox(NULL,TEXT("Can Not Create MainWnd"),_T("标题"),MB_OK);
return NULL;
}
ShowWindow(hWnd,SW_SHOWNORMAL);
UpdateWindow(hWnd);
return hWnd;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询