R
Rodrigus Makon
Hello,
I have a piece of code that removes the check boxes from a treeview control
at run time for the .NET 2.0 of Desktop. I have changed the dll reference
from user32.dll to coredll.dll and compiled it and trying to use it in my
CF 2.0 application, but it does not work (does not remove the check boxes
for the nodes that I am trying to remove, does not give any error either).
I am not familier with interop services, so can some one with knowledge in
that area, please help me with this code.
Here is the code that works for the Desktop .NET 2.0.
using System;
using System.Runtime.InteropServices;
namespace tempnamespace
{
/// <summary>
/// Summary description for TreeViewExtender.
/// </summary>
public class TreeViewExtender
{
private const int TVIS_STATEIMAGEMASK = 0xF000;
private const int TVIF_TEXT = 0x0001,
TVIF_IMAGE = 0x0002,
TVIF_STATE = 0x0008,
TVIF_HANDLE = 0x0010,
TVIF_SELECTEDIMAGE = 0x0020,
TVM_SETITEM = 0x110D;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private class TVITEM
{
public int mask;
public System.IntPtr hItem; // HTREEITEM
public int state;
public int stateMask;
public System.IntPtr pszText; // string
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public System.IntPtr lParam;
}
static public void RemoveCheckBoxFromNode(System.Windows.Forms.TreeNode
node)
{
//
// It is important that the TreeView.Handle is accessed before
// the TreeNode Handle. If it is not, the results of TreeNode.Handle
// are non-deterministic.
//
System.IntPtr treeViewHandle = node.TreeView.Handle;
TVITEM tvi = new TVITEM();
tvi.hItem = node.Handle;
tvi.mask = TVIF_HANDLE | TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0;
User32.SendMessage(treeViewHandle, TVM_SETITEM, System.IntPtr.Zero,
tvi);
}
public class User32
{
[DllImport("coredll.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
internal static extern System.IntPtr SendMessage(IntPtr hWnd, int
msg, IntPtr wParam, [In, Out, MarshalAs(UnmanagedType.AsAny)] Object lParam);
}
}
I have already changed the dllimport to coredll.dll above.
Here is a sample code that uses the above code.
// TreeView tv = new TreeView.....
// assigned imagelist ...
TreeNode tn = tv.Nodes.Add("First");
TreeNode prefix = tn.Nodes.Add("Id Prefix - " );
prefix.SelectedImageIndex = 1;
prefix.ImageIndex = 1;
TreeViewExtender.RemoveCheckBoxFromNode(prefix);
Any help please.
Regards
I have a piece of code that removes the check boxes from a treeview control
at run time for the .NET 2.0 of Desktop. I have changed the dll reference
from user32.dll to coredll.dll and compiled it and trying to use it in my
CF 2.0 application, but it does not work (does not remove the check boxes
for the nodes that I am trying to remove, does not give any error either).
I am not familier with interop services, so can some one with knowledge in
that area, please help me with this code.
Here is the code that works for the Desktop .NET 2.0.
using System;
using System.Runtime.InteropServices;
namespace tempnamespace
{
/// <summary>
/// Summary description for TreeViewExtender.
/// </summary>
public class TreeViewExtender
{
private const int TVIS_STATEIMAGEMASK = 0xF000;
private const int TVIF_TEXT = 0x0001,
TVIF_IMAGE = 0x0002,
TVIF_STATE = 0x0008,
TVIF_HANDLE = 0x0010,
TVIF_SELECTEDIMAGE = 0x0020,
TVM_SETITEM = 0x110D;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
private class TVITEM
{
public int mask;
public System.IntPtr hItem; // HTREEITEM
public int state;
public int stateMask;
public System.IntPtr pszText; // string
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public System.IntPtr lParam;
}
static public void RemoveCheckBoxFromNode(System.Windows.Forms.TreeNode
node)
{
//
// It is important that the TreeView.Handle is accessed before
// the TreeNode Handle. If it is not, the results of TreeNode.Handle
// are non-deterministic.
//
System.IntPtr treeViewHandle = node.TreeView.Handle;
TVITEM tvi = new TVITEM();
tvi.hItem = node.Handle;
tvi.mask = TVIF_HANDLE | TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0;
User32.SendMessage(treeViewHandle, TVM_SETITEM, System.IntPtr.Zero,
tvi);
}
public class User32
{
[DllImport("coredll.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
internal static extern System.IntPtr SendMessage(IntPtr hWnd, int
msg, IntPtr wParam, [In, Out, MarshalAs(UnmanagedType.AsAny)] Object lParam);
}
}
I have already changed the dllimport to coredll.dll above.
Here is a sample code that uses the above code.
// TreeView tv = new TreeView.....
// assigned imagelist ...
TreeNode tn = tv.Nodes.Add("First");
TreeNode prefix = tn.Nodes.Add("Id Prefix - " );
prefix.SelectedImageIndex = 1;
prefix.ImageIndex = 1;
TreeViewExtender.RemoveCheckBoxFromNode(prefix);
Any help please.
Regards