Count Specific Characters in CString


First of all, CString doesn’t have a direct member function for this. But at times we will need this functionality and it is not that particular not to add it as a member if they can add functions like SpanIncluding, Replace etc. So lets discuss some ways which I found that can get the job done.

SpanIncluding/SpanExcluding

NO!!! This is not even a way to do it. One can think that what if we use SpanExcluding using the character we want as a delimiter and subtract the length of resultant string from the length of original string. Similar  is the case for SpanIncluding. Now, listen to this! These functions will not return all the characters other than the delimiters. Its somewhat continuous. For example, calling SpanExluding for string “abc:cd” with delimiter “:” will return string “abc” only. Same is the case with SpanIncluding.

Remove/Replace

This is the most easiest way in terms of lines of code. Both function returns the count of the character that is replaced or removed. Take care to make a copy of the string before doing this. It involves editing of the string; especially the Remove function requires shifting of characters after the removed character. So this method is not suitable for large strings as it can cause performance hit.

Find (recommended)

I searched a lot for a function that works like magic in a single line of code. The conclusion as I could find was, There is no highway option! The best thing you can do is to use Find function in a while loop to find each character. Is that complex? here’s the code:

int CharacterCount( CString& csString_i, LPCTSTR sChar_i )
{
	if( csString_i.IsEmpty() || L"" == sChar_i )
	{
		return 0;
	}
	int nFind = -1;
	int nCount = 0;
	while( -1 != ( nFind = csString_i.Find( sChar_i, nFind + 1 )))
	{
		nCount++;
	}
	return nCount;
}

Got a better way? Please give your thoughts as comments.

Tagged , , , , , , ,

How to Retain Control Size after Dialog Font Change – MFC


Scenario

Suppose you have designed a dialog that are having a lot of controls, and you made it look perfect. Now your client says: “Oh, The font looks a bit small. Can you enlarge it a bit and keep up the same look and feel?”. So, what’s the big deal lets change the font size. OK, Changed? So what do you see? The size of the dialog including all the controls in it has changed according to the font size and everything got bigger. Now maintaining the same font size, you will need to resize and re-position all the controls in it one by one, which is really a hard thing.

Reason

Well, whats the reason behind this? Your resource file (.rc) uses something called dialog units to specify the position and size of the controls. This thing is completely dependent on the font size you set to the dialog, and that is the reason for the size of each control in the dialog even though there is no text in the same. ( refer the msdn doc: http://support.microsoft.com/kb/145994  to get a better understanding about dialog units)

Solution

One solution is to do all things dynamically, that is using SetWindowPos function to set the correct position and sizes in pixels. But that too is hard as if you already designed it. So I decided to create an application that parse your resource code block, identify the control co-ordinates and convert them in such a way that there will not be a size change according to the font size. All you have to do is:

  1. Run the application
  2. Input the previous font name and font size, the new font size and font name
  3. Open the resource file that has the code of the dialog
  4. Copy the code starting from the line below “BEGIN” statement up to the previous line of “END” statement
  5. Click “Convert” button, and you can see the co-ordinate adjusted code on the edit window on right side.
  6. Replace it to the resource file after comparing the original version (ideally using beyond compare).

[Contd. See next page]

Tagged , , , , , ,
Follow

Get every new post delivered to your Inbox.

Join 216 other followers