Archive for July, 2008

Placing Dialog at the center

How can we center our MFC Dialog on the center of the screen ?

MFC provides one function named CenterWindow(); for accomplishing this task.
But CenterWindow() centers a window relative to its parent. You can set the position relative to the screen by calling another function before CenterWindow().
ie CWnd::GetDesktopWindow()

the code snippet is just like.

BOOL CTest11Dlg::OnInitDialog()
CWnd* pWnd = GetDesktopWindow();
return TRUE


There is no limit to what you can imagine!


An Internet Protocol (IP) address control allows the user to enter an IP address in an easily understood format.

How can we Set/Get an IP address from an CIPAddressCtrl?

MFC provides two methods like CIPAddressCtrl::GetAddress() and CIPAddressCtrl::SetAddress() for getting and setting data from/to the control.

You can also set/get an IP Address by using one more simple method.

Let me provide one clue here. 😉

class CIPAddressCtrl : public CWnd

Got anything ?? Neh ?? 🙂

Ok.. no problem..

class CIPAddressCtrl is derived from CWnd. so..??

SetWindowText() and GetWindowText() is a member of CWnd. 🙂

So you can easily set/get data to/from IPAddressCtrl.

like this..

CString csIPAddress(_T("255.125.25");
CIPAddressCtrl ipAddressCtrl;




Thanks to my buddy..

Keep smiling Always 🙂

 If the facts don't fit the theory, change the facts!

Using SHBrowseForFolder()

Most of the MFC GUI based applications always deals with saving or opening data files to or from physical media. Its is a good practice, that allow the user to select location for these operations. The familiar CFileDialog() is doing the things so neatly.

The description from MSDN is like this…

Common file dialog boxes provide an easy way to implement File Open and File Save As dialog boxes (as well as other file-selection dialog boxes) in a manner consistent with Windows standards.

ok.. ??

Good 🙂

CFileDialog is using basically for dealing with FILES.

For FOLDER based operations, for eg:selecting a folder rather than selecting a File, windows provides another dialog. Like this.

Windows provide a set of Shell functions for performing these types of operations.

We can create a simple dialog by using the following small..small code. 🙂

    BROWSEINFO   bi; 
    ZeroMemory(&bi,   sizeof(bi)); 
    TCHAR   szDisplayName[MAX_PATH]; 
    szDisplayName[0]    =   '';  

    bi.hwndOwner        =   NULL; 
    bi.pidlRoot         =   NULL; 
    bi.pszDisplayName   =   szDisplayName; 
    bi.lpszTitle        =   _T("Please select a folder for storing received files :"); 
    bi.ulFlags          =   BIF_RETURNONLYFSDIRS;
    bi.lParam           =   NULL; 
    bi.iImage           =   0;  

    LPITEMIDLIST   pidl   =   SHBrowseForFolder(&bi);
    TCHAR   szPathName[MAX_PATH]; 
    if   (NULL   !=   pidl)
         BOOL bRet = SHGetPathFromIDList(pidl,szPathName);
         if(FALSE == bRet)

Compile the code..

Ten,Nine,Eight,Seven,Six,Five,Four,Three,Two,One,Zero, LIFT OFF…..



Hope most of them seen the Options Dialog provided by the MSN Messenger. It looks like this..

MSN Settings Dialog

MSN Settings Dialog

Note the Height of the Items in the Left side ListBox. How can we achieve that in our MFC program ?

By Default, Windows provide the minimum height for each of the item. Like this


You can increase the height by using the member function SetItemHeight(..).

extern CListBox* pmyListBox;
pmyListBox->SetItemHeight( 0, 25 );

Now our dialog also showing the same effect 🙂

Feeling good now ??

Yes.. really…


This is a common test case, proposed by most of the Test Engineers for MFC applications. Yes, really its a good scenario. 🙂

By default MFC provide no APIs or methods to satisfy this. But, by using MUTEX, the work is so simple.

For this we need a GUID (Globally Unique Identifier). You can create this by using the MFC application


So the steps is as follows.

1. Create a GUID

2. Implement one method for eg:

BOOL CThreadTestApp::SingleTest (LPSTR szName)
        HANDLE hMutex = CreateMutex (NULL, TRUE, szName);
        if (GetLastError() == ERROR_ALREADY_EXISTS)
           return FALSE;
return TRUE;

3. Call the method in InitInstance() like

if (SingleTest (_T("SingleTest_48C56927-A0DB-4e31-8C32-FE15FBA45043")))
   AfxMessageBox(_T("Error: application is already running!"));
   return FALSE;

Ok na?


const_cast is an odd !!!

const_cast Operator

Most of them noticed the peculiarity of  “const_cast”. The const_cast operator can be used to remove the const, volatile, and __unaligned attribute(s) from a class.

You cannot use const_cast for any other types of casts, such as casting a base class pointer to a derived class pointer. If you do so, the compiler will report an error.

Keep smiling.. 🙂

Resources such as Bitmap,Cursor,Icon etc can be attached to Win32/MFC projects as well as to DLL projects, really 🙂

Suppose we have a DLL Project, and as part of the project we have some resources. How can I call one function implemented in the DLL project from one calling project or application ??

Simple ??  really !!!..

you are right.. you can find a function named FindResource().

as from MSDN the description of FindResource() is as below

The FindResource function determines the location of a resource with the specified type and name in the specified module.

Happy  🙂 ???

Hope your problems are solved now

then what’s the problem??

Instead of default NULL argument , we must pass the Module Handle.

You can get the Module Handle by the function GetModuleHandle(NULL);

But unfortunately, GetModuleHandle(NULL) will return a handle of the calling process (.exe) which is not good for you. 😦



Just pass the name of the DLL in the GetModuleHandle();

for example GetModuleHandle(_T(“SampleDLL”);

HMODULE hMod = GetModuleHandle(_T("SampleDLL.dll"));
if(hSrc == NULL)
      return 0;