Thread: _T Macro Question

  1. #1
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483

    _T Macro Question

    I am new to MFC programming.

    How do you display variables using a _T macro

    Code:
    void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point)
    {
    	MessageBox(_T("You Have Left Clicked %d, %d", point.x, point.y), _T("LEFT CLICK"));
    
    }
    as you see i am trying to display where the user has clicked with the mouse

    But im not sure how to display the point.x and point.y falues

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    wsprintf the formatted text into a TCHAR buffer first. Then pass the buffer to MessageBox.

  3. #3
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Im not getting what you are saying... sorry
    could you show me?

    EDIT:

    NVM i got it, Thanks Dente!
    Last edited by mrafcho001; 06-10-2005 at 11:34 AM.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    When I am doing MFC programming (as opposed to API programming) I always use CString. It is an efficent class in terms of memory management, and all the string manipulation and conversion functions are already written (so you don't need to reinvent the wheel).

    For example:

    Code:
    // . . .
    CString str;
    str.Format("You clicked at point %d, %d", pt.x, pt.y);
    MessageBox(str);
    // . . .
    -Rog

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    you dont want to use wsprintf, because what if _UNICODE isn't defined? The correct way would be to use a tchar.h routine like _stprintf().

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Quote Originally Posted by bithub
    you dont want to use wsprintf, because what if _UNICODE isn't defined? The correct way would be to use a tchar.h routine like _stprintf().
    Are you confusing wsprintf (Windows sprintf) with swprintf? Anyway _stprintf is often a better choice as wsprintf provides only a subset of the functionality of _stprintf. For example, it does not support floating point values and has a short maximum buffer length. wsprintf depends on whether UNICODE is defined while _stprintf depends on whether _UNICODE is defined. You should always have both of these symbols defined, or neither.

  7. #7
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Quote Originally Posted by anonytmouse
    Are you confusing wsprintf (Windows sprintf) with swprintf? Anyway _stprintf is often a better choice as wsprintf provides only a subset of the functionality of _stprintf. For example, it does not support floating point values and has a short maximum buffer length. wsprintf depends on whether UNICODE is defined while _stprintf depends on whether _UNICODE is defined. You should always have both of these symbols defined, or neither.

    What do i define them as?

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    For simple stuff, eg:
    Code:
    #define UNICODE
    #define _UNICODE
    #include <windows.h>
    #include <tchar.h>
    It's better to define such macros for your whole project, though; check your compiler documentation for how to do this.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Im an MFC programmer (Well N00b programmer).

    Does TCHAR.h get included from afxwin.h?

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Does TCHAR.h get included from afxwin.h?<<

    No - check the header yourself.

    I don't do much mfc but I assume that if you define the unicode macros for your project, that should be good enough. Hopefully someone with considerably more than my next-to-none mfc experience will clarify this for you shortly.

    tchar.h (again, open it up and take a look) defines a bunch of unicode/non-unicode mappings for mainly string functions and the _T and TEXT macros (these latter two are also defined elsewhere, maybe winnt.h) so you should only include it if you need to. If you're using wsprintf, you won't need tchar.h because wsprintf is an api and should be available for you to use without any extra header includes.

    Actually, if you're using mfc just use the mfc cstring class (rog mentioned this already).

    msdn: Unicode Programming Summary.
    msdn: international programming (unicode, tchar.h etc).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. design question: opinion
    By ggs in forum C Programming
    Replies: 2
    Last Post: 01-29-2003, 11:59 AM
  5. macro (#define) question
    By Darrok in forum C++ Programming
    Replies: 30
    Last Post: 12-20-2001, 05:01 PM