我試圖從TreeView中捕獲TVN_SELCHANGING消息。我知道還有BeforeSelect事件,但我想了解爲什麼我無法捕捉到消息...c#中的TVN_SELCHANGING消息#
我讀過msdn TVN_SELCHANG(ED)(ING) LParam是一個指向NMTREEVIEW結構的指針。此外,代碼以WM_NOTIFY消息的形式發送。
所以我一直試圖實現它: (this helped me)
public partial class TreeviewEx : TreeView
{
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
private struct TVITEM
{
public uint mask;
public IntPtr hItem;
public uint state;
public uint stateMask;
public IntPtr pszText;
public int cchTextMax;
public int iImage;
public int iSelectedImage;
public int cChildren;
public IntPtr lParam;
}
[StructLayout(LayoutKind.Sequential)]
private struct NMHDR
{
public IntPtr hwndFrom;
public IntPtr idFrom;
public int code;
}
[StructLayout(LayoutKind.Sequential)]
private struct NMTREEVIEW
{
public NMHDR hdr;
public int action;
public TVITEM itemOld;
public TVITEM itemNew;
public POINT ptDrag;
}
private const int TVN_FIRST = -400;
private const int TVN_SELCHANGINGA = (TVN_FIRST - 1);
private const int TVN_SELCHANGINGW = (TVN_FIRST - 50);
private const int TVN_SELCHANGEDA = (TVN_FIRST - 2);
private const int TVN_SELCHANGEDW = (TVN_FIRST - 51);
private const int WM_NOFITY = 0x004e;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NOFITY)
{
var notify = (NMTREEVIEW)Marshal.PtrToStructure(m.LParam, typeof(NMTREEVIEW));
if (notify.action == TVN_SELCHANGINGA)
{
MessageBox.Show("changing");
}
}
base.WndProc(ref m);
}
我已經嘗試了所有的動作,但他們都不工作。我究竟做錯了什麼?
非常感謝漢斯的解釋!但是我確實已經嘗試添加WM_REFLECT,但它仍然不起作用...還有其他什麼可能是錯誤的? – VincentC
我在發佈之前對其進行了測試,只是爲了確保。你需要擺脫這些消息的A版本,Winforms是純Unicode。 –
再次感謝漢斯告訴我有關Unicode的東西!經過一些調試後,我真的不確定爲什麼這不適合我。我正在使用Visual Studio 2012並嘗試使用32位和64位。 我也嘗試打印出行動代碼,我沒有看到任何預定義的常量值(我確實擺脫了A行爲)。所以我想也許我的常值TVN _...是錯誤的? 然後我試圖打印出ptDrag POINT結構,這個也是錯誤的,它給了我453628288的X和0的Y.難道是我的一些結構被錯誤地定義了嗎? – VincentC