Solved

Windows messages

Posted on 2003-10-30
10
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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