关于透明窗口的一些收集

首先,用生成向导建立个基于对话框的程序框架,取名为PartTransparent

第二,重写BOOL CPartTransparentDlg::OnInitDialog(),也就是重写,对话框类的OnInitDialog()这个函数,在里面,return TRUE;前面加入下面的代码:

 

	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;
		fun = (MYFUNC)GetProcAddress(hInst,"SetLayeredWindowAttributes");
		if (fun)
			fun(this->GetSafeHwnd(),0,128,2);
		FreeLibrary(hInst);
	}


其中fun(this->GetSafeHwnd(),0,128,2);修改窗口的透明度,值越接近255越不透明。

 

 

方法二:

BOOL   CALLBACK   EnumChildFunc(HWND   hwnd,   LPARAM   lParam) 
{ 
	CRgn   *pRgn   =   (CRgn*)lParam; 
	CRect   rcChild; 
	::GetWindowRect(hwnd,   rcChild); 
	CRgn   rgnChild; 
	CRgn   rgnCopy; 
	rgnCopy.CreateRectRgn(0,   0,   1,   1); 
	rgnCopy.CopyRgn(pRgn); 
	rgnChild.CreateRectRgn(rcChild.left,   rcChild.top,   
		rcChild.right,   rcChild.bottom); 
	pRgn-> CombineRgn(&rgnCopy,   &rgnChild,   RGN_OR); 
	return   TRUE; 
} 

int   SetBackTransparent(CWnd   *pWnd,   BOOL   bClientOnly   =   TRUE) 
{ 
	CRgn   rgn; 
	if(bClientOnly) 
	{ 
		CRgn   rgnWindow,   rgnClient; 
		CRect   rcWindow,   rcClient,   rcRgn; 
		pWnd-> GetWindowRect(rcWindow); 
		pWnd-> GetClientRect(rcClient); 
		pWnd-> ClientToScreen(rcClient); 
		rgnWindow.CreateRectRgn(rcWindow.left,   rcWindow.top,   
			rcWindow.right,   rcWindow.bottom); 
		rgnClient.CreateRectRgn(rcClient.left,   rcClient.top,   
			rcClient.right,   rcClient.bottom); 
		rgn.CreateRectRgn(0,   0,   1,   1); 
		rgn.CombineRgn(&rgnWindow,   &rgnClient,   RGN_DIFF); 
	} 
	else 
	{ 
		rgn.CreateRectRgn(0,   0,   0,   0); 
	} 
	::EnumChildWindows(pWnd-> GetSafeHwnd(),   (WNDENUMPROC)EnumChildFunc,(LPARAM)&rgn); 
	return   pWnd-> SetWindowRgn(rgn,   TRUE); 
} 


 

   调用办法: 
   在CYourDialog::OnInitDialog里加上 
   SetBackTransparent(this); 
   如果要连非客户区都透明,用 
   SetBackTransparent(this,   FALSE);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值