Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / MFC

Exile 1.8 - The Password Manager

Rate me:
Please Sign up or sign in to vote.
4.57/5 (51 votes)
6 Mar 20058 min read 255.3K   7.4K   111  
Yet another password manager.
// AdvancedDialog.cpp : implementation file
//

#include "stdafx.h"
#include "exile.h"
#include "AdvancedDialog.h"
#include "typedefs.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAdvancedDialog dialog


CAdvancedDialog::CAdvancedDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CAdvancedDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAdvancedDialog)
	m_bEnable = FALSE;
	m_nLogin = 0;
	m_nPassword = 0;
	m_strWindowTitle = _T("");
	//}}AFX_DATA_INIT
	m_hFind = (HICON)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_FIND), IMAGE_ICON, 16, 16, 0);
	m_hFound = (HICON)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_FOUND), IMAGE_ICON, 16, 16, 0);
	m_hSearch = (HCURSOR)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDC_SEARCH), IMAGE_CURSOR, 32, 32, 0);
}

CAdvancedDialog::~CAdvancedDialog()
{
	::DestroyIcon(m_hFind);
	::DestroyIcon(m_hFound);
	::DestroyCursor(m_hSearch);
}


void CAdvancedDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAdvancedDialog)
	DDX_Control(pDX, IDC_FIND_WINDOWTITLE, m_ctrlFindWindowTitle);
	DDX_Control(pDX, IDC_FIND_PASSWORD, m_ctrlFindPassword);
	DDX_Control(pDX, IDC_FIND_LOGIN, m_ctrlFindLogin);
	DDX_Check(pDX, IDC_ENABLE, m_bEnable);
	DDX_Text(pDX, IDC_LOGIN, m_nLogin);
	DDX_Text(pDX, IDC_PASSWORD, m_nPassword);
	DDX_Text(pDX, IDC_WINDOWTITLE, m_strWindowTitle);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAdvancedDialog, CDialog)
	//{{AFX_MSG_MAP(CAdvancedDialog)
	ON_BN_CLICKED(IDC_ENABLE, OnEnable)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAdvancedDialog message handlers

BOOL CAdvancedDialog::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// Grouping controls
	m_gcControls.AddControl(1, GetDlgItem(IDC_WINDOWTITLE)->GetSafeHwnd());
	m_gcControls.AddControl(1, GetDlgItem(IDC_LOGIN)->GetSafeHwnd());
	m_gcControls.AddControl(1, GetDlgItem(IDC_PASSWORD)->GetSafeHwnd());
	m_gcControls.AddControl(1, GetDlgItem(IDC_FIND_WINDOWTITLE)->GetSafeHwnd());
	m_gcControls.AddControl(1, GetDlgItem(IDC_FIND_LOGIN)->GetSafeHwnd());
	m_gcControls.AddControl(1, GetDlgItem(IDC_FIND_PASSWORD)->GetSafeHwnd());

	m_gcControls.SwitchControls(1, (m_bEnable ? CGroupedControls::SWS_ENABLE : CGroupedControls::SWS_DISABLE));

	// Beauty
	m_ctrlFindWindowTitle.SetIcon(m_strWindowTitle.IsEmpty() ? m_hFind : m_hFound);
	m_ctrlFindLogin.SetIcon(m_nLogin <= 0 ? m_hFind : m_hFound); // I guess IDs should be > than 0
	m_ctrlFindPassword.SetIcon(m_nPassword <= 0 ? m_hFind : m_hFound);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CAdvancedDialog::OnEnable() 
{
	UpdateData();

	m_gcControls.SwitchControls(1, (m_bEnable ? CGroupedControls::SWS_ENABLE : CGroupedControls::SWS_DISABLE));
}

