How to use this WndProc in Windows Forms application?

后端 未结 2 1632
慢半拍i
慢半拍i 2021-01-06 23:36

Please guide me how to use this WndProc in Windows Forms application:

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam,         


        
相关标签:
2条回答
  • 2021-01-07 00:35

    You can use Application.AddMessageFilter Method.

    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    public class TestMessageFilter : IMessageFilter
    {
        public bool PreFilterMessage(ref Message m)
        {
            // Blocks all the messages relating to the left mouse button. 
            if (m.Msg >= 513 && m.Msg <= 515)
            {
                Console.WriteLine("Processing the messages : " + m.Msg);
                return true;
            }
            return false;
        }
    }
    
    0 讨论(0)
  • 2021-01-07 00:35

    The prototype for WndProc in C# is:

    protected virtual void WndProc(ref Message m)
    

    So, you need to override this procedure in your class, assumed that it's derived from Control.

    protected override void WndProc(ref Message m)
    {
        Boolean handled = false; m.Result = IntPtr.Zero;
        if (m.Msg == NativeCalls.APIAttach && (uint)m.Param == NativeCalls.SKYPECONTROLAPI_ATTACH_SUCCESS)
        {
            // Get the current handle to the Skype window
            NativeCalls.HWND_BROADCAST = m.WParam;
            handled = true;
            m.Result = new IntPtr(1);
        }
    
        // Skype sends our program messages using WM_COPYDATA. the data is in lParam
        if (m.Msg == NativeCalls.WM_COPYDATA && m.WParam == NativeCalls.HWND_BROADCAST)
        {
            COPYDATASTRUCT data = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
            StatusTextBox.AppendText(data.lpData + Environment.NewLine);
    
            // Check for connection
            if (data.lpData.IndexOf("CONNSTATUS ONLINE") > -1)
                ConnectButton.IsEnabled = false;
    
            // Check for calls
            IsCallInProgress(data.lpData);
            handled = true;
            m.Result = new IntPtr(1);
        }
    
        if (handled) DefWndProc(ref m); else base.WndProc(ref m);
    }
    
    0 讨论(0)
提交回复
热议问题