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()
{
	SYSTEMTIME st, lt;

	GetSystemTime(&st);
	GetLocalTime(<);

	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;
FLASHWINFO info;
void CreateFlashWindow()
{
//Create a window
m_pMyDlg = new CMyDialog();
m_pMyDlg->Create(IDD_MYDIALOG);

//Show window
m_pMyDlg->ShowWindow(SW_MINIMIZE);

//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()
{
::FlashWindowEx(&info);
}

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;

   do
   {

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

   } 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

Description

 

 

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.

HowCanIDoIt1

 

 

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.

MyCodeSnippet

 

 

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"));
   }
}

MyNote

 

 

Check the GetAsyncKeyState function too in the MSDN.