CHARFORMAT to LOGFONT



Thought someone might require this conversion.

CharformatToLogfont(CHARFORMAT & cf, LOGFONT & lf, COLORREF & cr)
{
 
	lf.lfCharSet = cf.bCharSet;
	lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	lf.lfEscapement = 0;
	lf.lfOrientation = 0;
	lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
	lf.lfQuality = DEFAULT_QUALITY;
	lf.lfPitchAndFamily = DEFAULT_PITCH;
 
 
	if ( (cf.dwEffects & CFE_BOLD) == CFE_BOLD)
	{
		lf.lfWeight = FW_BOLD;
	}
 
	CDC dc;
	dc.CreateDC(_T("DISPLAY"),NULL,NULL,NULL);
	lf.lfHeight = -MulDiv(cf.yHeight/20, dc.GetDeviceCaps(LOGPIXELSY), 72);
	dc.DeleteDC();
 
	lf.lfUnderline = ( (cf.dwEffects & CFE_UNDERLINE) == CFE_UNDERLINE);
	lf.lfStrikeOut = ( (cf.dwEffects & CFE_STRIKEOUT) == CFE_STRIKEOUT);
	lf.lfItalic = ( (cf.dwEffects & CFE_ITALIC) == CFE_ITALIC);
 
	lf.lfWidth = 0;
	_tcscpy_s(lf.lfFaceName, LF_FACESIZE, cf.szFaceName);
 
	//save color separately because LOGFONT does not accept color
	cr = cf.crTextColor;
 
}


LOGFONT to CHARFORMAT



Thought someone might require this conversion.

 
LogfontToCharformat(LOGFONT & lf, COLORREF & cr, CHARFORMAT & cf)
{
 
	cf.cbSize = sizeof(CHARFORMAT);
	cf.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE | CFM_CHARSET 
        | CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_OFFSET;
	cf.dwEffects = 0;
 
	if (lf.lfWeight >= FW_BOLD)
	{
		cf.dwEffects |= CFE_BOLD;
	}
 
	if (lf.lfUnderline)
	{
		cf.dwEffects |= CFE_UNDERLINE;
	}
 
	if (lf.lfItalic)
	{
		cf.dwEffects |= CFE_ITALIC;
	}
 
	if (lf.lfStrikeOut)
	{
		cf.dwEffects |= CFE_STRIKEOUT;
	}
 
	//temporary create DC
	CDC dc;
	dc.CreateDC(_T("DISPLAY"),NULL,NULL,NULL);
	cf.yHeight = 20*long( 0.5 + fabs(double(72*lf.lfHeight)/dc.GetDeviceCaps(LOGPIXELSY)));
	dc.DeleteDC();
 
	cf.yOffset = 0;
	cf.crTextColor = cr;
	cf.bCharSet = lf.lfCharSet;
	cf.bPitchAndFamily = lf.lfPitchAndFamily;
	_tcscpy_s(cf.szFaceName, LF_FACESIZE, lf.lfFaceName);
}


SendInput Function



We had to use the “SendInput Function” to send keystrokes to any active window in our program PikyFolders.

We had troubles using it first. We also referred to many other samples on the Internet. But still it was not clear that after sending every keystroke we must also send the same keystroke with the KEYEVENTF_KEYUP flag to indicate that the key is released.

Basically, to type the character ‘A’, you would need to fill in 2 INPUT structures, one for pressing (without KEYEVENTF_KEYUP flag) and another for releasing (with KEYEVENTF_KEYUP flag).

So for example, if you want to simulate Shift+A, the array of INPUT structures would be as follows.

Press Shift
Press A
Release A
Release Shift

Hope this helps somebody out there.



Using SetForegroundWindow to activate another process’ window



Thanks to the Dr. Dobb’s Portal article which revealed the secret of bringing another process’ window using SetForegroundWindow API.

Sometimes, you may launch another process using CreateProcess or ShellExecute API. But the focus may remain in the current process instead of the new process that you just spawned. You might see the new process’ window button blinking in the taskbar wanting the user to activate it.

Here is the code that will let you to activate another process’ window.

 
HWND hWndCurrentWindow = GetForegroundWindow();
CString strTitleNewWindow;
strTitleNewWindow.LoadString(IDS_APPTITLE_NEWWINDOW);
HWND hNewWindow = FindWindow(NULL, strTitle);
if (hNewWindow)
{
	DWORD hCurrentWindowThread = GetWindowThreadProcessId( hWndCurrentWindow, NULL );
	DWORD hNewWindowThread = GetWindowThreadProcessId( hNewWindow, NULL);
	AttachThreadInput( hCurrentWindowThread, hNewWindowThread, TRUE );
	SetForegroundWindow(hNewWindow);
	AttachThreadInput( hCurrentWindowThread, hNewWindowThread, FALSE );
}