GDI/GDI+這些破事本文是雜篇,純屬筆記,想到哪寫到那! 1、獲取像素的RGB以及填充 CPaintDC dc(m_hWnd); COLORREF color=dc.GetPixel(0,0); int R=GetRValue(color); int G=GetGValue(color); nt B=GetBValue(color); dc.FillSolidRect(m_rcWindow,RGB(R,G,B)); 2、從圖片獲取窗體Region HRGN CreateRegionFromBitmap(Bitmap* bitmap, BYTE alphaValve/* = 0*/) { UINT width = bitmap->GetWidth(); UINT height = bitmap->GetHeight(); Color color; HRGN hRegion = ::CreateRectRgn(0, 0, width, height); HRGN rgn = ::CreateRectRgn(0, 0, width, height); for (UINT h = 0; h < height; ++h) { for (UINT w = 0; w < width; ++w) { UINT start = w; while (w < width) { bitmap->GetPixel(w, h, &color); if (color.GetAlpha() > alphaValve) break; ++w; } if (w > start) { ::SetRectRgn(rgn, start, h, w, h + 1); ::CombineRgn(hRegion, hRegion, rgn, RGN_DIFF); } } } ::DeleteObject(rgn); return hRegion; } Bitmap bitmap(_T("圖片路徑")); HRGN m_rgn; m_rgn=CreateRegionFromBitmap(&bitmap,254);//不取半透明圖像 3、UpdateLayeredWindow Image *m_pImageBackground; 4、畫圖 CBitmap m_btm_main; HBITMAP GetBitmapFromFile( LPCWSTR pFile ) { std::auto_ptr<Bitmap> pBmp(new Bitmap(pFile)); if(!pBmp.get()) return NULL; HBITMAP hBmp = NULL; Color backColor = Color(255,0,0,0); if(Ok!=pBmp->GetHBITMAP(backColor,&hBmp)) return NULL; return hBmp; } void SetBgBmp(CString strMain) { m_btm_main.Attach(GetBitmapFromFile(strMain)); } BOOL DrawBmp( HDC hdc, CRect rect, HBITMAP hBitmap) { BITMAP bm; GetObject(hBitmap,sizeof(bm),(VOID*)&bm); INT nWidth = bm.bmWidth; INT nHeight = bm.bmHeight; CDC memdc; memdc.CreateCompatibleDC(hdc); CBitmap bitmap; bitmap.CreateCompatibleBitmap(hdc,nWidth,nHeight); memdc.SelectBitmap(hBitmap); BLENDFUNCTION bf = {0,0,255,1}; return ::AlphaBlend(hdc,rect.left,rect.top,nWidth,nHeight,memdc,0,0,nWidth,nHeight,bf); } 調(diào)用: CPaintDC dc(m_hWnd); DrawBmp(dc,m_rcWindow,m_btm_main); |
|