How to increase the tooltip display time

21 01 2018

The CToolTipCtrl class provides the functionality of the Windows common tool tip control, a small pop-up window that displays the text describing the purpose of a tool in an application. Sometime you may need to increase the display duration of the tool tip if it contains a lengthy text.

CToolTipCtrl provides SetDelayTime function to sets the initial, pop-up, and reshow durations for a tool tip control.

void SetDelayTime(DWORD dwDuration, int iTime);

dwDuration – DWORD that specifies the type of duration that you want to set.
iTime – Integer that specifies the length of the delay time, in milliseconds.

The type of duration for increasing the tool tip display time is TTDT_AUTOPOP.

CToolTipCtrl m_tooltip;
m_tooltip.SetDelayTime (TTDT_AUTOPOP, 10000); //Set tooltip display time to 10s

The default display duation is based on the double-click time of mouse, which can be calculated using windows API GetDoubleClickTime as shown below:

GetDoubleClickTime() * 10;

How to get the system color of a display element

2 10 2012

Sometime we may need to get the system defined RGB value of a display elements (user controls) like dialog, scrollbar, button, listbox etc. How do we get it?

Windows provides an API GetSysColor to retrieve the current color of the specified display element.

DWORD GetSysColor(int nIndex);

int nIndex – The display element whose color is to be retrieved. It can be CTLCOLOR_DLG, CTLCOLOR_SCROLLBAR, CTLCOLOR_BTN, CTLCOLOR_LISTBOX etc.

The function returns the red, green, blue (RGB) color value of the given element.

void GetSystemDefinedColor()
	//Dialog System defined color
	DWORD dwControlColor = ::GetSysColor(CTLCOLOR_DLG);
	int r = GetRValue(dwControlColor);
	int g = GetGValue(dwControlColor);
	int b = GetBValue(dwControlColor);

	//Listbox System defined color
	dwControlColor = ::GetSysColor(CTLCOLOR_LISTBOX);
	r = GetRValue(dwControlColor);
	g = GetGValue(dwControlColor);
	b = GetBValue(dwControlColor);

SetSysColors API can be used to set the colors for specified display elements.

How to get and set Date and Time

11 02 2012

Sometimes we may really need to set/get date and time values to/from system. So whenever we think about to change these parameters generally we should think about time zones and daylight savings time, because most of the time our applications need to run globally in a safe manner.

The time related functions can return time in different formats. The below two are the commonly used formats.

1) System timeUTC based time (Coordinated Universal Time)
2) Local Time – Time based on local time zone

Win32 API provides different functions to set/get System Time and Local Time separately.
GetSystemTime/SetSystemTime – To set or retrieve current system date and time. This is expressed in UTC.
GetLocalTime/SetLocalTime – To set or retrieve current local date and time.

These all functions are using a pointer to a SYSTEMTIME structure as a parameter to receive or contains the current or new system date and time respectively.

void GetTime()


	CString strSystemTime, strLocalTime;

	strSystemTime.Format( _T("Year = %04d Month = %02d Day = %02d Hour = %02d Min = %02d"),
		st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute);

	strLocalTime.Format( _T("Year = %04d Month = %02d Day = %02d Hour = %02d Min = %02d"),
		lt.wYear, lt.wMonth, lt.wDay, lt.wHour, lt.wMinute);

	AfxMessageBox(_T("SystemTime : " + strSystemTime + "\n") + "LocalTime : " + strLocalTime);

The MFC framework simplified the working with times by introducing two wrapper classes, the CTime and COleDatetime classes.

How to Flash a Window icon in the taskbar

28 01 2012

You must have noticed that in the chat applications like MSN messenger or Google Talk, when we get a message and our chat window is minimized at that instant, it will start blinking on the task bar. That means in any occasion where the application has to notify the user that  it needs their attention to minimized window in the task bar.

Windows applications provide APIs FlashWindow and FlashWindowEx to flashes the specified window.

Before using this function we have to create a FLASHWINFO structure and fill it with flash status of the window and number of times the system has to flash the window.


//CDialog derived class
CMyDialog* m_pMyDlg;
void CreateFlashWindow()
//Create a window
m_pMyDlg = new CMyDialog();

//Show window

//Fill blinking info
if (m_pMyDlg->m_hWnd)
info.cbSize = sizeof(info);
info.hwnd = m_pMyDlg->m_hWnd;
info.dwFlags = FLASHW_ALL;
info.dwTimeout = 0;
info.uCount = 3;

//any time the minimized application window
//need to get user attention, call this method
void OnNotifyUser()

If you just want to flash the window once, use FlashWindow function. To flash the window a specified number of times, use FlashWindowEx functions.

How to read filenames from a folder

6 01 2010



How can we read the names of all files in a folder with a specific extension?



Windows has following APIs FindFirstFile and FindNextFile to get the handle of all files in a folder and thus it can extract the relevant information of a file to a WIN32_FIND_DATA structure.



bool FindFilesFromFolder()
   HANDLE            hFile;
   WIN32_FIND_DATA   FindFileData;
   std::vector fileList;

   char chFolderpath[_MAX_PATH];
   CString strExtension   = _T("*.txt");

   strcpy(chFolderpath, _T("D:\\san\\"));
   strcat(chFolderpath, strExtension);

   hFile = FindFirstFile(chFolderpath, &FindFileData);

   if (hFile == INVALID_HANDLE_VALUE) {

      AfxMessageBox(_T("Inavlid file handle."));
      return false;

   CString filepath;


      filepath.Format(_T("%s%s"), _T("D:\\san\\"), FindFileData.cFileName);

   } while(FindNextFile(hFile, &FindFileData));

   return true;



