How to Retain Control Size after Dialog Font Change – MFC


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.


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)


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).

Application interface


Very simple. The application looks for and reads the first 4 comma separated integers in each line, which will obviously be the x,y, width and height values of a control. When it gets such a group of values, it will generate the values using the formula in the aforementioned doc. That means, you can also give the desired values in the format: “x,y,width,height” (not “left,top,right,bottom”) to get the converted value, which you will have to do in order to convert co-ordinates of the dialog which in code isn’t properly comma separated.

Note: If the text in new font does not fit in the previous control size, you will need to manually adjust the size of such controls in order for the text to fit in.

Download: DlgUnitFontAdjuster.zip


Yanking the Kill Ring: Emacs

The title of the post might confuse the normal windows users, like me of-course. There was a situation where I’m forced to use Ububtu, a Linux based operating system, and that too in kernel mode only. The scenario is my engineering college where they do this in programming labs in-order to avoid students copying other’s programs by giving them a login name each and restricting GUI mode.

The only editors available is nano, emacs and vi editor. Emacs was my choice as I simply didn’t like nano and wasn’t ready to use simple but powerful vi since it requires memorizing lot of editor commands. As I’m not only a student, but also a software engineer by profession, The transition from an intellisense enabled Visual Studio 2010 to the very basic emacs editor wasn’t cool.

Emacs was more horrible than a notepad editor since don’t know how to do the basic things a software engineer must know: Copy, Paste and Undo. Undo was a nightmare since the shortcut key Ctrl + z in windows will result in the termination of emacs.

Emacs editor have a menubar and you can access it using the key F10. It can be easily navigated and the shortcut for the command can be got from there. Anyway let me list some of the commands below.

  1. Cut / Kill line: Ctrl + K
  2. Copy / Add to kill ring: ALT + W
  3. Paste / Yank: Ctrl + Y
  4. Mark text: Ctrl + @
  5. Undo: Ctrl + _