Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Windows messages

Posted on 2003-10-30
10
Medium Priority
?
258 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 260 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 240 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed 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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

885 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