If you want to get all files from a folder irrespective of its extension then just use *.* as filter.

How to delete a section from an INI file

4 01 2010



If you have ever needed to remove a section completely from an INI file.



We can use WritePrivateProfileStruct API to achieve this. We have to set NULL as the 2nd parameter to delete a section completely and set NULL as the 3rd parameter to delete a particular key only.



void DeleteSectionFromIniFile() 
   WritePrivateProfileStruct(_T("Section 1"), NULL, NULL, 0, _T("C:\\test.ini"));



WritePrivateProfileString too use for the same purpose.

How to read/write an INI file

30 12 2009



INI files are plain text files that contain configuration information. Each INI files contains one or more sections and each sections contain zero or more entries. How shall we read an entry from a section or write to a section.



Read and write operations of an initialization file can be performed bu using the following APIs  GetPrivateProfileString and WritePrivateProfileString respectively.



Read from INI file

void ReadFromIniFile()
   TCHAR szKeyValue[32];
   GetPrivateProfileString(_T("Section 1"), _T("Key 1"), _T("") ,szKeyValue, 32, _T("C:\\test.ini"));


Write to INI file

void WriteToIniFile()
  WritePrivateProfileString(_T("Section 2"), _T("Key 2"), _T("Hello World"), _T("C:\\test.ini"));



Integer value associated with a key in the specified section of an INI file can be retrieved by using the  API GetPrivateProfileInt.

How to check the mouse button state

28 10 2009




A computer mouse is an input pointing device which having two buttons, known as Left & Right buttons. These buttons clicks primary source of events in the Windows Programming model. How can we check whether the mouse button is pressed or not from our application on a particular event.




Windows API provides GetKeyState function to retrieves the status of a specified virtual key. That means we have to use VK_LBUTTON or VK_RBUTTON, which are the virtual keys corresponding to the mouse L and R buttons. If the high order bit is 1, the key is down, otherwise key is up. Actually the return value of this function specifies the status of the given virtual key.




void CheckMouseButtonStatus()
   //Check the mouse left button is pressed or not
   if ((GetKeyState(VK_LBUTTON) & 0x80) != 0)
      AfxMessageBox(_T("LButton pressed"));
   //Check the mouse right button is pressed or not
   if ((GetKeyState(VK_RBUTTON) & 0x80) != 0)
      AfxMessageBox(_T("RButton pressed"));




Check the GetAsyncKeyState function too in the MSDN.

How to get version information of an application

30 03 2009


Sometimes we really want to know the version of our application. It’s not a matter of concern, just right click on the application and look it at on the second tab of the Properties dialog. Ok that is fine. But how could we check it in our C++ application.


Window API’s have different methods to accomplish this. GetFileVersionInfo function retrieves the version information of a file. Call GetFileVersionInfoSize function to get the size of the version information buffer. VerQueryValue function gets the information into a corresponding structure.


void GetFileVersionOfApplication()
   //give your application full path
   LPTSTR lpszFilePath = "D:\\SAN\\MyTest.exe";

   DWORD dwDummy;
   DWORD dwFVISize = GetFileVersionInfoSize( lpszFilePath , &dwDummy );

   LPBYTE lpVersionInfo = new BYTE[dwFVISize];

   GetFileVersionInfo( lpszFilePath , 0 , dwFVISize , lpVersionInfo );

   UINT uLen;

   VerQueryValue( lpVersionInfo , _T("\\") , (LPVOID *)&lpFfi , &uLen );

   DWORD dwFileVersionMS = lpFfi->dwFileVersionMS;
   DWORD dwFileVersionLS = lpFfi->dwFileVersionLS;

   delete [] lpVersionInfo;

   printf( "Higher: %x\n" , dwFileVersionMS );

   printf( "Lower: %x\n" , dwFileVersionLS );

   DWORD dwLeftMost     = HIWORD(dwFileVersionMS);
   DWORD dwSecondLeft   = LOWORD(dwFileVersionMS);
   DWORD dwSecondRight  = HIWORD(dwFileVersionLS);
   DWORD dwRightMost    = LOWORD(dwFileVersionLS);

   CString str;

   str.Format("Version: %d.%d.%d.%d\n" , dwLeftMost, dwSecondLeft,
      dwSecondRight, dwRightMost);



Don’t forget to include Version.lib to your application.

How to get the size of a file without opening it

24 03 2009




Consider a situation that we are recording a WAV file using any of the Windows sound recorders to a particular path. But do you notice that the size of the file is updating every couple of seconds if you look it in the Windows Explorer.  That means Windows Explorer could check the file size without open it even though it is still being recorded.




There is WIN32_FIND_DATA structure for that, which describes a file found by the FindFirstFile, FindFirstFileEx or FindNextFile function. This structure helps us to get the file creation time, last access time, last write time, file size etc. These whole things could be accessed without open the file.




ULONGLONG GetFileSizeEx( CString strPath )
   WIN32_FIND_DATA FindData = { 0 };
   //gets a file search handle
   HANDLE hFirstFile = FindFirstFile( strPath, &FindData );  
   //if the handle is valid
   if( hFirstFile != INVALID_HANDLE_VALUE )
      //closes the file search handle
      FindClose( hFirstFile );
      ULONGLONG FileSize = FindData.nFileSizeHigh;
      FileSize <<= sizeof( FindData.nFileSizeHigh ) * 8; // Push by count of bits
      FileSize |= FindData.nFileSizeLow;
      return FileSize;
   return 0; // File not found




Not all file systems can record creation and last access time and not all file systems can record then in the same manner.