GDI/GDI+这些破事

时间:2021-07-21 17:39:31

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

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;

   ModifyStyleEx(0, 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,400,233); SelectObject(hdcMem, hBitmap); Graphics graph(hdcMem); graph.DrawImage(m_pImageBackground, 0, 0, 400, 233); 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-400)/2; int y = (nClientHeigh-233)/2; POINT dstPoint = {x,y}; POINT srcPoint = {0,0}; SIZE size = {400,233}; BLENDFUNCTION m_Blend; m_Blend.BlendOp=0; m_Blend.BlendFlags=0; m_Blend.AlphaFormat=1; m_Blend.SourceConstantAlpha=255; UpdateLayeredWindow(m_hWnd,hdc,&dstPoint,&size,hdcMem,&srcPoint,0,&m_Blend,2); 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(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);
    
  }

调用:
 CPaintDC dc(m_hWnd);    
 DrawBmp(dc,m_rcWindow,m_btm_main);
        

5、设置顶层窗体

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