Programmatically send keystrokes to active current window in Windows



Here is the code to send keys (key strokes) to active current application in Windows using C++/Win API.

void SendKey(WORD wVkey)
{
	INPUT inputs[2];
	inputs[0].type = inputs[1].type = INPUT_KEYBOARD;
	inputs[0].ki.dwExtraInfo = inputs[1].ki.dwExtraInfo = 0;
	inputs[0].ki.wVk = inputs[1].ki.wVk = wVkey;
	inputs[0].ki.dwFlags = 0;
	inputs[1].ki.dwFlags = KEYEVENTF_KEYUP;
 
	SendInput(2, inputs, sizeof(INPUT));
}


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;
		}
 
	}
 
}