Solved

CListCtrl and Unicode(?) Problem

Posted on 2001-06-27
4
442 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

770 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