Click here to Skip to main content
15,895,084 members
Articles / Desktop Programming / ATL

"Select Computer" Dialog

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
16 Apr 20013 min read 203.2K   4.4K   36  
The ATL and MFC versions of the class that implements a dialog for selecting users(computers) within the Windows Network.
// SelectComputerDialog.h: interface for the CSelectComputerDialog class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SelectComputerDialog_H__C20CADC5_5515_44C4_84DA_53972682D1B0__INCLUDED_)
#define AFX_SelectComputerDialog_H__C20CADC5_5515_44C4_84DA_53972682D1B0__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <atlwin.h>

#pragma warning(push)
#pragma warning(disable : 4786)

#include "Sizer.h"

#include "SelectComputerResource.h"
#include "SelectComputerNetwork.h"

#include <vector>
#include <string>

#include <commctrl.h>

using namespace std;

class CSelectComputerDialog : public CDialogImpl<CSelectComputerDialog> 
{
public:
    CSelectComputerDialog();
    virtual ~CSelectComputerDialog();

public:
	enum {IDD = IDD_SELECT_COMPUTER};

public:
	const pair<basic_string<TCHAR>, basic_string<TCHAR> > & GetComputerName() const
	{
		return m_Names[0];
	}

	const vector<pair<basic_string<TCHAR>, basic_string<TCHAR> > > & GetComputerNames() const
	{
		return m_Names;
	}

	bool SingleSelection(bool bSelectionMode = true)
	{	
		bool tmp = m_bSingleSelection;
		m_bSingleSelection = bSelectionMode;
		
		return tmp;
	}

	bool NoALLDomain(bool bOnlyOneDomainInTime = true)
	{
		bool tmp = m_bOnlyOneDomainInTime;
		m_bOnlyOneDomainInTime = bOnlyOneDomainInTime;

		return tmp;
	}

private:
	bool InitComboBox();
	void InitListControl();
	void InitResizeService();

private:
	vector<pair<basic_string<TCHAR>, basic_string<TCHAR> > > m_Names;
	bool	m_bSingleSelection;
	bool	m_bOnlyOneDomainInTime;

    vector<basic_string<TCHAR> > m_vecstrDomains;
    vector<basic_string<TCHAR> > m_vecstrServers;

    CSelectComputerNetwork m_Network;
    auto_ptr<CSizerCollection> m_pSizerCollection;  

    //imagelist for a combo box
    HIMAGELIST m_imagelist;

BEGIN_MSG_MAP( CSelectComputerDialog )
   MESSAGE_HANDLER( WM_INITDIALOG, OnInitDialog )
   COMMAND_HANDLER( IDOK, BN_CLICKED, OnOK )
   COMMAND_HANDLER(IDCANCEL, BN_CLICKED, OnCancel)
   NOTIFY_HANDLER(IDC_LIST, LVN_ITEMCHANGED, OnItemchangedList)
   COMMAND_HANDLER(IDC_COMBO, CBN_SELCHANGE, OnSelchangeCombo)
   MESSAGE_HANDLER(WM_SIZE, OnSize)
   MESSAGE_HANDLER(WM_SIZING, OnSizing)
   MESSAGE_HANDLER(WM_PAINT, OnPaint)
   COMMAND_HANDLER(IDC_COMBO, CBN_DROPDOWN, OnDropdownCombo)
   MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
END_MSG_MAP()

void RefreshListControl(const basic_string<TCHAR>& strDomain);


LRESULT OnOK(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    EndDialog(IDOK);
    return 1;
}


LRESULT OnCancel(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	EndDialog(IDCANCEL);
	return 1;
}

LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	m_imagelist = ImageList_Create(::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), ILC_COLOR, 0, 0);  

	HICON hicon = ::LoadIcon(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_SELECT_COMPUTER_USER));
	ImageList_AddIcon(m_imagelist, hicon);
	::DestroyIcon(hicon);

	hicon = ::LoadIcon(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_SELECT_COMPUTER_USERS));
	ImageList_AddIcon(m_imagelist, hicon);
	::DestroyIcon(hicon);

	InitResizeService();

	InitListControl();

	if (InitComboBox())
		RefreshListControl(m_vecstrDomains[0]);

	InvalidateRect(NULL);

    return 1;
}


