Solved

Windows messages

Posted on 2003-10-30
10
245 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

791 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