MFC中静态文本框贴图处理

1.静态文本控件贴png图片

    在创建控件时添加SS_BITMAP属性SS_NOTIFY

    CImage m_Icon.Load(wstring(L"tupian.png").c_str());
    RECT rect;
    HDC pDC = ::GetWindowDC(pImageStatic->m_hWnd);
    ::GetWindowRect(pImageStatic->m_hWnd,&rect);
    m_Icon.TransparentBlt(pDC,rect,RGB(255,255,255));
    pImageStatic->SetBitmap(m_Icon);
    pImageStatic->ShowWindow(SW_SHOW);
    ::ReleaseDC(pImageStatic->m_hWnd,pDC);


2.静态文本控件贴bmp图片

    在创建控件时添加SS_BITMAP与SS_NOTIFY属性

    HBITMAP m_Icon = (HBITMAP)::LoadImage( NULL,L"tupian.bmp").c_str(), IMAGE_BITMAP, 21,21, LR_LOADFROMFILE);
    m_pImageStatic->SetBitmap(m_Icon);
    m_pImageStatic->ShowWindow(SW_SHOW);

 

3.静态文本控件贴ico图片

    在创建控件时添加SS_ICON与SS_CENTERIMAGE属性
    HICON m_Icon = (HICON)::LoadImage( NULL,(L"tupian.ico").c_str(), IMAGE_ICON, 16,16, LR_LOADFROMFILE);

    m_pImageStatic->SetIcon(m_Icon);

 

 4.设置对话框中控件颜色和背景

    HBRUSH CDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
        HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
        switch(pWnd->GetDlgCtrlID())
       {
           case ID_STATUS_COLOR:
          {
              pDC->SetBkMode(TRANSPARENT);     //设置透明
              pDC->SetTextColor(RGB(150,150,150));    //设置字体
              pDC->SetBkColor(RGB(255,255, 255));    //设置背景色
              break;
         }
        default:
            break;
       }
   
       return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
    }

 

5.过滤对话框中的背景颜色

    SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
    GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)|0x80000);

    HINSTANCE hInst = LoadLibrary(L"User32.DLL");
    if(hInst)
    {           
          typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD);         
          MYFUNC fun = NULL;
          //取得SetLayeredWindowAttributes函数指针    
         fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
         if(fun)
        {
            fun(this->GetSafeHwnd(),RGB(100,100,100),10,1);     //100,100,100 是要过滤的颜色
        }
 
       FreeLibrary(hInst);
   }

 

 6.更新对话框的主背景bmp图片

    BOOL CDlg::OnEraseBkgnd(CDC* pDC)
    {
        CRect rect; 
        CDC   memDC; 
        CBitmap* pOldMemBmp = NULL;

        GetWindowRect(&rect); 

        CBitmap* pBitmap = NULL;
        CImage image;

        image.Load(m_ImagePath.c_str());   //更新背景只需要更新此处的背景图片的路径

        HBITMAP hbmp = image.Detach();
        pBitmap = CBitmap::FromHandle(hbmp);

        memDC.CreateCompatibleDC(pDC); 
        pOldMemBmp = memDC.SelectObject(pBitmap); 
        pDC->BitBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0, SRCCOPY); 
        if(pOldMemBmp)
        {
            memDC.SelectObject(pOldMemBmp);
        }

        image.Destroy();
        DeleteObject(pBitmap);
        DeleteObject(hbmp);
        memDC.DeleteDC();
        return   TRUE;

     }

你可能感兴趣的:(MFC,mfc,图片,控件)