对话框
#include "resource.h"
class CXFDialog : public CDialog
{
public:
CXFDialog( );
CXFDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd = NULL);
CXFDialog(UINT nIDTemplate, CWnd* pParentWnd = NULL); // standard constructor
void SetAutoDelete( BOOL bAutoDelete = FALSE );
void SetTransparent( BOOL bTransparent = TRUE, BYTE byteDeep = 244 );
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual void PostNcDestroy();
protected:
BOOL m_bAutoDelete;
BOOL m_bTransparent;
BYTE m_byteDeep;
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
virtual BOOL OnInitDialog();
virtual void OnOK();
virtual void OnCancel();
DECLARE_MESSAGE_MAP()
};
CXFDialog::CXFDialog( )
{
m_bAutoDelete = FALSE;
m_bTransparent = FALSE;
m_byteDeep = 250;
}
CXFDialog::CXFDialog(LPCTSTR lpszTemplateName, CWnd* pParent /*=NULL*/)
: CDialog(lpszTemplateName, pParent)
{
m_bAutoDelete = FALSE;
m_bTransparent = FALSE;
m_byteDeep = 250;
}
CXFDialog::CXFDialog(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
: CDialog(nIDTemplate, pParent)
{
m_bAutoDelete = FALSE;
m_bTransparent = FALSE;
m_byteDeep = 250;
}
void CXFDialog::SetAutoDelete( BOOL bAutoDelete )
{
m_bAutoDelete = bAutoDelete;
}
void CXFDialog::SetTransparent( BOOL bTransparent, BYTE byteDeep )
{
m_bTransparent = bTransparent;
m_byteDeep = byteDeep;
}
void CXFDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
void CXFDialog::PostNcDestroy()
{
CDialog::PostNcDestroy();
if( m_bAutoDelete && NULL != this )
delete this;
}
BEGIN_MESSAGE_MAP(CXFDialog, CDialog)
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
HBRUSH CXFDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}
BOOL CXFDialog::OnInitDialog()
{
CDialog::OnInitDialog();
if( m_bTransparent )
{
SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE, GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
typedef BOOL (WINAPI *TRANSPARENTFUNC)(HWND,COLORREF,BYTE,DWORD);
TRANSPARENTFUNC fun = NULL;
//取得SetLayeredWindowAttributes函数指针
fun = (TRANSPARENTFUNC)GetProcAddress( hInst, "SetLayeredWindowAttributes" );
if(fun)
fun( GetSafeHwnd(), 0, m_byteDeep, 2 );
FreeLibrary(hInst);
}
}
return TRUE;
}
void CXFDialog::OnOK()
{
CDialog::OnOK();
PostNcDestroy( );
}
void CXFDialog::OnCancel()
{
CDialog::OnCancel();
PostNcDestroy( );
}
class CMyDlg : public CDialog
{
public:
CMyDlg();
};
CMyDlg::CMyDlg() : CDialog(IDD_DIALOG1)
{
}
class CMyWinApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
CMyWinApp theApp;
BOOL CMyWinApp::InitInstance()
{
CXFDialog dlgEx(IDD_DIALOG1);
dlgEx.SetTransparent(TRUE,135);
// dlgEx.OnCtlColor();
dlgEx.DoModal();
CMyDlg dlg;
dlg.DoModal();
return TRUE;
}