TVM_GETITEM - HELP in C#!!

E

emerson.suguimoto

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!
 
C

Claes Bergefall

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

-----------
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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top