Click here to Skip to main content
15,884,472 members
Articles / Desktop Programming / MFC

Window Unhidder

Rate me:
Please Sign up or sign in to vote.
4.69/5 (15 votes)
14 Feb 20061 min read 39.7K   1.5K   40  
A utility to list and hide/unhide all application windows.
// ListCtrlEx.cpp : implementation file
//

#include "stdafx.h"
#include "ListCtrlEx.h"

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

/////////////////////////////////////////////////////////////////////////////
// CListCtrlEx

CListCtrlEx::CListCtrlEx()
{
	m_doSort = true;
}

CListCtrlEx::~CListCtrlEx()
{
}


BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
	//{{AFX_MSG_MAP(CListCtrlEx)
	ON_NOTIFY_REFLECT(LVN_COLUMNCLICK, OnColumnclick)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CListCtrlEx message handlers
BOOL CListCtrlEx::AddColumn(LPCTSTR strItem,int nItem,int tam,int nFmt,int nSubItem,int nMask)
{
	LV_COLUMN lvc;
	lvc.mask = nMask;
	lvc.fmt = nFmt;
	lvc.pszText = (LPTSTR) strItem;
	int tam2 = GetStringWidth(lvc.pszText) + 15;
	tam = tam>tam2?tam:tam2;
	lvc.cx = tam;
	if(nMask & LVCF_SUBITEM){
		if(nSubItem != -1)
			lvc.iSubItem = nSubItem;
		else
			lvc.iSubItem = nItem;
	}
	return InsertColumn(nItem,&lvc);
}
////////////////////////////////////////////
BOOL CListCtrlEx::AddItem(int nItem,int nSubItem,string strItem,int nImageIndex)
{
	return AddItem(nItem, nSubItem, strItem.c_str(), nImageIndex);
}
////////////////////////////////////////////
BOOL CListCtrlEx::AddItem(int nItem,int nSubItem,LPCTSTR strItem,int nImageIndex)
{
	LV_ITEM lvItem;
	lvItem.mask = LVIF_TEXT;
	lvItem.iItem = nItem;
	lvItem.iSubItem = nSubItem;
	lvItem.pszText = (LPTSTR) strItem;
	if(nImageIndex != -1){
		lvItem.mask |= LVIF_IMAGE;
		lvItem.iImage |= LVIF_IMAGE;
	}
	if(nSubItem == 0)
	{
		BOOL res=InsertItem(&lvItem);
		SetItemData(nItem,nItem);
		return res;
	}
	return SetItem(&lvItem);
}
BOOL CListCtrlEx::DelItem(int nItem)
{
	DeleteItem(nItem);
	return TRUE;
}
BOOL CListCtrlEx::DelAll()
{
	DeleteAllItems();
	return TRUE;
}

int CALLBACK Comparacion(LPARAM lParam1, LPARAM lParam2,LPARAM lParamSort)
{

	CListCtrlEx *GE = (CListCtrlEx *)lParamSort;
	int ValorDevuelto = 0;
	if (GE)
	{
	int Item1 = -1;
	int	Item2 = -1;
	int p=-1;
	
	bool rapido=0;
	
	LVFINDINFO FindInfo;
	FindInfo.flags = LVFI_PARAM;
	FindInfo.lParam = lParam1;
	Item1 = GE->FindItem( &FindInfo);
	if(rapido)p=Item1;
	FindInfo.lParam = lParam2;
	Item2 = GE->FindItem( &FindInfo,p);
	if(Item2<0) Beep(400,400);
	// Obtenemos el item por el que debemos ordenar (lo dice TipoOrdenacion)
	CString It1 = GE->GetItemText(Item1,GE->TipoOrdenacion);
	CString It2 = GE->GetItemText(Item2,GE->TipoOrdenacion);
	// Si ya hab�amos ordenado anteriormente por ese tipo de ordenaci�n (flag
	// ordenacionAnterior a true), devolvemos justo lo contrario de la comparaci�n
	// (para poder hacer orden ascendente y descendente)
	
	if(GE->m_SortCallbacks.size() > GE->TipoOrdenacion
		&& GE->m_SortCallbacks[GE->TipoOrdenacion])
		ValorDevuelto = (*(GE->m_SortCallbacks[GE->TipoOrdenacion]))(It1,It2);
	else
		ValorDevuelto =  strcmp(It1,It2);
	
	ValorDevuelto =(GE->OrdenacionAnterior)? -ValorDevuelto :ValorDevuelto ;
	
	return ValorDevuelto;
	}

	return 0;
}

void CListCtrlEx::OnColumnclick(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	if (TipoOrdenacion != pNMListView->iSubItem)
		// Nueva ordenacion:
		OrdenacionAnterior = false;
	else  // Invertimos la ordenacion:
		OrdenacionAnterior = !OrdenacionAnterior;
	// Obtenemos el tipo de ordenacion:
	TipoOrdenacion = pNMListView->iSubItem;
	// Ordenamos:
	if (m_doSort)SortItems( Comparacion, (LPARAM)this);
	
	*pResult = 0;
}

void CListCtrlEx::SetColumnSortCallback(int item, CallBack cb)
{
	if(m_SortCallbacks.size()<item+1)
		m_SortCallbacks.resize(item+1);
	m_SortCallbacks[item] = cb;
}

int IntComp(CString s1, CString s2)
{
	int i1 = atoi(s1);
	int i2 = atoi(s2);
	if(i1==i2)return 0;
	if(i1<i2) return -1;
	return 1;
}

BOOL CListCtrlEx::SelectItem(int item)
{
	return SetItem( item,0,LVIF_STATE,NULL,0,LVNI_SELECTED | LVNI_FOCUSED,LVNI_SELECTED | LVNI_FOCUSED,NULL); 
}

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
Software Developer (Senior)
Spain Spain
Javier Jimenez Shaw. Born in Madrid in 1975. Civil engineer, is working developing financial algorithms for exotic products.

Comments and Discussions