Click here to Skip to main content
15,891,184 members
Articles / Desktop Programming / Windows Forms

Cool, Semi-transparent and Shaped Dialogs with Standard Controls for Windows 2000 and Above

Rate me:
Please Sign up or sign in to vote.
4.85/5 (95 votes)
28 Sep 2012CPOL3 min read 2.9M   32.8K   350  
This article tries to find a way to show Windows standard controls on layered windows. Provides both Native MFC and WinForms source code.
#include "StdAfx.h"
#include "./Utility.h"


//-------------------------------------------------------------------------
// Function Name    :GetModulePath
// Parameter(s)     :HMODULE hModule	Get module handle
// Return           :The module file path
// Create			:2007-1-10 11:24	Jerry.Wang
// Memo             :Retrieve the path of the given module
//-------------------------------------------------------------------------
CString CUtility::GetModulePath(HMODULE hModule /* = NULL */)
{
	TCHAR buf[MAX_PATH] = {'\0'};
	CString strDir, strTemp;

	::GetModuleFileName( hModule, buf, MAX_PATH);
	strTemp = buf;
	strDir = strTemp.Left( strTemp.ReverseFind('\\') + 1 );
	return strDir;
}


//-------------------------------------------------------------------------
// Function Name    :IsFileExist
// Parameter(s)     :LPCTSTR lpszFilePath	File path
// Return           :
// Create			:2007-4-23 15:13	Jerry.Wang
// Memo             :verify the file exist
//-------------------------------------------------------------------------
BOOL CUtility::IsFileExist(LPCTSTR lpszFilePath)
{
	BOOL bExist = FALSE;
	HANDLE hFile = NULL;

	hFile = CreateFile( lpszFilePath
		, GENERIC_READ
		, FILE_SHARE_READ | FILE_SHARE_WRITE
		, NULL
		, OPEN_EXISTING
		, 0
		, 0
		);

	if( hFile != INVALID_HANDLE_VALUE )
	{
		CloseHandle( hFile );
		bExist = TRUE;
	}

	return bExist;
}


//-------------------------------------------------------------------------
// Function Name    :ExtractResourceToFile
// Parameter(s)     :UINT nResID			The resource id
//					:LPCTSTR lpszFilename	The file name
//					:HMODULE hModule		The module handle
// Return           :BOOL					Success on TRUE
// Create			:2007-1-8 17:35		Jerry.Wang
// Memo             :Extract resource to file
//-------------------------------------------------------------------------
BOOL CUtility::ExtractResourceToFile( LPCTSTR lpszType
									 , UINT nResID
									 , LPCTSTR lpszFilename
									 , HMODULE hModule
									 )
{
	HRSRC hRes = ::FindResource( hModule, MAKEINTRESOURCE(nResID), lpszType);
	if( hRes == NULL )
	{
		ATLASSERT(FALSE);
		return FALSE;
	}

	DWORD dwSize = ::SizeofResource( hModule, hRes); 
	if( dwSize == 0 )
	{
		ATLASSERT(FALSE);
		return FALSE;
	}

	HGLOBAL hGlobal = ::LoadResource( hModule, hRes); 
	if( hGlobal == NULL )
	{
		ATLASSERT(FALSE);
		return FALSE;
	}

	LPVOID pBuffer = ::LockResource(hGlobal); 
	if( pBuffer == NULL )
	{
		ATLASSERT(FALSE);
		::FreeResource(hGlobal); 
		return FALSE;
	}

	HANDLE hFile = ::CreateFile( lpszFilename
		, GENERIC_WRITE
		, FILE_SHARE_WRITE | FILE_SHARE_READ
		, NULL
		, CREATE_ALWAYS
		, 0
		, NULL
		);
	if( hFile == NULL )
	{
		ATLASSERT(FALSE);
		::FreeResource(hGlobal); 
		return FALSE;
	}

	DWORD dwWritten = 0;
	::WriteFile( hFile, pBuffer, dwSize, &dwWritten, NULL);
	if( dwWritten != dwSize )
	{
		ATLASSERT(FALSE);
		::FreeResource(hGlobal); 
		return FALSE;
	}

	::FlushFileBuffers(hFile);
	::CloseHandle(hFile);
	::FreeResource(hGlobal); 

	return TRUE;
}// ExtractResourceToFile


//-------------------------------------------------------------------------    
// Function Name    :LoadImage
// Parameter(s)     :UINT nID				The resource id
//					:LPCTSTR lpszType		The resource type
//					:HINSTANCE hInstance	The module handle
// Return           :Image *
// Create			:2009-3-12 14:28 Jerry.Wang                                     
// Memo             :Load the GDIPlus::Image from resource
//-------------------------------------------------------------------------   
Image * CUtility::LoadImage( UINT nID, LPCTSTR lpszType, HINSTANCE hInstance /*=NULL*/)
{
	Image * pImage = NULL;

	if( lpszType == RT_BITMAP )
	{
		HBITMAP hBitmap = ::LoadBitmap( hInstance, MAKEINTRESOURCE(nID) );
		pImage = (Image*)Bitmap::FromHBITMAP(hBitmap, 0);
		::DeleteObject(hBitmap);
		return pImage;
	}		

	hInstance = (hInstance == NULL) ? ::AfxGetResourceHandle() : hInstance;
	HRSRC hRsrc = ::FindResource ( hInstance, MAKEINTRESOURCE(nID), lpszType); 
	ASSERT(hRsrc != NULL);

	DWORD dwSize = ::SizeofResource( hInstance, hRsrc);
	LPBYTE lpRsrc = (LPBYTE)::LoadResource( hInstance, hRsrc);
	ASSERT(lpRsrc != NULL);

	HGLOBAL hMem = ::GlobalAlloc(GMEM_FIXED, dwSize);
	LPBYTE pMem = (LPBYTE)::GlobalLock(hMem);
	memcpy( pMem, lpRsrc, dwSize);
	IStream * pStream = NULL;
	::CreateStreamOnHGlobal( hMem, FALSE, &pStream);

	pImage = Image::FromStream(pStream);

	::GlobalUnlock(hMem);
	pStream->Release();
	::FreeResource(lpRsrc);

	return pImage;
}

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
Jerry is from China. He was captivated by computer programming since 13 years old when first time played with Q-Basic.



  • Windows / Linux & C++
  • iOS & Obj-C
  • .Net & C#
  • Flex/Flash & ActionScript
  • HTML / CSS / Javascript
  • Gaming Server programming / video, audio processing / image & graphics


Contact: vcer(at)qq.com
Chinese Blog: http://blog.csdn.net/wangjia184

Comments and Discussions