win32 api绘制图片背景

让人想犯罪 __ 提交于 2019-11-26 19:49:42

一. bmp静态框控件

static HBITMAP g_bg_bmp;

//获取窗口大小  
GetWindowRect(hWnd,&rect);  
nWinX = rect.right - rect.left;  
nWinY = rect.bottom - rect.top;

g_bg_bmp = LoadBitmap(hInst, (LPCTSTR)IDB_BITMAP1); 

//图片静态框
CreateWindowEx(0, "static", "静态框bmp", WS_CHILD|WS_VISIBLE|SS_SUNKEN, 0, 0, nWinX, nWinY,         
    hWnd, (HMENU)ID_USR_BMP, hInst, NULL);
//设置SS_BITMAP风格
HWND hWndBmp = GetDlgItem(hWnd, ID_USR_BMP);
LONG nStyle = GetWindowLong(hWndBmp, GWL_STYLE);
SetWindowLong(hWndBmp, GWL_STYLE, nStyle|SS_BITMAP);
//设置图片
SendDlgItemMessage(hWnd, ID_USR_BMP, STM_SETIMAGE, IMAGE_BITMAP, (long)g_bg_bmp);

二. 在WM_PAINT消息中填充图片背景

HDC hdc;
PAINTSTRUCT ps;
BITMAP	bminfo;

hdc = BeginPaint(hWnd, &ps);

HDC memdc = CreateCompatibleDC(hdc);  //内存DC
GetObject(g_bg_bmp, sizeof(bminfo), &bminfo);  //获取图片大小信息
SelectObject(memdc, g_bg_bmp);        //将g_bg_bmp选入内存DC
BitBlt(hdc,0,0,bminfo.bmWidth,bminfo.bmHeight,memdc,0,0,SRCCOPY);  //贴图到目标DC 
DeleteDC(memdc);

EndPaint(hWnd, &ps);

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!