Solved

CListCtrl and Unicode(?) Problem

Posted on 2001-06-27
4
444 Views
Last Modified: 2013-11-20
This little bug's got me stumped. I've got a listctrl that I'm dynamically creating in a formview. It is a report style listctrl, which allows the editing of the items.  

I'm handling the EndLabelEdit message with the message maps, in the formview:
ON_NOTIFY(LVN_ENDLABELEDITA,IDC_REPOSITORY, OnEndlabeleditRepository)
     ON_NOTIFY(LVN_ENDLABELEDITW,IDC_REPOSITORY, OnEndlabeleditRepository)

I ended up using these, as a solution to a previous question I asked, where handling LVN_ENDLABELEDIT was not enough to catch the message.

Up to this point, things work fine, and I can handle and validate the edited text.  The problem is here:

void CMyView::OnEndlabeleditRepository(NMHDR* pNMHDR, LRESULT* pResult)
{
 CMyView::OnEndlabeleditRepository(pNMHDR, pResult);
 LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;    
 CString strText = pDispInfo->item.pszText;
 //do something with the text...
 *pResult = TRUE;
}

If I was to enter "Test" then
pDispInfo->item.pszText = T\0e\0s\0t\0
so strText ends up equalling "T"

I think this is a UNICODE issue, and I don't currently have the preprocessor _UNICODE defined.  But it's my understanding that a CString should take this and convert it correctly, right?

This works correctly on Win98, and I get this problem on WinNT.

Any ideas on how to remedy this?

Thanks,
Marcus
0
Comment
Question by:marcus78
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
migel earned 50 total points
ID: 6234003
Hi!
no CString doesn`t convert chars to the UNICODE and vice versa.
you must perform such conversion manually:
for example unicode string to char
{
          short nSize = wcslen(pDispInfo->item.pszText);
          char* pStr = strText.GetBufferSetLength(nSize+1);
          if(!WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)(pDispInfo->item.pszText), -1, pStr,
                                             nSize,          // size of buffer
                                             NULL, NULL))
               {
               DWORD dwErrorCodes = GetLastError();
               }
          strCaption.ReleaseBuffer();
}
strCaption now contain 'normal' string
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 6234259
Windows NT uses UNICODE internally (all API calls with ANSI parameters get converted to Unicode).

I guess the solution is something around

CString str;
if (::IsWindowUnicode(lisctrl.m_hWnd)) {
  // migel's code here
}
else
  str = pDispInfo->item.pszText;

Peter
0
 
LVL 12

Expert Comment

by:migel
ID: 6234399
also you can separate notifications handler for
LVN_ENDLABELEDITA and
LVN_ENDLABELEDITW
0
 

Author Comment

by:marcus78
ID: 6234999
Migel,

This is what I'm looking for. Thanks!

Marcus
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question