Groups | Blog | Home
all groups > c# > june 2006 >

c# : TVM_GETITEM - HELP in C#!!


emerson.suguimoto NO[at]SPAM gmail.com
6/12/2006 11:42:38 PM
Hello

I can't figure out how to make my SendMessage work with TVM_GETITEM in
C#.
There is no way it works! I=B4ve tried everything possible!
I could get every item handle... but I still dont know how to make it
work!

[DllImport("User32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent,
IntPtr hwndChildAfter, string strClassName, string strWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
uint wParam, // first message parameter
int lParam // second message parameter
);
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr window, int message,
IntPtr wParam,
IntPtr lParam);

[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError =3D true)]
public static extern bool PostMessage(IntPtr hWnd, UInt32 Msg,
UInt32 wParam, Int32 lParam);

public enum TV_Messages
{
TVM_GETNEXTITEM =3D (TV_FIRST + 10),
TVM_GETITEM =3D (TV_FIRST + 62),
TVM_GETCOUNT =3D (TV_FIRST + 5),
TVM_SELECTITEM =3D (TV_FIRST + 11),
TVM_DELETEITEM =3D (TV_FIRST + 1),
TVM_EXPAND =3D (TV_FIRST + 2),
TVM_GETITEMRECT =3D (TV_FIRST + 4),
TVM_GETINDENT =3D (TV_FIRST + 6),
TVM_SETINDENT =3D (TV_FIRST + 7),
TVM_GETIMAGELIST =3D (TV_FIRST + 8),
TVM_SETIMAGELIST =3D (TV_FIRST + 9),
TVM_GETISEARCHSTRING =3D (TV_FIRST + 64),
TVM_HITTEST =3D (TV_FIRST + 17),
}

public enum TVM_EXPAND_wParam
{
TVE_COLLAPSE =3D 0x1,
TVE_EXPAND =3D 0x2,
TVE_TOGGLE =3D 0x3,
TVE_EXPANDPARTIAL =3D 0x4000
}

public enum TVM_GETNEXTITEM_wParam
{
TVGN_ROOT =3D 0x0,
TVGN_NEXT =3D 0x1,
TVGN_PREVIOUS =3D 0x2,
TVGN_PARENT =3D 0x3,
TVGN_CHILD =3D 0x4,
TVGN_FIRSTVISIBLE =3D 0x5,
TVGN_NEXTVISIBLE =3D 0x6,
TVGN_PREVIOUSVISIBLE =3D 0x7,
TVGN_DROPHILITE =3D 0x8,
TVGN_CARET =3D 0x9,
TVGN_LASTVISIBLE =3D 0xA
}

[StructLayout(LayoutKind.Sequential, CharSet =3D CharSet.Auto)]
public struct TVITEM
{
public int mask;
public IntPtr hItem;
public int state;
public int stateMask;
//[MarshalAs(UnmanagedType.LPTStr)]
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public IntPtr lParam;
}

public const int TVIF_TEXT =3D 0x0001;
public const int TVIF_PARAM =3D 0x4;
private void button1_Click(object sender, EventArgs e)
{
TVITEM tvItem =3D new TVITEM();
tvItem.hItem =3D (IntPtr)Int32.Parse(textBox1.Text);
tvItem.cchTextMax =3D 512;
tvItem.mask =3D TVIF_TEXT;
tvItem.pszText =3D Marshal.AllocHGlobal(512);

IntPtr ptrTvi =3D
Marshal.AllocHGlobal(Marshal.SizeOf(tvItem));
Marshal.StructureToPtr(tvItem, ptrTvi, false);

int m_a =3D SendMessage(TreeHandle,
(int)TV_Messages.TVM_GETITEM, IntPtr.Zero, ptrTvi);
MessageBox.Show(m_a.ToString());

string text =3D Marshal.PtrToStringAuto(tvItem.pszText);

MessageBox.Show(text);
Marshal.FreeHGlobal(tvItem.pszText);
Marshal.FreeHGlobal(ptrTvi);
}

The Treeview that I want to get information is from a old program...
from 1999-2002... maybe it=B4s too old and works with ANSI.
The worst thing is that if I try to get the same information from a
Visual Studio 2005=B4s TreeView... it returns garbage... but something
seems to be readable, but from that old program... it=B4s just
Garbage: "WndMarshalAsClass".