LRESULT OnItemchangedList(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)
{
	HWND hwndList = GetDlgItem(IDC_LIST);

	const UINT uiSelectedCount = ListView_GetSelectedCount(hwndList);
	::EnableWindow(GetDlgItem(IDOK), uiSelectedCount);
	
	if (uiSelectedCount)
	{
		m_Names.clear();

		int iItemIndex = ListView_GetNextItem(hwndList, -1, LVNI_SELECTED | LVNI_ALL);

		while (iItemIndex != -1)
		{
			TCHAR szItemText[MAX_PATH];
			ListView_GetItemText(hwndList, iItemIndex, 0, szItemText, MAX_PATH);
			TCHAR szSubItemText[MAX_PATH];
			ListView_GetItemText(hwndList, iItemIndex, 1, szSubItemText, MAX_PATH);

			m_Names.push_back(make_pair(basic_string<TCHAR>(szItemText), basic_string<TCHAR>(szSubItemText)));

			iItemIndex = ListView_GetNextItem(hwndList, iItemIndex, LVNI_SELECTED | LVNI_ALL);
		}
	}
	
	return 1;
}

LRESULT OnSelchangeCombo(WORD /*wNotifyCode*/, WORD /*wID*/, HWND hWndCtl, BOOL& /*bHandled*/)
{
	HWND hwndCombo = hWndCtl;//GetDlgItem(IDC_COMBO);

	const int iItemIndex = ::SendMessage(hwndCombo, CB_GETCURSEL, 0, 0);
    const int iTextLength = ::SendMessage(hwndCombo, CB_GETLBTEXTLEN, iItemIndex, 0);

	auto_ptr<TCHAR> szText(new TCHAR[iTextLength + 1]);

    ::SendMessage(hwndCombo, CB_GETLBTEXT, iItemIndex, reinterpret_cast<LPARAM>(szText.get()));

	RefreshListControl(basic_string<TCHAR>(szText.get()));

	return 1;
}


LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/)
{
	m_pSizerCollection->RecalcLayout(LOWORD(lParam), HIWORD(lParam));
        
	return 1;
}

LRESULT OnSizing(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
	m_pSizerCollection->VerifyDragRectangle(wParam, lParam);
	
	InvalidateRect(NULL, FALSE);

	return 0;
}

LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	RECT rect;
	GetClientRect(&rect);
    rect.left	= rect.right	- ::GetSystemMetrics(SM_CXHSCROLL); 
    rect.top	= rect.bottom	- ::GetSystemMetrics(SM_CYVSCROLL); 
	
	PAINTSTRUCT ps;
	HDC hDC = GetDC();
	BeginPaint(&ps);

	::DrawFrameControl(hDC, &rect, DFC_SCROLL, DFCS_SCROLLSIZEGRIP);

	EndPaint(&ps);
	return 0;
}


LRESULT OnDropdownCombo(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	HWND hwndList = GetDlgItem(IDC_LIST);	
	
	for (int i = 0; i < ListView_GetItemCount(hwndList); ++i)
		ListView_SetItemState(hwndList, i, 0, LVIS_SELECTED);

	return 1;
}

LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	::ImageList_Destroy(m_imagelist);
	return 0;
}

};

#pragma warning(pop)

#endif // !defined(AFX_SelectComputerDialog_H__C20CADC5_5515_44C4_84DA_53972682D1B0__INCLUDED_)

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
Other
Australia Australia
Igor Sukhov is %a someone who can take your project from user requirements to successfull completion% residing in Sydney, NSW, Australia.

Igor has been working on design, development and maintenance (surprise!) of many large-scale business-critical enterprise applications.

Igor holds MS degree in Computer Science/Applied Mathematics from Kazan State University.

MCAD .NET

Tag cloud:

.NET 2.0

C#

Business applications

Design

Enterprise applications

Development

Sydney


Comments and Discussions