473,383 Members | 1,785 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,383 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 5978
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 ... :-))...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.