Thank you!
Claes Bergefall
6/13/2006 10:38:46 AM
You can overload SendMessage to take a TVITEM structure as parameter. It
will automatically marshal it for you

[DllImport("user32.dll")]
static extern int SendMessage(IntPtr window, int message, IntPtr wParam,
TVITEM lParam);

Also note that the cchTextMax parameter in TVITEM is the size in characters,
not bytes

/claes

-----------
[quoted text, click to view]
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 how to make it
work!

[DllImport("User32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent,
IntPtr hwndChildAfter, string strClassName, string strWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr SendMessage(
IntPtr hWnd, // handle to destination window
int Msg, // message
uint wParam, // first message parameter
int lParam // second message parameter
);
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr window, int message,
IntPtr wParam,
IntPtr lParam);

[DllImport("user32.dll")]
public static extern int SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, UInt32 Msg,
UInt32 wParam, Int32 lParam);

public enum TV_Messages
{
TVM_GETNEXTITEM = (TV_FIRST + 10),
TVM_GETITEM = (TV_FIRST + 62),
TVM_GETCOUNT = (TV_FIRST + 5),
TVM_SELECTITEM = (TV_FIRST + 11),
TVM_DELETEITEM = (TV_FIRST + 1),
TVM_EXPAND = (TV_FIRST + 2),
TVM_GETITEMRECT = (TV_FIRST + 4),
TVM_GETINDENT = (TV_FIRST + 6),
TVM_SETINDENT = (TV_FIRST + 7),
TVM_GETIMAGELIST = (TV_FIRST + 8),
TVM_SETIMAGELIST = (TV_FIRST + 9),
TVM_GETISEARCHSTRING = (TV_FIRST + 64),
TVM_HITTEST = (TV_FIRST + 17),
}

public enum TVM_EXPAND_wParam
{
TVE_COLLAPSE = 0x1,
TVE_EXPAND = 0x2,
TVE_TOGGLE = 0x3,
TVE_EXPANDPARTIAL = 0x4000
}

public enum TVM_GETNEXTITEM_wParam
{
TVGN_ROOT = 0x0,
TVGN_NEXT = 0x1,
TVGN_PREVIOUS = 0x2,
TVGN_PARENT = 0x3,
TVGN_CHILD = 0x4,
TVGN_FIRSTVISIBLE = 0x5,
TVGN_NEXTVISIBLE = 0x6,
TVGN_PREVIOUSVISIBLE = 0x7,
TVGN_DROPHILITE = 0x8,
TVGN_CARET = 0x9,
TVGN_LASTVISIBLE = 0xA
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct TVITEM
{
public int mask;
public IntPtr hItem;
public int state;
public int stateMask;
//[MarshalAs(UnmanagedType.LPTStr)]
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public IntPtr lParam;
}

public const int TVIF_TEXT = 0x0001;
public const int TVIF_PARAM = 0x4;
private void button1_Click(object sender, EventArgs e)
{
TVITEM tvItem = new TVITEM();
tvItem.hItem = (IntPtr)Int32.Parse(textBox1.Text);
tvItem.cchTextMax = 512;
tvItem.mask = TVIF_TEXT;
tvItem.pszText = Marshal.AllocHGlobal(512);

IntPtr ptrTvi =
Marshal.AllocHGlobal(Marshal.SizeOf(tvItem));
Marshal.StructureToPtr(tvItem, ptrTvi, false);

int m_a = SendMessage(TreeHandle,
(int)TV_Messages.TVM_GETITEM, IntPtr.Zero, ptrTvi);
MessageBox.Show(m_a.ToString());

string text = Marshal.PtrToStringAuto(tvItem.pszText);

MessageBox.Show(text);
Marshal.FreeHGlobal(tvItem.pszText);
Marshal.FreeHGlobal(ptrTvi);
}

The Treeview that I want to get information is from a old program...
from 1999-2002... maybe it´s too old and works with ANSI.
The worst thing is that if I try to get the same information from a
Visual Studio 2005´s TreeView... it returns garbage... but something
seems to be readable, but from that old program... it´s just
Garbage: "WndMarshalAsClass".

Thank you!

AddThis Social Bookmark Button