GDI/GDI+这些破事

时间:2024-01-02 09:05:14

本文是杂篇,纯属笔记,想到哪写到那!

1、获取像素的RGB以及填充

CPaintDC dc(m_hWnd);
COLORREF color=dc.GetPixel(,);
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(, , width, height);
HRGN rgn = ::CreateRectRgn(, , width, height);
for (UINT h = ; h < height; ++h)
{
for (UINT w = ; 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 + );
::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;

   ModifyStyleEx(, WS_EX_LAYERED);
typedef BOOL (WINAPI*UpdateLayeredWindowFunc)(HWND,HDC,POINT*,SIZE*,HDC,POINT*,COLORREF,BLENDFUNCTION*,DWORD);
m_pImageBackground = Image::FromFile(_T("图片路径")); PAINTSTRUCT ps;
HDC hdc = BeginPaint( &ps);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hBitmap = CreateCompatibleBitmap(hdc,,); SelectObject(hdcMem, hBitmap); Graphics graph(hdcMem);
graph.DrawImage(m_pImageBackground, , , , ); HMODULE hDll = LoadLibrary(_T("user32.DLL"));
UpdateLayeredWindowFunc UpdateLayeredWindow=(UpdateLayeredWindowFunc)GetProcAddress(hDll, "UpdateLayeredWindow"); int nClientWith= GetSystemMetrics(SM_CXFULLSCREEN);
int nClientHeigh= GetSystemMetrics(SM_CYFULLSCREEN);
int x = (nClientWith-)/;
int y = (nClientHeigh-)/; POINT dstPoint = {x,y};
POINT srcPoint = {,};
SIZE size = {,};
BLENDFUNCTION m_Blend;
m_Blend.BlendOp=;
m_Blend.BlendFlags=;
m_Blend.AlphaFormat=;
m_Blend.SourceConstantAlpha=; UpdateLayeredWindow(m_hWnd,hdc,&dstPoint,&size,hdcMem,&srcPoint,,&m_Blend,); FreeLibrary(hDll);

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(,,,);
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 = {,,,}; return ::AlphaBlend(hdc,rect.left,rect.top,nWidth,nHeight,memdc,,,nWidth,nHeight,bf); } 调用:
CPaintDC dc(m_hWnd);
DrawBmp(dc,m_rcWindow,m_btm_main);

5、设置顶层窗体

在初始化函数中调用
SetWindowPos(HWND_TOPMOST,,,,,SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOSIZE);