Click here to Skip to main content
15,891,248 members
Articles / Desktop Programming / MFC

Perfect Semi-transparent & Shaped Dialogs with Standard, ActiveX, and Translucent Controls for Windows 2000+

Rate me:
Please Sign up or sign in to vote.
4.93/5 (119 votes)
9 Feb 2010CPOL5 min read 3.2M   23.5K   226  
This article tries to find a way to show standard controls, ActiveX controls, translucent controls on layered windows. Native MFC source code provided.
// ContentDialog.cpp : implementation file
//

#include "stdafx.h"
#include "StandardDialog.h"


// CStandardDialog dialog

IMPLEMENT_DYNAMIC(CStandardDialog, CDialog)
CStandardDialog::CStandardDialog(UINT nIDTemplate, COLORREF clrTransparent/* = RGB(0, 255, 0)*/, CWnd* pParentWnd/* = NULL*/)
	: CDialog(nIDTemplate, pParentWnd)
	, m_clrTransparent(clrTransparent)
{
}

CStandardDialog::~CStandardDialog()
{
}

BEGIN_MESSAGE_MAP(CStandardDialog, CDialog)
	ON_WM_CTLCOLOR()
END_MESSAGE_MAP()


// CStandardDialog message handlers

void CStandardDialog::OnOK()
{
	CWnd* hParent = GetParent();
	if (hParent != NULL)
	{
		HWND hWnd = hParent->m_hWnd; 
		::SendMessage(hWnd, WM_CLOSE, 0, 0); 
	}

	CDialog::OnOK();
}

void CStandardDialog::OnCancel()
{
	CWnd* hParent = GetParent();
	if (hParent != NULL)
	{
		HWND hWnd = hParent->m_hWnd; 
		::SendMessage(hWnd, WM_CLOSE, 0, 0); 
	}

	CDialog::OnCancel();
}

void CStandardDialog::EndDialog(int nResult)
{
	CWnd* hParent = GetParent();
	if (hParent != NULL)
	{
		HWND hWnd = hParent->m_hWnd; 
		::SendMessage(hWnd, WM_CLOSE, 0, 0); 
	}

	CDialog::EndDialog(nResult);
}

HBRUSH CStandardDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	if (nCtlColor == CTLCOLOR_DLG)
		return m_brush;

	return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
}

BOOL CStandardDialog::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	DWORD dwStyle = ::GetWindowLong(m_hWnd, GWL_STYLE);
	VERIFY((dwStyle & WS_POPUP) != 0);
	VERIFY((dwStyle & WS_BORDER) == 0);
	VERIFY((dwStyle & WS_SIZEBOX) == 0);
	VERIFY((dwStyle & WS_DLGFRAME) == 0);

	m_brush.CreateSolidBrush(m_clrTransparent);
	DWORD dwExStyle = ::GetWindowLong(m_hWnd, GWL_EXSTYLE);
	VERIFY((dwExStyle & WS_EX_APPWINDOW) == 0);
	::SetWindowLong(m_hWnd, GWL_EXSTYLE, dwExStyle | 0x80000);

	HMODULE hInst = LoadLibrary(_T("User32.DLL")); 
	typedef BOOL (WINAPI *MYFUNC)(HWND, COLORREF, BYTE, DWORD); 
	MYFUNC SetLayeredWindowAttributes = NULL; 
	SetLayeredWindowAttributes = (MYFUNC)::GetProcAddress(hInst, "SetLayeredWindowAttributes"); 
	SetLayeredWindowAttributes(GetSafeHwnd(), m_clrTransparent, 0, 1); 
	::FreeLibrary(hInst);

	return TRUE;
}

void CStandardDialog::ShowDialog()
{
	Create(m_lpszTemplateName, m_pParentWnd);
	ShowWindow(SW_SHOW);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
China China
D.K.Wang is working for a leading interactive entertainment media company located in Shanghai, China. Skilled in Windows C++, MFC, WTL, PHP etc. Started with professional games development since 2005, now devoted to the core functionality such as graphics rendering, physics simulation and scene management for a household game engine.

Comments and Discussions