Archive

Posts Tagged ‘TCHAR’

A TCHAR version of std stream and string classes

Quite simple…

// A TCHAR based std::string
typedef std::basic_string<tchar> tstring;
// A TCHAR based std::ifstream;
typedef std::basic_ifstream</tchar><tchar , std::char_traits<TCHAR> > tstream;
// A TCHAR based std::stringstream
typedef std::basic_stringstream</tchar><tchar , std::char_traits<TCHAR>, std::allocator</tchar><tchar> > tstringstream;

So now no need to worry about UNICODE and ANSI, should work as CString, since TCHAR becomes char/wchar_t based on _UNICODE macro definition.

Also note that stl has provided UNICODE versions of these classes for e.g. wstring, wstringstream, wifstream, but since windows has a type that switches automagically between char/wchar_t, we are making use of it.

So the idea behind this is that stl classes are mostly template based, so this means you can add your own version of an stl class for a custom type just like I’ve done. As a conclusion we can say that std::string can be called a vector<char> but with dedicated string operations.

Advertisements

Get key names as a string!

February 22, 2008 Leave a comment

Well how do you get key names, for eg: when you type “A” you should get a string like “A ” or when you press page down you get a string like “Page Down”!

It’s easy, there is a windows API for this purpose called GetKeyNameText.

LRESULT CMyDlg::WindowProc(UINT Message, WPARAM wParam, LPARAM lParam)
{
   switch( Message )
   {
   case WM_KEYDOWN:
      TCHAR szKeyname[MAX_PATH] = { 0 };
      GetKeyNameText( lParam, szKeyname, MAX_PATH );
      MessageBox( szKeyname );
      break;
   }
   return CDialog::WindowProc(Message, wParam, lParam);
}// End WindowProc
%d bloggers like this: