标签:picture控件 client item getc top 加载 int() mask bitblt
参考:https://blog.csdn.net/pudongdong/article/details/69396600
之前写过win32动态显示图片的博客,这次用到了mfc。原理是一样的。
OnInitDialog函数初始化
BOOL CXxxDlg::OnInitDialog() { CDialog::OnInitDialog(); if (m_bmp.m_hObject != NULL) m_bmp.DeleteObject(); /*载入图片*/ HBITMAP hbmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),L"initPic.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE); if (hbmp == NULL) return FALSE; /*取得加载的BMP的信息*/ m_bmp.Attach(hbmp); DIBSECTION ds; BITMAPINFOHEADER &bminfo = ds.dsBmih; m_bmp.GetObject(sizeof(ds), &ds); int cx = bminfo.biWidth; int cy = bminfo.biHeight; /*得到了图像的宽度和高度后,对图像大小进行适应,即调整控件的大小,让它正好显示一张图片*/ CRect rect; GetDlgItem(IDC_PIC_ZONE)->GetWindowRect(&rect); ScreenToClient(&rect); /*调整大小*/ GetDlgItem(IDC_PIC_ZONE)->MoveWindow(rect.left, rect.top, cx, cy, true); return TRUE; }
OnPaint具体画图。
IDC_PIC_ZONE为picture control控件。
void CXxxDlg::OnPaint() { if (m_bmp.m_hObject != NULL) m_bmp.DeleteObject(); /*重新载入图片,因为可能刷新*/ HBITMAP hbmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), L"initPic.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE); if (hbmp == NULL) return; /*取得加载的BMP的信息*/ m_bmp.Attach(hbmp); DIBSECTION ds; BITMAPINFOHEADER &bminfo = ds.dsBmih; m_bmp.GetObject(sizeof(ds), &ds); /*若用此句,得到的是对话框的DC,图片将被绘制在对话框上*/ //CPaintDC dc(this); /*用此句,得到picture控件的DC,图像将被绘制在控件上 */ CPaintDC dc(GetDlgItem(IDC_PIC_ZONE)); /*若用此两句,得到的是屏幕的DC,图片将被绘制在屏幕上*/ // CDC dc; // dc.m_hDC=::GetDC(NULL); CRect rcclient; GetDlgItem(IDC_PIC_ZONE)->GetClientRect(&rcclient); CDC memdc; memdc.CreateCompatibleDC(&dc); CBitmap bitmap; bitmap.CreateCompatibleBitmap(&dc, rcclient.Width(), rcclient.Height()); memdc.SelectObject(&bitmap); CWnd::DefWindowProc(WM_PAINT, (WPARAM)memdc.m_hDC, 0); CDC maskdc; maskdc.CreateCompatibleDC(&dc); CBitmap maskbitmap; maskbitmap.CreateBitmap(rcclient.Width(), rcclient.Height(), 1, 1, NULL); maskdc.SelectObject(&maskbitmap); maskdc.BitBlt(0, 0, rcclient.Width(), rcclient.Height(), &memdc, rcclient.left, rcclient.top, SRCCOPY); CBrush brush; brush.CreatePatternBrush(&m_bmp); dc.FillRect(rcclient, &brush); dc.BitBlt(rcclient.left, rcclient.top, rcclient.Width(), rcclient.Height(), &memdc, rcclient.left, rcclient.top, SRCPAINT); brush.DeleteObject(); }
注意,要想程序运行到OnPaint方法,需要在BEGIN_MESSAGE_MAP注册ON_WM_PAINT()
效果:
标签:picture控件 client item getc top 加载 int() mask bitblt
原文地址:https://www.cnblogs.com/rixiang/p/10024741.html