Skip to content

Instantly share code, notes, and snippets.

@bcachet
Forked from chrisforbes/VariableHeightItems.cs
Last active April 8, 2016 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcachet/849a07d202e919e00bb1b1695544f448 to your computer and use it in GitHub Desktop.
Save bcachet/849a07d202e919e00bb1b1695544f448 to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
static class VariableHeightItems
{
[DllImport("user32")]
static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr wp, IntPtr lp);
[StructLayout(LayoutKind.Sequential)]
struct TVITEMEX
{
public Mask mask;
public IntPtr item;
public uint state;
public uint stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int iChildren;
public IntPtr lParam;
public int iIntegral;
// ... plus some windows 6+ crap
}
[Flags]
enum Mask : uint
{
Text = 1,
Image = 2,
Param = 4,
State = 8,
Handle = 16,
SelectedImage = 32,
Children = 64,
Integral = 128,
}
const int TVM_GETITEM = 0x1100 + 62;
const int TVM_SETITEM = 0x1100 + 63;
const int TVM_GETITEMHEIGHT = 0x1100 + 28;
const int TVM_SETITEMHEIGHT = 0x1100 + 27;
public static int GetHeight(this TreeNode tn)
{
TVITEMEX tvix;
tvix.mask = Mask.Handle | Mask.Integral;
tvix.item = tn.Handle;
tvix.iIntegral = 0;
unsafe { SendMessage(tn.TreeView.Handle, TVM_GETITEM, IntPtr.Zero, new IntPtr((void*)&tvix)); }
return tvix.iIntegral;
}
public static void SetHeight(this TreeNode tn, int height)
{
TVITEMEX tvix;
tvix.mask = Mask.Handle | Mask.Integral;
tvix.item = tn.Handle;
tvix.iIntegral = height;
unsafe { SendMessage(tn.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, new IntPtr((void*)&tvix)); }
}
public static int GetItemHeight(this TreeView tv)
{
var none = new IntPtr(0);
unsafe { return SendMessage(tv.Handle, TVM_GETITEMHEIGHT, none, none).ToInt32(); }
}
public static void SetItemHeight(this TreeView tv, int height)
{
unsafe { SendMessage(tv.Handle, TVM_SETITEMHEIGHT, new IntPtr(height), new IntPtr(0)); }
}
public static void SetHeightInPixels(this TreeNode tn, int heightInPixels)
{
if (tn.TreeView.GetItemHeight() != 1)
tn.TreeView.SetItemHeight(1);
tn.SetHeight(heightInPixels);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment