473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

TVM_GETITEM does not return unicode label

Hi all

My TreeView has unicode and english labels. The treeview shows OK on the
screen.
When I am trying to get an item's label using TVM_GETITEM API message, the
buffer returned by SendMessage always contains single-byte coded labels
(ASCII) even though I use SendMessageW entry point. In other words, buffer is
unicode string each character of which contains two ASCII letters of
corresponding label.

English labels are returned fine (as ASCII), but for unicode label, buffer
contains only question marks (0x3F) - one byte per letter.
(Don't ask me why I am using Win32 API to read treeview labels - this is
another story)

Here is my code. Is there any way to get unicode label from treeview node?
Thank you
(OS = Windows 2000 Pro with all service packs and updates)
[DllImport("user32", EntryPoint = "SendMessageW")]
private static extern int SendMessage(IntPtr hWnd, int wMsg, int wParam, int
lParam);

[DllImport("user32", EntryPoint = "SendMessageW")]
private static extern int SendMessageTVI(IntPtr hWnd, int wMsg, int wParam,
ref TVITEM tvi);

[ DllImport("kernel32.dll")]
public static extern IntPtr LocalAlloc(uint flags, uint cb);

[ DllImport("kernel32.dll")]
public static extern IntPtr LocalFree(IntPtr p);
private struct TVITEM
{
public int mask;
public int hItem;
public int state;
public int stateMask;
public int pszText; //if string, must be preallocated
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public int lParam;
public int iIntegral;
}

private const int TV_FIRST = 0x1100;
private const int TVM_GETITEM = (TV_FIRST + 12);
private const int TVM_GETNEXTITEM = (TV_FIRST + 10);
private const int TVGN_ROOT = 0x0;
private const int TVGN_NEXT = 0x1;
private const int TVGN_CHILD = 0x4;
private const int TVIF_TEXT = 0x1;
private const int MY_MAXLVITEMTEXT = 260;
....
private void Form1_Load(object sender, System.EventArgs e)
{
TreeNode node = new TreeNode("English"); //root = english label
string t = "" + '\u0434' + '\u0432' + '\u043e' + '\u0440'; //child =
unicode label
node.Nodes.Add(t);
this.treeView1.Nodes.Add(node);
}

private void buttonGetTreeItems_Click(object sender, System.EventArgs e)
{
int hRoot = SendMessage(this.treeView1.Handle, TVM_GETNEXTITEM, TVGN_ROOT,
0);
string strText = GetTreeItem_Local(hRoot);

int hChild = SendMessage(this.treeView1.Handle, TVM_GETNEXTITEM,
TVGN_CHILD, hRoot);
strText = GetTreeItem_Local(hChild);
}

private string GetTreeItem_Local(int hItem)
{
int ret;
TVITEM tvi = new TVITEM();
IntPtr pszText = LocalAlloc(0x40, MY_MAXLVITEMTEXT);

tvi.mask = TVIF_TEXT;
tvi.hItem = hItem;
tvi.cchTextMax = MY_MAXLVITEMTEXT;
tvi.pszText = (int)pszText;

ret = SendMessageTVI(this.treeView1.Handle, TVM_GETITEM, 0, ref tvi);
string buffer = Marshal.PtrToStringUni((IntPtr)tvi.pszText,
MY_MAXLVITEMTEXT);

char[] arr = buffer.ToCharArray(); //<== use this array to look at the
bytes
//in debug mode

LocalFree(pszText);
return buffer;
}

Mar 22 '07 #1
2 5982
Alex,
>Here is my code. Is there any way to get unicode label from treeview node?
[...]
>private const int TVM_GETITEM = (TV_FIRST + 12);
Just like there are ANSI and Unicode versions of the SendMessage API,
there are two different versions of the TVM_GETITEM message. From
Commctrl.h:

#define TVM_GETITEMA (TV_FIRST + 12)
#define TVM_GETITEMW (TV_FIRST + 62)

#ifdef UNICODE
#define TVM_GETITEM TVM_GETITEMW
#else
#define TVM_GETITEM TVM_GETITEMA
#endif

You have to use TVM_GETITEMW to get back unicode strings.

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 22 '07 #2

"Alex K." <Al***@discussions.microsoft.comwrote in message
news:1A**********************************@microsof t.com...
Hi all

My TreeView has unicode and english labels. The treeview shows OK on the
screen.
When I am trying to get an item's label using TVM_GETITEM API message, the
buffer returned by SendMessage always contains single-byte coded labels
(ASCII) even though I use SendMessageW entry point. In other words, buffer
is
unicode string each character of which contains two ASCII letters of
corresponding label.
A window's unicodeness is set when the window class is registered, and all
messages sent to it are converted so the window procedure sees only one
style of string.

The relevant method is
System.Windows.Forms.NativeWindow+WindowClass.Regi sterClass It looks like
it picks unicode or ANSI based on
System.Runtime.InteropServices.Marshal.SystemDefau ltCharSize.

Since your TreeView is showing unicode labels, the NativeWindow probably is
unicode. Something in the way your p/invoke of SendMessageW is causing the
conversion.

Your API declarations are a mess.

You shouldn't use int to hold a pointer, because it's only a valid
assumption on 32-bit processors.

Why p/invoke LocalAlloc when there is Marshal.AllocHGlobal?

You haven't set CharSet on your DllImport attributes.

You've a problem because you allocate MY_MAXLVITEMTEXT bytes, but try to
retrieve MY_MAXLVITEMTEXT characters. Each unicode character takes two
bytes.

But none of these really explain why you would get ANSI characters.

Ahhh, here is your problem. Looking in CommCtrl.h:

#define TVM_GETITEMA (TV_FIRST + 12)

#define TVM_GETITEMW (TV_FIRST + 62)

#ifdef UNICODE

#define TVM_GETITEM TVM_GETITEMW

#else

#define TVM_GETITEM TVM_GETITEMA

#endif
Mar 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Eric Brunel | last post by:
Hi all, I'm in the process of localizing a Tkinter application and I'm struggling with a problem for which I can't figure out a solution. The following code: -------------------------------...
5
by: Ali | last post by:
I was wondering how one would go about displaying unicode in a Label object in a Tkinter window. I am trying to display text in another language. Please help.
2
by: Stuart Norris | last post by:
Hi All, I am new to csharp and Visual Studio and trying to learn. I would like to have a button with an up arrow as it "label" instead of text? Is this possible using the normal button in...
4
by: foo | last post by:
"An unhandled exception of type 'System.NullReferenceException' occurred in Project.exe Additional information: Object reference not set to an instance of an object." This worked in ver 6,...
1
by: emerson.suguimoto | last post by:
Hello I can't figure out how to make my SendMessage work with TVM_GETITEM in C#. There is no way it works! I´ve tried everything possible! I could get every item handle... but I still dont know...
2
by: Jim Carr | last post by:
Upon entering the site www.FutureByDesign-Music.com with IE6, my clipboard is erased and then disabled in all other Windows XP applications. Navigating to another site returns clipboard...
17
by: Stuart McGraw | last post by:
In the announcement for Python-2.3 http://groups.google.com/group/comp.lang.python/msg/287e94d9fe25388d?hl=en it says "raw_input(): can now return Unicode objects". But I didn't see anything...
0
by: Kevin McKinley | last post by:
Below i've put the code for a program that i wrote. I need help on lines 384-403. If you run this program you will notice on the first tab when have it produce an answer the $ is surrounded with...
9
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I have a MDI Container Form, with one label control on the Background of this container form. When I now open a child form, this form is behind the label ... and this looks ugly ... :-))...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.