Sunday, April 16, 2006

Closing the dialog smoothly(fade out)

http://www.codeguru.com/cpp/w-d/dislog/animation/article.php/c5063/

I "found" how to close a dialog box smoothly that is fade away.....

code snippets:

#include //for User32.dll
HMODULE hUserDll;
#define WS_EX_LAYERED 0x00080000
#define LWA_ALPHA 0x00000002

hUserDll = ::LoadLibrary(_T("USER32.dll"));
//Change style to layered window style...
::SetWindowLong(m_hWnd, GWL_EXSTYLE, ::GetWindowLong(m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED );
SetTransparent(m_hWnd, 0, 255 * 100/100 , LWA_ALPHA );

//The above will set the window to a layered style and the initial transparency value is set

void CMyDialog::SetTransparent(HWND hWnd, COLORREF crKey,
BYTE bAlpha, DWORD dwFlags)
{
BOOL bRet = TRUE;
typedef BOOL (WINAPI* lpfnSetTransparent)(HWND hWnd,
COLORREF crKey,
BYTE bAlpha,
DWORD dwFlags);

// Check that "USER32.dll" library has been
// loaded successfully...
if (hUserDll )
{
lpfnSetTransparent pFnSetTransparent = NULL;
pFnSetTransparent =
(lpfnSetTransparent)GetProcAddress(hUserDll,
"SetLayeredWindowAttributes");

if (pFnSetTransparent )
bRet = pFnSetTransparent(hWnd, crKey, bAlpha, dwFlags);
else
bRet = FALSE;
} // if( m_hUserDll )

return bRet;
} // End of SetTransparent function

//The above will set the transparency of the window(here dialog)
void CMyDialog::CloseSmoothly()
{
// Increase transparency one percent each time...
for(int nPercent=100; nPercent >= 0 ;nPercent--)
SetTransparent( m_hWnd, 0, 255 * nPercent/100, LWA_ALPHA);
if( hUserDll )
FreeLibrary(hUserDll);
}

Thursday, April 06, 2006

Creating a Tabbed Property Sheet with 2 embedded Property pages:

//This is a simple SDK approach u can also use CPropertypage

Creating a Tabbed Property Sheet with 2 embedded Property pages:

Description:
============
PROPSHEETHEADER m_PropSheet;
PROPSHEETPAGE m_psp[2];

LRESULT CALLBACK SheetProc(HWND, UINT, WPARAM, LPARAM);
//callback for Property sheet
LRESULT CALLBACK Page1DlgProc(HWND, UINT, WPARAM, LPARAM);
//callback for Property Page 1
LRESULT CALLBACK Page2DlgProc(HWND, UINT, WPARAM, LPARAM);
//callback for Property Page 2

memset(m_psp, 0, sizeof(m_psp));
memset(&m_PropSheet, 0, sizeof(m_PropSheet));

//allocate memory for property pages
//initialize property page structure
m_psp[0].dwSize = sizeof(PROPSHEETPAGE);
m_psp[0].dwFlags = PSP_DEFAULT|PSP_USETITLE;
m_psp[0].pszTemplate = (LPCSTR)IDD_PAGE1;
//IDD_PAGE1 is the dialog box for page1,create it in the resource
m_psp[0].pszTitle = "Page 1";
m_psp[0].pfnDlgProc = (DLGPROC)Page1DlgProc;
//Page1DlgProc is the callback procedure for Page1 dialog

//do the same for page 2

m_psp[1].dwSize = sizeof(PROPSHEETPAGE);
m_psp[1].dwFlags = PSP_USETITLE;
m_psp[1].pszTemplate = (LPCSTR)IDD_PAGE2;
m_psp[1].pszTitle = "Page 2";
m_psp[1].pfnDlgProc = (DLGPROC)Page2DlgProc;

//Initialize property sheet structure
m_PropSheet.dwSize = sizeof(PROPSHEETHEADER);
m_PropSheet.dwFlags = PSH_PROPSHEETPAGE | PSH_USECALLBACK;
m_PropSheet.pszCaption = (LPSTR) "Property Sheet";
m_PropSheet.nPages = 2;
m_PropSheet.nStartPage = 0;
m_PropSheet.ppsp = (LPCPROPSHEETPAGE)m_psp;
m_PropSheet.pfnCallback = (PFNPROPSHEETCALLBACK) SheetProc;

Call this code where required say froma dialog or from a window….pass the appropriate handle.
m_PropSheet.hwndParent = CPropSheetDlg::m_hWnd;
PropertySheet(&m_PropSheet);//invokes the property sheet with 2 pages

Source code:

I have popped the property sheet from a Dialog PropsheetDialog on click of "Prop "button

Create an MFC dialog application called PropSheetDlg and add the following code in PropSheetDlg.cpp
This is the propsheetdlg.cpp code

// PropSheetDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PropSheet.h"
#include "PropSheetDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA

// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPropSheetDlg dialog

CPropSheetDlg::CPropSheetDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPropSheetDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPropSheetDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPropSheetDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPropSheetDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPropSheetDlg, CDialog)
//{{AFX_MSG_MAP(CPropSheetDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_PROP, OnProp)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPropSheetDlg message handlers
PROPSHEETHEADER m_PropSheet;
PROPSHEETPAGE m_psp[2];
LOGFONT m_lfont;

