用VC MFC Applizaed创建一个基于对话框的工程,保持默认选项。
向对话框初始化函数中添加代码如下:
向对话框中添加一个滑动条和编译框。并利用ClassWizard添加变量m_slide m_edit,添加一个滑动条响应函数,代码如下:
.............
// TODO: Add extra initialization here
::SetWindowPos(m_hWnd,HWND_TOPMOST,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
m_slide.SetRange(10,255);
m_slide.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;
fun=(MYFUNC)GetProcAddress(hInst,"SetLayeredWindowAttributes");
if(fun)
fun(this->GetSafeHwnd(),0,128,2);
FreeLibrary(hInst);
}
............................
void CMyDlg::OnReleasedcaptureSlider1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
BYTE eff=(BYTE)m_slide.GetPos();
HINSTANCE hInst=LoadLibrary("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,eff,2);
FreeLibrary(hInst);
}
CString str;
str.Format("%d",100*eff/255);
GetDlgItem(IDC_EDIT1)->SetWindowText(str);
*pResult = 0;
}
然后运行程序,拖动滑动条。可以看到窗口的透明度不断变化。
太神奇了....