MFC设置透明窗口

MFC设置透明窗口,第1张

得到屏幕大小,把窗口设置为全屛

      int m_xScreen = GetSystemMetrics(SM_CXSCREEN)

int m_yScreen = GetSystemMetrics(SM_CYSCREEN)

SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,WS_EX_TOOLWINDOW)SetLayeredWindow(m_hWnd,80)SetWindowPos(&CWnd::wndTopMost,0,0,m_xScreen,m_yScreen,SWP_SHOWWINDOW)SetForegroundWindow()

实现透明函数

void SetLayeredWindow(HWND m_hWnd,int nRate)

{HINSTANCE hInstif ((hInst = LoadLibrary(_T("USER32.dll"))) != NULL)

    {

        SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED)typedef BOOL (WINAPI* lpfnSetTransparent)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags)if (hInst){lpfnSetTransparent pFnSetTransparent = NULLpFnSetTransparent= (lpfnSetTransparent)GetProcAddress(hInst, "SetLayeredWindowAttributes")if (pFnSetTransparent)pFnSetTransparent(m_hWnd, 0, (255 * nRate) / 100, LWA_ALPHA)}

    }

}

CDC *pDC=CDC::FromHandle(::GetDC(/*这里是那个控件的句柄*/))

pDC->SetBkMode(TRANSPARENT)//透明   

::GetStockObject(NULL_BRUSH) 

我这有个函数希望能帮到你

/*****************************************************************

函数名称:yan_se_tou_ming

函数功能:让指定颜色区域变得透明化

函数参数:

      HWND hwnd,窗口句柄

      int tou_ming_de_yan_se,要透明的颜色

      int tou_ming_du=0 透明度,默认为0完全透明

返回值:成功返回TRUE 失败返回FALSE

/***************************************************/

/*****VC6.0需要加入以下代码在前面*************

#define WS_EX_LAYERED 0x00080000

#define LWA_COLORKEY 0x00000001

#define LWA_ALPHA 0x00000002

typedef BOOL (FAR WINAPI *LAYERFUNC)(HWND,COLORREF,BYTE,DWORD)

BOOL SetLayeredWindowAttributes(HWND hwnd,COLORREF crKey,BYTE bAlpha,DWORD dwFlags)

{

      LAYERFUNC SetLayer

      HMODULE hmod = LoadLibrary("user32.dll")

      SetLayer=(LAYERFUNC)GetProcAddress(hmod,"SetLayeredWindowAttributes")

      BOOL bReturn = SetLayer(hwnd,crKey,bAlpha,dwFlags)

      FreeLibrary(hmod)

      return bReturn

}

//*********************************************************/

//可以在OnPaint里添加 在OnInitDialog添加这段代码是无效的

BOOL yan_se_tou_ming(HWND hwnd,int tou_ming_de_yan_se,int tou_ming_du=0)

{

      int FengGe=GetWindowLong(hwnd,GWL_EXSTYLE)

      if (FengGe==0)

      {

             return FALSE

      }

      if (SetWindowLong(hwnd,GWL_EXSTYLE,FengGe|524288)==0)

      {

              return FALSE

      }

      DWORD flag=0

      if (tou_ming_du==0)

      {

              flag=LWA_COLORKEY

      }

      else

      {

              flag=LWA_ALPHA

      }

      if (SetLayeredWindowAttributes(hwnd,tou_ming_de_yan_se,tou_ming_du,flag)==0)

      {

              return FALSE

      }

   return TRUE

}

你的变量i是不是重新变为0了,当你再滚动一次?

而且有些地方需要你优化下:

你可以不必引入User32.dll,因为SetLayeredWindowAttributes函数已经在Winuser.h有定义了,直接用就可以了。

设置窗口为WS_EX_LAYERED只用设置一次就好了,没必要每次都设置。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/7774491.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-09
下一篇 2023-04-09

发表评论

登录后才能评论

评论列表(0条)

保存