LRESULT CALLBACK SheetProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK Page1DlgProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK Page2DlgProc(HWND, UINT, WPARAM, LPARAM);


BOOL CPropSheetDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

memset(m_psp, 0, sizeof(m_psp));
memset(&m_PropSheet, 0, sizeof(m_PropSheet));

m_psp[0].dwSize = sizeof(PROPSHEETPAGE);
m_psp[0].dwFlags = PSP_DEFAULT|PSP_USETITLE;
//m_psp[0].hInstance = NULL;
m_psp[0].pszTemplate = (LPCSTR)IDD_PAGE1;
m_psp[0].pszTitle = "Page 1";
m_psp[0].pfnDlgProc = (DLGPROC)Page1DlgProc;
//m_psp[0].pfnCallback = (LPFNPSPCALLBACK) Page1Proc;

m_psp[1].dwSize = sizeof(PROPSHEETPAGE);
m_psp[1].dwFlags = PSP_USETITLE;
//m_psp[1].dwFlags = PSP_USETITLE | PSP_USECALLBACK;
//m_psp[1].dwFlags = PSP_USECALLBACK;
//m_psp[1].hInstance = hInstance;
m_psp[1].pszTemplate = (LPCSTR)IDD_PAGE2;
m_psp[1].pszTitle = "Page 2";
m_psp[1].pfnDlgProc = (DLGPROC)Page2DlgProc;
//m_psp[1].pfnCallback = (LPFNPSPCALLBACK) Page2Proc;

m_PropSheet.dwSize = sizeof(PROPSHEETHEADER);
m_PropSheet.dwFlags = PSH_PROPSHEETPAGE | PSH_USECALLBACK;
//m_PropSheet.dwFlags = PSH_PROPSHEETPAGE | PSH_USECALLBACK | PSH_MODELESS;
// m_PropSheet.hInstance = hInstance;
m_PropSheet.pszCaption = (LPSTR) "Property Sheet";
m_PropSheet.nPages = 2;
m_PropSheet.nStartPage = 0;
m_PropSheet.ppsp = (LPCPROPSHEETPAGE)m_psp;
m_PropSheet.pfnCallback = (PFNPROPSHEETCALLBACK) SheetProc;

return TRUE; // return TRUE unless you set the focus to a control
}

void CPropSheetDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CPropSheetDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CPropSheetDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

LRESULT CALLBACK SheetProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{


// set a bold font to the tabs
m_lfont.lfHeight = 8;
m_lfont.lfWeight = FW_NORMAL;
m_lfont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
strcpy(m_lfont.lfFaceName, _T("Arial"));
HWND hTabCtrl = PropSheet_GetTabControl(hDlg);
SendMessage(hTabCtrl, WM_SETFONT, (WPARAM)&m_lfont, 0);
switch( message )
{
case WM_INITDIALOG:
return TRUE;
}
return FALSE;
}

// Dialog proc for page 1
LRESULT CALLBACK Page1DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{

switch (message)
{
/*case WM_INITDIALOG:
{
//SetWindowText(GetDlgItem(hDlg, IDC_EDITX), "A * cos(u) * cos(v)");
//SetWindowText(GetDlgItem(hDlg, IDC_EDITY), "A * sin(u) * cos(v)");
// SetWindowText(GetDlgItem(hDlg, IDC_EDITZ), "A * sin(v)");
return TRUE;
}
case WM_COMMAND:
//if the textboxes are changed
if (HIWORD(wParam) == EN_CHANGE) {
SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM)hDlg, 0);
}
break;
case WM_NOTIFY:
switch (((NMHDR FAR *) lParam)->code) {
case PSN_APPLY:
//user clicked APPLY
MessageBox(hDlg, "Apply", "Page1", 0);
break;
case PSN_KILLACTIVE:
//user clicked OK or selected another page
MessageBox(hDlg, "OK", "Page1", 0);
break;
}*/
}
return FALSE;

}

LRESULT CALLBACK Page2DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
/*case WM_INITDIALOG:
return TRUE;
case WM_NOTIFY:
// switch (((NMHDR FAR *) lParam)->code) {
//case PSN_APPLY:
//user clicked APPLY
// MessageBox(hDlg, "Apply", "Page2", 0);
//break;
case PSN_KILLACTIVE:
//user clicked OK or selected another page
// MessageBox(hDlg, "OK", "Page2", 0);
// break;
}*/
}
return FALSE;

}// Page2DlgProc(HWND, UINT WPARAM, LPARAM)


void CPropSheetDlg::OnProp()
{
// TODO: Add your control notification handler code here
m_PropSheet.hwndParent = CPropSheetDlg::m_hWnd;
PropertySheet(&m_PropSheet);


}