Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / MFC
Tip/Trick

FindWhat

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
29 Jan 2013CPOL1 min read 10.5K   158   3  
Find file or use wildcard i.e. dlg*.h, or *.*

Image 1 

Introduction

This utility allows finding files in your C Projects folder by filename or the use of wildcards i.e. std*.h or *dlg.c and *.* the filenames, file size and the item number are displayed in a ClistCtrl that, when double clicked, opens the appropriate program (i.e. Notepad, Visual Studio IDE, etc.).

Background

I have been collecting C, C++ source code since 1998 (3.5 GB of files) and when I want a piece of code that I think was in a dialog this makes it easier to find.

Using the Code

The program uses the newer CFolderPickerDialog (MSVS 2010) see DirDialog.cpp and .h to use the older CFolderDialog and it resizes the controls see ResizeDlg.cpp and .h.

To use the CFolderPickerDlg:

void CFindWhatDlg::OnBrowse() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);
	CString szPath = _T("C:\\");
	CFolderPickerDialog dlg(szPath, 0, NULL, 0); 
	if (dlg.DoModal()) 
	{
		szPath = dlg.m_szFileName ;
		m_edit1 = szPath;
		m_strFile = szPath;
		UpdateData(FALSE);
	}		
}

I never did like the old tree.

Sample Image

To use ResizeDlg add all the controls in OnInitDialog()

// Buttons
AddControl(IDOK, CST_REPOS, CST_REPOS, CST_NONE, CST_NONE, 0);
AddControl(IDCANCEL, CST_REPOS, CST_REPOS, CST_NONE, CST_NONE, 0);
AddControl(IDC_BROWSE, CST_REPOS, CST_REPOS, CST_NONE, CST_NONE, 0);
AddControl(IDC_SEARCH, CST_REPOS, CST_REPOS, CST_NONE, CST_NONE, 0);

// Edit Controls
AddControl(IDC_EDIT1, CST_RESIZE, CST_RESIZE, CST_NONE, CST_NONE, 1);
AddControl(IDC_EDIT2, CST_RESIZE, CST_RESIZE, CST_NONE, CST_NONE, 1);

// Check Box
AddControl(IDC_NO_CASE, CST_REPOS, CST_REPOS, CST_NONE, CST_NONE, 1);
AddControl(IDC_LOG_FILE, CST_REPOS, CST_REPOS, CST_NONE, CST_NONE, 1);

// List Box
AddControl(IDC_LIST, CST_RESIZE, CST_RESIZE, CST_NONE, CST_RESIZE, 1);

// Static Control
AddControl(IDC_STATIC_SEARCH, CST_NONE, CST_NONE, CST_NONE, CST_REPOS, 1);

Points of Interest

I have used a method: SetSortIcon(int nCol) that I got out of a book on Programming Windows. It works fine if all the fields (columns) are left justified, but if one is right justified and you click it first the other fields become right justified and the file names jump over to the right, yuck.

/*** Display or hide sort icon on column to be sorted ************************/
void CCheckingDlg::SetSortIcon(int nCol)
{
	ASSERT(m_list1);
	CHeaderCtrl* pHeaderCtrl = m_list1.GetHeaderCtrl();
	ASSERT(pHeaderCtrl);
	
	HDITEM hdrItem;
	int    nPhysicalCol = nCol;
	int col;
	for (col = pHeaderCtrl->GetItemCount(); col >= 0; col--)
    {
		hdrItem.mask = HDI_FORMAT | HDI_IMAGE;
		pHeaderCtrl->GetItem(nPhysicalCol, &hdrItem);
		if (m_nSortColumn     != 0 &&
			m_nSortColumn - 1 == col)
		{
			hdrItem.iImage = m_nUpArrow;
			hdrItem.fmt    = hdrItem.fmt & HDF_JUSTIFYMASK |
				HDF_IMAGE | HDF_STRING | HDF_BITMAP_ON_RIGHT;
		}
		else if (m_nSortColumn     != 0 &&
			    -m_nSortColumn - 1 == col)
		{
			hdrItem.iImage = m_nDownArrow;
			hdrItem.fmt    = hdrItem.fmt & HDF_JUSTIFYMASK |
				HDF_IMAGE | HDF_STRING | HDF_BITMAP_ON_RIGHT;
		}
		else
			hdrItem.fmt = hdrItem.fmt & HDF_JUSTIFYMASK | HDF_STRING;
		pHeaderCtrl->SetItem(col, &hdrItem);
    }
}

Here is a better version that keeps the justification of each field.

// Display or hide sort icon on column to be sorted
void CFindWhatDlg::SetSortIcon()
{
	ASSERT(m_list);
	CHeaderCtrl* pHeaderCtrl = m_list.GetHeaderCtrl();
	ASSERT(pHeaderCtrl);
	
	HDITEM hdrItem;
	for (int nCol = 0; nCol < pHeaderCtrl->GetItemCount(); nCol++)
    {
		hdrItem.mask = HDI_FORMAT | HDI_IMAGE;
		pHeaderCtrl->GetItem(nCol, &hdrItem);
		if(bAscending)
		{
			hdrItem.iImage = m_nUpArrow;
			hdrItem.fmt    = hdrItem.fmt & HDF_JUSTIFYMASK |
				HDF_IMAGE | HDF_STRING| HDF_BITMAP_ON_RIGHT;
		}
		else
		{
			hdrItem.iImage = m_nDownArrow;
			hdrItem.fmt    = hdrItem.fmt & HDF_JUSTIFYMASK |
				HDF_IMAGE | HDF_STRING| HDF_BITMAP_ON_RIGHT;
		}
		pHeaderCtrl->SetItem(nCol, &hdrItem);
    }
}

The method that handles the search of wildcards:

void CFindWhatDlg::FindToken(CString szSearch, CString szFile, CString szPath)
{
	CString szStr;
	CString szExt;
	int nLen;
	int nPos;
	nPos = szSearch.FindOneOf("*");
	if(nPos == 0)
	{
		szSearch.Delete(0, 1);
		nPos = szSearch.FindOneOf(".");
	}
	szStr = szSearch.Left(nPos);
	nLen = szSearch.GetLength();
	nPos = szSearch.ReverseFind('.');
	szExt = szSearch.Right(nLen - nPos);
	szFile.MakeLower();
	if(szStr.IsEmpty())
	{
		if(szFile.Find(szExt) > 0)
		{
			m_nCount++;
			AddItem(szPath);
			m_szaFiles.Add(szFullPath);
			Sleep(10);	// Wait for List Control
		}
	}
	else
	if((szFile.Find(szStr) >= 0) && (szFile.Find(szExt) > 0))
	{
		m_nCount++;
		AddItem(szPath);
		m_szaFiles.Add(szFullPath);
		Sleep(10);	// Wait for List Control
	}
}

History

Initial release - January 26, 2013 - Version 1.1

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --