79528053

Date: 2025-03-22 20:13:49
Score: 0.5
Natty:
Report link

After some insight from Jimi in the comments of the post. I have come to a solution using WndProc.

Below was my solution to resolve this issue but as a side note, as mentioned by Jimi

this requires strict testing, and, in the end, also might require (depending on the use-case) re-implementing some of the safety features (that handle some edge cases) that are left out when the override is in place

private const int WM_LBUTTONDOWN = 0x0201; // Left mouse button down

protected override void WndProc(ref Message m)
{
    //Checks if the Left mouse button down is being sent as the message
    if (m.Msg == WM_LBUTTONDOWN)
    {
        //Gathers click location for HitTest
        int x = (int)(m.LParam.ToInt64() & 0xFFFF);
        int y = (int)((m.LParam.ToInt64() >> 16) & 0xFFFF);
        Point clickPoint = new Point(x, y);

        TreeViewHitTestInfo hitTestInfo = HitTest(clickPoint);
        if (hitTestInfo.Node != null)
        {
            //For my case, I am using a Image as a checkbox icon, this section handles the selection of this.
            if (hitTestInfo.Location == TreeViewHitTestLocations.Image)
            {
                int currentState = GetEffectiveState(hitTestInfo.Node);

                // Toggle check state
                SetNodeChecked(hitTestInfo.Node, currentState != 1);
                UpdateStateIndices(hitTestInfo.Node);

                // Suppress selection
                m.Msg = 0; // Block the message
                return;
            } //This section is used for handling the click of the label, or just right of the label (preventing highlight)
            else if (hitTestInfo.Location == TreeViewHitTestLocations.Label || 
                hitTestInfo.Location == TreeViewHitTestLocations.RightOfLabel)
            {
                return; // Do nothing, preventing selection highlight
            }
        }
    }

    base.WndProc(ref m);
}

I did my best to provide context of what is happening on each line. Thanks again Jimi 😁

Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: VoidSharp