void CAdvancedDialog::OnLButtonDown(UINT nFlags, CPoint point) 
{
	HWND hWnd = ::RealChildWindowFromPoint(*this, point);

	if((0 != hWnd) && m_bEnable)
	{
		if(hWnd == m_ctrlFindWindowTitle.GetSafeHwnd())
		{
			m_bSearch = TRUE;
			m_nSearch = WINDOWTITLE;
			::SetCapture(*this);
			m_hOld = ::SetCursor(m_hSearch);
		} // if
		else if(hWnd == m_ctrlFindLogin.GetSafeHwnd())
		{
			m_bSearch = TRUE;
			m_nSearch = LOGIN;
			::SetCapture(*this);
			m_hOld = ::SetCursor(m_hSearch);
		} // else if
		else if(hWnd == m_ctrlFindPassword.GetSafeHwnd())
		{
			m_bSearch = TRUE;
			m_nSearch = PASSWORD;
			::SetCapture(*this);
			m_hOld = ::SetCursor(m_hSearch);
		} // else if
	} // if

	CDialog::OnLButtonDown(nFlags, point);
}

void CAdvancedDialog::OnLButtonUp(UINT nFlags, CPoint point) 
{
	if(m_bSearch)
	{
		::ReleaseCapture();
		::SetCursor(m_hOld);
		m_bSearch = FALSE;
		m_nSearch = NOTHING;
	} // if

	CDialog::OnLButtonUp(nFlags, point);
}

void CAdvancedDialog::OnMouseMove(UINT nFlags, CPoint point) 
{
	if(m_bSearch)
	{
		ClientToScreen(&point);
		HWND hWnd = ::GetAncestor(::WindowFromPoint(point), GA_ROOT);

		// Do not process ourselves
		if((0 != hWnd) && ((HWND)*this != hWnd) && ((HWND)*GetParent() != hWnd))
		{
			// What are we looking for
			if(WINDOWTITLE == m_nSearch)
			{
				UpdateData(TRUE);

				m_strWindowTitle = GetWindowTitle(hWnd);
				m_ctrlFindWindowTitle.SetIcon(m_strWindowTitle.IsEmpty() ? m_hFind : m_hFound);

				UpdateData(FALSE);
			} // if
			else if(LOGIN == m_nSearch)
			{
				// Return if window titles do not match
				UpdateData(TRUE);

				if(!m_strWindowTitle.IsEmpty())
				{
					if(m_strWindowTitle != GetWindowTitle(hWnd))
						return;
				} // if
				else
				{
					m_strWindowTitle = GetWindowTitle(hWnd);
				} // else

				// ChildWindowFromPoint() required client coordinates
				::ScreenToClient(hWnd, &point);
				m_nLogin = ::GetDlgCtrlID(::ChildWindowFromPoint(hWnd, point));
				::ClientToScreen(hWnd, &point);

				m_ctrlFindLogin.SetIcon(m_nLogin <= 0 ? m_hFind : m_hFound); // I guess IDs should be > than 0

				UpdateData(FALSE);
			} // else if
			else if(PASSWORD == m_nSearch)
			{
				// Return if window titles do not match
				UpdateData(TRUE);

				if(!m_strWindowTitle.IsEmpty())
				{
					if(m_strWindowTitle != GetWindowTitle(hWnd))
						return;
				} // if
				else
				{
					m_strWindowTitle = GetWindowTitle(hWnd);
				} // else

				// ChildWindowFromPoint() required client coordinates
				::ScreenToClient(hWnd, &point);
				m_nPassword = ::GetDlgCtrlID(::ChildWindowFromPoint(hWnd, point));
				::ClientToScreen(hWnd, &point);

				m_ctrlFindPassword.SetIcon(m_nPassword <= 0 ? m_hFind : m_hFound);

				UpdateData(FALSE);
			} // else if
		} // if

		ScreenToClient(&point);
	} // if
	
	CDialog::OnMouseMove(nFlags, point);
}

CString CAdvancedDialog::GetWindowTitle(HWND hWnd)
{
	// Just return title of hWnd
	CString strTitle;
	TCHAR szTitle[nWindowTitleLength];

	::GetWindowText(::GetAncestor(hWnd, GA_ROOT), szTitle, nWindowTitleLength);

	strTitle = szTitle;

	return strTitle;	
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
I'll think about it later on...

Comments and Discussions