I want C# to generate an event when I receive a window message.

Asked 2 months ago, Updated 2 months ago, 0 views

Thank you for your help.
 As the title suggests, we would like to have an event happen when we receive a window message on C#.
 For the time being, I was able to process receiving window messages and create an event class, but I am not sure how to generate an event.
 The code below seems to cause a compilation error.
 Could you give me some advice?
 Thank you for your cooperation.

using System;
using System.Collections.General;
using System.Text;
using System.Windows.Forms;

namespace WndMessage {
    public class WndMessage {

        // DELIGATE FOR MESSAGE PROCESSING FUNCTION
        private delete int D_MyWndProc(IntPtr hwnd, int msg, intwParam, intlParam);

        // API for subclustering windows
        private static int GWL_WNDPROC=-4;
        System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="GetWindowLongA")]
        external static int GetWindowLong (IntPtr hwnd, intnIndex);
        System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="SetWindowLongA")]
        external static int SetWindowLong (IntPtr hwnd, intnIndex, intdwNewLong);
        System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint="SetWindowLongA")]
        external static int SetWindowLong (IntPtr hwnd, intnIndex, D_MyWndProcdwNewLong);
        [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "CallWindowProcA")]
        extern static int CallWindowProc(int lpPrevWndFunc, IntPtr hwnd, int msg, int wParam, int lParam);

        message processing function of the default /
        private static int lngWnP;

        message processing is started its own //
        public void StartReceiveMessage(IntPtr hwnd){
            lngWnP = GetWindowLong(hwnd, GWL_WNDPROC);
            SetWindowLong(hwnd, GWL_WNDPROC, MyWndProc);
        }

        // original message processing is finished
        public void ExitReceiveMessage(IntPtr hwnd){
            SetWindowLong(hwnd, GWL_WNDPROC, lngWnP);
        }

        window message comes to sorting function /
        private static int MyWndProc(IntPtr hwnd, int msg, int wParam, int lParam){
            return CallWindowProc(lngWnP, hwnd, msg, wParam, lParam);
        }

        public class MessageEventArgs : EventArgs {
            private readonly int msg;
            private readonly intlParam;
            private readonly intwParam;

            public MessageEventArgs(int msg, intlParam, intwParam){
                this.msg = msg;
                This.lParam = lParam;
                This.wParam =wParam;
            }

            public int msg {
                get {return msg;}
            }

            public intlParam{
                get {returnlParam;}
            }

            public int wParam {
                get {returnwParam;}
            }
        }
    }
}

c#

2022-09-30 12:02

1 Answers

error CS0102: Type 'WndMessage.MessageEventArgs' already contains definition of 'msg'.

Duplicate names in the private readonly int msg; field and the public int msg {get {return msg;}} property.Identifiers with the same name cannot be used in fields, properties, methods, etc.

private static intlngWnP;

Not limited to this, we keep the window procedure in int, but it fails in a 64-bit environment. Use IntPtr and consider Get/SetWindowLongPtr.


2022-09-30 12:02

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.