Click here to Skip to main content
Licence 
First Posted 3 Jan 2007
Views 16,921
Bookmarked 8 times

Reaction-less TreeView

By | 3 Jan 2007 | Article
TreeView without reaction to user mouse action.

Sample Image - ReactionlessTreeView.png

Introduction

This TreeView is Windows standard treeview but has no default interaction to user mouse actions.
Even selecting, expanding or collapsing node is are all blocked. So you have to implement them yourself.

So, what's good for this? That's the event handling timing. Because reaction code is implemented yourself, you can know when the reaction raises, and also can control whether those reactions operated or not.

In my case, my application have to block expanding node by only mouse action. But the default TreeView control's BeforeExpand event does not pass right reason. The expanding from mouse click is passed as 'Unknown' reason event. So I couldn't use that. And finally I found this way.

What this code does

This code offers derived TreeView class that does not react any user's mouse action. And those reaction can be implemented easily in default mouse event handler.

How this does

This class blocks Windows mouse messages from DefWndProc() method, so those reaction code does not fires. However, I do not know detailed behavior about this. This just works. See next section for more information.

How to use this

You can just use this control directly on your windows form. But I do not recommend that. This class is just show-sample, So I recommend you implement this behavior youself.

Implementation is simple. Derive a TreeView and override DefWndProc().
And ignore all mouse messages. (Do not call base.DefWndProc() when the message code is mouse related.) I defined mouse messages in NativeMethod class. You can refer my sample project.

        protected override void DefWndProc(ref Message m)
        {
            if (!react)
            {
                // Ignores all mouse messages.
                NativeMethods.MouseMessages mm = (NativeMethods.MouseMessages)m.Msg;
                switch (mm)
                {
                    case NativeMethods.MouseMessages.WM_LBUTTONDBLCLK: return;
                    case NativeMethods.MouseMessages.WM_LBUTTONDOWN: return;
                    case NativeMethods.MouseMessages.WM_LBUTTONUP: return;
                    case NativeMethods.MouseMessages.WM_MBUTTONDBLCLK: return;
                    case NativeMethods.MouseMessages.WM_MBUTTONDOWN: return;
                    case NativeMethods.MouseMessages.WM_MBUTTONUP: return;
                    case NativeMethods.MouseMessages.WM_RBUTTONDBLCLK: return;
                    case NativeMethods.MouseMessages.WM_RBUTTONDOWN: return;
                    case NativeMethods.MouseMessages.WM_RBUTTONUP: return;
                }
            }
            base.DefWndProc(ref m);
        }

Or you can just copy the source code into your project. Modify it as you want.

Because the .NET Control's mouse event is raised in another place, (maybe WndProc() method), you will get those mouse event normally even the messages are ignored.

However, I do not know detailed behavior of those Windows Messages.
Do not ask me any background behavior of those message. I don't know too.
This is JUST WORK sample. Of course I cannot guarantee anything work perfectly.
I post this text just to share my discorver.

Sample Project

The source and sample code is made with VCSE(Visual C# Express 2005). It contains VCSE project. But may work with VS. I did not tested.

P.S.

I'm not an English speaker, so main text can contain some wrong words. Thank you for your hard reading!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Foon (drawtree)

Web Developer

Korea (Republic Of) Korea (Republic Of)

Member

Foon

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhat about keyboard? PinmemberDkarpezoUkr21:43 3 Jan '07  
AnswerRe: What about keyboard? PinmemberFoon (drawtree)5:12 4 Jan '07  
GeneralAn alternative PinprotectorMarc Clifton5:44 3 Jan '07  
GeneralRe: An alternative PinmemberFoon (drawtree)5:50 3 Jan '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 3 Jan 2007
Article Copyright 2007 by Foon (drawtree)
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid