窗体透明度设置

时间:2022-08-06 08:08:09

效果

窗体透明度设置 窗体透明度设置 窗体透明度设置


新建对话框程序


修改对话框

窗体透明度设置


WindowAlphaSettingDlg.h : 头文件

public:
	CSliderCtrl m_sld;
	afx_msg void OnNMReleasedcaptureSlider1(NMHDR *pNMHDR, LRESULT *pResult);


WindowAlphaSettingDlg.cpp : 实现文件

BOOL CWindowAlphaSettingDlg::OnInitDialog()

	//窗口置顶
	::SetWindowPos(m_hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);

	//控件范围
	m_sld.SetRange(10,255);
	m_sld.SetPos(128);

	//窗口样式
	SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
	HINSTANCE hInst = LoadLibrary("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(),0,128,2);
		FreeLibrary(hInst);
	}

void CWindowAlphaSettingDlg::OnNMReleasedcaptureSlider1(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	BYTE eff = (BYTE)m_sld.GetPos();
	HINSTANCE hInst = LoadLibrary("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(),0,eff,2);
		FreeLibrary(hInst);
	}

	CString str;
	str.Format("%d%%",100*eff/255);
	GetDlgItem(IDC_STATIC1)->SetWindowText(str);

	*pResult = 0;
}