How to add font to system, programmatically?

29 10 2008

! It was my favorite game during my childhood. Chopping down monsters, bats… Wow!!! I love it. Every time when i start the program one thing that attracted me is its font.Later when I searched net, i found that there is a font named diablo. But what will happen if the font is not present in the system? The game screen will be wired? The application might be installing the font, if its not present in the system. But how to do it?

Well, You can use the api – AddFontResource(). You have to provide the path of font file to be added to system. Have a look at the code snippet.

// Path of font to be added.
CString csFontPath = _T( "C:\\Diablo.ttf" );

// Add font to system.
if( 0 == AddFontResource( csFontPath ))
   // Adding font to system failed.

Well, the latest version of diablo is Diablo III. Its superb! Have a look at it. 😉

Targeted Audience – Intermediate.

How to kill process in batch by using wildcard filters?

24 10 2008

I often get irritated while working in huge frameworks. A lot of application might be running simultaneously and it fills up my task bar. Usually I drag and adjust my taskbar, 3X more taller than usual. But still they fill up my taskbar. Most of them were servers running in command prompts or executable have some common prefix and Its a real burden while closing all windows. I’ve to close them one by one. Is there any command gun to kill them all in one shot? 😉

You could use the command taskkill. Have a look at the syntax.

Syntax: taskkill /IM <ExeName>
C:\>taskkill /IM note*
SUCCESS: The process "notepad.exe" with PID 2428 has been terminated.
SUCCESS: The process "notepad.exe" with PID 3588 has been terminated.
SUCCESS: The process "notepad.exe" with PID 4960 has been terminated.
SUCCESS: The process "notepad.exe" with PID 3320 has been terminated.
SUCCESS: The process "notepad.exe" with PID 4704 has been terminated.

Note that taskkill command is available from Windows XP onwards. So take care.

Targeted Audience – Beginners.

How to set Transparent Dialogs?

7 10 2008

The Invisible Man” by HG Wells. I still remember reading the translated version of that classic, when i was a kid. And even thought to conduct some experiments to become invisible. You could guess, what happened then. 🙂 I couldn’t. But now I feel happy that atleast I could find a magic portion which can make dialogs invisible. 😉  Well, how to change the transparency of dialogs?

The secret is Layered windows. For that you’ve to enable WS_EX_LAYERED style and set the alpha of dialog by calling SetLayeredWindowAttributes(). See the code snippet below.

// Enable WS_EX_LAYERED window extended style.
LONG ExtendedStyle = GetWindowLong( GetSafeHwnd(),
                                    GWL_EXSTYLE );
SetWindowLong( GetSafeHwnd(),
               ExtendedStyle | WS_EX_LAYERED );

// Select the transparency percentage.
// The alpha will be calculated accordingly.
double TransparencyPercentage = 50.0;

// Set the alpha for transparency.
// 0 is transparent and 255 is opaque.
double fAlpha = TransparencyPercentage * ( 255.0 /100 );
BYTE byAlpha = static_cast<BYTE>( fAlpha );
SetLayeredWindowAttributes( GetSafeHwnd(),
                            LWA_ALPHA );

Layered windows are available from Windows 2000 onwards. So don’t forget to add _WIN32_WINNT=0x0500 to project settings for preparing the dialog invisible portion. 😉

Targeted Audience – Intermediate.

How to adjust the drop down width of ComboBox?

5 10 2008

Combobox is good, because they utilize less space and at the same time they can show a list of options. But did you noticed one thing? By default the dropdown width of combobox is same as the width of combobox itself. If you add a loooong string to combobox, it will be displayed partially in the drop down list. So how to stretch the with of dropdown list of combobox?

If you are using MFC, you could use the api – CComboBox::SetDroppedWidth() or else you could use the message CB_SETDROPPEDWIDTH. First of all you’ve to iterate all strings in the combobox list and find out the required width. Then set the new drop down width of combobox. See the MFC code snippet for doing so. Code snippet is taken from MSDN and has been modified appropriately.

void CComboBoxDemoDlg::AdjustDropDownWidth()
    // Find the longest string in the combo box.
    CComboBox* pComboBox =
        ( CComboBox* ) GetDlgItem( IDC_CMB_STRINGS );
    int MaxWidth = 0;
    CDC* pDC = pComboBox->GetDC();

    // Iterate through all strings in Combobox and get MaxWidth
    CString String;
    CSize TextSize;
    for ( int Index = 0; Index < pComboBox->GetCount(); Index++ )
        // Get n'th string.
        pComboBox->GetLBText( Index, String );

        // Get TextExtend
        TextSize = pDC->GetTextExtent( String );

        // Get MaxWidth.
        if( > MaxWidth )
            MaxWidth =;

    pComboBox->ReleaseDC( pDC );

    // Adjust the width for the vertical scroll bar and
    // the left and right border.
    MaxWidth += ::GetSystemMetrics(SM_CXVSCROLL) +
                2 * ::GetSystemMetrics(SM_CXEDGE);

    // Set the dropdown width of combobox.
    pComboBox->SetDroppedWidth( MaxWidth );

You could achieve the same by sending CB_SETDROPPEDWIDTH by using SendMessage().

Targeted Audience – Intermediate.

_chdir() pitfall – error while opening file.

4 10 2008

I still remember that bug! Because it grabbed my two days and delayed the delivery. Well, the story goes like this – It was during late summer of 2004. I was working in a DVD writer project. Internally we were using a third party DVD writer library named BHA Gold. Well, my project was a wrapper library for the BHA Gold library.

Well, the bug was this – There is a config file in my project, which exist in the same folder, together with exe. Before writing DVD, I’ve to read some settings from the config file. But the bug is this. During startup, I could open the file. But if I write one DVD using BHA gold, then when i tried to open the same file once again, it shows error!

At last its been found that the culprit is the api – _chdir(). The DVD writing library was calling this api internally to prepare the directory tree, and once this api is called, the default directory will be changed to the specified directory and during next time, while opening the setting file, it shows an error that “file not found”! Since the setting file does not exist in the new default directory.

Well, in such cases you could use the api – SetCurrentDirectory(). See the code snippet to set the current directory as exe’s folder path.

#include "Shlwapi.h"
// Get the full path of current exe file.
TCHAR FilePath[MAX_PATH] = { 0 };
GetModuleFileName( 0, FilePath, MAX_PATH );

// Strip the exe filename from path and get folder name.
PathRemoveFileSpec( FilePath );    

// Set the current working directory.
SetCurrentDirectory( FilePath );

Don’t forget to add Shlwapi.lib to project settings. 😉

Targeted Audience – Advanced.

I’m Back Again!!!

3 10 2008


Life! Everything that happens are so unexpected. During last few weeks, me too had some unexpected encounters in my personal life. The bad news is, during those days, I couldn’t make Weseetips alive. 😦 Well, the good news is, at last I’ve overcame the barriers and am smiling now! 😀 Now I am back to business! I thank my fellow readers for being patient. Yes! Lets rock on…