Get size (rectangle) of complete virtual screen (all display monitors together)



Here is the code to get the complete virtual screen rectangle that includes all display monitors. Code is in C++/MFC.

//gets the coordinates of the complete virtual screen which is includes all the monitors present
CRect GetVirtualScreenRect()
{
	CRect rcVirtualScreen;
	rcVirtualScreen.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
	rcVirtualScreen.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
	rcVirtualScreen.right = rcVirtualScreen.left + GetSystemMetrics(SM_CXVIRTUALSCREEN);
	rcVirtualScreen.bottom = rcVirtualScreen.top + GetSystemMetrics(SM_CYVIRTUALSCREEN);
 
	return rcVirtualScreen;
}


Check if email address is valid in C++/MFC



 
Here is the C++/MFC code to check if an email address is valid. 
 
bool IsEmailAddressValid(CString emailAddress)
{
	emailAddress.Trim();
 
	if (emailAddress.IsEmpty()) return false;
 
	int idx;
	CString rightPart;
 
	idx = emailAddress.Find(_T('@'));
	if (idx == -1) return false;
 
	//is the first character @?
	if (idx == 0) return false;
 
	rightPart = emailAddress.Right(emailAddress.GetLength() - idx - 1);
 
	idx = rightPart.Find(_T('.'));
	if (idx == -1) return false;
 
	//character immediately after @ is .?
	if (idx == 0) return false;
 
	//is there only period?
	if (rightPart.GetLength() == 1) return false;
 
	///is the last character period and no characters after that?
	if (rightPart.GetLength() - 1 == idx) return false;
 
	return true;
 
}


Convert array/list to string using a separator in C++ (MFC)



Following is the code to convert array to string using a specific separator. Language used is C++ using MFC (Microsoft Foundation Classes).

This function can trim each term in the array and also ignore blank (empty) string.

You can easily modify it for list or any other similar collection class.

void ArrayToString(CAtlArray<CString> & arrValue, CString & strValue, CString strSeparator
					, BOOL bTrim/*=TRUE*/, BOOL bIgnoreEmptyString/*=TRUE*/)
{
	int i;
	strValue.Empty();
	CString strItem;
 
	//append all strings with ; as separator
	for(i=0;i<int(arrValue.GetCount());i++)
	{
		strItem = arrValue[i];
 
		if (bTrim)
		{
			strItem.Trim();
		}
 
		if (bIgnoreEmptyString)
		{
			if (strItem.IsEmpty())
			{
				continue;
			}
		}
 
		strValue += strItem;
 
 
		if (i < (int(arrValue.GetCount())-1))
		{
			strValue += strSeparator;
		}
 
	}
 
}


Splitting CString to array in MFC



The following function converts string to array in MFC & ATL. By default it trims each string in the array and also ignores empty strings. It uses CString MFC class and CAtlArray ATL class.

//converts semicolon separated string to array of strings
void StringToArray(CString & strValue, CAtlArray<CString> & arrValue, TCHAR szSeparator, BOOL bTrim/*=TRUE*/, BOOL bIgnoreEmptyString/*=TRUE*/)
{
	int idx;
	CString strTemp,strRest;
 
	strRest = strValue;
 
	if (strRest.IsEmpty()) {
		arrValue.RemoveAll();
		return;
	}
	do
	{
		idx = strRest.Find(szSeparator);
 
		if (idx != -1)
		{
			//take the left part
			strTemp = strRest.Left(idx);
 
			if (bTrim)
			{
				strTemp.Trim();
			}
 
			if ( (bIgnoreEmptyString) && strTemp.IsEmpty() )
			{
				//ignore
			}else
			{
				arrValue.Add(strTemp);
			}
 
			strRest = strRest.Right(strRest.GetLength()-idx-1);
 
		}else
		{
			if (bTrim)
			{
				strRest.Trim();
			}
 
			if (!strRest.IsEmpty())
			{
				//this is the last element, so lets add it
				arrValue.Add(strRest);
			}
		}
 
	}while(idx != -1);
 
}