Solved

Windows messages

Posted on 2003-10-30
10
246 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where does legacy ASP.NET initialize language? 2 63
c#, case, if 4 21
C# LINQ 5 27
Inserting LocalDB Table to SQL Server C# 3 20
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

726 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