Solved

Windows messages

Posted on 2003-10-30
10
241 Views
Last Modified: 2010-04-16
When overriding the WndProc method how does one stop messages from being dispatched?

For example, I ran a test where I caught the mouse down (left or right). In my switch statement when I capture the mouse down I return from the method without doing anything else. If I have a button on the form or some kind of control they still recieve the button down message.

Basically, I am trying to implement a readonly mode in my app. When the mode is readonly I don't want any of the mouse down messages from being dispatched to the controls.

How can I achieve this?

0
Comment
Question by:jeremiahscott
  • 6
  • 3
10 Comments
 
LVL 6

Accepted Solution

by:
zrh earned 65 total points
ID: 9654150
Havent' tried yet, but try overiding the PreProcessMessage method, and returning true.

ZRH
0
 

Author Comment

by:jeremiahscott
ID: 9655544
this is what i have tried. i thought i read somewhere that you can only trap key messages in the preprocess method, but i couldnt find that again just now.

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
        public override bool PreProcessMessage(ref Message msg)
        {
       
            const int WM_LBUTTONDOWN = 0x0201;
            const int WM_LBUTTONUP = 0x0202;
            const int WM_RBUTTONDOWN = 0x0204;
            const int WM_RBUTTONUP = 0x0205;

            //Console.Error.WriteLine("Msg " + msg.WParam.ToInt32());

            switch(msg.WParam.ToInt32())
            {

                case WM_LBUTTONDOWN:
                    Console.Error.WriteLine("PreProcessMessage WM_LBUTTONDOWN");
                    if(readOnly)
                    {
                        return true;
                    }
     
                    break;
            }

            return base.PreProcessMessage (ref msg);
        }
0
 

Author Comment

by:jeremiahscott
ID: 9655556
Remarks
PreProcessMessage is called by the application's message loop to preprocess input messages before they are dispatched. Possible values for the msg.message field are WM_KEYDOWN, WM_SYSKEYDOWN, WM_CHAR, and WM_SYSCHAR.

When overriding preProcessMessage(), a control should return true to indicate that it has processed the message. For messages that are not processed by the control, the result of base.preProcessMessage() should be returned. Controls will typically override one of the more specialized methods such as isInputChar(), isInputKey(), processCmdKey(), processDialogChar(), or processDialogKey() instead of overriding preProcessMessage().
0
 
LVL 6

Expert Comment

by:zrh
ID: 9655700
Ok, got to do it the hard way...

// Creates a  message filter.
 public class ClickMessageFilter : IMessageFilter {
    // Our min-terms... :)
    const int WM_LBUTTONDOWN = 0x0201;
    const int WM_LBUTTONUP = 0x0202;
    const int WM_RBUTTONDOWN = 0x0204;
    const int WM_RBUTTONUP = 0x0205;

    public bool PreFilterMessage(ref Message m) {
       return (m.WParam == WM_LBUTTONDOWN || m.WParam == WM_LBUTTONUP || m.WParam == WM_RBUTTONDOWN || m.WParam == WM_RBUTTONUP);
    }
  }

Then when you want to make readonly do:
ClickMessageFilter ClickFilter = new ClickMessageFilter()
Application.AddMessageFilter(ClickFilter);

To make clickable:
Application.RemoveMessageFilter(ClickFilter);

Haven't tested either, but hope that helps,
PS: This method will probably hurt performance a little.
ZRH
0
 
LVL 20

Assisted Solution

by:TheAvenger
TheAvenger earned 60 total points
ID: 9656058
Try this:

From your WndProc method: return true and don't call the basic WndProc function. I think this will not process the message with the default handlers.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:jeremiahscott
ID: 9657928
I will try these tonight and get back to you guys.
0
 

Author Comment

by:jeremiahscott
ID: 9661258
you cant return a bool from wndproc

 protected override void WndProc(ref Message msg)

i do return but the msgs still get passed. i will try the other method.
0
 

Author Comment

by:jeremiahscott
ID: 9661262
perhaps it would be better if i had all of the controls check a proeprty from the parent to see if the mode is readonly.
0
 

Author Comment

by:jeremiahscott
ID: 9661429
i couldnt get the message filter to work. the correct message number wasnt coming thru when i moused clicked.

i did this kinda ulgy thing from within my control mouse down.
i set the mainform tag to a bool value on readonly etc.

                if((bool)Parent.Parent.Parent.Tag)
                {
                    return;
                }

if you guys know of a cleaner way of doing this please let me know. thanks for the help.
0
 
LVL 6

Expert Comment

by:zrh
ID: 9661859
Ok, try this:
Instead of comparing m.WParam with the constants, try comparing m.Msg with the constants.  I read this somewhere...

Hope that helps some,
ZRH
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now